//jquery version
//console.log($().jquery);

//PLUGIN duplicate
(function($) {
	$.fn.duplicate = function(count, cloneEvents) {
		var tmp = [];
		for ( var i = 0; i < count; i++ ) {
			$.merge( tmp, this.clone( cloneEvents ).get() );
		}
		return this.pushStack( tmp );
	};	
})(jQuery);

/*##################################################################################################*/
//ERGO 1
//HOME horizontal scroll images
/*##################################################################################################*/

$(document).ready(function(){	
	
	//size of the bloc to reproduce in order to reach the window size
	var objects_total_width = $("#index_container .objects").outerWidth(false);
	var empty_space = 1920 - objects_total_width;//based on a max 1920 width of browser window
	if(objects_total_width > 0) {
		var number_of_times_to_add = Math.ceil(empty_space/objects_total_width);		
		
		//content to append
		var content_to_add = $('ul.objects').html();
		if($('body').hasClass('home')) {
			for ( var i = 0; i < number_of_times_to_add; i++ ) {
			$('ul.objects').append(''+content_to_add+'');
			}
		}
	}
	
	
	var number_of_li = $('ul.objects li').length;
	//console.log(number_of_li);
	//then when we added the necessary li's we can move it all horizontally with this modified plugin:
	$(function(){
	
	
	$('#index_container').infiniteCarousel({
				transitionSpeed: 3500,
				displayTime: 0,
				displayProgressBar: false,
				displayThumbnails: false,
				displayThumbnailNumbers: false,
				displayThumbnailBackground: false,
				inView: number_of_li,
				padding: '0px',
				advance: 1,
				showControls: false,
				autoHideControls: false,
				autoHideCaptions: false,
				autoStart: true,
				prevNextInternal: true
	});
	
	
});
});//end document ready

/*##################################################################################################*/
//ERGO 2
//HOME popup more infos on an object
/*##################################################################################################*/

$(document).ready(function(){	
	
	$('ul.objects .box').mouseover(function(){
				$(this).find('.box_popup').show();
	});
	
	$('.box_popup').mouseout(function(){
				$(this).hide();
	});


});//end document ready

/*##################################################################################################*/
//ERGO 3
//Toggle filter on list object
/*##################################################################################################*/

$(document).ready(function(){	
	
	$('.filterButton').toggle(function() {
  		$('#filters').slideDown({
			duration: 500, 
			easing: 'swing'});
		$(this).addClass('opened');
	}, function() {
	  	$('#filters').slideUp({
			duration: 333, 
			easing: 'easeInCirc'});
		$(this).removeClass('opened');
	});
	if($('#price_from').val() || $('#price_to').val() || $('#piece_from').val() || $('#piece_to').val()) {
		$('#filters').slideDown({
			duration: 500, 
			easing: 'swing'});
		$('.filterButton').addClass('opened');
	}
});//end document ready

/*##################################################################################################*/
//ERGO 4
//Objects details
/*##################################################################################################*/

$(document).ready(function(){	
	
	//tabs switcher
	$('.tabs a').click(function(){			
		//4 possibilities: galerie - details - interested - pdf - financing		
		if ($(this).hasClass('interest')) { 
			var whichpanel = 'interest';
		} else if ($(this).hasClass('pdf')) {
			var whichpanel = 'pdf';
			if('' != $(this).attr('target')) {
				//openPdfDocument($(this).attr('href'));
				var whichpanel = 'doNotOpen';
				return true;
			}		
		} else if ($(this).hasClass('financing')) {
			var whichpanel = 'financing';
		} else if ($(this).attr('id') == 'external'){
			var whichpanel = 'ext';
			return true;
		} else {			
			var whichpanel = $(this).attr('id');
		}
		if ('pdfRentingRequests' != $(this).attr('id') && 'doNotOpen' != whichpanel){
			$('.tabs a').removeClass('selected');
			$('.panel').hide();
			$(this).addClass('selected');
			$('.'+whichpanel).show();
		}
		return false;
	});

});//end document ready

/*##################################################################################################*/
//ERGO 5
//Objects submenus caracteristics + descriptif + visit
/*##################################################################################################*/

$(document).ready(function(){	
	
	//tabs switcher
	$('.submenu a').click(function(){
		$('.details .submenu a').removeClass('selected');
		$('.subPanel').hide();
		
		$(this).addClass('selected');
		
		//3 possibilities: caracterisitcs - descriptif - visit
		if ($(this).hasClass('caracteristiques')) { 
			var whichpanel = 'caracteristics';
		} else if ($(this).hasClass('descriptif')) {
			var whichpanel = 'commodities';
		} else if ($(this).hasClass('situation_submenu')) {
            var whichpanel = 'situation';
        } else if ($(this).hasClass('access_submenu')) {
            var whichpanel = 'access';
        } else if ($(this).hasClass('business_submenu')) {
            var whichpanel = 'business';
        } else if ($(this).hasClass('transport_submenu')) {
            var whichpanel = 'transport';
        } else if ($(this).hasClass('construct_submenu')) {
            var whichpanel = 'construct';
        } else if ($(this).hasClass('soussol_submenu')) {
            var whichpanel = 'soussol';
        } else if ($(this).hasClass('rez_inf_submenu')) {
            var whichpanel = 'rez_inf';
        } else if ($(this).hasClass('rez_submenu')) {
            var whichpanel = 'rez';
        } else if ($(this).hasClass('etage1_submenu')) {
            var whichpanel = 'etage1';
        } else if ($(this).hasClass('etage2_submenu')) {
            var whichpanel = 'etage2';
        } else if ($(this).hasClass('etage3_submenu')) {
            var whichpanel = 'etage3';
        } else if ($(this).hasClass('etage4_submenu')) {
            var whichpanel = 'etage4';
        } else if ($(this).hasClass('comble_submenu')) {
            var whichpanel = 'comble';
        } else if ($(this).hasClass('annexe_submenu')) {
            var whichpanel = 'annexe';
        } else if ($(this).hasClass('amen_ext_submenu')) {
            var whichpanel = 'amen_ext';
        } else if ($(this).hasClass('roofing_submenu')) {
            var whichpanel = 'roofing';
        } else if ($(this).hasClass('particularite_submenu')) {
            var whichpanel = 'particularite';
        } else if ($(this).hasClass('remarque_submenu')) {
            var whichpanel = 'remarque';
        } else {
			var whichpanel = 'visit';
		}
		$('.'+whichpanel).show();
		return false;
	});

});//end document ready

/*##################################################################################################*/
//ERGO 6
//Contact if interested loaded via Ajax directly in the object details
//Contact form
/*##################################################################################################*/
var openPdfDocument = function(target){		
		window.open(target);		
	}


$(document).ready(function(){	
	
	//this happens only on object details
	//no loading is needed because the contact page appears in a hidden panel at first
	var targetobject = $('a.interest').attr('href');
	if($('body').hasClass('detailObject')) $('.panel.interest').load(''+targetobject+' .box_contact',afterLoading);
	
	var targetobject2 = $('a.financing').attr('href');	
	if($('body').hasClass('detailObject')) $('.panel.financing').load(''+targetobject2+' .box_content', afterLoadingForm);
	
	var targetobject3 = $('a.pdf').attr('href');	
	if($('body').hasClass('detailObject') && !$('a.pdf').hasClass('_blank')) $('.panel.pdf').load(''+targetobject3+' .box_content', '');
	
	function afterLoadingForm () {
		$("#simulationFormSubmit").click(function submitFinancing() {					
			$('.panel.financing').load(''+targetobject2+' .content',
										{
											price: 		$("[name=price]").val(),
											fp: 		$("[name=fp]").val(),
											fp_hidden: 	$("[name=fp_hidden]").val(),
											tx_facq: 	$("[name=tx_facq]").val(),
											tx_cedule: 	$("[name=tx_cedule]").val(),
											tx_fmut: 	$("[name=tx_fmut]").val(),
											tx_fp: 		$("[name=tx_fp]").val(),
											tx_fp_hidden:$("[name=tx_fp_hidden]").val(),
											tx_int_hyp1:$("[name=tx_int_hyp1]").val(),
											tx_int_hyp2:$("[name=tx_int_hyp2]").val(),
											tx_am_hyp1: $("[name=tx_am_hyp1]").val(),
											tx_am_hyp2: $("[name=tx_am_hyp2]").val(),
											tx_chex: 	$("[name=tx_chex]").val(),
											charge_expl:$("[name=charge_expl]").val()											
										}, 
										afterLoadingForm);
		});
	}

	
	function afterLoading () {
		//submenu
		$('.box_contact .submenu a').click(function(){
		$('.box_contact .submenu a').removeClass('selected');
		$('.contactPanel').hide();
		
		$(this).addClass('selected');
		
		//2 possibilities: coordonnees - interested by object
		if ($(this).hasClass('objectCoordonnees')) { 
			var whichpanel = 'coordonnees';
		} else {
			var whichpanel = 'interested';
		}
		$('.'+whichpanel).show();
		return false;
		});
		
		
		//process contact form
		formProcessor();
	};
	

});//end document ready

$(document).ready(function(){
	//activate the form processor if on the global contact page
	if($('#contactLink a').hasClass('selected')) formProcessor();
});//end document ready


function formProcessor () {
	
	//print the contact Map only
	$('a.printMap').click(function(){
		$('#googleMapContainer').printElement(); 
	});

	//empty and put back the format phone number if phone number is empty
	var recall = $("input.text.telephone").val();
	$("input.text.telephone").click(function(){
		$(this).val('');
		$(this).removeClass('telephone');
	});
	
	$("input.text.telephone").blur(function(){
		if ($(this).val() == '') {
			$(this).val(recall);
			$(this).addClass('telephone');
		}
	});

	
	//validate form
	/* var contactButton = $("#form_contact a.button");
	contactButton.click(function(){
		
		//process the contact form
		//data
		var firstname = $("input#firstname").val();
		var lastname = $("input#lastname").val();
		var societe = $("input#societe").val();
		var street = $("input#street").val();
		var postalcode = $("input#postalcode").val();
		var city = $("input#city").val();
		var telephone = $("input#telephone").val();
		var email = $("input#email").val();
		var message = $("textarea#texte").val();
		var contact = $("input#contact").val();
		var form_protector_canary = $("input[name=form_protector_canary]").val();
		var know_agency = $("input#know_agency").val();
		
		var phpfile = $("form#form_contact").attr('action');
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		//errors?
		if (firstname == "") {  
			$("input#firstname").addClass('errorInput');
			$("span#spanFirstname").addClass('error');
			return false;  
		} 
		if (firstname != "") {  
			$("input#firstname").removeClass('errorInput');
			$("span#spanFirstname").removeClass('error');  
		}
		
		if (lastname == "") {  
			$("input#lastname").addClass('errorInput'); 
			$("span#spanLastname").addClass('error');
			return false;  
		}  
		if (lastname != "") {  
			$("input#lastname").removeClass('errorInput'); 
			$("span#spanLastname").removeClass('error');
		}  
		
		if (telephone == "") {  
			$("input#telephone").addClass('errorInput'); 
			$("span#spanTelephone").addClass('error');
			return false;  
		}  
		if (telephone != "") {  
			$("input#telephone").removeClass('errorInput'); 
			$("span#spanTelephone").removeClass('error');
		}  
		
		if (email == "") {  
			$("input#email").addClass('errorInput'); 
			$("span#spanEmail").addClass('error');
			return false;  
		}
		if (email != "") {  
			$("input#email").removeClass('errorInput'); 
			$("span#spanEmail").removeClass('error');
		}

		if (!emailReg.test(email)) {
			$("input#email").addClass('errorInput'); 
			$("span#spanEmail").addClass('error');
			return false;  
		}
		
		var dataString = 	'&firstname='+ firstname 
							+ '&lastname=' + lastname 
							+ '&societe=' + societe 
							+ '&street=' + street 
							+ '&postalcode=' + postalcode 
							+ '&city=' + city 
							+ '&telephone=' + telephone 
							+ '&email=' + email 
							+ '&texte=' + message 
							+ '&contact=' + contact 
							+ '&form_protector_canary=' + form_protector_canary
							+ '&know_agency=' + know_agency
							;  

		$.ajax({  
		  type: "POST",  
		  url: phpfile,  
		  data: dataString,  
		  success: function() {  
			$("form#form_contact .tbl").css('display','none');
			$("#contactOK").css('display','block');  
		  }  
		});  
		return false;  
	});*/

}//end function formProcessor ()

/*##################################################################################################*/
//ERGO 7
//personal pages reconstruction in order not to change all pages
/*##################################################################################################*/

$(document).ready(function(){	
	
	if($('body').hasClass('defaultAttitude')) {
	//rebuild box_header
	var contentOfH1 = $('.box_header h1').text();
	var contentOfH2 = $('.box_header h2').text();
	var contentOfH3 = $('.box_header h3').text();
	$('.box_header h1').remove();
	$('.box_header h2').remove();
	$('.box_header h3').remove();
	var contentOfHeader = $('.box_header').html();
	//console.log(contentOfHeader.length);

	$('.box_header').replaceWith('<div class="box_header"><div class="leftContent"><div class="imagePersoWrapper defaultImage">&nbsp;</div></div></div>');
	
	//rebuild content
	$('.box_content').addClass('box_contenu').removeClass('box_content');
	$('.box_contenu').wrap('<div class="box_content"><div class="panels limitWidth"><div class="panel wrapper"></div></div></div>');
	
	$('.box_content').prepend('<h1 class="pagesTitle">'+contentOfH1+' '+contentOfH2+' '+contentOfH3+'</h1>');
	$('.box_content').append('<div class="panelsShadow limitWidth wrapper"><div class="floatLeft"></div><div class="floatRight"></div></div>');
	
	$('.panel.wrapper').prepend(contentOfHeader);
	
	
	//tabs are not active links
	$('a.inactive').click(function(){
			return false;
	});
	
	}//end if defaultAttitude

});//end document ready


/*##################################################################################################*/
//IE6 PNGFIX
/*##################################################################################################*/
$(document).ready(function(){
	$('#menu_object .rent').ifixpng(); 
	$('#menu_object .sale').ifixpng(); 
	$('#menu_object .saleWhite').ifixpng(); 
	$('#menu_object .rentWhite').ifixpng(); 
	$('#horizontalShadow').ifixpng(); 
});//end document ready

/*##################################################################################################*/
//IE6 HOVER FIX
/*##################################################################################################*/
$(document).ready(function(){
	$('#navigation > ul > li').hover(function() {
	  $(this).addClass('hoverEffect');
	}, function() {
	  $(this).removeClass('hoverEffect');
	});
});//end document ready

/*##################################################################################################*/
//BROWSER DETECTION
/*##################################################################################################*/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
 
};
BrowserDetect.init();

/*##################################################################################################*/
//IF IE7
/*##################################################################################################*/

$(document).ready(function(){
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version =="7") {
		$('ul.objects .box').mouseover(function(){
			$(this).next().css('visibility','hidden');
		});
			
		$('ul.objects .box').mouseout(function(){
			$(this).next().css('visibility','visible');
		});
			
		if ($('body').hasClass('home')) {
			$('#horizontalShadow').remove();
			$('#top .rightSide').prepend('<div id="horizontalShadow"></div>');
		}
		
	}
	
	
});//end document ready


   
   
   /*##################################################################################################*/
//IF IE6 --> UPGRADE
/*##################################################################################################*/

$(document).ready(function(){
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version =="6") {					   
		$('#IE6upgrade').fadeIn({
			
		});
		$('#IE6upgrade .upgradeClose').click(function(){
				$('#IE6upgrade').fadeOut({
					
				});
		})
	}
});//end document ready


