$(function(){
	$("span.ville").click(function(){
		$("#ou").val($(this).html());
		$("#ou").focus();
	});
	$("span.typebien").click(function(){
		$("#quoi").val($(this).html());
		$("#quoi").focus();
	});
	$('#chercher').click(function(){
		$('#cp').val('1');
		var quoi 		= 'immobilier';
		var ou			= 'maroc';
		var transaction	= '';
		if($('#quoi').val() != ''){
			quoi = $('#quoi').val();
		}
		if($('#ou').val() != ''){
			ou = $('#ou').val();
		}
		if($('#transactionVente').is(':checked') == true || $('#transactionLocation').is(':checked') == true){
			if($('#transactionVente').is(':checked') == true){
				transaction = 'Vente';
			}
			if($('#transactionLocation').is(':checked') == true){
				transaction	= 'Location';
			}
			$('#searchForm').attr('action','http://www.maskane.com/'+transaction+'-'+str_replace(' ','+',quoi)+'-'+str_replace(' ','+',ou)+'/');
			$('#searchForm').submit();
		}else{
			jAlert("Veuillez séléctionner une transation");
		}
	});
	
	$("#ou").autocomplete(villes);
	$("#quoi").autocomplete(typesBiens);
	
	$('.pagenav a').click(function(event){
		event.preventDefault();
		$('#cp').val($(this).attr('data'));
		var quoi 		= 'immobilier';
		var ou			= 'maroc';
		var transaction	= '';
		if($('#quoi').val() != ''){
			quoi = $('#quoi').val();
		}
		if($('#ou').val() != ''){
			ou = $('#ou').val();
		}
		if($('#transactionVente').is(':checked') == true || $('#transactionLocation').is(':checked') == true){
			if($('#transactionVente').is(':checked') == true){
				transaction = 'Vente';
			}
			if($('#transactionLocation').is(':checked') == true){
				transaction	= 'Location';
			}
			$('#searchForm').attr('action','http://www.maskane.com/'+transaction+'-'+str_replace(' ','+',quoi)+'-'+str_replace(' ','+',ou)+'/p'+$('#cp').val()+'/');
			$('#searchForm').submit();
		}else{
			jAlert("Veuillez séléctionner une transation");
		}
	});
	
	$('#tri').change(function(){
		$('#ordre').val($(this).val());
		$('#cp').val('1');
		var quoi 		= 'immobilier';
		var ou			= 'maroc';
		var transaction	= '';
		if($('#quoi').val() != ''){
			quoi = $('#quoi').val();
		}
		if($('#ou').val() != ''){
			ou = $('#ou').val();
		}
		if($('#transactionVente').is(':checked') == true || $('#transactionLocation').is(':checked') == true){
			if($('#transactionVente').is(':checked') == true){
				transaction = 'Vente';
			}
			if($('#transactionLocation').is(':checked') == true){
				transaction	= 'Location';
			}
			$('#searchForm').attr('action','http://www.maskane.com/'+transaction+'-'+str_replace(' ','+',quoi)+'-'+str_replace(' ','+',ou)+'/p'+$('#cp').val()+'/');
			$('#searchForm').submit();
		}else{
			jAlert("Veuillez séléctionner une transation");
		}
	});
	
	$('#checkPhoto').change(function(){
		if($('#checkPhoto').is(':checked')){
			$('#avecPhoto').val('1');
		}else{
			$('#avecPhoto').val('0');
		}
		$('#cp').val('1');
		var quoi 		= 'immobilier';
		var ou			= 'maroc';
		var transaction	= '';
		if($('#quoi').val() != ''){
			quoi = $('#quoi').val();
		}
		if($('#ou').val() != ''){
			ou = $('#ou').val();
		}
		if($('#transactionVente').is(':checked') == true || $('#transactionLocation').is(':checked') == true){
			if($('#transactionVente').is(':checked') == true){
				transaction = 'Vente';
			}
			if($('#transactionLocation').is(':checked') == true){
				transaction	= 'Location';
			}
			$('#searchForm').attr('action','http://www.maskane.com/'+transaction+'-'+str_replace(' ','+',quoi)+'-'+str_replace(' ','+',ou)+'/p'+$('#cp').val()+'/');
			$('#searchForm').submit();
		}else{
			jAlert("Veuillez séléctionner une transation");
		}
	});

	$('#transactionAchat').change(function(){
		$('#cp').val('1');
		$('#prix').val('');
		$('#surface').val('');
	});
	
	$('#transactionLocation').change(function(){
		$('#cp').val('1');
		$('#prix').val('');
		$('#surface').val('');
	});
	
	$('.filtreTypeBien').click(function(){
		$('#cp').val('1');
		$('#quoi').val($(this).children('span').html());
		$('#chercher').click();
	});
	
	$('.filtreTypeBienOff').click(function(){
		$('#cp').val('1');
		$('#quoi').val('');
		$('#chercher').click();
	});		$('.filtreTransaction').click(function(){		var transasction =	$(this).children('span').html();		if(transasction == 'Vente'){			$('#transactionVente').attr('checked', true);		}		if(transasction == 'Location'){			$('#transactionLocation').attr('checked', true);		}		$('#cp').val('1');		$('#chercher').click();	});
	
	$('.filtreVille').click(function(){
		$('#cp').val('1');
		$('#ou').val($(this).children('span').html());
		$('#chercher').click();
	});
	
	$('.filtreVilleOff').click(function(){
		$('#cp').val('1');
		$('#ou').val('');
		$('#chercher').click();
	});
	
	$('.filtrePrix').click(function(){
		$('#cp').val('1');
		$('#prix').val($(this).children('span').html());
		$('#chercher').click();
	});
	$('.filtrePrixOff').click(function(){
		$('#cp').val('1');
		$('#prix').val('');
		$('#chercher').click();
	});
	
	$('.filtreSurface').click(function(){
		$('#cp').val('1');
		$('#surface').val($(this).children('span').html());
		$('#chercher').click();
	});
	$('.filtreSurfaceOff').click(function(){
		$('#cp').val('1');
		$('#surface').val('');
		$('#chercher').click();
	});
	
	$("a.alerte").fancybox({
		'frameWidth': 500,
		'frameHeight': 100,
		'hideOnContentClick': false 
	});
	
	$('form[name=alert]').live('submit',function(){
		var container = $(this).parent(':first');
		var	obj		  = $(this);
		$.ajax({
			type: "post",
			url: "/alerte.php",
			data: "to=" + container.find('input[name=to]').val() + "&key=" + container.find('input[name=key]').val(),
			success: function(msg){
				obj.show();
				$('#loader').remove();
				jAlert(msg);
			}
		});
		obj.hide();
		container.append('<img src="/Templates/Default/images/load.gif" id="loader" />');
		return false;
	});
	
	$("a.fancybox").fancybox({
		'frameWidth': 500,
		'frameHeight': 470,
		'hideOnContentClick': false 
	}); 
	
	// add a "rel" attrib if Opera 7+
    if(window.opera) {
        if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
            $("a.jqbookmark").attr("rel","sidebar");
        }
    }
   
    $("a.jqbookmark").click(function(event){
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = "http://www.maskane.com/";
        var title = "Maskane - Le moteur de recherche de l'immobilier au maroc";
       
        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            jAlert('Votre navigateur ne support pas cette action');
        }
    });
});
