// Newsletter Signup Popup
var popupStatus = 0;

function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		popupStatus = 1;  
	}  
}  

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//request data for centering
	//var windowWidth = $(window).width();
	var windowWidth = myWidth;
	var windowHeight = myHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
	"position": "absolute",
	"top": (windowHeight/2-popupHeight/2) + $(window).scrollTop(),
	"left": (windowWidth/2-popupWidth/2)
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
	"height": $(document).height()
	});
}

function displayPopup() {
	var randomnumber=Math.floor(Math.random()*3);
	if ($.cookie('popNewsDay') == 1 || $.cookie('popNewsMonth') == 1 || $.cookie('newsSignedUp') == 1 || randomnumber > 0) {
	// do nothing
	} else  {
		centerPopup();
		loadPopup();
		$(window).scroll(function () { 
			centerPopup();
		});
		date = new Date();
        date.setTime(date.getTime() + (20 * 60 * 1000));
		$.cookie('popNewsDay', '1', { expires: date, path: '/' });
	}
}
// End Newsletter Signup Popup

function openWindow(loc,title,params) {
	popupWin = window.open(loc, title, params);
}

function toggleDivSlide(item_id) {
	$('#'+item_id).toggle("slow", function() {
		if($(this).hasClass("close")) {
			$(this).removeClass("close");
			alert("Close removed!");
		} else {
			$(this).addClass("close");
			alert("Close added!");
		}
	});
}

// Basic jscript for all pages, includes Site Topics hover boxes
$(document).ready(function() {	
	headerClicked = false;
	topnavClicked = false;
	/*
	$('#header').click(function() {
		if (headerClicked) {
			$(this).css("background-image", "url(/images/layout/header_bk3.jpg)");
			headerClicked = false;
		} else {
			$(this).css("background-image", "url(/images/layout/header_bk4.jpg)");
			headerClicked = true;
		}
	});
	$('#topNav').click(function() {
		if (headerClicked) {
			$(this).css("background-image", "url(/images/layout/topnav_bk4.jpg)");
			$(this).find("a").css("color", "#FFFFFF");
			topnavClicked = false;
		} else {
			$(this).css("background-image", "url(/images/layout/topnav_bk5.jpg)");
			$(this).find("a").css("color", "#003399");
			topnavClicked = true;
		}
	});
	*/
	
	$("input.searchTerm, input#email").focus(function() {
		origSearchTerm = $(this).val();
		$(this).val("");
	});
	$("input.searchTerm, input#email").blur(function() {
		if ($(this).val() == "") 
		$(this).val(origSearchTerm);
	});
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$("#popupLater").click(function(){
		$.cookie('popNewsMonth', '1', { expires: 30, path: '/' });
		disablePopup();
	});
	
	// For topnav hover behavior in IE6
	$("#topNav li").mouseenter(function(){
		$(this).addClass("hov");
	});
	$("#topNav li").mouseleave(function(){
		$(this).removeClass("hov");
	});
	
	// References section updater
	if ($('p:contains("References:")').length) {
		$ref_contents = $('p:contains("References:")').nextUntil('p#article_requirements').detach();
		$('p:contains("References:")').replaceWith('<div id="references_wrapper"><h3 id="link_references">References</h3><div id="references"></div></div>');
		$('#references').append($ref_contents);
	}
	
	$('#link_references').click(function() {
		if ($(this).hasClass('close')) {
			$(this).removeClass('close');
		} else {
			$(this).addClass('close');
		}
		$(this).next().toggle(400);
		return false;
	});
	
	var t = window.setTimeout(displayPopup,15000);
	
	// Default popup
	$("a.popup").click(function() {
		popupWin = window.open($(this).attr('href'), '', 'toolbar=yes,scrollbars,resizable,width=1000,height=650');
		return false;
	});
});
