/* Thickbox 3.1 - One Box To Rule Them All.
/* Original by Cody Lindley (http://www.codylindley.com)
/* Copyright (c) 2007 cody lindley
/* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

//on page load call tb_init
$(document).ready(function(){   
	tb_init('a.thickbox');//pass where to apply thickbox
	tb_init('table.thickbox tr td a');//pass where to apply thickbox
});

//add thickbox to elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
	tb_show(this.href);
	this.blur();
	return false;
	});
}

function tb_show(url) {//function called when the user clicks on a thickbox link
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}

		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		//$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash

		TB_WIDTH = 700;
		TB_HEIGHT = 398;
		ajaxContentW = TB_WIDTH - 30;
		ajaxContentH = TB_HEIGHT - 45;
		urlNoQuery = url.split('TB_');
		$("#TB_iframeContent").remove();
		$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 30)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");		
		tb_position();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#TB_window").css({display:"block"});
		}
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe(){
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}