//
//------------------------------------------------------------------------------------
// Modifications :
//****************
//
// 20/09/2009 par punglas - fonction qui affiche les boites modales avec appel Ajax
// 14/10/2009 par pascal: Ajout du script pour créer un objet console bidon si
//                        celui pour Firebug n'est pas présent.
//------------------------------------------------------------------------------------
//

var trace_evo_active = false;

//
// Crée un objet console bidon si celui pour Firebug n'est pas présent.
// Ca permet de laisser dans le code JS des traces pour Firebug sans se soucier
// de la configuration du navigateur.
//
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

function trace(message)
{
    if (trace_evo_active)
        console.log(message);
}

//
// Définit la méthode 'indexOf' pour les tableaux. Requis pour IE.
//
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i=0; i<this.length; i++) {
            if (this[i]==obj) {
                return i;
            }
        }
        return -1;
    }
}

// $() is a css selector. First I choose what triggers the event. In this case when  
// you CLICK on a LINK with the js-ajax CLASS. I changed the example to use the live() 
// function so that any javascript-generated html will be binded too instead of click(). 
$('a.js-ajax').live('click', function() { 
    // Now we simply make the ajax call. load($url) will pull the url's VIEW and put it  
    // into ther innerhtml of whatever tag you called load on. In this case, I want to fill  
    // up my #overlayer div with the results of the ajax. 
    document.getElementById("overlayer").innerHTML = 'Chargement en cours';
    $('#overlayer').dialog({
			bgiframe: true,
			height: 140,
			modal: true
		});
    $('#overlayer').load( 
        // Here is the tricky part. Instead of hard-coding a url to pass, I just had jquery  
        // go look at what the link (from the outside scope, .click() part) was already going  
        // to (href) and used that as the argument. 
        $(this).attr('href') 
    , function () { 
        // This is a callback, after the ajax gets loaded, the #overlayer div gets faded in at 300 miliseconds. 
        //$(this).fadeIn(300); 
        $('#overlayer').dialog('destroy');
        $('#overlayer').dialog({
					bgiframe: true,
					modal: true,
					width : 700
				});
        
    }); 
    // And finally to prevent actually making the link go anywhere 
    return false; 
});

$('a.js-ajax-log_window').live('click', function() { 
    // Now we simply make the ajax call. load($url) will pull the url's VIEW and put it  
    // into ther innerhtml of whatever tag you called load on. In this case, I want to fill  
    // up my #overlayer div with the results of the ajax. 
    document.getElementById("overlayer").innerHTML = 'Chargement en cours';
    $('#overlayer').dialog({
			bgiframe: true,
			height: 140,
			modal: true
		});
    $('#overlayer').load( 
        // Here is the tricky part. Instead of hard-coding a url to pass, I just had jquery  
        // go look at what the link (from the outside scope, .click() part) was already going  
        // to (href) and used that as the argument. 
        $(this).attr('href') 
    , function () { 
        // This is a callback, after the ajax gets loaded, the #overlayer div gets faded in at 300 miliseconds. 
        //$(this).fadeIn(300); 
        $('#overlayer').dialog('destroy');
        $('#overlayer').dialog({
					bgiframe: true,
					modal: true,
					width : 700,
					height : 700
				});
        
    }); 
    // And finally to prevent actually making the link go anywhere 
    return false; 
});

function faitPatienter(on, texte)
{
    $('body').css('cursor', (on ? 'wait' : 'default'));

    var id = 'fait-patienter';
    if (on) {
        var w_height = $(window).height();
        var w_width = $(window).width();
        //console.debug('Window = ', w_width, 'x', w_height, ' pixels.');
        var popup_width = Math.min(350, Math.round(0.20*w_width));
        //console.debug('Popup width = ', popup_width);
        var popup_top = Math.round(0.40*w_height);
        //console.debug('Popup top = ', popup_top);
        var popup_left = Math.round((w_width - popup_width) / 2);
        //console.debug('Popup left = ', popup_left);
        var style = 'with:' + popup_width + 'px; opacity:0.67; border:2px solid #A52A2A; padding: 1.5em 3em 1.5em 3em; top: ' + popup_top + 'px; left:' + popup_left + 'px; position: fixed; background-color:#FFB6C1; color:maroon; z-index:10000; font-size:140%; font-weight: bold';
        var html = '<div id="' + id + '" style="' + style + '">' + texte + '</div>'
        $('body').prepend(html);
    }
    else
        $('#' + id).remove();
}



