/*
 * Eco Calculette - Bouygues Immobilier - Developpement Durable - Habitat & Environnement.
 * Web Agency : DesGensBien.com
 *
 * Design : Cedric Montana
 * Coding : Pierre Figueroa
 *
 * release 1.0 (01 June 2009)
*/


	/*****************************************************/
	/* Config part                                       */
	/*****************************************************/


	var myQuizzURL = ""; //must be initialiazed with the QUIZZ URL or left blank-----> var myQuizzURL = "http://www.desgensbien.com"


	/*****************************************************/
	/* end config part                                   */
	/*****************************************************/

	var myCurrentStep = 1;
	var couldMove = true;
	var isStepBis = false;
	var myCurrentBis = "electricity";
	var myBlinkIndex = 0;
	var myBlinkTimer;
	var myBlinkTempo;

	var mySlideShowTimer;
	myCurrentSlideShowImage = 0;
	mySlideShowImages = new Array();
	mySlideShowImages[0] = "/scripts/dgb_calculette_HE/images/isolation/murs.jpg";
	mySlideShowImages[1] = "/scripts/dgb_calculette_HE/images/isolation/toit.jpg";
	mySlideShowImages[2] = "/scripts/dgb_calculette_HE/images/isolation/fenetres.jpg";
	
	var mySurface	= "";
	var myAge		= "";
	var myAgeLib	= "";
	var myNbPers	= "";

	var myElectricConsumptionIndex = new Array();	// consommation au mē
	myElectricConsumptionIndex[0] = 400;			// avant 1965
	myElectricConsumptionIndex[1] = 250;			// entre 1965 et 1990
	myElectricConsumptionIndex[2] = 185;			// entre 1990 et 2000
	myElectricConsumptionIndex[3] = 150;			// entre 2000 et 2005
	myElectricConsumptionIndex[4] = 110;			// depuis 2006

	myBouyguesElectricConsumptionIndex = 100;

	myWaterConsumptionIndex = 365.25*137; //137 daily litre --> x365 for a year
	myBouyguesWaterSavingFactor = 0.525; // = (0,2*0,4)+((0,13+0,39)*0,4)+(0,15)+(0,15*(0,13+0,39+0,06)) taken from hardcoded values in provided .xls file



	$(document).ready(function(){
		if($("#dgb_calculetteHE").length > 0){
			$("#titre").hide();

			$.getScript("/scripts/dgb_calculette_HE/jquery.numeric.js");
			$.getScript("/scripts/dgb_calculette_HE/jquery.thickbox.js");
			

			var fileref=document.createElement("link");
			  fileref.setAttribute("rel", "stylesheet");
			  fileref.setAttribute("type", "text/css");
			  fileref.setAttribute("href", "/scripts/dgb_calculette_HE/eco-calculette.css");
			  document.getElementsByTagName("head")[0].appendChild(fileref);
			var fileref2=document.createElement("link");
			  fileref2.setAttribute("rel", "stylesheet");
			  fileref2.setAttribute("type", "text/css");
			  fileref2.setAttribute("href", "/scripts/dgb_calculette_HE/jquery.thickbox.css");
			  document.getElementsByTagName("head")[0].appendChild(fileref2);


			$("#animflash")
				.hide()
				.css("left", "215px")
				.css("top", "125px")
				.empty()
				.css("height", "380px")
				.load("/scripts/dgb_calculette_HE/eco-calculette.php?rnd=" + Math.random(), 
						function(){
							
							$(".rounded").append("<div class=\"top-right-corner\"></div><div class=\"bottom-right-corner\"></div><div class=\"top-left-corner\"></div><div class=\"bottom-left-corner\"></div>");
							
							$("#animflash .calculette-nav .step1").hover(function(){if(myCurrentStep > 1) $(".rollover", this).show().css("cursor", "pointer").bind("click", function(){GoToStep(1)});},function(){if(myCurrentStep > 1) $(".rollover", this).hide().css("cursor", "normal").unbind("click");});
							$("#animflash .calculette-nav .step2").hover(function(){if(myCurrentStep > 2) $(".rollover", this).show().css("cursor", "pointer").bind("click", function(){GoToStep(2)});},function(){if(myCurrentStep > 2) $(".rollover", this).hide().css("cursor", "normal").unbind("click");});
							$("#animflash .calculette-nav .step3").hover(function(){if(myCurrentStep > 3) $(".rollover", this).show().css("cursor", "pointer").bind("click", function(){GoToStep(3)});},function(){if(myCurrentStep > 3) $(".rollover", this).hide().css("cursor", "normal").unbind("click");});
							$("#animflash .calculette-nav .step4").hover(function(){if(myCurrentStep > 4) $(".rollover", this).show().css("cursor", "pointer").bind("click", function(){GoToStep(4)});},function(){if(myCurrentStep > 4) $(".rollover", this).hide().css("cursor", "normal").unbind("click");});

							$("#animflash .calculette-nav .slider").bind("click", function(){GoNextStep()});
							$("#animflash .calculette-saving-rollover .spot1").hover(function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px -280px'});$("." + myCurrentBis + "-spot1, ." + myCurrentBis + "-source-spot1").show();StopBlink();},
																					 function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px 0px'});$("." + myCurrentBis + "-spot1, ." + myCurrentBis + "-source-spot1").hide();InitBlink();});
							$("#animflash .calculette-saving-rollover .spot2").hover(function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px -560px'});$("." + myCurrentBis + "-spot2, ." + myCurrentBis + "-source-spot2").show();StopBlink();},
																					 function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px 0px'});$("." + myCurrentBis + "-spot2, ." + myCurrentBis + "-source-spot2").hide();InitBlink();});
							$("#animflash .calculette-saving-rollover .spot3").hover(function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px -840px'});$("." + myCurrentBis + "-spot3, ." + myCurrentBis + "-source-spot3").show();StopBlink();},
																					 function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px 0px'});$("." + myCurrentBis + "-spot3, ." + myCurrentBis + "-source-spot3").hide();InitBlink();});
							$("#animflash .calculette-saving-rollover .spot4").hover(function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px -1120px'});$("." + myCurrentBis + "-spot4, ." + myCurrentBis + "-source-spot4").show();StopBlink();},
																					 function(){$(".calculette-saving-rollover .rolloverimage").css({backgroundPosition: '0px 0px'});$("." + myCurrentBis + "-spot4, ." + myCurrentBis + "-source-spot4").hide();InitBlink();});
							$("#animflash .calculette-content .numeric").numeric();

							$("<div class='start-button' />")
								.bind("click", function(){
									$(".calculette-content .step0, .start-button").hide();
									$(".calculette-content .step1, .calculette-nav").show();
									})
								.appendTo($("#animflash"));
							
							if(myQuizzURL != ""){
								$(".go-quizz")
									.attr("target", "_blank")
									.attr("href", myQuizzURL)
							}
							$(this).show();
						});

		}
	});


	// CalculateHEBenefits : perform calculation of benefits (and display the result)
	//
	function CalculateHEBenefits(){
		mySurface	= parseInt(mySurface);
		myAge		= parseInt(myAge);
		myNbPers	= parseInt(myNbPers);
		$(".user-settings-display-size").html(mySurface + " m&sup2;");
		$(".user-settings-display-age").html(myAgeLib);
		$(".user-settings-display-pers").html(myNbPers + " personne" + ((myNbPers>1)? "s " : " "));
		
		//electricity
		$(".calculette-content .step2 .conso-normale SPAN").html(FormatNumberForDisplay(mySurface*myElectricConsumptionIndex[myAge]) + " KWH / AN *");
		$(".calculette-content .step2 .conso-bouygues SPAN").html(FormatNumberForDisplay(mySurface*myBouyguesElectricConsumptionIndex) + " KWH / AN **");
		myElectricSaving = Math.round((1-(myBouyguesElectricConsumptionIndex/myElectricConsumptionIndex[myAge]))*100);

		$(".calculette-content .step2 .electric_saving").html(myElectricSaving + "%**");
		$(".calculette-content .step4 .electric_saving").html(myElectricSaving + "%*");
		
		//water
		$(".calculette-content .step3 .conso-normale SPAN").html(FormatNumberForDisplay(Math.round(myNbPers*myWaterConsumptionIndex)) + " L d'eau *");
		$(".calculette-content .step3 .conso-bouygues SPAN").html(FormatNumberForDisplay(Math.round(myNbPers*myWaterConsumptionIndex*(1-myBouyguesWaterSavingFactor))) + " L d'eau **");
		WaterSaving = Math.round(myBouyguesWaterSavingFactor*100);

		$(".calculette-content .step3 .water_saving").html(WaterSaving + "%**");
		$(".calculette-content .step4 .water_saving").html(WaterSaving + "%*");
			
	}

	// ShowSavingRollOver : launch the rollover initialization for steps 2 and 3
	//
	function ShowSavingRollOver(){
		$(".calculette-saving-rollover .rolloverimage").removeClass("electricity").removeClass("water");
		$(".calculette-saving-rollover .rolloverimage DIV").hide();
		switch(myCurrentStep){
			case 2:
				myCurrentBis = "electricity";
				$(".calculette-saving-rollover .spot4").bind("click", function(){LaunchSlideShow()});
				$(".calculette-saving-rollover .rolloverimage").addClass("electricity");
				$(".calculette-saving-rollover .instructions-electricity").show();
				$(".calculette-saving-rollover .instructions-water").hide();
				$(".calculette-content .step2 .limited-width-content").fadeOut(function(){$(".calculette-saving-rollover").fadeIn();})
				break;
			case 3:
				myCurrentBis = "water";
				$(".calculette-saving-rollover .spot4").unbind("click");
				$(".calculette-saving-rollover .rolloverimage").addClass("water");
				$(".calculette-saving-rollover .instructions-electricity").hide();
				$(".calculette-saving-rollover .instructions-water").show();
				$(".calculette-content .step3 .limited-width-content").fadeOut(function(){$(".calculette-saving-rollover").fadeIn();})
				break;
		}
	}


	// GoNextStep : calculate the next Step and launch the transition function (GoToStep(...))
	//
	function GoNextStep(){
		GoToStep((myCurrentStep < 5) ? myCurrentStep+1 : 1);
	}
	
	// GoToStep : Transition function from a step to an other
	// Note: "step bis" are steps with rollover images (step 2 and 3)
	function GoToStep(myStepNumber){
		if((myCurrentStep != myStepNumber) && (couldMove)){
			if(((myCurrentStep == 2) || (myCurrentStep == 3)) && (!isStepBis) && (myStepNumber > myCurrentStep)){
				isStepBis = true;
				InitBlink();
				ShowSavingRollOver();
				return(false);
			} else {
				isStepBis = false;
				clearTimeout(mySlideShowTimer);
				clearInterval(myBlinkTimer);
				$(".calculette-saving-rollover:visible").fadeOut("fast");
			}
			
			couldMove = false;
			$(".error").hide();
			if(myCurrentStep == 1){
				//check the values of the 1rst form
				mySurface	= $("INPUT[name='txt_size']").attr("value");
				myAge		= $("SELECT[name='select_year']").attr("value");
				myAgeLib	= $("SELECT[name='select_year'] OPTION[value='" + myAge + "']").text();
				myNbPers	= $("INPUT[name='txt_nb_pers']").attr("value");

				if((mySurface == "") || (!CheckNumeric(mySurface))){
					$(".error_size").show();
					couldMove = true;
					return(false);
				}
				if((myNbPers == "") || (!CheckNumeric(myNbPers))){
					$(".error_pers").show();
					couldMove = true;
					return(false);
				}
				
				CalculateHEBenefits();
			}
			
			$("#animflash .calculette-nav .step" + myCurrentStep + " .rollover").fadeOut();
			$("#animflash .calculette-content .step" + myCurrentStep).fadeOut(function(){$("#animflash .calculette-content .step" + myStepNumber).fadeIn();couldMove = true;});
			myCurrentStep = myStepNumber;
			switch(myStepNumber){
				case 2:
					mySliderPosX = "169px";
					myBackgroundPosY = "-18px";
					mySliderOpacity = 1;
					break;
				case 3:
					mySliderPosX = "273px";
					myBackgroundPosY = "-36px";
					mySliderOpacity = 1;
					break;
				case 4:
					mySliderPosX = "376px";
					myBackgroundPosY = "-54px";
					mySliderOpacity = 1;
					break;
				case 5:
					mySliderPosX = "450px";
					myBackgroundPosY = "-54px";
					mySliderOpacity = 0;
					break;
				case 1:
				default:
					mySliderPosX = "60px";
					myBackgroundPosY = "0px";
					mySliderOpacity = 1;
					//re-init rollover parts
					break;
			}
			$("#animflash .calculette-nav .slider")
				.css("backgroundPosition" , "0px " + myBackgroundPosY)
				.animate({"left": mySliderPosX, "opacity" : mySliderOpacity}, "slow");
			$("#animflash .calculette-nav .step" + myCurrentStep + " .rollover").fadeIn("normal", function(){$(".calculette-content .limited-width-content").show();});
			
		}
	}


	// FormatNumberForDisplay : Format a number from 1000000 to 1 000 000
	//
	function FormatNumberForDisplay(myNumber){
		myWorkingNumber = myNumber.toString();
		myResult = "";
		while (myWorkingNumber.length>0){
			if(myResult != "") myResult = " " + myResult;
			myPosStart = myWorkingNumber.length - 3;
			if(myPosStart < 0) myPosStart = 0;
			myResult = myWorkingNumber.substr(myPosStart,3) + myResult;
			myWorkingNumber = myWorkingNumber.substr(0,myWorkingNumber.length - 3)
		}
		return(myResult);
	}

	// AutoTrimStep1 : trim the value of the Object (remove also spaces inside the string)
	//
	function AutoTrimStep1(myObject){
		myObject.value = TrimString(myObject.value);
		myResultString = "";
		for(cptChar = 0; cptChar < myObject.value.length; cptChar++){
			myCharTmp = myObject.value.charAt(cptChar);
			if(myCharTmp != ' ') myResultString += myCharTmp;
		}
		myObject.value = myResultString;
	}


	/***************************************************************/
	/* Slideshow functions (on step 2, after click on point 4)     */
	/***************************************************************/

	var ChangeSlideShowImage = function(){
		myCurrentSlideShowImage++;
		if(myCurrentSlideShowImage+1 > mySlideShowImages.length) myCurrentSlideShowImage = 0;
		$("#TB_window IMG").fadeOut(500,function(){
													$(this).attr("src", mySlideShowImages[myCurrentSlideShowImage]).fadeIn(500, function(){mySlideShowTimer = setTimeout(ChangeSlideShowImage,3000);});
													});
	}
	
	function LaunchSlideShow(){
		tb_show("", "#TB_inline?height=360&width=580&inlineId=hiddenModalMessageContent&modal=true", null);
		mySlideShowTimer = setTimeout(ChangeSlideShowImage,3000);
	}


	/*****************************************************/
	/* Blinkin functions (on step 2 and 3 rollovers)     */
	/*****************************************************/

	function InitBlink(){
		clearInterval(myBlinkTimer);
		clearTimeout(myBlinkTempo);
		myBlinkIndex = 1;
		myBlinkTempo = setTimeout(	function(){
										myBlinkTimer = setInterval("BlinkLinks()",100);
									}, 1000);
	}

	function StopBlink(){
		clearInterval(myBlinkTimer);
		clearTimeout(myBlinkTempo);
		myBlinkIndex = 0;
		BlinkLinks();
	}

	function BlinkLinks(){
		mustBlink = ($(".blink-link").length == 0);

		if(!mustBlink){
			$(".calculette-saving-rollover .rolloverimage A").removeClass("blink-link");
			if(myBlinkIndex == 1){
				InitBlink();
			}
		}
		
		if(myBlinkIndex == 0){
			clearInterval(myBlinkTimer);
			clearTimeout(myBlinkTempo);
		} else {
			if(mustBlink){
				$(".calculette-saving-rollover .rolloverimage A.spot" + myBlinkIndex).addClass("blink-link");
				if(myBlinkIndex == 4){
					myBlinkIndex = 1;
				} else {
					myBlinkIndex++;
				}
			}
		}
	}


	/*****************************************************/
	/* Common functions (toolbox)                        */
	/*****************************************************/


	function CheckNumeric(myString){
		return (myString.search("[^0-9]") < 0);
	}

	function LTrimString(myString){
		if (myString.length > 0) {
			if (myString.substr(0,1) == ' ' ) return(LTrimString(myString.substr(1)));
		}
		return (myString);
	}
	function RTrimString(myString){
		if (myString.length > 0) {
			if (myString.substr(myString.length - 1,1) == ' ' ) return(RTrimString(myString.substr(0, myString.length - 1)));
		}
		return (myString);
	}

	function TrimString(myString){
		return(LTrimString(RTrimString(myString)));
	}

	