var arrPageSizes;

jQuery(document).ready(function() {			
	jQuery('input#search-term').bind('focus click', function () {
		jQuery(this).val('').css('color','#666');
	});
	
	arrPageSizes = ___getPageSize();			

	jQuery('a#nl-open').bind('click', function () {
		overlay_in();
		$newsletter_signup = jQuery("div#nl-box");
		$newsletter_signup.css('top', (windowState.getScrollY() + (windowState.getHeight()/2 - 149)));
		$newsletter_signup.css('left', (windowState.getScrollX() + (windowState.getWidth()/2 - 232)));
		$newsletter_signup.fadeIn();
		
		jQuery("a#nl-submit").bind("click", function(){
			var email = jQuery("#nl-email").val();
			var email2 = jQuery("#nl-email2").val();
			
			if (email != email2 || email == '' || email2 == '') {
				jQuery('div#nl-message').text('The entered e-mail address does not match.');
			} else {
				document.forms['nlform'].submit();
			}
			
			return false;
		});
		
		jQuery("a#nl-cancel").bind("click", function(){
			$newsletter_signup.fadeOut();
			overlay_out();
			return false;
		});
		return false;
	});
});

function overlay_in()
{
	jQuery('#jquery-overlay').css({
		opacity:			0.9,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();
}

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

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

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

	jQuery('#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);
		}
	};
})()
