/*
 * 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 = "images/commun/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$j(document).ready(function(){   
//	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$j(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function showIframe(url, width, height, caption, closeCaption, scrollable, modal) {
	
	modal = modal || false; // defaults to false
	scrollable = scrollable || false; // defaults to false
	
	createOverlay();
	showLoader();

	try {
		
		var windowHeight = height;		// don't care about about horizontal scrollbar
		var windowWidth = width;	
		if (scrollable) { 			
			// Recalculer la valeur pour que ça entre dans l'écran
			if (height > getWindowHeight()) {
				height = getWindowHeight() - 50;	// laisser une bordure en haut et en bas
				windowHeight = height - 15;			// bordure pour le titre
			}
			windowWidth = width;
		} 
		
		var titleBar =
	 	"<div id='TB_title'>" +		
		"<div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>" + closeCaption + " X</a></div>" +
		"</div>";
		var iFrameContentArea;
		if (scrollable) {
			iFrameContentArea = "<iframe frameborder='0' hspace='0' vspace='0' scrolling='yes' src='" + url + "' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:" + ( windowWidth ) + "px;height:" + windowHeight + "px;overflow:auto;'></iframe>";
		} else {
			iFrameContentArea = "<iframe frameborder='0' hspace='0' vspace='0' scrolling='no' src='" + url + "' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:" + ( windowWidth ) + "px;height:" + windowHeight + "px;'></iframe>";
		}
	
		$j("#TB_iframeContent").remove();
		$j("#TB_inner_window").append(titleBar + iFrameContentArea);
		$j("#TB_closeWindowButton").click(tb_remove);

		positionWindow(width, height);

		if ($j.browser.safari) {//safari needs help because it will not fire iframe onload
			$j("#TB_load").remove();
			//$j("#TB_window").css({display: "block"});
			$j("#TB_window").fadeIn(1000);
		}

		if (modal) {		
			$j("#TB_overlay").unbind(); // unbind tb_remove from overlay
		} else {		
			bindEscapeKeyToRemove(); // escape key will also close window
		}

	} catch(e) {
		//alert(e);
	}
}

function showAjax(url, width, height, modal) {
	
	modal = modal || false; // defaults to false
	
	createOverlay();
	showLoader();
	
	$j("body").append("<div id='TB_window'><div id='TB_inner_window'></div></div>");
	
	try {
		//var windowWidth = width + 17;	// vertical scrollbar is 17px.
		var windowWidth = width;	
		var windowHeight = height;		// don't care about about horizontal scrollbar
		
		var ajaxContentArea = "<div id='TB_ajaxContent' style='width:100%;height:" + windowHeight + "px;' onclick='tb_remove()'></div>";	
		if($j("#TB_window").css("display") != "block"){
			$j("#TB_inner_window").append(ajaxContentArea);			
		}else{ //window is already up, we are just loading new content via ajax
			$j("#TB_ajaxContent")[0].style.width = windowWidth +"px";
			$j("#TB_ajaxContent")[0].style.height = windowHeight +"px";
			//$j("#TB_ajaxContent")[0].scrollTop = 0;			
		}

		positionWindow(width, height);

		$j("#TB_ajaxContent").load(url, function() {//to do a post change this load method
			$j("#TB_load").remove();
			//$j("#TB_window").css({display:"block"});
			$j("#TB_window").fadeIn(1000);
		});

		if (modal) {			
			$j("#TB_overlay").unbind(); // unbind tb_remove from overlay
		} else {
			bindEscapeKeyToRemove(); // escape key will also close window
		}
		
	} catch(e) {
		//alert(e);
	}
}

function createOverlay() {

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$j("body", "html").css({
				height: "100%",
				width: "100%"
			});
			$j("html").css("overflow", "hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$j("body").append("<iframe id='TB_HideSelect' src='javascript:void()'></iframe><div id='TB_overlay'></div><div id='TB_window'><div id='TB_inner_window'></div></div>");
				$j("#TB_overlay").click(tb_remove);
			}
		}
		else {//all others
			if (document.getElementById("TB_overlay") === null) {
				$j("body").append("<div id='TB_overlay'></div><div id='TB_window'><div id='TB_inner_window'></div></div>");
				$j("#TB_overlay").click(tb_remove);
			}
		}
		
		if (tb_detectMacXFF()) {
			$j("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}
		else {
			$j("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
			//$j("#TB_overlay").show();
		}
	} 
	catch (e) {
	// alert(e);
	}
}

function bindEscapeKeyToRemove(){

	document.onkeyup = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		}
		else { // mozilla
			keycode = e.which;
		}
		if (keycode == 27) { // close
			tb_remove();
		}
	};
}

function showLoader(){

	try {
		$j("body").append("<div id='TB_load'><img src='" + imgLoader.src + "' /></div>");//add loader to the page
		$j('#TB_load').show();//show loader
	} 
	catch (e) {
	// alert(e);
	}
}

function positionWindow(w, h) {
	$j("#TB_window").css({marginLeft: '-' + parseInt((w / 2),10) + 'px', width: w + 8 + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$j("#TB_window").css({marginTop: '-' + parseInt((h / 2),10) + 'px'});
	}

}

//helper functions below
function tb_showIframe(){	
	$j("#TB_load").remove();
	//$j("#TB_window").css({display:"block"});
	$j("#TB_window").show("slow");
}

function tb_remove() {
 	$j("#TB_imageOff").unbind("click");
	$j("#TB_closeWindowButton").unbind("click");
	$j("#TB_window").fadeOut("fast",function(){$j('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$j("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$j("body","html").css({height: "auto", width: "auto"});
		$j("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
$j("#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
		$j("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_parseQuery ( query ) {
   var Params = {};
   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_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_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}


javascript:hide('view');