/*
 * ThinBox 1.0 - By Creuna (http://www.creuna.se)
 * A modification of Thickbox 3.1 By Cody Lindley (http://www.codylindley.com)
 * ThinBox Copyright (c) 2007 Creuna. Thickbox Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
var ThinBox = new function(){

	var m_LoaderImage			= new Image();// preload image
	var m_PopUpWidth;
	var m_PopUpHeight;
	var m_HasBeenShown			= "hasBeenShown";
	var m_CheckIfClosedInterval	= null;
	
	m_LoaderImage.src = "images/loadingAnimation.gif";
	
	this.Show = function(caption, url, width, height, modal, timeSpan){
		if(!timeSpan){
			timeSpan = -1;
		}else{
			timeSpan = timeSpan * 1000;
		}

		var date		= new Date();
		var timeElapsed = 0;
		var cookieKey	= escape(url);
		var cookieValue	= ThinBox.ReadCookie(cookieKey);
		if(cookieValue != m_HasBeenShown){
			if (cookieValue != null){
				timeElapsed = date - (new Date(cookieValue));
			}else{
				ThinBox.CreateCookie(cookieKey, date);
			}
			timeSpan = timeSpan - timeElapsed;
			
			if(timeSpan > 1){
				setTimeout(function(){ThinBox.ShowInternal(caption, url, width, height, modal);}, timeSpan);
			}else{
				ThinBox.ShowInternal(caption, url, width, height, modal);
			}
		}else{
			/*if(confirm("The popup has already been shown. Delete Cookie?")){
				ThinBox.EraseCookie(cookieKey);
			}*/
		}
	}
	
	this.ShowInternal = function(caption, url, width, height, modal){
		var cookieKey = escape(url);
		ThinBox.CreateCookie(cookieKey, "hasBeenShown", 365);
		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(ThinBox.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(ThinBox.Remove);
				}
			}
			
			if(ThinBox.DetectMacXFF()){
				$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
			}else{
				$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
			}
			
			if(caption===null){caption="";}
			$("body").append("<div id='TB_load'><img src='"+m_LoaderImage.src+"' /></div>");//add loader to the page
			$('#TB_load').show();//show loader
			
			m_PopUpWidth = (width*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
			m_PopUpHeight = (height*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL

			$("#TB_iframeContent").remove();
			if(!modal){//iframe no modal
				$("#TB_overlay").unbind();
				$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'><span>X</span></a></div></div><iframe frameborder='0' hspace='0' src='"+url+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='ThinBox.ShowIframe()' style='width:"+(m_PopUpWidth - 1)+"px;height:"+(m_PopUpHeight - 28)+"px;' > </iframe>");
			}else{//iframe modal
				$("#TB_overlay").unbind();
				$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+url+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='ThinBox.ShowIframe()' style='width:"+(m_PopUpWidth - 1)+"px;height:"+(m_PopUpHeight - 28)+"px;'> </iframe>");
			}
					
			$("#TB_closeWindowButton").click(ThinBox.Remove);

			ThinBox.Position();
			if($.browser.safari){//safari needs help because it will not fire iframe onload
				$("#TB_load").remove();
				$("#TB_window").css({display:"block"});
			}

			if(!modal){
				document.onkeyup = function(e){ 	
					var keycode;
					if (e == null) { // ie
						keycode = event.keyCode;
					} else { // mozilla
						keycode = e.which;
					}
					if(keycode == 27){ // close
						ThinBox.Remove();
					}	
				};
			}
			
			m_CheckIfClosedInterval = setInterval(ThinBox.CheckIfClosed, 10);
		} catch(e) {
			//nothing here
		}
	}
	
	//helper functions below
	this.ShowIframe = function(){
		$("#TB_load").remove();
		$("#TB_window").css({display:"block"});
	}
	
	this.CheckIfClosed = function(){
		try{
			if($("#TB_iframeContent")[0].contentWindow.location.href == location.href){
				ThinBox.Remove();
			}
		}catch(e){}
	}
	
	this.Remove = function() {
		if(m_CheckIfClosedInterval != null){
			clearInterval(m_CheckIfClosedInterval);
		}
 		$("#TB_imageOff").unbind("click");
		$("#TB_closeWindowButton").unbind("click");
		$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
		$("#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;
	}
	
	this.Position = function() {
		$("#TB_window").css({marginLeft: '-' + parseInt((m_PopUpWidth / 2),10) + 'px', width: m_PopUpWidth + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
			$("#TB_window").css({marginTop: '-' + parseInt((m_PopUpHeight / 2),10) + 'px'});
		}
	}
	this.DetectMacXFF = function() {
	  var userAgent = navigator.userAgent.toLowerCase();
	  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	  }
	}
	
	this.CreateCookie = function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	this.ReadCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	this.EraseCookie = function(name) {
		ThinBox.CreateCookie(name, "", -1);
	}	
	
}