/*
 * Thickbox 2.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 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.blur();
		return false;
	});
}

function TB_show(caption) {//function called when the user clicks on a thickbox link
	try {
		if (document.getElementById("TB_HideSelect") == null) {
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		}
	
		if(caption==null){caption=""};
		
		$(window).scroll(function(){
				TB_overlaySize();
				TB_position();
			});
		
		TB_overlaySize();
		
		$("body").append("<div id='TB_load'><img src='/client/manager/images/indicator.gif' /></div>");
		TB_load_position();
			
		var pagesize = TB_getPageSize();
		
		TB_WIDTH = 200;
		TB_HEIGHT = 100;
		$("#TB_window").append("<div id='TB_caption'>"+caption+"</div>"); 		
							
		TB_position();
		$("#TB_load").remove();
		$("#TB_window").css({display:"block"}); //for safari using css instead of show
		
		$(window).resize(function(){
				TB_overlaySize();
				TB_position();
			});
		
	} catch(e) {
		alert( e );
	}
}

//helper functions below

function TB_remove() {
	$("#TB_overlay").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
	$("#TB_load").remove();
	return false;
}

function TB_position() {
	var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();
	var heightDiv = $("#TB_window").height();
	/**Modif affichage du message de mise à jour du site**/
	if (heightDiv > 100){
		TB_HEIGHT = 500;
		pagesize[1] = 600;
		$("#TB_window").css({'text-align':"left"});
		$("#TD_cancelButton").hide();
	}
	/***************************************************/	
	$("#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" });
}

function TB_overlaySize(){
	var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();	
	$("#TB_overlay").css({"height":(arrayPageScroll[1] + pagesize[1]) +"px", "width":(arrayPageScroll[0] + pagesize[0]) +"px"});
	$("#TB_HideSelect").css({"height":(arrayPageScroll[1] + pagesize[1]) +"px", "width":(arrayPageScroll[0] + pagesize[0]) +"px"});
}

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

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 = new Array(w,h) 
	return arrayPageSize;
}
