/*
 * 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
*/

$(document).ready(function(){// preload image
	imgLoader = new Image();
	imgLoader.src = "img/thickbox.gif";
});

function tb_show(url) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6 ===> is this needed ? to be tested
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
		}
		if(document.getElementById("TB_window") === null){
			$("body").append("<div id='TB_window'></div>");
		}
				
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
		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 || 630; //defaults to 630 if no paramaters were added to URL
		TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = TB_WIDTH - 30;
		ajaxContentH = TB_HEIGHT - 45;
		
		urlNoQuery = url.split('?TB_');
		$("#TB_iframeContent").remove();
		$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
						
		tb_position();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"});
		}
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
	$("#TB_window").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 = "";
}

function tb_position() {
$("#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
		$("#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 doWin(myUrl){ // function to open thickbox called from non-HTML source (flash etc...)
	originalUrl = myUrl;
	
	var de = document.documentElement;
	var x = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var y = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	
	var rapport = 650/400;
	taille = "large";
	if (x/y > rapport){
		myHeight=Math.floor(y/1.7);
		myWidth=Math.floor(y*rapport/1.4);
		if( y < 650 ) taille = "medium";
		if( y < 450 ) taille = "small";
	}else{
		myWidth=Math.floor(x/1.4);
		myHeight=Math.floor(x/rapport/1.7);
		if( x < 1000 ) taille = "medium";
		if( x < 650 ) taille = "small";
	}
	url = myUrl+'&taille='+taille+'?TB_iframe=true&height='+myHeight+'&width='+myWidth;
	tb_show(url);
}

function tb_resize(){
	if(document.getElementById('TB_window') !== null){
		tb_remove();
		doWin(originalUrl);
	}
}

