/***********************************
	Currency Converter
***********************************/

$('#currencyConverter select').change(function(){
	var code = $(this).val();
	var amount = $(this).parent().children('input').val();
	$.ajax({
		type: "POST",
		url: base_path + "properties/currency/",
		data: "code=" + code + "&amount=" + amount,
		success: function(data) {
			$('#currencyConverter p').text(Math.ceil(data.replace(",","")));
		}
	});
});

/***********************************
	Photo List (Property Details)
***********************************/

$('.photoList ul li img').hover(function(){
	var img = $(this).attr('src');
	$(this).parent().parent().parent().parent().children('p').children('a').attr('href',img).children('img').attr('src',img);
});
$('.photoList a').colorbox();

/***********************************
	Sort Property Results
***********************************/

$('#sortProperties select').change(function(){
	$.ajax({
		type: "POST",
		url: base_path + "properties/sort/",
		data: "sort_by=" + $(this).val(),
		success: function(data) {
			window.location = window.location.pathname;
		}
	});
});

/***********************************
	Refine Search
***********************************/

$('#refineSearch > p a').click(function(e) {
	e.preventDefault();
	$(this).parent().parent().children('ul').slideToggle();
});

/***********************************
	Forms: clear default value
***********************************/

$('#sidebar input, #leaveComment input:not(\'.submit\'), #leaveComment textarea').click(function() {
	$(this).val('');
});

/***********************************
	Thumb Hover
***********************************/

/*$('#properties .thumb img, .featured .photo img').thumbPopup({
	popupCSS: { 'border' : '0', '-moz-box-shadow' : '0 0 16px rgba(0, 0, 0, .5)' }
});*/

/***********************************
	Quick Look
***********************************/

$('#properties li a.view').click(function(e) {
	e.preventDefault();
	$(this).parent().children('ul').toggle();
});

/***********************************
	Star Rating
***********************************/

$.fn.stars = function() {
    $(this).each(function() {
        var val = parseFloat($(this).html());
        val = val > 5 ? 5 : (val < 0 ? 0 : val);
        val = Math.round(val * 2) / 2;
        var size = 16 * val;
        var stars = $('<span class="stars"><span></span></span>');
        stars.find('span').width(size);
        $(this).replaceWith(stars);
    });
}
$('span.stars').stars();

/***********************************
	Expand Comments
***********************************/

$('a.expandComment').click(function(e) {
	e.preventDefault();
	$(this).parent().children('div').slideToggle();
});

/***********************************
	Expand Blog
***********************************/

$('a.expandBlog').click(function(e) {
	e.preventDefault();
	$(this).parent().parent().parent().children('div').slideToggle();
});

/***********************************
	Translator
***********************************/

$('a#showTranslator').click(function(e) {
	e.preventDefault();
	$('#translator').toggle();
	$(this).hide();
});

/***********************************
	Land Only Form Handling
***********************************/

/*$('select#property_type').change(function(){
	var propType = $(this).val();
	if(propType == 3)
	{
		$('.notForLandOnly').hide();
	}else{
            $('.notForLandOnly').show();
        }
});*/


var getPropSubType = function(prop_type){
    
    var path = jQuery('#props_sub_type_ajax_url').val();
    
    jQuery.ajax({
        url: path,
        type: 'post',
        data: {prop_type_id:prop_type},
        success: function(resp){
            //alert(resp);
            jQuery('select#property_sub_type').html(resp);
        }
    });
    
}


var loadPropSubType = function(obj){

    var propType = $(obj).val();
    getPropSubType(propType);
}


$('select#property_type').live('change', function(){
	loadPropSubType(this);
});


$('select#listing_type').live('change', function(){
	if(jQuery(this).val() == 4){
            //jQuery('select#property_type option').show();
            jQuery('select#property_type option').each(function(){
               if(jQuery(this).val() == 4){
                   jQuery(this).show();
               } 
            });
        }else{
            //jQuery('select#property_type option').hide();
            jQuery('select#property_type option').each(function(){
               if(jQuery(this).val() == 4){
                   jQuery(this).hide();
                   if(jQuery('select#property_type').val()==4){
                        jQuery('select#property_type option').removeAttr('selected');
                        jQuery('select#property_type option:first').attr('selected','selected');
                        loadPropSubType($('select#property_type'));
                   }
               } 
            });
        }
    });
    
    
var checkStudioOption = function(obj){
    jQuery('select.studio option').removeAttr('selected');
    if(jQuery(obj).val() == 3){  // id of studio is 3
        //jQuery('select.studio option').hide();
        //jQuery('select.studio option:eq(1)').show();       
        jQuery('select.studio option').each(function(){
           if(jQuery(this).val() !="" && jQuery(this).val() == 0){
               jQuery(this).show();
               jQuery(this).attr('selected','selected');
           }else{
               jQuery(this).hide();
           }
           
        });
    }else{
        jQuery('select.studio option').each(function(){
           if(jQuery(this).val() !="" && jQuery(this).val() == 0){
               jQuery(this).hide();
           }else{
               jQuery(this).show();
           }
        });
        jQuery('select.studio option:first').attr('selected','selected');
    }
}    

$('select#property_sub_type').live('change', function(){
    
    checkStudioOption(this);
    
});



/***********************************
	Form Validation
***********************************/

$('form').submit(function(e) {
	$(this).find('.req').each(function() {
		if( $(this).val() == '' )
		{
			e.preventDefault();
			alert('Please complete all required fields.');
			//alert($(this).attr('name')); // for debugging
			return false;
		}
	});
	// require checkbox be checked
	$(this).find('.reqCheckbox').each(function() {
		if( $(this).is(':checked') )
		{
			// all good
		}
		else
		{
			e.preventDefault();
			alert('You must read and agree to the Terms and Conditions in order to register.');
			return false;
		}
	});
	// confirm password matches
	if( $('.match1').length > 0 && $('.match1').val() != $('.match2').val() )
	{
		e.preventDefault();
		alert('Your password and password confirmation must match.');
		return false;
	}
});

/***********************************
	Form Datepicker
***********************************/

$("input.date").datepicker({
	dateFormat:		'mm-dd-yy',
	showAnim:		'fadeIn'
});

/***********************************
	Confirm Action
***********************************/

$('a.confirmAction').click(function(e) {
	if(!confirm('Are you sure you wish to perform this action?'))
	{
		e.preventDefault();
	}
});






