/*
 * 
 * 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 
 * 
 * Modified again to use ajax.options instead of a simple load to resolve caching an sync requests
 * Andrea 28/03/09 
 *
 * 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);


var TB_options = {
	dataType: 'html',
	url: '/',
	type: 'GET',	        					
	async: false,
	cache: false,
	data: "",
	timeout:30, //ms
	success: function(data,textStatus) { 
	   TB_set(data); 		  	    						 										 
	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {	
		alert('An error occured, please try again ('+ textStatus + ' ' + errorThrown + ')');				
	},
	complete: function (XMLHttpRequest, textStatus) {		
		// this; // the options for this ajax request
	}
};
	
	
	


//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, closeFn) {//function called when the user clicks on a thickbox link

	try {
		
		if (closeFn != null)
			this.closeFn = closeFn;

		//hide any select box
		$("select").css("display","none");
		
		//hide scrollbars
		$("body").css({overflow:"hidden"}); 
		
		//IE7 fix
		$("html").css("overflow","hidden");
 		
		
		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>");
			$("body").append("<div id='TB_window' style='display:none;'></div>");
			$("#TB_overlay").click(TB_remove);
			
		} else {
			
			$("body").append("<div id='TB_window' style='display:none;'></div>");
		}
		
		if(caption==null){caption=""};	
			
		$(window).scroll(TB_position);
 		
		TB_overlaySize();
		 
		$("body").append("<div id='TB_load'><img src='/images/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);
		TB_HEIGHT = (params['height']*1);
		
		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>");
		}
			
		
		//set the url and call the ajax to get the html content fot the thickbox
		TB_options.url =url;
		
		$.ajax(TB_options);	
		
		
		$(window).resize(TB_resize);
		
		
		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 );
	}
}




function TB_set(html) {
	
	$("#TB_content").html(html);
	$("#TB_window").css("display","none");
	TB_position();
	$("#TB_load").remove();
	
}




//helper functions below


function TB_remove() {
	
	//restore any select box
	$("select").css("display","inline");
	
		 	
	$("#TB_overlay").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	
	
	$(".shadow").attr("class","shadowless");
	$(".shadowcorner").attr("class","shadowlesscorner");
	
	$("#TB_window").fadeOut("fast",function(){
		
		$('#TB_window,#TB_overlay').remove();
		
		//restore scrollbars
		$("body").css({overflow:""}); 
		//IE7 fix
		$("html").css("overflow","");
	});
	
	$("#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 = "";
	

	if (this.closeFn) this.closeFn();
	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: 134 +"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" });
	$("#TB_window").css("display","block");
	
}

function TB_resize() {
	TB_position();
	TB_overlaySize();
}



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" });
	$("#TB_overlay").css({left: "0px", top: "0px" });

}


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


function TB_switchTo(url){



	$("#TB_load").remove();	
	$("#TB_window").fadeOut("fast",function(){

		$('#TB_window').remove();
		TB_show(null,url);

	});
	
	return false;

}



