/*************************************************************
*METHODS ENABLE TO APPLY RULES OF PRESENTATION TO THE HEADER** 
****************AND THE MENU ABOVE****************************
************Code by EOS -- JANUARY 2010 --********************
**************************************************************/

/* The both methods below enables to apply rules of presentation to the header.
If the my disneyland si not linked, the method moveLayerWithoutLink is called , else the
method moveLayerWithLink is called by the public method moveDownLayer */

// Private

function moveLayerWithLink() {
    var menuHeader = document.getElementById("menuHeader");
    var roundFrame = document.getElementById("menuRoundFrame");
    var menuInsideHeader = document.getElementById("menuInsideHeader");
    var menuHeaderLink = document.getElementById("menuHeaderLink").offsetWidth;
    var frameWidth = roundFrame.offsetWidth;
    var menuHeaderWidth = menuHeader.offsetWidth;    
    var widthMenuInsideHeader = menuHeaderWidth- (menuHeaderLink+ frameWidth+ 30);
    var menuInsideHeaderUl = new Array()
    menuInsideHeaderUl = menuInsideHeader.getElementsByTagName("ul");
    menuInsideHeader.style.width=widthMenuInsideHeader+"px";
    var menuHeaderHeight = menuInsideHeader.offsetHeight;    
    if (menuHeaderHeight > 31) {
        for (var i=0; i<menuInsideHeaderUl.length; i++) {
            menuInsideHeaderUl[i].style.height="22px";
        }
        menuInsideHeader.style.width=(widthMenuInsideHeader-20)+"px";
        menuInsideHeader.style.marginTop="20px";
        menuInsideHeader.style.marginLeft="30px";
    }
    menuHeader.style.visibility="visible";
}
// Private

function moveLayerWithoutLink() {
    var menuHeader = document.getElementById("menuHeader");
    var menuInsideHeader = document.getElementById("menuInsideHeader");
    menuInsideHeader.style.width="600px";
    menuInsideHeader.style.height="10px";
    var menuInsideHeaderli = new Array();
    menuInsideHeaderli = menuInsideHeader.getElementsByTagName("li");
    for (var i = 0; i<menuInsideHeaderli.length; i++) {
        menuInsideHeaderli[i].style.cssFloat="left";
        menuInsideHeaderli[i].style.width="auto";
    }
    menuHeader.style.visibility="visible";
}

// Public

function moveDownLayer() {
    if (document.getElementById("menuRoundFrame") != null) {
        moveLayerWithLink();
    } else {
        moveLayerWithoutLink();
    }
}

// This method enables to make distinction between Internet Explorer Versions - Private

function msieVersion() {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf ( "MSIE " );
    if ( msie > 0 ) {
        return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
    }
    else {
        return 0;
    }
}

// This method applies specific presentation rules to the rollover menu
// If the text of the menu is longer than the box, the menu is affected by
// a sepific styleSheet which allows to display the menu on two lines
// - Public


function displayTwoLines() {
    var menu = document.getElementById("menuContainer");
    var menuUl = menu.getElementsByTagName("ul");
    var menuUlFirstLevel = new Array();
    var i = 0;
    var j = 0;
    var z = 0;
    var navigateur = navigator.appName;
    
    var ie = "Microsoft Internet Explorer";
    var menuLi = menu.getElementsByTagName("li");
    var menuStyle = new Array();
    var menuLength = new Array();
    var stringData = new Array();
    var arrayString = new Array();
    var nbreEspace = new Array();
    var stringDataLi = new Array();
    var lineHeihtToLine = new Array();

    // Test IE & OPERA
    if (msieVersion() > 6 || navigateur=="Opera" || navigator.platform=="MacPPC") {
        document.getElementById("blueLine").style.marginTop="-1px";
        document.getElementById("imageRightMenu").style.marginTop="-16px";
    }
    //alert("menuLi.length "+menuLi.length);
    for (i =0; i<menuUl.length; i+=2) {
        menuUlFirstLevel[j++]=menuUl[i];
    }
    
    for (i=0; i<menuLi.length; i++) {
        stringDataLi[i] = menuLi[i].firstChild.firstChild.data;
    }
    
    for (i = 0; i<menuUlFirstLevel.length; i++) {
         
        if (navigateur == ie && msieVersion()<9) {          
            stringDataLi.splice(stringDataLi.indexOf(menuUlFirstLevel[i].firstChild.firstChild.firstChild.data),1);
        }
        else {
            stringDataLi.splice(stringDataLi.indexOf(menuUlFirstLevel[i].childNodes[1].firstChild.firstChild.textContent),1);
        }

    }

    for (i = 0; i<stringDataLi.length; i++) {
        if (stringDataLi[i].length>29) {
            lineHeihtToLine[z++]=stringDataLi[i];
        }
    }

    for (i=0; i<menuLi.length; i++) {
        for (var y=0; y<lineHeihtToLine.length; y++) {
            if (lineHeihtToLine[y]==menuLi[i].firstChild.firstChild.data) {
                menuLi[i].firstChild.style.lineHeight="18px";
                menuLi[i].firstChild.style.height="35px";
            }
        }
    }

    for (y=0; y<menuUlFirstLevel.length;y++) {

        if (navigateur == ie ) {
            menuStyle[y] = (msieVersion() < 9) ? menuUlFirstLevel[y].firstChild.firstChild.style : menuUlFirstLevel[y].childNodes[1].firstChild.style;
            stringData = (msieVersion() < 9) ? trim(menuUlFirstLevel[y].firstChild.firstChild.firstChild.data) : trim(menuUlFirstLevel[y].childNodes[1].firstChild.firstChild.data);
            
            menuLength[y] = (stringData.length)*6;
            arrayString = stringData.split(" ");
            nbreEspace = arrayString.length-1;

            if ((menuLength[y]>102 && menuLength[y]<109) || (menuLength[y]>105 && menuLength[y]<170 && nbreEspace>3)) {
                menuStyle[y].letterSpacing="0.05em";
            }
            if( menuLength[y]>102) {
                menuStyle[y].lineHeight="14px";
                if (msieVersion() == 6 ) {
                    menuStyle[y].backgroundPosition="0em -0.118em";
                    menuStyle[y].marginTop="4px";
                } else {
                    menuStyle[y].backgroundPosition="-0.45em -0.3em";
                    if (msieVersion() == 9) {
                        menuStyle[y].marginTop="2px";
                    }
                }
            }
        }else {
            menuStyle[y] = menuUlFirstLevel[y].childNodes[1].firstChild.style;
            stringData = trim(menuUlFirstLevel[y].childNodes[1].firstChild.firstChild.textContent);
            menuLength[y] = (stringData.length)*6;
            arrayString = stringData.split(" ");
            nbreEspace = arrayString.length-1;
            var goTo = false;
            if (nbreEspace>2 && menuLength[y]==96) {
                menuStyle[y].lineHeight="14px";
                goTo = true;
            }
            else if (menuLength[y]>=108 && goTo == false) {
                menuStyle[y].lineHeight="14px";
                if (menuLength[y]<=112) {
                    menuStyle[y].letterSpacing="1pt";
                }
            }
        }
    }
}

// Fix indexOf in IE
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
        for (var i = (start || 0), j = this.length; i < j; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}


function trim (myString) {
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}


/* This method enables to display the login/logout system depending
of the identification mode of the guest - Public */

function displayLoginLogout(){
    var menuInsideHeaderRight = document.getElementById("menuInsideHeaderRight");
    var navBarLi = menuInsideHeaderRight.getElementsByTagName("li");
    if (Get_Cookie( 'LOGIN' )) {
        navBarLi[2].style.display="block";
        navBarLi[1].style.display="none";
    }
    else {
        navBarLi[1].style.display="block";
        navBarLi[2].style.display="none";
    }
}

// This method enables to highlight rollover menu depending where the guest is
// connected - Public

function highLightLink(menuHeaderSelected) {
    var backgroundUrlQa="url(http://qa-visiter-fr.emea.wdpr.disney.com/UK/EN/Neutral/Images/107x28-backBlueMenu.jpg)";
    var backgroundUrlProd="url(http://www.disneylandparis.co.uk/UK/EN/Neutral/Images/107x28-backBlueMenu.jpg)";
    var menu = document.getElementById("menuContainer");
    var menuUl = new Array();
    menuUl = menu.getElementsByTagName("ul");
    var menuUlFirstLevel = new Array();
    var j = 0;
    var i =0;
    var ApplyToMenu = "";
    var navigateur = navigator.appName; 
    if(menuHeaderSelected == '8'){
        var MenuImageRightSrc = document.MenuImageRight.src;
        MenuImageRightSrc = MenuImageRightSrc.replace(".jpg", "Select.jpg");
        document.MenuImageRight.src = MenuImageRightSrc;
    }
    else{               
        for (i =0; i<menuUl.length; i+=2) {
            menuUlFirstLevel[j++]=menuUl[i];
        }
        ApplyToMenu =  menuUlFirstLevel[menuHeaderSelected].firstChild.firstChild || menuUlFirstLevel[menuHeaderSelected].childNodes[1].firstChild;
        with(ApplyToMenu.style) {
            background = backgroundUrlProd;
            backgroundRepeat="no-repeat";
            backgroundPosition="center";
            color="#fff";
            }
        
        if (navigateur == "Microsoft Internet Explorer" ) {      
            with(ApplyToMenu.style) {               
                if (msieVersion() > 6) {
                    width="107px";
                    marginLeft="-1px";
                    marginTop="-1px";
                    menu.style.height = "30px";
                   
                }
                }
            if (ApplyToMenu.style.lineHeight == "14px"){
                ApplyToMenu.style.marginTop="2px";
                if (msieVersion() > 6) {
                    ApplyToMenu.style.marginTop="1px";
                } else {
                    ApplyToMenu.style.marginTop="2px";
                }
            }
        }
        else {            
            with(ApplyToMenu.style) {                
                width="106px";
                marginLeft="0px";               
                if (ApplyToMenu.style.lineHeight == "14px"){
                    marginTop="1px";
                }
                }
            if (navigateur == "Opera") {
                ApplyToMenu.style.backgroundPosition="50% -0.03em";
            }
        }
    }
}

/*************************************************************
*METHODS ENABLE TO APPLY RULES OF PRESENTATION TO THE HEADER** 
****************AND THE MENU ABOVE****************************
***************ROLLOVER ON MYDISNEYLAND**********************
************Code by EOS -- JUNE 2010 --********************
**************************************************************/
function getMenuRoundFrameWidth() {
    var Middle = document.getElementById("menuRoundFrameMiddle").offsetWidth;
    var Left = document.getElementById("menuRoundFrameLeft").offsetWidth;
    var Right = document.getElementById("menuRoundFrameRight").offsetWidth;
    var width = Middle+Left+Right;
    return width;
}

function diplayOffers() {
    var menuRoundFrame = document.getElementById('menuRoundFrame');
    P_addListener(menuRoundFrame, 'mouseover', offersBlock);
}

function hideOffers (){
    var menuRoundFrame = document.getElementById('menuRoundFrame');
    P_addListener(menuRoundFrame, 'mouseout', offersNone);
}

function offersBlock () {
    var imageSize = 400;
    var rolloverOffers = document.getElementById('rolloverOffers');
    var displayLine = document.getElementById('displayLine');
    if (getMenuRoundFrameWidth()<imageSize) {
        rolloverOffers.style.width = imageSize+"px";
        rolloverOffers.style.marginLeft = (getMenuRoundFrameWidth()-imageSize)+"px";
        if (msieVersion() == 6) {
            rolloverOffers.style.marginLeft = "-"+imageSize+"px";
        }
    } else {
        rolloverOffers.style.width = getMenuRoundFrameWidth()+"px";
        displayLine.style.marginLeft = (getMenuRoundFrameWidth()-imageSize)+"px";
        if (msieVersion() == 6) {
            displayLine.style.marginLeft = ((getMenuRoundFrameWidth()-imageSize)/2)+"px";
            rolloverOffers.style.marginLeft = "-"+getMenuRoundFrameWidth()+"px";
        }
    }
    rolloverOffers.style.display='block';
}

function offersNone (){
    var rolloverOffers = document.getElementById('rolloverOffers');
    rolloverOffers.style.display='none';
}

/****************END ROLLOVER****************************/
// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
    var start = document.cookie.indexOf( name + "=" );
    if (start!=1){
        start = document.cookie.indexOf(" "+ name + "=" );
    }

    var len = start + name.length + 1;
    if ( ( !start ) &&
        (name != document.cookie.substring( 0, name.length ) ) )
        {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

// put the menu selected to the last tab...
var menuHeaderSelected = '8'; 


// Put function to zero
function Aleatoire(integer) {
}
function displayOffers() {
}
function hideOffers () {
    
}
function displayFreeFooterText () {
}
function P_addListener() {
}
function displayPopUpUnder () {
}
var  googleSearchLanguage = "";
function initGsc() {
}

function retrieveIframe() {
}


function deleteEndSep () {
    if (document.getElementById('menuFooter')) {
        var  menuFooter = document.getElementById('menuFooter');
        var spanMenuFooter = menuFooter.getElementsByTagName('span');
        var nbrSspanMenuFooter = spanMenuFooter.length;
        spanMenuFooter[nbrSspanMenuFooter-1].style.backgroundImage = "none";
    }
    if ( document.getElementById('choices')) {
        var  choices = document.getElementById('choices');
        var spanChoices = choices.getElementsByTagName('span');
        var nbrSpanChoices = spanChoices.length;
        spanChoices[nbrSpanChoices-1].style.backgroundImage = "none";
    }
}

var listOn = false;



// Management of the footer listbox

var inited = false;
function myload() {
    if (inited) return;
    inited = true;
    document.body.onclick = function() {
        kill();
    };//alert('body.onclick')
    add_handler(document.body, 'body', 'click', false);
}

function add_handler(obj, objname, evname, useCapture, as_string) {
    var desc;
    var func;
    if (obj.addEventListener) {
        if (!func) func = evname == 'load' ? function() {
            myload();
        } : function() {};
        obj.addEventListener(evname, func, useCapture);
    }
    else if (obj.attachEvent) {
        if (useCapture && !as_string) return;
        if (!func) func = evname == 'load' ? function() {
            myload();
        } : function() {};
        obj.attachEvent('on' + evname, func);
    }
}

add_handler(window, 'window', 'load', false);
add_handler(window, 'window', 'load', true);


function openSelect(id){
    if(document.getElementById(id).style.display == 'none'){
        changestyle(id,'block');
        a=window.pageYOffset;
        /*pagel= window.document.body.offsetHeight-200;
		document.getElementById(id).style.top = pagel+"px"; */
        //		window.scroll(0,a);
        listOn = true;
    //setTimeout("openSelect('selectOpt');",time)
    }else{
        changestyle(id,'none');
        a=window.pageYOffset;
    //		window.scroll(0,a);
    }
}

function findObj(n, d) { //v4.0
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n);
    return x;
}

function changestyle(couche, style) {
    if (!(layer = findObj(couche))) return;
    layer.style.display = style;
}

function changecolor(couche, name) {
    if (!(layer = findObj(couche))) return;
    layer.className = name;

}

function openSelect1(id,left,top){
    openSelect(id);
    top -= document.getElementById(id).offsetHeight+8;
    document.getElementById(id).style.left=left+"px";
    document.getElementById(id).style.top=top+"px";
}

function ExOpen(idLink){
    openSelect1('selectOpt',(findPosX(document.getElementById(idLink))- 75),(findPosY(document.getElementById(idLink))));
}

function kill(){
    if(document.getElementById('selectOpt')){
        if(document.getElementById('selectOpt').style.display == 'block'){
            if (listOn == false){
                document.getElementById('selectOpt').style.display = 'none';
            }else{
                listOn = false;
            }
        }
    }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}


function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}


function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
        || this.searchVersion(navigator.appVersion)
        || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++)	{
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
    {
        string: navigator.userAgent,
        subString: "OmniWeb",
        versionSearch: "OmniWeb/",
        identity: "OmniWeb"
    },
    {
        string: navigator.vendor,
        subString: "Apple",
        identity: "Safari"
    },
    {
        prop: window.opera,
        identity: "Opera"
    },
    {
        string: navigator.vendor,
        subString: "iCab",
        identity: "iCab"
    },
    {
        string: navigator.vendor,
        subString: "KDE",
        identity: "Konqueror"
    },
    {
        string: navigator.userAgent,
        subString: "Firefox",
        identity: "Firefox"
    },
    {
        string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
    },
    {		// for newer Netscapes (6+)
        string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE",
        identity: "Explorer",
        versionSearch: "MSIE"
    },
    {
        string: navigator.userAgent,
        subString: "Gecko",
        identity: "Mozilla",
        versionSearch: "rv"
    },
    { 		// for older Netscapes (4-)
        string: navigator.userAgent,
        subString: "Mozilla",
        identity: "Netscape",
        versionSearch: "Mozilla"
    }
    ],
    dataOS : [
    {
        string: navigator.platform,
        subString: "Win",
        identity: "Windows"
    },
    {
        string: navigator.platform,
        subString: "Mac",
        identity: "Mac"
    },
    {
        string: navigator.platform,
        subString: "Linux",
        identity: "Linux"
    }
    ]

};
BrowserDetect.init();

