/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Overhauled by: Aaron Barker - Added passed in variables, resizing based on window size, and a few other custom things. CSS overlay (instead of png) "borrowed" from Thickbox
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var gb_hd = 500; // height default
var gb_wd = 500; // width default
var gb_a = false; // animation
var gb_h;
var gb_w;
/*
p = Prefix
gf = Get From
tr = To Return
r = Regex
rs = ReSults
*/
function getVar(p,gf){
	var tr = null;
	var r = new RegExp(p+"(\\w*)","i");
	var rs = r.exec(gf);
	if(rs) tr = rs[1];
	return tr;
};

function GB_show(theGB) {
	var t = theGB.title || theGB.innerHTML || theGB.href;
	gb_h = (getVar("gbh_",theGB.className) || gb_hd)-0;
	gb_w = (getVar("gbw_",theGB.className) || gb_wd)-0;
	try {
		if(!$("#GB_overlay").length ){
			$("body").append('<div id="GB_overlay"></div><div id="GB_window"><div id="GB_head"><div id="GB_caption"></div><a href="#d">Close</a></div><div id="GB_body"></div><div id="GB_foot"></div></div>');
			$("#GB_window a").click(GB_hide);
			$(window).resize(GB_position);
			$(window).scroll(GB_position);
		}

		if(getVar("gbm_",theGB.className)){
			$("#GB_overlay").unbind( "click", GB_hide )
		}  else {
			$("#GB_overlay").click(GB_hide);
		}
		var ranNum= Math.floor(Math.random()*1000000000);// This is a fix for safari's iframe cache bug
		$("#GB_body").empty();
		$("#GB_body").append('<iframe id="GB_f'+ranNum+'" class="GB_frame" src="'+theGB+'" border="0" frameborder="0"></iframe>').css({width:gb_w+"px",height:gb_h+"px"});

		$("#GB_caption").html(t);
		$("#GB_head").css({width:gb_w+"px"});
		$("#GB_overlay").show();
		GB_position();

		if(gb_a)
			$("#GB_window").slideDown("slow");
		else
			$("#GB_window").show();
		GB_position();
	} catch(e) {
		alert( e );
	}
}

function GB_hide() {
  $("#GB_window,#GB_overlay").remove(); // removing instead of hiding for a safari bug that dupicates flash movies out to the middle of no where
	$(window).unbind("resize",GB_position);
	$(window).unbind("scroll",GB_position);
}

function GB_position()
{
	var de = document.documentElement;
	var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;

	var headH = $("#GB_head").get(0).offsetHeight;
	var footH = $("#GB_foot").get(0).offsetHeight;
	var offset = headH + footH + 30;
	var height = h < gb_h+offset ? h - offset : gb_h;


	$("#GB_window").css({marginLeft: '-' + parseInt((gb_w / 2),10) + 'px', width:gb_w+"px"});
	if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // take away IE6
		$("#GB_window").css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
	}
	//TODO make this work in IE
	$("#GB_body").css("height",height - offset +"px"); // height update works for everyone but IE.  Checks to see if layer is taller then the window and compensates.

}
