/*
 * 
 * Modified Version of Thickbox 3, removed unused parts and fixed minor bugs
 * but the major change is that is customised for OUR STYLE ! 
 * Andrea 24/05/07 
 * 
 *
 * Thickbox 3 - 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
 * Thickbox is built on top of the very light weight jQuery library.
 */


//on page load call TB_init
$(document).ready(TB_init);

//add thickbox to href elements that have a class of .thickbox
function TB_init(){
	$("a.thickbox").click(function(){
	var t = this.title || this.name || null;	
	TB_show(t,this.href);
	this.blur();
	return false;
	});
}

function TB_show(caption, 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_overlay") == null) {
			$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(TB_remove);
		} else {
			$("body").append("<div id='TB_window'></div>");
		}
		
		if(caption==null){caption=""};		
		//$(window).scroll(TB_position);
 		
		//TB_overlaySize();
		 
		$("body").append("<div id='TB_load'><img src='/core/includes/js/jquery/jquery.thickbox/loading.gif' /></div>");
		$('#TB_load').show();//show loader
//TB_load_position();
					
		var baseURL;
	   if(url.indexOf("?")!==-1){ //If there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{ 
	   		baseURL = url;
	   }
	   			
			
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = TB_parseQuery( queryString );
				
		TB_WIDTH = (params['width']*1) +30;
		TB_HEIGHT = (params['height']*1) + 40;
		
		var TB_CONTENT_WIDTH = TB_WIDTH;
		var TB_CONTENT_HEIGHT = TB_HEIGHT;
		
		if(caption.length > 0){			
			TB_CONTENT_HEIGHT = TB_CONTENT_HEIGHT - 30; 	
			$("#TB_window").append("<div id='TB_title'><div id='TB_windowTitle'>"+caption+"</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_content' style='width:"+TB_CONTENT_WIDTH+"px;height:"+TB_CONTENT_HEIGHT+"px;'></div>");
			$("#TB_closeWindowButton").click(TB_remove);
		}		
		else {			
			$("#TB_window").append("<div id='TB_content' style='width:"+TB_CONTENT_WIDTH+"px;height:"+TB_CONTENT_HEIGHT+"px;'></div>");
		}
			
				
		$("#TB_content").load(url, function(){			
			TB_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
		});
			
	
		
		//$(window).resize(TB_position);
		
		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					TB_remove();
				}	
			};
		}
		
	} catch(e) {
		alert( e );
	}
}

//helper functions below


function TB_remove() {
	 	
	$("#TB_overlay").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay').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 = "";
	return false;
}

function TB_position() {

$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}
/*
function TB_position() {
	
	var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();

	//$("#TB_window").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
	//$("#TB_window").css({width:TB_WIDTH+"px",left: (780 - TB_WIDTH)/2+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
	
	//$("#TB_window").css({width:TB_WIDTH+"px",left: "0px", top: (arrayPageScroll[1])+"px" });	
	$("#TB_window").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + 105)+"px" });
	
}
*/
/*
function TB_overlaySize(){
	
	
	
		if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	
  	$("#TB_overlay").css({"height":yScroll +"px", "width":xScroll +"px"});
	
  	
  	
	//$("#TB_overlay").css({"height": "100%", "width":"100%"});
}
*/

/*
function TB_load_position() {
	var pagesize = TB_getPageSize();
	var arrayPageScroll = TB_getPageScrollTop();
	$("#TB_load")
	//MODIFIED .css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
	//.css({left: ( (780 - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })

	//.css({left: ( (990 - 100)/2)+"px", top: (arrayPageScroll[1] + ((570-100)/2))+"px" })
	.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
	.css({display:"block"});
	
	$("#TB_overlay").css({left: (arrayPageScroll[0])+"px", top: (arrayPageScroll[1])+"px" });

}
*/

function TB_parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}
/*
function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}
*/

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;
}



