/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * 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
*/
		  
var tb_pathToImage = "/i/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){   
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	if($('#message').html() != null && $('#message').html() != '' && $('#message').html() != undefined)
	{
		$('object').css('visibility','hidden');
		var data = $('#message').html().split("|");
		if(data[1] != undefined)
		{
			var str = '<div id="MY_TB_message_top">'+data[0]+'</div><div id="MY_TB_message_bottom">'+data[1]+'</div>';
		}else
		{
			var str = '<div id="MY_TB_message_top">'+data[0]+'</div><div id="MY_TB_message_bottom"></div>';
		}
		my_tb_show(str);
	}
});

//add thickbox to href & area elements that have a class of .thickbox


function my_tb_show(text) {//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("MY_TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='MY_TB_HideSelect'></iframe><div id='MY_TB_overlay'></div><div id='MY_TB_window'></div>");
				$("#MY_TB_overlay").click(my_tb_remove);
				$("#MY_TB_window").click(my_tb_remove);
			}
		}else{//all others
			if(document.getElementById("MY_TB_overlay") === null){
				$("body").append("<div id='MY_TB_overlay'></div><div id='MY_TB_window'></div>");
				$("#MY_TB_overlay").click(my_tb_remove);
				$("#MY_TB_window").click(my_tb_remove);
			}
		}
		
		if(my_tb_detectMacXFF()){
			$("#MY_TB_overlay").addClass("MY_TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#MY_TB_overlay").addClass("MY_TB_overlayBG");//use background and opacity
		}

		$("body").append("<div id='MY_TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#MY_TB_load').show();//show loader

		TB_WIDTH = 400;//(params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
		TB_HEIGHT = 160;//(params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = TB_WIDTH - 100;
		ajaxContentH = TB_HEIGHT - 22;
		
		if($("#MY_TB_window").css("display") != "block"){
			$("#MY_TB_window").append('<table class="wintab" cellspacing="0" cellpadding="0"><tr><td class="enterlt"><img src="/i/sp.gif" width="11" height="11" border="0" alt=""></td><td class="entert"><img src="/i/sp.gif" width="1" height="11" border="0" alt=""></td><td class="enterrt"><img src="/i/sp.gif" width="11" height="11" border="0" alt=""></td></tr><tr><td class="enterl"><img src="/i/sp.gif" width="11" height="1" border="0" alt=""></td><td class="entercenter"><div id="MY_TB_ajaxContent" style="font-size:32px;line-height:100%;width:'+ajaxContentW+'px;"></div></td><td class="enterr"><img src="/i/sp.gif" width="11" height="1" border="0" alt=""></td></tr><tr><td class="enterlb"><img src="/i/sp.gif" width="11" height="11" border="0" alt=""></td><td class="enterb"><img src="/i/sp.gif" width="1" height="11" border="0" alt=""></td><td class="enterrb"><img src="/i/sp.gif" width="11" height="11" border="0" alt=""></td></tr></table>');
			
		}else{//this means the window is already up, we are just loading new content via ajax
			$("#MY_TB_ajaxContent")[0].style.width = ajaxContentW +"px";
			$("#MY_TB_ajaxContent")[0].style.height = ajaxContentH +"px";
			$("#MY_TB_ajaxContent")[0].scrollTop = 0;
		}
				
		//$("#MY_TB_closeWindowButton").click(my_tb_remove);
		$("#MY_TB_ajaxContent").html(text);
		my_tb_position();
		$("#MY_TB_load").remove();
		$("#MY_TB_window").css({display:"block"});

		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					my_tb_remove();
				}	
			};
		}
		
	} catch(e) {
		//nothing here
	}
}

function my_tb_remove() {
 	$("#MY_TB_imageOff").unbind("click");
	$('object').css('visibility','visible');
	//$("#MY_TB_closeWindowButton").unbind("click");
	$("#MY_TB_window").fadeOut("fast",function(){$('#MY_TB_window,#MY_TB_overlay,#MY_TB_HideSelect').trigger("unload").unbind().remove();});
	$("#MY_TB_load").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 my_tb_position() {
$("#MY_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
		$("#MY_TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function my_tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}