// main.js
var arrPageSizes;

$(document).ready(function(){
	arrPageSizes = ___getPageSize();
	
	$("a#open-newsletter-signup").bind("click", function(){
		show_newsletter_signup();
		return false;
	});

	$("div#dropdown-casestudies").bind("click", function(){
		var $list = $("div#list-casestudies");
		if($list.is(":hidden")) {
			$list.slideDown(150);
		} else if ($list.is(":visible")) {
			$list.slideUp(150);
		}
	});
});

function show_newsletter_signup()
{
	overlay_in();
	$newsletter_signup = $("div#newsletter-signup");
	$newsletter_signup.css('top', (windowState.getScrollY() + (windowState.getHeight()/2 - 152)));
	$newsletter_signup.css('left', (windowState.getScrollX() + (windowState.getWidth()/2 - 262)));
	$("div#newsletter-signup > div.form").show();
	$("div#newsletter-signup > div.thanks").hide();
	$("p#nl_message").text('');
	$newsletter_signup.fadeIn();
	$("a#nl_cancel, a#nl_close").bind("click", function(){
		close_newsletter_signup();
		return false;
	});
	$("a#nl_submit").bind("click", function(){
		$("p#nl_message").text('Processing...');
		var email = $("#nl_email").val();
		var name = $("#nl_name").val();
		$.ajax({
			url : "index.php?ajaxrequest=newslettersignup&email=" + email + "&name=" + name,
			success : function (data) {
			   if(data=="OK") { 
   				$("#newsletter-signup > div.form").hide();
				$("#newsletter-signup > div.thanks").animate({ "opacity": "toggle" }, "slow");
				pageTracker._trackPageview("/newsletter-signup"); 
			   } else {
				$("p#nl_message").text(data);  
			   }
			}
		});
		return false;
	});
}

function close_newsletter_signup()
{
	$("#newsletter-signup").hide();
	overlay_out();
}

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

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

