/*
 * Popup-Lightbox for understand.com animations
 * by James Thomas / ThreeLeaf.tv
 * Stripped/altered jquery lightbox from
 * http://leandrovieira.com/projects/jquery/lightbox/
 * (c) 2008 Leandro Vieira Pinho (leandrovieira.com)
 * @license CC Attribution-No Derivative Works 2.5 Brazil - http://creativecommons.org/licenses/by-nd/2.5/br/deed.en_US
 */

$(document).ready(function() {
	$('.popuplightbox').click(function(){
		openCenteredWindow(this.href);
		makeDarkenOverlay();
	    return false;
    });
});

function openCenteredWindow(url) {  //from http://www.java2s.com/Code/JavaScript/Window-Browser/Openawindowandcenterit.htm
    var width = 701;
    var height = 557;
    var left = parseInt(($(window).width() / 2) - (width / 2) + ((document.all)?window.screenLeft:window.screenX));
    var top = parseInt(($(window).height() / 2) - (height / 2) + ((document.all)?window.screenTop:window.screenY) + 60);
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    AnimationPopup = window.open(url, "AnimationPopup", windowFeatures);
	AnimationPopup.focus();
	return false;
};

/*
function openCenteredWindow(url) {  //from http://www.java2s.com/Code/JavaScript/Window-Browser/Openawindowandcenterit.htm
    var width = 701;
    var height = 557;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2))- 40;
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    AnimationPopup = window.open(url, "AnimationPopup", windowFeatures);
	AnimationPopup.focus();
	return false;
};
*/
function ___getPageSize(){ //from jquery lightbox plugin
	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;
};

function _finish() {
	$('#darken-overlay').fadeOut(function() { $('#darken-overlay').remove(); });
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
};

function makeDarkenOverlay() {
	var arrPageSizes = ___getPageSize();
	$('body').append('<div id="darken-overlay"></div>');
	$('#darken-overlay').css({
		backgroundColor:	'#91AEBD',
		opacity:			0.6,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
		
	}).fadeIn();
	
	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		$('#darken-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
	});	
	$(window).focus(function(){
		AnimationPopup.close();	
		_finish();
	});
	// Assigning click events in elements to close overlay
	$('#darken-overlay').click(function() {
		AnimationPopup.close();	
		_finish();				
	});	
};


