// JavaScript Document
var selectTipAgain = 100;
var selectTip = 100;

$(function(){

//tran popup
$(".tran").click(function(){			
	//load the popup div
	loadPopup();
	//center popup
	centerPopup();
});

$("#backgroundPopup").click(function(){
	disablePopup();									 
});

//energy saving tip bubble
$(".energy").click(function(e){
	e.preventDefault();
	
	//run function to choose a random energy saving tip
	displayRandom();
	
	$("#est").fadeIn(100);
});

$("#est").click(function(e){
	$("#est").fadeOut(100);				 
});


//dropdown menu initialization and mouseOver delay code		   
	
	//heating
	$('area#heating').hoverIntent({ 
		sensitivity: 2, // sensitivity of the mouseOver
		interval: 100,  // milliseconds until browser checks for mouseOver event 
		over: showHeating,
		out: {}
	}); 

	$('div.heating').hoverIntent({
		sensitivity: 2,
		timeout: 100, // milliseconds until mouseOut event occurs
		over: {},
		out: function(){
			$('div.heating').fadeOut();		  
		}						 
	});
	
	//cooling
	$('area#cooling').hoverIntent({ 
		sensitivity: 2, // sensitivity of the mouseOver
		interval: 100,  // milliseconds until browser checks for mouseOver event 
		over: showCooling
	}); 

	$('div.cooling').hoverIntent({
		sensitivity: 2,
		timeout: 100, // milliseconds until mouseOut event occurs
		out: function(){
			$('div.cooling').fadeOut();		  
		}						 
	});
	
	//attempting to remove dropdowns
	$("#main, #left_nav, #header").hover(function(){
			$('div.cooling').fadeOut();	
			$('div.heating').fadeOut();	
	});
	
});

//Random Energy Saving Tip Function
function displayRandom(){

	// number of images to rotate
	var numberOfTips = 10;  
	
	// array of energy saving tips
	var estips = new Array();
	estips[0] = "During the cooling season, leave shades and drapes closed during the day to prevent solar gain.";
	estips[1] = "During the heating season, open shades during the day to gain heat from the sun, and close at night to retain heat and keep out chill from cold windows.";
	estips[2] = "Set your thermostat in winter to 68 degrees or less during the daytime, and 55 degrees before going to sleep, set to as warm as is comfortable in the summer.";
	estips[3] = "Set your water heater between 120-130 degrees.";
	estips[4] = "If you have ceiling fans, in the winter, change the setting so the blades spin the opposite direction, pushing warm air down into the room.";
	estips[5] = "Avoid using the oven during hot summer months, instead grill outside or chose low heat cooking methods to save on cooling costs.";
	estips[6] = "Unplug chargers when you aren’t charging. They use energy even when not in use.";
	estips[7] = "Replace light-bulbs with energy saving bulbs to save up to 75% on lighting costs. ";
	estips[8] = "Buy energy-efficient appliances such as refrigerators and air conditioners.";
	estips[9] = "Make sure your home is well insulated, especially the attic, where up to 20% of heat can escape.";
	
	// select a random number between 1 and numberOfImages
	while( selectTip == selectTipAgain ){
		selectTip = parseInt(Math.random() * numberOfTips);
	}
	
	selectTipAgain = selectTip;
	
	//change text of energy saving tip bubble to the randomly selected tip
	$("#est_content").text(estips[selectTip]);
	
}

//showDrop | display the dropdown menu, if the menu is longer than 200px, give it a scroll bar!

//heating
function showHeating(){

	$('div.heating').css({
		'width': 'auto',
		'height': 'auto',
		'top': '210px',
		'left': '120px',
		'z-index': '10'
	});
	
	$('div.heating').fadeIn();	
	$('div.cooling').fadeOut();
	
	$('div.heating').dcCreate({
		   imgPrefix: "images/nav/corners/",
		   fileType:  ".png",
		   expand: 18,
		   position: "outside"
	});
}

//cooling
function showCooling(){

	$('div.cooling').css({
		'width': 'auto',
		'height': 'auto',
		'top': '310px',
		'left': '100px',
		'z-index': '10'
	});
	
	$('div.cooling').fadeIn();
	$('div.heating').fadeOut();

	$('div.cooling').dcCreate({
		   imgPrefix: "images/nav/corners/",
		   fileType:  ".png",
		   expand: 18,
		   position: "outside"
	});
}

//popup code ************************************

//0 means disabled; 1 means enabled;  
var popupStatus = 0;

function disablePopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#popup").fadeOut("slow"); 
		popupStatus = 0;
		
	}  
}

//centering popup  
function centerPopup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popup").height();  
	var popupWidth = $("#popup").width();  
	//centering  
	$("#popup").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
	//only need force for IE6  
 
	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
  
}  

//load popup
function loadPopup(){
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popup").fadeIn("slow");  
		popupStatus = 1; 
		
/*		//add divcorners
		$("#popup").dcCreate({
			imgPrefix: "images/corners/popup-",
			fileType:  ".png",
			expand: 22,
			position: "outside"		 
		});
*/
		
	}
}

