var IdCity = -1;

function checkForm() {
	if (document.search.keyword.value=="") return false;
	return true;
}	
function doClear(theText) { if (theText.value == theText.defaultValue) { theText.value = "" } }	
function doDefault(theText) { if (theText.value == "") { theText.value = theText.defaultValue } }


$(document).ready(function() {
	
	$('.phone').keydown(function(event) {
		var value = $(this).val();
		var key = event.which;
		return (key <= 13 || (key >= 48 && key <= 57) || key == 46 || key == 45 || key == 32 || key == 190)? true : false;
	});
	
	function jboxDialog(cnt){
		$('.error-alert').html(cnt);
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		$( "#dialog-message" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog( "close" );
					$('#loader').hide();
					$('#btn_submit_frm1').show();
				}
			}
		});
	}
	
	$('#contact_form2').submit(function() {
		var firstname = $('#first_name').val();
		var lastname = $('#last_name').val();
		var email = $('#email').val();
		var phone = $('#phone').val();
		var message = $('#comment').val();
		var subject = $('#subject').val();
		var url = $('#url_p').val();
		$.ajax({
			url: URL_SITE+'send-mail-contact',
			type: 'POST', 
			data: ({send: true, name: firstname, lastname: lastname, email: email, phone: phone, subject:subject, comment: message }),
	        dataType: 'text',
			success: function(data) {
				if( data == 'OK') {
					htm = 'Thank you for contacting Majestic Properties. Should you require immediate assistance, please <br/> call us at  (305) 677-5000.';
					 $('#first_name').val('');
					 $('#last_name').val('');
					 $('#email').val('');
					 $('#phone').val('');
					 $('#comment').val('');
					 $(".show-message").html('');
					 $('#dialog-message').attr('title', 'Majestic '+subject);
					jboxDialog(htm); 
					
				} else {
					$('.show-message').html(data); 
				}
		    }
		});
		return false; 
	});
	
	$('#contact_forms').submit(function() {
		var firstname = $('#first_name').val();
		var lastname = $('#last_name').val();
		var email = $('#email').val();
		var phone = $('#phone').val();
		var message = $('#comment').val();
		var subject = $('#subject').val();
		var url = $('#url_p').val();
		$.ajax({
			url: URL_SITE+'send-mail-contact',
			type: 'POST', 
			data: ({send: true, name: firstname, lastname: lastname, email: email, phone: phone, subject:subject, comment: message }),
	        dataType: 'text',
			success: function(data) {
				if( data == 'OK') {
					htm = 'Thank you for contacting Majestic Properties. Should you require immediate assistance, please <br/> call us at  (305) 677-5000.';
					 $('#first_name').val('');
					 $('#last_name').val('');
					 $('#email').val('');
					 $('#phone').val('');
					 $('#comment').val('');
					 $(".show-message").html('');
					 $('#dialog-message').attr('title', 'Majestic '+subject);
					jboxDialog(htm); 
					
				} else {
					$('.show-message').html(data); 
				}
		    }
		});
		return false; 
	});
	
	$('#contact_container_form').submit(function() {
		var firstname = $('#join_firstname').val();
		var lastname = $('#join_lastname').val();
		var email = $('#join_email').val();
		var phone = $('#join_phone').val();
		var message = $('#join_comments').val();
		var cod = $('#cod_mls_num').val();
		var url = $('#url_p').val();
		var address = $('#address').val();
		$.ajax({
			url: URL_SITE+'send-mail',
			type: 'POST', 
			data: ({send: true, name: firstname, lastname: lastname, address:address, email: email, phone: phone, code:cod, url:url, comment: message }),
	        dataType: 'text',
			success: function(data) {
				if( data == 'OK') {
					htm = 'Thank you for contacting Majestic Properties. Should you require immediate assistance, please <br/> call us at  (305) 677-5000.';
					 $('#join_firstname').val('');
					 $('#join_lastname').val('');
					 $('#join_comments').val('');
					 $('#join_phone').val('');
					 $('#join_email').val('');
					 $(".show-message").html('');
					 $('#dialog-message').attr('title', 'Majestic property contact');
					jboxDialog(htm); 
					
				} else {
					$('.show-message').html(data); 
				}
		    }
		});
		return false; 
	});
	
	$('.idx_control_type').change(function() {
		type = $("option:selected", this).val();
		if(type =="-1") return false;
		form = $('#form').val();
		min_price = $('#mprice').val();
		url_max_price = URL_SITE+'select/max-price/'+type+'/'+form+'/'+min_price ;
		url_min_price = URL_SITE+'select/min-price/'+type+'/'+form+'/'+min_price ;
		jQuery('#min-price').load(url_min_price);
		jQuery('#max-price').load(url_max_price);
	});
	
	$('.idx_type').change(function() {
		type = $(this+":checked").val();
		if(type =="-1") return false;
		form = $('#form').val();
		url_max_price = URL_SITE+'select/max-price/'+type+'/'+form;
		url_min_price = URL_SITE+'select/min-price/'+type+'/'+form;
		jQuery('#min-price').load(url_min_price);
		jQuery('#max-price').load(url_max_price);
	});
	
	$('.list-tabs').live('click', function() {
		 var code = $(this).attr('rel');
		 $.ajax({
	          type: 'GET',
	          url: URL_SITE+'tab-page/'+code,
	          success: function(data) {
			    $('.content-tab').html(data);
	          }
	      });
	});
	
	$('.img_banner').live('click', function() {
		var img = $(this).attr('rel');
		var id = $(this).attr('id');
		$('#project_dev_'+id).attr('src', img);
	});
	
	
	
	
	$('.vcity').live('change', function() {
		IdCity = $("option:selected", this).val();
		if(IdCity != -1) {
			vcnt = '<input id="submit-form" type="image" src="'+URL_SITE+'public/image/idx_btn_mini_search.png" border="0" />';
			$('#btn_submit').html(vcnt);
		} else
			$('#btn_submit').html('<img src="'+URL_SITE+'public/image/idx_btn_mini_search_off.png" border="0" />');
	});
	
	 $('#idx_advanced_search_city').change(function () {
         var str = "";
         $("#idx_advanced_search_city option:selected").each(function () {
        	 x = $(this).val();
        	 if( x != '-1') { str += $(this).val() + "|"; }
             });
         $("#idx_advanced_search_cities").val(str);
    }).trigger('change');
	
	$('.ofname').live('click', function() {
		 var code = $(this).attr('rel');
		 $.ajax({
	          type: 'GET',
	          url: URL_SITE+'contact/filter/'+code,
	          dataType: 'html',
	          success: function(html) {
	        	  $('.map-contact').html(html);
	          }
	      });
	});
	
	$('.item_video').live('click', function() {
		 var code = $(this).attr('rel');
		 content = '<iframe class="youtube-player" type="text/html" width="640" height="399" src="http://www.youtube.com/embed/'+code+'" frameborder="0" style="border:0px;"></iframe>';
		 $('.cnt_video').html(content);
	});
	
	
 });

 function pagination(url){
	jQuery('.content-right').load(url);
 }
 
 function submit_select(value, type) {
	if(value == 0) return false;
	switch(type) {
		case 1: url = URL_SITE+'properties/foreclosure/'+value; break;
		case 2: url = URL_SITE+'properties/condos/'+value; break; 
	}
	window.location.href = url;
 }
 
$('.add-favorites').live('click', function(){
	var mslid = $(this).attr('rel');
	$.ajax({
		url: URL_SITE+'properties/add-favorites/'+mslid,
		type: 'GET', 
		dataType: 'text',
		success: function(data) {
			if(data == 'login')
				document.location = URL_SITE+'login';
			else
			alert(data);
	    }
	});
});

$('.remove-favorites').live('click', function(){
	var mslid = $(this).attr('rel');
	$.ajax({
		url: URL_SITE+'properties/remove-favorites/'+mslid,
		type: 'GET', 
		dataType: 'text',
		success: function(data) {
			alert(data);
	    }
	});
});

function property_order(order) {
	$.ajax({
    	url: URL_SITE+'list-properties/order/'+order,
    	type: 'GET', 
    	success: function(data){
    			$('#idx_result_property').html(data);    		
    	}
    });
}

function property_order_map(order) {
	$.ajax({
    	url: URL_SITE+'list-properties-map/order/'+order,
    	type: 'GET', 
    	success: function(data){
    			$('#idx_result_property').html(data);    		
    	}
    });
}
