/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT, GB_WIDTH;
var GB_ANIMATION = true;
var GB_OPEN = false;
var GB_CANSHRINK = false;
var GB_WIDTHOFFSET = 40; // px offset to account for scrollbar etc
var GB_HEIGHTOFFSET = 60;
var GB_CLOSECHALLENGE = false;

function GB_show(caption, url, width, height, canShrink, closeChallenge) {
	GB_HEIGHT = height || 400;
	GB_WIDTH = width || 600;
	GB_CANSHRINK = (typeof canShrink != 'undefined')?canShrink:true;
	GB_CLOSECHALLENGE = closeChallenge || false;
	if(!GB_DONE) {
		$(document.body)
		  //.append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
			.append("<div id='GB_overlay'></div><div id='GB_container'><div id='GB_window'>"
			+ "<img src='/assets/images/ui/close.png' alt='Close' id='GB_Close'/></div></div>")
			.keyup(function(evtObj) {
				if (GB_OPEN && evtObj.keyCode == 27) { // hit escape
					GB_hide();
				}
			})
		;
		$("#GB_window img").click(GB_hide);
		//$("#GB_overlay").click(GB_hide);
		//$(window).resize(GB_position);
		ResizeEvents.bind('x-initial-sizes, x-window-resize', GB_resize);
		GB_DONE = true;
	}
	
	$("#GB_frame").remove();
	$("#GB_window").append("<iframe id='GB_frame' src='"+url+"' title='"+caption+"'></iframe>");
	$("#GB_window").width(GB_WIDTH);
	
	//$("#GB_caption").html(caption);
	
	$("#GB_overlay, #GB_container").show();
	//GB_position();
	GB_OPEN = true;
	GB_resize(null, null, null, getWindowWidth(), getWindowHeight());
	
	if(GB_ANIMATION) {
		$("#GB_window").stop().slideDown("slow");
	} else {
		$("#GB_window").stop().show();
	}
	
	// shift focus to iframe (this is acceptable under WCAG2 as it was user initiated change of context)
		$("#GB_frame").focus();
};

function GB_hide() {
	if (GB_CLOSECHALLENGE === false || confirm(GB_CLOSECHALLENGE)) {
		$("#GB_window, #GB_overlay, #GB_container").stop().hide();
		GB_OPEN = false;
		// return focus to page (this is acceptable under WCAG2 as it was user initiated change of context)
			$('body').focus();
	}
};

function GB_resize(evtObj, emPixels, textHeight, windowWidth, windowHeight) {
	//console.log(emPixels+', '+textHeight+', '+windowWidth);
	if (GB_OPEN && GB_CANSHRINK) {
		if (windowWidth-GB_WIDTHOFFSET < GB_WIDTH) {
			$("#GB_window").width(windowWidth-GB_WIDTHOFFSET);
		} else {
			$("#GB_window").width(GB_WIDTH);
		}
		
		if (windowHeight-GB_HEIGHTOFFSET < GB_HEIGHT) {
			$("#GB_window").height(windowHeight-GB_HEIGHTOFFSET);
			$("#GB_frame").height(windowHeight-GB_HEIGHTOFFSET);
		} else {
			$("#GB_window").height(GB_HEIGHT);
			$("#GB_frame").height(GB_HEIGHT);
		}
	}
};
/*
function GB_position() {
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	$("#GB_window").css({
		width: GB_WIDTH+"px",
		height: GB_HEIGHT+"px",
		left: ((w - GB_WIDTH)/2)+"px"
	});
	$("#GB_frame").css("height", GB_HEIGHT +"px");
}*/
