var url = window.location.href;
var index= url.indexOf("?");
var xtorLocalParam = "";
var xtorParam = "";
var lesailesParam;
var currentPopup = null;

if (index != -1){
    xtorParam = "&"+url.substring(index+1,url.length);
}

if(url.indexOf("?lesailes") != '-1'){
    lesailesParam = url.substring(index+1,url.length);
}

function openWindow(link){
    if(link.indexOf("?xtor") == '-1'){
        if (xtorLocalParam!= null){
            link= link+xtorLocalParam;
        }
    }

    var hermesWindow =  window.open(link,"Hermes","toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,width="+screen.availWidth+",height="+screen.availHeight+",left=0,top=0");

    if (hermesWindow == undefined) {
        ampm_screen_link(true, link);
    } else {
    	currentPopup = hermesWindow;
    }

    if (currentPopup != null && !currentPopup.closed) {
        currentPopup.focus();
        return;
    }
    
}

function getUrl(link){
    if (xtorParam!= null){
        link= link+ xtorParam;
    }
    document.location.href = link;
}

function getLocalUrl(link){
    if (xtorLocalParam!= null){
        link= link+ xtorLocalParam;
    }
    document.location.href = link;
}

function putXtorParam(){
    var xtorElement= document.getElementById("xtor");
    if (xtorElement!= null){
        var index_eq=gal= xtorParam.indexOf("=");
        if (index_eq!= -1){
            var valeur=xtorParam.substring(index_eq+1,xtorParam.length);
            xtorElement.value= valeur;  
        }
    }
}

function openLesAilesLink() {

    if(lesailesParam != null) {

        var index_eq_ailes = lesailesParam.indexOf("=");
        if (index_eq_ailes != -1){
            var valeur_ailes = lesailesParam.substring(index_eq_ailes+1, lesailesParam.length);
            openWindow("http://lesailes.hermes.com/" + decodeURIComponent(valeur_ailes));
        }
        
    }
}

function ampm_screen_link(visible, link)
{
    var colMenu_width = jQuery('td.colMenu').width();
    jQuery('td.colMenu').css('width', colMenu_width);

    jQuery('td.colMenu ul').toggle();
    jQuery('div.bgLad1').toggle();
    jQuery('div.bgLad2').toggle();
    jQuery('.tbLinks').toggle();
    jQuery('#suscribeNewsLetter').toggle();
    jQuery('#heart_and_craft_button').toggle();
    jQuery('span.msgAnnoucementImportant').toggle();

    if (visible)
    {
        var popup_title = getMessageDefault('redirection','popup_title');
        var popup_link_message = getMessageDefault('redirection','popup_link_message');
        var lad2_src = jQuery('div.bgLad2 img').attr('src');
        var new_object_link = '<div style="text-align: center" id="object_link"><a href=\"'+link +'\" rel=\"popup\"><img src=\"'+lad2_src+'\" alt=\"Hermès\"></a><div style="width: 335px; margin: 0 auto; font-size: 11px; color: rgb(102, 69, 47)"><p style="text-align: center">'+popup_title+'</p><a href=\"'+link+'\" style=\"width: inherit; \" rel=\"popup\" class=\"link\"><span class="chip"> </span>'+popup_link_message+'</a></div></div>';
        jQuery('div.bgLad2').css('text-align', 'center');
        jQuery('div.colFiletContent').prepend(new_object_link);

        jQuery('a[rel="popup"]').bind("click", function(e){
            openWindow( jQuery(this).attr('href') );
            ampm_screen_link(false);

            return false;
        });
    }
    else
    {
    	jQuery('#object_link').html('');
    } 

}
function ampm_init()
{
	jQuery('a[rel="popup"]').bind("click", function(e){
        openWindow( jQuery(this).attr('href') );

        return false;
    });
}

function ampm_extra()
{
    jQuery('a[rel="popup"]').bind("click", function(e){
        openWindow( jQuery(this).attr('href') );
        return false;
    });
}

function initialize() {

    if(typeof langRedirection != 'undefined' && langRedirection!=null && langRedirection!='DEFAULT' && langRedirection!=''){
        if (typeof(descDefault[langRedirection]) != 'undefined') {
            var extra = descDefault[langRedirection]["redirection"]["extraFile"];
            if (typeof extra != 'undefined'){
                ampm_extra();
            }
        }
        
        openLesAilesLink();
    }
}

function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}
