﻿/* objet qui va être stocké dans un array au niveau des divs */
function AjaxCacheScript( key, script, html ) {
    this.key = key;
    this.script = script;
    this.html = html;
    this.js = null;
    this.getHtml=function()
    {
        if( this.html == null )
        {
            // Ajax Call
            AjaxCache.incAjaxCall();
            eval( this.script );
            //alert('calling');
        }
        return this.html;
    }
    this.addHtml = function( html ) 
    {
        this.html = html;
    }
}

/*groupe de methodes associées au div */
var AjaxCacheZone = 
{
    getFirstScript:function()
    {
        return this.scripts[0];
    },
    getScript:function( key )
    {
        for( var i = 0; i < this.scripts.length; i++ )
        {
            if( this.scripts[i].key == key ) 
            {
                return this.scripts[i];
            }
        }
        return null;
    }
}


var AjaxCache = 
{
    zones:new Array(),
    ajaxCall:0,
    addCache:function( divId, key, script )
    {
        var zone = document.getElementById( divId );
        if (!zone.getFirstScript) 
        {
            zone.getFirstScript = AjaxCacheZone.getFirstScript;
            zone.getScript = AjaxCacheZone.getScript;
            zone.key = 0;
            zone.scripts = new Array();
            AjaxCache.zones.push(zone);
        }
        var html = null;
        if( script == null )
        {
            html = zone.innerHTML;
        }
        var script = new AjaxCacheScript( key, script, html );
        zone.scripts.push( script );
    },
    activate:function( divId, key )
    {
        //JGO: I comment this line because it makes problems with pages with more than one paged controls (i.e new home micro). This line seems to not impact others pages.
        //AjaxCache.close( divId );
        AjaxCache.internalActivate( divId, key );
    },
    close:function( divId )
    {
        
        var zones = AjaxCache.zones;
        for( var i = 0; i < zones.length; i++ )
        {
            var zone = zones[i];
            if( zone.id != divId )
            {
                var script = zone.getFirstScript();
                if( script != null )
                {
                    zone.key = script.key;
                    var html = script.getHtml();
                    // In cache
                    if( html != null )
                    {
                        zone.innerHTML = html;
                        hoverblock.init(zone);
                    }
                }
            }
        }
    },
    internalActivate:function( divId, key )
    {
        var zone = document.getElementById( divId );
        if( zone != null )
        {
            zone.key = key;
            var script =  zone.getScript( key );
            var html = script.getHtml();
            if( html != null )
            {
                zone.innerHTML = html;
                hoverblock.init(zone);
            }
            if (script.js != null)
                eval(script.js);
        }
    },
    updateCache:function( divId, html )
    {
        AjaxCache.decAjaxCall();
        var zone = document.getElementById( divId );
        if( zone != null )
        {
            zone.innerHTML = html;
            hoverblock.init(zone);
            var script = zone.getScript( zone.key );
            if( script != null )
            {
                script.html = html;
            }
        }
    },
    updateJsCache:function( divId, js )
    {
        var zone = document.getElementById( divId );
        if( zone != null )
        {
            hoverblock.init(zone);
            var script = zone.getScript( zone.key );
            if( script != null )
            {
                script.js = js;
            }
        }
    },
    incAjaxCall:function()
    {
        AjaxCache.ajaxCall += 1;
        if( AjaxCache.ajaxCall > 0 )
        {
            waitingMsg.show();
            waitingMsg.setHideTimer();
            //document.style.cursor = "wait";
        }
    },
    decAjaxCall:function()
    {
        AjaxCache.ajaxCall -= 1;
        if( AjaxCache.ajaxCall == 0 )
        {
            //alert('uncall');
            waitingMsg.hide();
            //document.style.cursor = "auto";
        }
    }
}

function JSHandlerStructCache( response )
{
    var array = response.split( '¤' );
    var prevDivId = null;
    for( var i = 0; i < array.length; i+=2 )
    {
        var divId = array[i];
        if( i+1 < array.length )
        {
            var text = array[i+1];
            if( divId != "script" )
            {
                var div = document.getElementById( divId );
                if( div != null )
                {
                    prevDivId = divId;
                    AjaxCache.updateCache( divId, text );
                }
            }
            else
            {
                eval( text );
                if (prevDivId != null)
                {
                    AjaxCache.updateJsCache( prevDivId, text );
                    prevDivId = null;
                }
            }
        }
    }
}

function RegisterRendering( divId, key, script )
{
    AjaxCache.addCache( divId, key, script );
    return false;
}

function ActivateRendering( divId, key )
{
    AjaxCache.activate( divId, key );
    return false;
}

function UpdateCacheRendering( divId, key, html )
{
    AjaxCache.updateCache( divId, key, html );
    return false;
}

function DoRendering( divId, key, script )
{
    var zone = document.getElementById( divId );
    if (zone != null)
    {
        if (!zone.getFirstScript) 
        {
            RegisterRendering( divId, key, script );
            }
        else
        {
            var Zonescript = zone.getScript( key );
            if( Zonescript == null )
            {
                RegisterRendering( divId, key, script );
            }
        }
        ActivateRendering( divId, key );
    }
    return false;
}

function DirectRendering( script )
{
    eval(script);
    return false;
}

function DirectRenderingWithMsg( script )
{
    AjaxCache.incAjaxCall();
    return DirectRendering(script);
}

function JSHandlerStructWithMsg( response )
{
    JSHandlerStruct(response);
    AjaxCache.decAjaxCall();
}
