/*!
    Exo_AdReload
        Reloads Ads w/o reloading the page.

    Copyright:
        2008, Exozet Berlin GmbH
*/
/**
 * The single Ad-Reloader
 * No Class needed
 *
 * @version     $Id: exo_ad_reload.js 7000 2008-11-24 10:58:44Z rschulze $
 * @author      Robert Schulze, exozet interact <robert.schulze@exozet.com>
 */
var Exo_AdReload = {
    
    groups: new Hash(),
    
    /**
     * group : the name of the group to create
     * parameters : {
     *   'url': where to fetch the ads from (!! This must be the plan url, whith no parameters added. Add Parameters using "url_parms" !!)
     *   'url_parms': Parameters to add to the url. This is an Object like {'param': 'value', 'param1': '1'}
     *   'sandbox': the _name_ of the sandbox (iframe) to use
     *   'callbacks': an array of callbacks to call after mooving of all elements was done
     * }
     */
    createGroup: function (group, parameters)
    {
        if (!this.groups.has(group))
        {
            this.groups.set(group, new Hash({'children': new Hash()}));
        }
        this.groups.get(group).set('parameters', new Hash($merge({'url_parms': {}, 'callbacks': []}, parameters)));
    },
    
    /**
     * group  : the group name to register the target on
     * target : the dom id of the target wrapper
     * parameters: {
     *    'source' : optional, the dom id of the element to get the ad from source
     * }
     */
    registerPart: function (group, target, parameters)
    {
        if (this.groups.has(group))
        {
            this.groups.get(group).get('children').set(target, new Hash($merge({'source': target, 'target': target}, parameters)));
        }
    },
    
    addCallback: function (group, callback, parameters)
    {
        this.groups.get(group).get('parameters').get('callbacks').include({'callback': callback, 'parameters': (parameters || [])});
    },
    
    runCallbacks: function (group)
    {
        this.groups.get(group).get('parameters').get('callbacks').each(function (call_back)
        {
            call_back.callback.attempt(call_back.parameters);
        });
    },

    /**
     * group: the Groups name to update
     * params: Additional parameters to add to the URL as an Object like {'param': 'value', 'param1': '1'}
     *         This parameters will be merged whith (and overwrite) those registrated in createGroup()
     */
    update: function (group, params)
    {
        var params = params || {};
        if (this.groups.has(group))
        {
            this.groups.get(group).set('is_loading', true);
            var query_params = $merge(this.groups.get(group).get('parameters').get('url_params'), {'_cache_buster': $time()}, params);
            var sandbox = this.groups.get(group).get('parameters').get('sandbox');
            var f = $(document.body).getElement('iframe[name=' + sandbox + ']');
            if (f)
            {
                f.src = this.groups.get(group).get('parameters').get('url') + '?' + Hash.toQueryString(query_params);
            }
        }
    },
    
    ready: function (group)
    {
        //wait a moment for the iframe to get ready
        (function ()
        {
            this.groups.get(group).set('is_loading', false);
            if (this.groups.has(group))
            {
                var sandbox = this.groups.get(group).get('parameters').get('sandbox');
                this.groups.get(group).get('children').each(function (child)
                {
                    // empty the target - allways
                    $(child.get('target')).empty();
                    
                    try
                    {
                        //fetch the new ad
                        var grain = frames[sandbox].document.getElementById( child.get('source') );
                    }
                    catch(e)
                    {
                    }
                    
                    if (grain)
                    {
                        $(child.get('target')).innerHTML = grain.innerHTML.stripScripts();
                    }
                });
                
                this.runCallbacks(group);
            }
        }.bind(this)).delay(100);
    },
    
    isLoading: function (group)
    {
        if (this.groups.has(group))
        {
            return this.groups.get(group).get('is_loading');
        }
        return false;
    }
};




