//
//	23.09.2009	JG	Modification de la forme
//	10.03.2009	JG	Personalisation
/*
 * Smoothbox v20080623 by Boris Popoff (http://gueschla.com)
 * To be used with mootools 1.2
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// on page load call TB_init

//window.addEvent('domready', TB_init);
if (document.addDefferedExec) addDefferedExec(TB_init);
else window.addEvent('domready', TB_init);



// prevent javascript error before the content has loaded
TB_WIDTH = 0;
TB_HEIGHT = 0;
TB_WIDTH_init = 0;
TB_HEIGHT_init = 0;
TB_bFullScreen = false
TB_fromID = false;

var TB_doneOnce = 0;
var tb_bwait = false;
// add smoothbox to href elements that have a class of .smoothbox
function TB_init(){
    $$("a.smoothbox").each(function(el){
        el.onclick = TB_bind
    });
}

function TB_bind(event){
    var event = new Event(event);
    // stop default behaviour
    event.preventDefault();
    // remove click border
    this.blur();
    // get caption: either title or name attribute
    var caption = this.title || this.name || "";
    // get rel attribute for image groups
    var group = this.rel || false;
    // display the box for the elements href
    TB_show(caption, this.href, group);
    this.onclick = TB_bind;
    return false;
}

// called when the user clicks on a smoothbox link
function tb_show(caption, url, rel){

	return TB_show(caption, url, rel);

}

function tb_is_wait(){
	return tb_bwait;
}


function tb_wait() 
{
	tb_waitmsg("");
}

function tb_waitmsg(s)
{


	//alert("tb_wait");
	// Reset de la fenetre 
	if($("TB_window"))	$("TB_window").innerHTML='';
	
    // create iframe, overlay and box if non-existent
    
    if (!$("TB_overlay")) 
	{
        new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
        $('TB_HideSelect').setOpacity(0);
        new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
        $('TB_overlay').setOpacity(0.6);
        TB_overlaySize();
		
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        //$('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        $('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        TB_load_position();
        
        //$('TB_overlay').setOpacity(0.6);
		//.set('tween', {
        //    duration: 400
        //});
        //$('TB_overlay').tween('opacity', 0, 0.6);
        
    }
    
    if (!$("TB_load"))
	{
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        //$('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        $('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        TB_load_position();
    }

//    if (!$("TB_window")) 
    if (!document.getElementById("TB_window")) 
	{
        new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
        $('TB_window').setOpacity(0);
    }
    
    //$("TB_overlay").onclick = TB_remove;
    window.onscroll = TB_position;
  
  	if(s.length>0) s+="<BR><BR>";
  	//$("TB_window").innerHTML = "<img src='/res/smoothbox/loading.gif' />";
  	$("TB_window").innerHTML = "<DIV ID=TB_WaitContent>"+s+"<img src='/res/smoothbox/loading.gif' /></DIV>";
  	//$("TB_window").innerHTML += "<TABLE border=0 cellspacing=0 cellpadding=0 ID=TB_Box ><TR> <TD class=CADRE_top_left></TD> <TD class=CADRE_top_center></TD> <TD class=CADRE_top_right align=right valign=top></TD></TR><TR> <TD class=CADRE_left></TD> <TD class=CADRE_inside><DIV ID=TB_WaitContent>"+s+"<img src='/res/smoothbox/loading.gif' /></DIV></TD> <TD class=CADRE_right></TD></TR><TR> <TD class=CADRE_bottom_left></TD> <TD class=CADRE_bottom_center></TD> <TD class=CADRE_bottom_right></TD></TR></TABLE>";
  

  	TB_WIDTH_init = TB_WIDTH = "208";
  	TB_HEIGHT_init = TB_HEIGHT = "13";
	TB_position();
	TB_showWindow();  
 	tb_bwait = true;
	
	 $('TB_overlay').setOpacity(0.6);
	   $("TB_overlay").setStyles({
        "height": '2000px',
        "width": '2000px',
		"visibility": 'visible'
    });	
}

var tb_string = "";
function tb_alert( string , title,params){ // Inversion des paramettre a la demande de Pascal
	tb_string = string;
	if(!title)  title = "";
	if(!params) params = 'height=200&width=500'

	tb_show(title,'#TB_string?'+params);

}

function TB_show(caption, url, rel)
{

	tb_bwait = false;
	TB_fromID = "";
	// Reset de la fenetre 
	if($("TB_window"))	$("TB_window").innerHTML='';
	
    // create iframe, overlay and box if non-existent
    
    if (!$("TB_overlay"))
	{
        new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
        $('TB_HideSelect').setOpacity(0);
        new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
		
        $('TB_overlay').setOpacity(0);
        TB_overlaySize();
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        //$('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        $('TB_load').innerHTML = "<img src='/res/smoothbox/images/loader.gif' />";
        TB_load_position();
        
        $('TB_overlay').set('tween', {
            duration: 400
        });
        $('TB_overlay').tween('opacity', 0, 0.6);
        
    }
    
    if (!$("TB_load")) {
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        //$('TB_load').innerHTML = "<img src='/res/smoothbox/loading.gif' />";
        $('TB_load').innerHTML = "<img src='/res/smoothbox/images/loader.gif' />";
        TB_load_position();
    }
    
    if (!$("TB_window")) {
        new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
        $('TB_window').setOpacity(0);
    }
    
    if (url.indexOf('modal') == -1)
		if ($("TB_overlay")) 
			$("TB_overlay").onclick = TB_remove;
    window.onscroll = TB_position;
 
//	$('TB_window').mbEnlarged;
 
    // check if a query string is involved
    var baseURL = url.match(/(.+)?/)[1] || url;
    
    // regex to check if a href refers to an image
    var imageURL = /\.(jpe?g|png|gif|bmp)/gi;
    
    // check for images
    if (baseURL.match(imageURL))
	{
        var dummy = {
            caption: "",
            url: "",
            html: ""
        };
        
        var prev = dummy, next = dummy, imageCount = "";
        
        // if an image group is given
        if (rel)
		{
            function getInfo(image, id, label)
			{
                return {
                    caption: image.title,
                    url: image.href,
                    html: "<span id='TB_" + id + "'>&nbsp;&nbsp;<a href='#'>" + label + "</a></span>"
                }
            }
            
            // find the anchors that point to the group
            var imageGroup = [];
            $$("a.smoothbox").each(function(el){
                if (el.rel == rel) {
                    imageGroup[imageGroup.length] = el;
                }
            })
            
            var foundSelf = false;
            
            // loop through the anchors, looking for ourself, saving information about previous and next image
            for (var i = 0; i < imageGroup.length; i++) {
                var image = imageGroup[i];
                var urlTypeTemp = image.href.match(imageURL);
                
                // look for ourself
                if (image.href == url) {
                    foundSelf = true;
                    imageCount = "Image " + (i + 1) + " of " + (imageGroup.length);
                }
                else {
                    // when we found ourself, the current is the next image
                    if (foundSelf) {
                        next = getInfo(image, "next", "Next &gt;");
                        // stop searching
                        break;
                    }
                    else {
                        // didn't find ourself yet, so this may be the one before ourself
                        prev = getInfo(image, "prev", "&lt; Prev");
                    }
                }
            }
        }
        
        imgPreloader = new Image();
        imgPreloader.onload = function(){
		
            imgPreloader.onload = null;
            
            // Resizing large images
            var x = window.getWidth() - 150;
            var y = window.getHeight() - 150;
            var imageWidth = imgPreloader.width;
            var imageHeight = imgPreloader.height;
            if (imageWidth > x) {
                imageHeight = imageHeight * (x / imageWidth);
                imageWidth = x;
                if (imageHeight > y) {
                    imageWidth = imageWidth * (y / imageHeight);
                    imageHeight = y;
                }
            }
            else 
                if (imageHeight > y) {
                    imageWidth = imageWidth * (y / imageHeight);
                    imageHeight = y;
                    if (imageWidth > x) {
                        imageHeight = imageHeight * (x / imageWidth);
                        imageWidth = x;
                    }
                }
            // End Resizing
            
            // TODO don't use globals
            TB_WIDTH = imageWidth + 30;
            TB_HEIGHT = imageHeight + 60;
            
            // TODO empty window content instead
            $("TB_window").innerHTML += "<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='" + url + "' width='" + imageWidth + "' height='" + imageHeight + "' alt='" + caption + "'/></a>" + "<div id='TB_caption'>" + caption + "<div id='TB_secondLine'>" + imageCount + prev.html + next.html + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div>";
            
            $("TB_closeWindowButton").onclick = TB_remove;
            
            function buildClickHandler(image){
                return function(){
                    $("TB_window").dispose();
                    new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
                    
                    TB_show(image.caption, image.url, rel);
                    return false;
                };
            }
            var goPrev = buildClickHandler(prev);
            var goNext = buildClickHandler(next);
            if ($('TB_prev')) {
                $("TB_prev").onclick = goPrev;
            }
            
            if ($('TB_next')) {
                $("TB_next").onclick = goNext;
            }
            
            document.onkeydown = function(event){
                var event = new Event(event);
                switch (event.code) {
                    case 27:
                        TB_remove();
                        break;
                    case 190:
                        if ($('TB_next')) {
                            document.onkeydown = null;
                            goNext();
                        }
                        break;
                    case 188:
                        if ($('TB_prev')) {
                            document.onkeydown = null;
                            goPrev();
                        }
                        break;
                }
            }
            
            // TODO don't remove loader etc., just hide and show later
            $("TB_ImageOff").onclick = TB_remove;
            TB_position();
            TB_showWindow();
        }
        imgPreloader.src = url;
        
    }
    else { //code to show html pages
        var queryString = url.match(/\?(.+)/)[1];
        var params = TB_parseQuery(queryString);
  
        TB_WIDTH_init = params['width'];
        TB_HEIGHT_init = params['height'];
  
   	   var x = window.getWidth() - 150;
	   var y = window.getHeight() - 150;
        if(!params['width'] || params['width']>x) params['width'] = x;
        if(!params['height'] || params['height']>y) params['height'] = y;

      
        TB_WIDTH = (params['width'] * 1) + 30;
        TB_HEIGHT = (params['height'] * 1) + 40;
        
        var ajaxContentW = TB_WIDTH - 30, ajaxContentH = TB_HEIGHT - 45;
        
   

        if (url.indexOf('TB_iframe') != -1) {
            urlNoQuery = url.split('TB_');
            //$("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close' class=MultiBoxClose><!--IMG SRC='/res/smoothbox/images/close.png' BORDER='0'--></a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TB_showWindow()'> </iframe>";
  			$("TB_window").innerHTML += "<a href='#' id='TB_closeWindowButton' title='Close' class=MultiBoxClose><IMG SRC='/1.gif' width=32 BORDER='0'></a><TABLE border=0 cellspacing=0 cellpadding=0 ID=TB_Box style='width:" + (ajaxContentW + 29 +58 ) + "px;height:" + (ajaxContentH + 17 +58) + "px;' ><TR> <TD class=CADRE_top_left></TD> <TD class=CADRE_top_center></TD> <TD class=CADRE_top_right align=right valign=top></TD></TR><TR> <TD class=CADRE_left></TD> <TD class=CADRE_inside><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TB_showWindow()'> </iframe></TD> <TD class=CADRE_right></TD></TR><TR> <TD class=CADRE_bottom_left></TD> <TD class=CADRE_bottom_center></TD> <TD class=CADRE_bottom_right></TD></TR></TABLE>";
  
            //$("TB_window").innerHTML += "<a href='#' id='TB_closeWindowButton' title='Close' class=MultiBoxClose><!--IMG SRC='/res/smoothbox/images/close.png' BORDER='0'--></a><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TB_showWindow()'> </iframe>";
			//pdivFrame.mbEnlarged = false;
        }
        else {
            //$("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><!--div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' class=MultiBoxClose><IMG SRC='/res/smoothbox/images/close.png' BORDER='0'></a></div--></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";
     	
     	
     	// Non exploitation du tile à l ademande JP 23.09.2009
		//if(caption!="")
		//	$("TB_window").innerHTML += "<div id='TB_title' style='width:" + ajaxContentW + "px;'>" + caption + "<div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' class=MultiBoxClose></a></div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";
		//else
		//$("TB_window").innerHTML += "<div id='TB_title' style='width:" + ajaxContentW + "px; height:1px'><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' class=MultiBoxClose></a></div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";
  		$("TB_window").innerHTML += "<a href='#' id='TB_closeWindowButton' title='Close' class=MultiBoxClose><IMG SRC='/1.gif' width=32 BORDER='0'></a><TABLE border=0 cellspacing=0 cellpadding=0 ID=TB_Box style='width:" + (ajaxContentW + 29 +58 ) + "px;height:" + (ajaxContentH + 17 +58) + "px;' ><TR> <TD class=CADRE_top_left></TD> <TD class=CADRE_top_center></TD> <TD class=CADRE_top_right align=right valign=top></TD></TR><TR> <TD class=CADRE_left></TD> <TD class=CADRE_inside><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div></TD> <TD class=CADRE_right></TD></TR><TR> <TD class=CADRE_bottom_left></TD> <TD class=CADRE_bottom_center></TD> <TD class=CADRE_bottom_right></TD></TR></TABLE>";
			
        }
        
        if($("TB_closeWindowButton"))
	        $("TB_closeWindowButton").onclick = TB_remove;
			
		if (url.indexOf('TB_parentreloadonclose') != -1)
	        $("TB_closeWindowButton").onclick = TB_removeAndReload;
        
        
        if (url.indexOf('TB_string') != -1)
		{
            $("TB_ajaxContent").innerHTML = tb_string;
            TB_position();
            TB_showWindow();
        }
        else         
        
	        if (url.indexOf('TB_inline') != -1)
			{
	            TB_fromID = params['inlineId'];
			  $("TB_ajaxContent").innerHTML = ($(params['inlineId']).innerHTML);
	            $(params['inlineId']).innerHTML="";
	            TB_position();
	            TB_showWindow();
	        }
	        else 
	            if (url.indexOf('TB_iframe') != -1)
				{
	                TB_position();
	                if (frames['TB_iframeContent'] == undefined)
					{//be nice to safari
	                    $(document).keyup(function(e){
	                        var key = e.keyCode;
	                        if (key == 27) {
	                            TB_remove()
	                        }
	                    });
	                    TB_showWindow();
	                }
	            }
	            else
				{
	                var handlerFunc = function(){
	                    TB_position();
	                    TB_showWindow();
	                };
	
					new Request.HTML({
	                    method: 'get',
	                    update: $("TB_ajaxContent"),
	                    onComplete: handlerFunc
	                }).get(url);
	            }
    }
    
    window.onresize = function(){
    	   if(TB_bFullScreen)TB_fullscreen();
        TB_position();
        TB_load_position();
        TB_overlaySize();
    }
    
    document.onkeyup = function(event){
        var event = new Event(event);
        if (event.code == 27) { // close
            TB_remove();
        }
    }
    
}

//helper functions below

function TB_showWindow(){
    //$("TB_load").dispose();
    //$("TB_window").setStyles({display:"block",opacity:'0'});
	
	//if (! document.getElementById("TB_window")) return;
    
    if (TB_doneOnce == 0) {
        TB_doneOnce = 1;
        
        $('TB_window').set('tween', {
            duration: 250,
            onComplete: function(){
                if ($('TB_load')) {
                    $('TB_load').dispose();
                }
            }
        });
        $('TB_window').tween('opacity', 0, 1);
        
    }
    else {
        $('TB_window').setStyle('opacity', 1);
        if ($('TB_load')) {
            $('TB_load').dispose();
        }
    }
}

function tb_remove(){
	return TB_remove()

}

function TB_removeAndReload()
{
	TB_remove();
	window.location.reload( true );
}
function TB_remove()
{
	//alert("TB_remove");

     if (TB_fromID){
          $(TB_fromID).innerHTML = $("TB_ajaxContent").innerHTML;
          TB_fromID = false;
      }


	if ($("TB_overlay")) 
		$("TB_overlay").onclick = null;
    document.onkeyup = null;
    document.onkeydown = null;
    
    if ($('TB_imageOff'))       		 $("TB_imageOff").onclick = null;
    if ($('TB_closeWindowButton'))       $("TB_closeWindowButton").onclick = null;
    if ($('TB_prev')) 			{        $("TB_prev").onclick = null;    }
    if ($('TB_next')) 			{        $("TB_next").onclick = null;    }
    
    if($('TB_window'))
	{
		$('TB_window').set('tween', {
			duration: 250,
			onComplete: function(){
				$('TB_window').dispose();
			}
		});
		$('TB_window').tween('opacity', 1, 0);
    }
    
    if ($('TB_overlay'))
	{
		$('TB_overlay').set('tween', {
			duration: 400,
			onComplete: function(){
				$('TB_overlay').dispose();
			}
		});
		$('TB_overlay').tween('opacity', 0.6, 0);
    }
    window.onscroll = null;
    window.onresize = null;
    
	if ($('TB_HideSelect'))
	{
		$('TB_HideSelect').dispose();
	}
    TB_init();
    TB_doneOnce = 0;
    tb_bwait = false;
    
    return false;
}

function TB_fullscreen(){
	if (!$("TB_window")) return;
	TB_bFullScreen = true;

	TB_WIDTH_init = window.getWidth() - 60;
	TB_HEIGHT_init = window.getHeight() - 60;

	TB_position();
}


function TB_position()
{
var stag, sid;

	if (!$("TB_window")) return;

/*	if(!p)
	{
        pp = new Element('div');
        pp.setProperty('id', 'TB_window');
		pp.injectInside(document.body);
		
        $('TB_window').setOpacity(0);
	}
*/	

	if ($('TB_window').mbEnlarged)
	{
//		$('TB_window').setWidth ( window.getWidth()-60 );
//		$('TB_window').setHeight ( window.getHeight()-60 );
	}


    $('TB_window').set('morph', {
        duration: 75
    });

	

/*
	
	if(tb_bwait){
		w = TB_WIDTH;	// il faut un reglage différent pour le wait, je sais pas pourkoi. JG
	}else{
		w = $('TB_window').getWidth();
	*/	
		//alert("Redim")
		width = TB_WIDTH_init;
		height = TB_HEIGHT_init;
		
		var x = window.getWidth() - 60;
		var y = window.getHeight() - 60;
		if(!TB_WIDTH_init || TB_WIDTH_init >x){
			width = x;
			//alert("Redim Width")
		}else{
			width = TB_WIDTH_init;
		}
		if(!TB_HEIGHT_init || TB_HEIGHT_init>y){
			height = y;
			//alert("Redim Height")
		}else{
			height = TB_HEIGHT_init
		}
		
		
		TB_WIDTH = (width * 1) ;//+ 30
		TB_HEIGHT = (height * 1) ;//+ 40
		
		w = TB_WIDTH;
//	}
	
	
//	alert("POS : "+TB_WIDTH_init+" x "+TB_HEIGHT_init)
	
	
    $('TB_window').morph({
		width: TB_WIDTH+58 + 'px',
		height: TB_HEIGHT+58 + 'px',
		left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH - 58) / 2) + 'px',
		top: (window.getScrollTop() + (window.getHeight() - TB_HEIGHT-58) / 2) + 'px'
	});	
	
	
	
	 var ajaxContentW = TB_WIDTH - 30, ajaxContentH = TB_HEIGHT - 45;

	if($('TB_iframeContent')){
		$('TB_iframeContent').setStyles({
		        "height": TB_HEIGHT +'px',
		        "width": TB_WIDTH  +'px'
		    });
	
	}


	if($('TB_Box')){
		$('TB_Box').setStyles({
		        "height": TB_HEIGHT+58 +'px',
		        "width": TB_WIDTH +58 +'px'
		    });
	
	}


	
}











function TB_overlaySize()
{
	if (!$("TB_overlay")) return;
	if (!$("TB_HideSelect")) return;


    // we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
    $("TB_overlay").setStyles({
        "height": '0px',
        "width": '0px'
    });
    $("TB_HideSelect").setStyles({
        "height": '0px',
        "width": '0px'
    });
    $("TB_overlay").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
    $("TB_HideSelect").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
}

function TB_load_position()
{
    if (!$("TB_load")) return ;
	
	$("TB_load").setStyles({
		//left: (window.getScrollLeft() + (window.getWidth() - 56) / 2) + 'px',
		left: ((window.getScrollLeft() + (window.getWidth() - $('TB_load').getWidth()) / 2) )  + 'px',//TB_WIDTH -40
		top: (window.getScrollTop() + ((window.getHeight() - 20) / 2)) + 'px',
		display: "block"
	});
    
}

function TB_parseQuery(query){
    // return empty object
    if (!query) 
        return {};
    var params = {};
    
    // parse query
    var pairs = query.split(/[;&]/);
    for (var i = 0; i < pairs.length; i++) {
        var pair = pairs[i].split('=');
        if (!pair || pair.length != 2) 
            continue;
        // unescape both key and value, replace "+" with spaces in value
        params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
    }
    return params;
}

