window.modalOverlay = {
	COOKIE_NAME : 'overlayShown',
	
	run : function(filepath) {
		if( ! modalOverlay.getCookie( modalOverlay.COOKIE_NAME ) ) {
			modalOverlay.showModal( filepath );
		} 
	},
	
	setOverlaySizes : function() {
		var idMask = '#mask';
		var idLayer = '#overlay';
		var idWin = '.owindow';
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width(); 
		
		//create overlay mask
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width(); 
		//Set height and width to mask to fill up the whole screen
		$(idMask).css({'width':maskWidth,'height':maskHeight}); 
		$(idWin).css('top',  winH/2-$(idWin).height()/2);
		$(idWin).css('left', winW/2-$(idWin).width()/2); 
	},
	
	attachEvents : function() {
		var idMask = '#mask';
		var idLayer = '#overlay';
		var idWin = '.owindow';
			
		$('a').click(function (e) {
			modalOverlay.setCookie( modalOverlay.COOKIE_NAME, 1 );
		} );

		//if close button is clicked
		$('.owindow .oclose').click(function (e) {
			//Cancel the link behavior
			e.preventDefault();
			modalOverlay.closeWindow();
		});		 

		//if mask is clicked
		$(idMask).click(function () {
			modalOverlay.setCookie( modalOverlay.COOKIE_NAME, 1 );
			modalOverlay.closeWindow();
		});
	},
	
	closeWindow : function() {
		var idMask = '#mask';
		var idWin = '.owindow';
		$( idWin ).fadeOut(250,function() {
			$( idMask ).fadeOut(250, function() {
				modalOverlay.showUnrulyElements();
			} );
		});
	},
	
	hideUnrulyElements : function() {
		// Yes, this is an ugly hack for IE 6 due to it's native-control z-ordering bug.
		if( $.browser.msie && $.browser.version <= 6 ) {
			$('select').hide();
		}
	},
	
	showUnrulyElements : function() {
		// Yes, this is an ugly hack for IE 6 due to it's native-control z-ordering bug.
		if( $.browser.msie && $.browser.version <= 6 ) {
			$('select').show();
		}
	},
	
	showModal : function(filepath) {
		var idMask = '#mask';
		var idLayer = '#overlay';
		var idWin = '.owindow';
			
		//Set the popup window to center 
		$('body').append('<div id="mask"></div>'); 
		$('body').append('<div id="overlay"></div>'); 
		$(idLayer).css('opacity',0);
		$(idLayer).load(filepath,function(){ 
			modalOverlay.attachEvents();
			modalOverlay.setOverlaySizes();
			$(window).resize(modalOverlay.setOverlaySizes);
			//transition effect 
			$(idWin).hide();
			$(idLayer).css('opacity',100).show();
			modalOverlay.hideUnrulyElements();
			$(idMask).css('opacity',0).show().fadeTo(500,0.8,function() {
				$(idWin).fadeIn(500);
			});
		})
	},
	
	getCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for( var i=0; i < ca.length; ++i ) {
			var c = ca[i];
			while (c.charAt(0)==' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	},

	setCookie : function(name,value) {
		var ex = new Date();
		ex.setYear( ex.getFullYear() + 1 );
		var cookieString = name + "=" + escape( value ) + "; expires=" + ex.toGMTString() + "; path=/";
		document.cookie = cookieString;
	}
};
