// JavaScript Document
$(document).ready(function(){
						   
	var counter 		= 1;
	var bounce			= 35;
	var container		= $('#survey_body');
	var btn_next		= $('.btn_next');
	var btn_prev		= $('.btn_prev');
	var btn_close		= $('.btn_close');
	var slideWidth		= 440+15; //manualy set as iframe can't call propper width
	var totalSlides		= $(".slide").length;
	var totalNotices	= $(".notice").length;
	var q_history		= new Array();
	q_history.push(1);
	
	$(container).width(slideWidth*(totalSlides+totalNotices));
	
	$(btn_next).bind("click", function(){
		//get answer for question and process
		process(counter, $("#a"+counter).val());
		if(counter > 1){
			$(btn_prev).css("visibility", "visible");
		}
	});
	
	$(btn_prev).bind("click", function(){
		q_history.pop();
		no = q_history.length-1;
		slideTo = q_history[no];
		slideFrom = counter;
		slide = (slideFrom - slideTo) * slideWidth;
		$(container).animate({"left": "+="+(slide)+"px"}, "500");
		counter = q_history[no];
		for (i=totalSlides;i>counter;i--){
			$("#n"+i).css("font-weight", "normal");
		}
		if(counter == 1){
			$(btn_prev).css("visibility", "hidden");
		}
		$(btn_next).css("visibility", "visible");
	});
	
	$(btn_close).bind("click", self.parent.tb_remove);

	function process(q, a){

		if (q == 1){
			//Which input do you require?
			if (a == "AC"){displayQuestion(2);}
			if (a == "DC"){displayNotice(1);}
		}
		if (q == 2){
			//Do you require more than 4 outputs?
			if (a == 1){displayQuestion(5);}
			if (a == 0){displayQuestion(3);}
		}
		if (q == 3){
			//Do you require more than 500 watts?
			if (a == 1){displayQuestion(5);}
			if (a == 0){displayQuestion(4);}
		}
		if (q == 4){
			//Do you requiire a special voltage?
			if (a == 1){displayQuestion(5);}
			if (a == 0){displayNotice(2);}
		}
		if (q == 5){
			//Will the AC input be Single or 3-Phase?
			if (a == "Single"){displayQuestion(7);}
			if (a == "3-phase"){displayQuestion(6);}
		}
		if (q == 6){
			//Do you require convection cooling?
			if (a == 1){displayNotice(3);}
			if (a == 0){displayFinish("iVS");}
		}
		if (q == 7){
			//Are output OR-ing diodes required?
			if (a == 1){displayNotice(4);}
			if (a == 0){displayQuestion(8);}
		}
		if (q == 8){
			//Is medical 60601-1 approval required?
			if (a == 1){displayQuestion(10);}
			if (a == 0){displayQuestion(9);}
		}
		if (q == 9){
			//Does the unit require intelligence, monitor or control?
			if (a == 1){displayQuestion(10);}
			if (a == 0){displayQuestion(11);}
		}
		if (q == 10){
			//What power requirements do you need?
			if (a == "750W-1500W"){displayFinish("iMP");}
			if (a == "1501W-4920W"){displayFinish("iVS");}
		}
		if (q == 11){
			//Will the output power be more than 1500 watts?
			if (a == 1){displayFinish("iVS");}
			if (a == 0){displayFinish("MP");}
		}
	}
	
	function displayQuestion(q){
		q_history.push(q);
		slideTo = q;
		slideFrom = counter;
		slide = (slideTo - slideFrom) * slideWidth;
		$(container).animate({"left": "-="+(slide)+"px"}, "500");
		counter = slideTo;
		for (i=1;i<=counter;i++){
			$("#n"+i).css("font-weight", "bold");
		}
	}
	
	function displayNotice(n){
		q_history.push("n");
		slideTo = totalSlides+n;
		slideFrom = counter;
		slide = (slideTo - slideFrom) * slideWidth;
		$(container).animate({"left": "-="+(slide)+"px"}, "500");
		counter = slideTo;
		$(btn_next).css("visibility", "hidden");
		$("#survey_nav li").css("font-weight", "bold");
	}
	
	function displayFinish(product){
		
		string = '';
		var i;
		for (i in q_history){
			q = 'q';
			q += q_history[i];
			string += q+'='+$("#a"+q_history[i]).val()+'&';
		}
		string += 'product='+product+'&';
		//string = string.substr(0,string.length-1);
		url = 'http://www.powerconversion.com/imp/complete.php?'+string+'KeepThis=true&TB_iframe=true&width=528&height=355&modal=true';
		self.parent.tb_show('', url, '');
		counter = 0;
	}
});


