/* htdocs/fergweb/corp/js/consultForm.js
   $Id: consultForm.js,v 1.10 2012-02-16 17:52:02 gwardwell Exp $ */
// Code to launch consultation

function closeQuiz(){
	$('#quiz-close').hide();
	$('#quiz-overlay').fadeOut();
	$('#quiz-wrap').hide();
}

function launchconsult(){
	$('#quiz-iframe').attr("src","/homeowner/consultation.pl");
	scroll(0,0);
	var arrPageSizes = getPageSize();
	var arrPageScroll = getPageScroll();	  	
	$('#quiz-overlay').css({
  	width:                arrPageSizes[0],
  	height:               arrPageSizes[1]
	}).fadeIn(function() {
	$('#quiz-wrap').css({
		width:900,
		height:536,
		display: "block",
		top:arrPageScroll[1] + (arrPageSizes[3] / 5),
		left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2
	}).fadeIn();
	$('#quiz-close').css({
		width:51,
		height:49,                	
		top:arrPageScroll[1] + (arrPageSizes[3] / 5)-13,
		left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2+900-39,
		background: 'url(/consultation/images/btn-close.png) no-repeat left top'            
	}).click(function(){
		closeQuiz();
	}).show();
	$("#quiz-close img").attr("src","/consultation/images/btn-close-hover.png");
	$("#quiz-close img").css({opacity:0,width:51,height:49});
	$("#quiz-close img").hover(function(){
			$(this).stop().animate({ opacity: 1 }, 'fast');
		}, function(){ // on mouse out
				$(this).stop().animate({ opacity: 0 }, 'fast');
		});
	});
}

function launchconsult_with_query(email,fname,lname){
	$('#quiz-iframe').attr("src","/homeowner/consultation.pl?email=" + email + "&fname=" + fname + "&lname="+ lname);
	scroll(0,0);   		
	var arrPageSizes = getPageSize();
	var arrPageScroll = getPageScroll();	  	

	$('#quiz-overlay').css({
		width:                arrPageSizes[0],
		height:               arrPageSizes[1]
	}).fadeIn(function() {
		$('#quiz-wrap').css({width:900,height:536,top:arrPageScroll[1] + (arrPageSizes[3] / 5),left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2}).fadeIn();
		$('#quiz-close').css({
			width:51,
			height:49,
			top:arrPageScroll[1] + (arrPageSizes[3] / 5)-13,
			left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2+900-39,
			background: 'url(/ferguson/corp.ferguson.com/images/btn-close.png) no-repeat left top'
		}).click(function(){
			closeQuiz();
		}).show();

		$("#quiz-close img").attr("src","/ferguson/corp.ferguson.com/images/btn-close-hover.png");
		$("#quiz-close img").css({opacity:0,width:51,height:49});
		$("#quiz-close img").hover(function(){
			$(this).stop().animate({ opacity: 1	}, 'fast');
				}, function(){ // on mouse out
					$(this).stop().animate({ opacity: 0 }, 'fast');
			});
		});
}

function goToConsultation(myStyle){
	closeQuiz();
	$('#quiz-iframe').attr("src","/homeowner/consultation.pl?style="+myStyle);
	scroll(0,0);   		
	var arrPageSizes = getPageSize();
	var arrPageScroll = getPageScroll();	  	

	$('#quiz-overlay').css({
		width:                arrPageSizes[0],
		height:               arrPageSizes[1]
	}).fadeIn(function() {
		$('#quiz-wrap').css({width:900,height:536,top:arrPageScroll[1] + (arrPageSizes[3] / 5),left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2}).fadeIn();
		$('#quiz-close').css({
			width:51,
			height:49,
			top:arrPageScroll[1] + (arrPageSizes[3] / 5)-13,
			left:arrPageScroll[0] + (arrPageSizes[0] - 900) / 2+900-39,
			background: 'url(/consultation/images/btn-close.png) no-repeat left top'
		}).click(function(){
			closeQuiz();
		}).show();

		$("#quiz-close img").attr("src","/consultation/images/btn-close-hover.png");
		$("#quiz-close img").css({opacity:0,width:51,height:49});

		$("#quiz-close img").hover(function(){
			$(this).stop().animate({ opacity: 1	}, 'fast');
		} , function(){ // on mouse out
			$(this).stop().animate({ opacity: 0 }, 'fast');
		});
	});
}

jQuery(document).ready ( function () {
	$('body').append('<div id="quiz-overlay"></div><div id="quiz-wrap"><iframe id="quiz-iframe" width="100%" height="100%" frameborder="0" border="0" cellspacing="0" scrolling="no" noresize="noresize" marginwidth="0" marginheight="0" src=""></iframe></div><div id="quiz-close"><img src="images/empty.gif" width="30" height="25"/></div>');
	$('#quiz-overlay').css({
		display:"none",
		backgroundColor:    "#02263C",
		opacity:            0.9,
		position:"absolute",
		top:0,
		left:0,
		zIndex:9000
	});

	$('#quiz-wrap').css({
		display:"none",
		width:"100%",
		position:"absolute",
		top:0,
		left:0,
		textAlign: "center",
		zIndex:10000,
		width:852,
		height:432
	});

	$('#quiz-close').css({
		display:"none",
		width:30,
		height:25,
		position:"absolute",
		top:0,
		left:0,
		zIndex:20000
	});

	var arrPageSizes = getPageSize();
	var arrPageScroll = getPageScroll();

	// Launch quiz
	$(".styleQuiz").click(function() {
		$('#quiz-iframe').attr("src","/quiz/quizHTML.html");        
		arrPageSizes = getPageSize();
		$('#quiz-overlay').css({
			width:                arrPageSizes[0],
			height:               arrPageSizes[1]
		}).fadeIn(function() {

		arrPageScroll = getPageScroll();
		$('#quiz-wrap').css({
			width:852,
			height:432,
			top:arrPageScroll[1] + (arrPageSizes[3] / 5),
			left:arrPageScroll[0] + (arrPageSizes[0] - 852) / 2
		}).fadeIn("fast",function(){
			$("#quiz-close img").attr("src","images/empty.gif");
			$("#quiz-close img").css({opacity:1,width:30,height:25});
			$("#quiz-close img").hover(function(){},function(){});            	                
			$('#quiz-close').css({
				width:30,
				height:25,                	
				top:arrPageScroll[1] + (arrPageSizes[3] / 5),
				left:arrPageScroll[0] + (arrPageSizes[0] - 852) / 2+852-30,
				background: 'url(images/empty.gif) no-repeat left top'
			}).click(function(){
				closeQuiz();
			}).hover(function(){
				$(this).css({cursor:"pointer"});
			},function(){}).show();
			});
		});
		return false;
	});

	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		var wstr=$('#quiz-wrap').css('width');
		var ww=Number(wstr.substring(0,wstr.length-2));
		// Get page sizes
		var arrPageSizes = getPageSize();
		// Style overlay and show it
		$('#quiz-overlay').css({
			width:    arrPageSizes[0]<900?900:arrPageSizes[0],
			height:   arrPageSizes[1]
		});
		// Get page scroll
		var arrPageScroll = getPageScroll();
		// Calculate top and left offset for the jquery-lightbox div object and show it
		$('#quiz-wrap').css({
			top:    arrPageScroll[1] + (arrPageSizes[3] / 5),
			left:    arrPageScroll[0] + (arrPageSizes[0] - ww) / 2
		});
		var closewstr=$('#quiz-close').css('width');
		var closeww=Number(closewstr.substring(0,closewstr.length-2));
		var borderW=12;
		if(closeww==30){
			borderW=0;
		}
		$('#quiz-close').css({
			top:arrPageScroll[1] + (arrPageSizes[3] / 5)-borderW,
			left:arrPageScroll[0] + (arrPageSizes[0] - ww) / 2+ww-(closeww-borderW)
		})
	});

	// This code launches the consult from an email link
	var email = getParameterByName('email');
	var fname = getParameterByName('fname');
	var lname = getParameterByName('lname');
	var popup = getParameterByName('pop');
	if (popup && (email || fname || lname)){
		launchconsult_with_query(email,fname,lname);
	} else if (popup){ 
		launchconsult();
	}

	// This code launches the consult from a click on the page
	jQuery(".launchHomeownerConsultationForm").click( function () {
		launchconsult();
	});

	// This code launches the consult from a click on a nav element
	jQuery(".launchHomeownerConsultationFormFromNav").parent().click( function () {
		launchconsult();
	});

});

