var arrPageSizes;

$(document).ready(function(){
	arrPageSizes = ___getPageSize();

	$("input#nl_email, input#nl_name").bind("focus click", function () { $(this).addClass("blank"); });
	$("input#nl_email, input#nl_name").bind("blur", function () { if ($(this).val() == '') { $(this).removeClass("blank"); } });
	
	$("input#nl_submit").bind("click", function(){
		var email = $("input#nl_email").val();
		var name = $("input#nl_name").val();
		$.ajax({
			url : "do?ajax=newslettersignup&email=" + email + "&name=" + name,
			success : function (data) {
				$m = $("div#nl_msg");
			   if(data=="OK") { 
			   	  $m.removeClass('error');
   				  $m.text('Thanks for signing up!');
			   } else {
			   	  $m.addClass('error');
				  $m.text(data);
			   }
			}
		});
		return false;
	});
});

$(document).keydown( function( e ) {
	if( e.which == 27) { overlays_close(); }
});

function overlays_close() {
	$(".overlay").fadeOut();
	overlay_out();
}

function overlay_in()
{
	$('#jquery-overlay').css({
		backgroundColor:	'#fff',
		opacity:			0.7,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn().bind('dblclick click', function () { overlays_close(); });	
}

function overlay_out()
{
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').hide(); });
}

$(window).resize(function() {
	get_page_sizes();
});

function get_page_sizes()
{
	var arrPageSizes = ___getPageSize();

	$('#jquery-overlay').css({
		width:		arrPageSizes[0],
		height:		arrPageSizes[1]
	});
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

var windowState = (function(){
	var readScroll = {scrollLeft:0,scrollTop:0};
	var readSize = {clientWidth:0,clientHeight:0};
	var readScrollX = 'scrollLeft';
	var readScrollY = 'scrollTop';
	var readWidth = 'clientWidth';
	var readHeight = 'clientHeight';
	function otherWindowTest(obj){
		if((document.compatMode)&&
				(document.compatMode == 'CSS1Compat')&&
				(document.documentElement)){
			return document.documentElement;
		}else if(document.body){
			return document.body;
		}else{
			return obj;
		}
	};
	if((typeof this.innerHeight == 'number')&&
			(typeof this.innerWidth == 'number')){
		readSize = this;
		readWidth = 'innerWidth';
		readHeight = 'innerHeight';
	}else{
		readSize = otherWindowTest(readSize);
	}
	if((typeof this.pageYOffset == 'number')&&
			(typeof this.pageXOffset == 'number')){
		readScroll = this;
		readScrollY = 'pageYOffset';
		readScrollX = 'pageXOffset';
	}else{
		readScroll = otherWindowTest(readScroll);
	}
	return {
getScrollX:function(){
			return (readScroll[readScrollX]||0);
		},
getScrollY:function(){
			return (readScroll[readScrollY]||0);
		},
getWidth:function(){
			return (readSize[readWidth]||0);
		},
getHeight:function(){
			return (readSize[readHeight]||0);
		}
	};
})() 