/**
*
* ajax-interface.js
* 
*/

function trap_error(error, url, line) {
// alert("There was an error from '" + url + "' at line '" + line + "'.\n\n" + error);
return true;
}

window.onerror = trap_error;



var Exo_AjaxInterface = new Class({

    Implements: [Options],

    options: {},

    initialize: function (options)
    {
        this.setOptions(options);
    },

    LoadUrl: function(url, route_name, css_id, to_update_area_id)
    {
        update_area_id = to_update_area_id || 'innerWrapper'; 

        var loadContainAjax = new Request.HTML({
            url:  		url,
            method:     'get',
            onSuccess:      function(r_tree, r_elements, r_html, r_js)
            {
                this.changeBodyCssId(css_id);
                this.reloadAd(css_id, route_name);
                $(update_area_id).set('html', r_html);
                $exec(r_js);
            }.bind(this),
            evalScripts: false,
            evalResponse: false,
            data:       
            {
                route_name: route_name
            }
            }).send();

            return false;
        },

        reloadAd: function(css_id, route) {
            var iframe = $('iframe_ad_reload');
            if (!iframe)
            {
                iframe = new Element('iframe', {'id': 'iframe_ad_reload'}).setStyle('display', 'none').inject(document.body);
            }
                            
            iframe.set('src', this.options.ad_url+'?css_id='+css_id+'&route='+route+'&refresh='+new Date().getTime());

        },

        changeBodyCssId: function(new_id)
        {
            if (new_id) {
                $$('body').set('id', new_id);
                if (new_id == 'start' || new_id == 'channels' || new_id == 'profile' || new_id == 'epg') {
                    $('navPlayer').getElements('li').removeClass('active');
                    $('mainnav_'+new_id).addClass('active');
                }
            }
        },


        SwitchBox: function(url, switch_id)
        {
            // alert(url)
            var loadContainAjax = new Request.HTML({
                url:  		url,
                method:     'post',
                update:     $('rightColumn'),
                // onSuccess:  function(result){alert(result)},
                // onFailure:  function(result){alert(result)},
                data:       {
                    // switch_id: switch_id
                }
                }).send();

                return false;
            },

            sendForm: function(form_id, to_update_area_id)
            {
                var newsletter_form = new Request.HTML({
                    url:            $(form_id).get('action'),
                    onSuccess:      function(r_tree, r_elements, r_html, r_js)
                    {
                        $(to_update_area_id).set('html', r_html);
                        $exec(r_js);
                    },
                    onFailure:      function(r_text)
                    {
                        $(to_update_area_id).set('html', r_text.responseText);
                    }
                    }).post($(form_id));

                    return false;
                },

                sendFormChannelSwitch: function(form_id, to_update_area_id)
                {
                    // alert($('switchBox').value);

                    new Request.HTML({
                        url: $(form_id).get('action')+'/'+$('switchBox').value,
                        update: $(to_update_area_id),
                        data: {
                            route_name: 'channels.camp'
                        }
                        }).send();

                        this.changeBodyCssId('liste');

                        return false;
                    }

                });
