/*
* Auteur : Xavier Vincent
* Specifications : Chargement dynamique de la barre de navigation et ajustement pour le pied de page
* Date : 04 aout 2011
* Copyright : Societe Radio-Canada (c) aout 2011
*/

var NotHaveGlobalScripts = [];

function loadingScript4BrowsingBar(url, pScriptsToExecute) {
    var scriptToLoad = document.createElement('script');
    scriptToLoad.setAttribute("type", "text/javascript");
    scriptToLoad.setAttribute("language", "javascript");
    scriptToLoad.setAttribute("src", url);

    if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
        scriptToLoad.onreadystatechange = function () {
            if (this.readyState == 'loaded' || this.readyState == 'complete') {
                eval('pScriptsToExecute();');
            }
        };
    }
    else {
        scriptToLoad.onload = pScriptsToExecute;
    }
    document.getElementsByTagName('head')[0].appendChild(scriptToLoad);
}

function in_array(needle, haystack) {
    for (var key in haystack) {
        if (needle === haystack[key]) {
            return true;
        }
    }

    return false;
}


function generateBrowsingBar() {
    return '';
}

function generateFooterBar() {
    return '';
}

function postLoading() {
}

function getPrefix(pIsInternal) {
    var host = document.location.hostname;
    var prefix = '';

    if (host.indexOf('lcl2-') != -1)
        prefix = "lcl2-";
    else if (host.indexOf('lcl-') != -1)
        prefix = "lcl-";
    else if (host.indexOf('doc-') != -1)
        prefix = "doc-";
    else if (host.indexOf('br2-') != -1)
        prefix = "br2-";
    else if (host.indexOf('br-') != -1)
        prefix = "br-";
    else if (host.indexOf('tr-') != -1)
        prefix = "tr-";
    else if (host.indexOf('pp-') != -1)
        prefix = "pp-";
    else if (host.indexOf('qa-') != -1)
        prefix = "qa-";
    else if (host.indexOf('.dev') != -1)
        prefix = "tr-";
    else if (pIsInternal)
        prefix = 'ca-';

    return prefix;
}

function loadAfterjQuery(pHost) {
    loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/_js/jquery.cookie.custom.js', function () {
        loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/_js/modules/modules.base.1.0.0.js', function () {
            if (location.href.indexOf("/jeunesse/") > -1) {
                loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/nav/navbar_jeunesse.js', function () {
                    RadioCanada.Lib.jQuery(document).ready(function () {
                        RadioCanada.Lib.jQuery('#BarreDeNavigation').append(generateBrowsingBar());
                        if (RadioCanada.Lib.jQuery('#PiedDePage').length > 0) {
                            RadioCanada.Lib.jQuery('#PiedDePage').append(generateFooterBar());
                        }
                        postLoading();
                    });
                    if (document.location.hostname.indexOf("blogues.radio-canada.ca") > -1) {
                        loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/_js/modules/modules.pub.1.0.4.js', function () { });
                    }
                });
            }
            else if (location.href.indexOf(".radio-canada.ca") == -1) {
                /// [BRovito: 2011-01-13] Hack Live right now : navbar_extern.js
                /// navbar_extern est écrasé au minute par HTMLStatique par le fichier 
                /// http://modules.radio-canada.ca/Common/Navigation/v6/generateJS.aspx?type=extern [\\fs.radio-canada.ca\public\ca\radio-canada\modules\wwwroot\Common\Navigation\v6
                /// On a pas eu le temps de faire la modif comme du monde, donc on a créé un ficher static (temporaire)
                /// nommé navbar_extern.tmp4liverightnow.js
                /// Il faudrait mettre le bug fix de navbar_extern.tmp4liverightnow.js dans le code de generateJS.aspx et de le rendre plus générique que juste pour liverightnow
                loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/nav/navbar_extern.tmp4liverightnow.js', function () {
                    RadioCanada.Lib.jQuery(document).ready(function () {
                        RadioCanada.Lib.jQuery('#BarreDeNavigation').append(generateBrowsingBar());
                        if (RadioCanada.Lib.jQuery('#PiedDePage').length > 0) {
                            RadioCanada.Lib.jQuery('#PiedDePage').append(generateFooterBar());
                        }
                        postLoading();
                    });
                    if (document.location.hostname.indexOf("blogues.radio-canada.ca") > -1) {
                        loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/_js/modules/modules.pub.1.0.4.js', function () { });
                    }
                });
            }
            else {
                loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/nav/navbar_legacy.js', function () {
                    RadioCanada.Lib.jQuery(document).ready(function () {
                        RadioCanada.Lib.jQuery('#BarreDeNavigation').append(generateBrowsingBar());
                        if (RadioCanada.Lib.jQuery('#PiedDePage').length > 0) {
                            RadioCanada.Lib.jQuery('#PiedDePage').append(generateFooterBar());
                        }
                        postLoading();
                    });
                    if (document.location.hostname.indexOf("blogues.radio-canada.ca") > -1) {
                        loadingScript4BrowsingBar('http://' + pHost + 'www.r-c.ca/_js/modules/modules.pub.1.0.4.js', function () { });
                    }
                });
            }
        });
    });
}

navBar_myHost = getPrefix(false);

if (typeof (oSrc) === 'undefined' && !in_array(document.location.hostname, NotHaveGlobalScripts)) {
    document.write('<sc' + 'ript language="Javascript" type="text/javascript" src="http://' + 'www.radio-canada.ca/lib/v3/js/global.js"></sc' + 'ript>');
}
if (typeof (geoip_country_code) === 'undefined') {
    document.write('<sc' + 'ript language="JavaScript" type="text/javascript" src="http://' + 'geoip.cloudapp.net/geoip.js"></sc' + 'ript>');
}
document.write('<link href="http://' + navBar_myHost + 'www.r-c.ca/_css/modules/modules.navigation.2.0.0.css" rel="stylesheet" media="screen" type="text/css" />');
document.write('<div id="BarreDeNavigation"></div>');

loadingScript4BrowsingBar('http://' + navBar_myHost + 'www.r-c.ca/_js/jquery-1.6.2.custom.min.js', function () {
    $j(document).ready(function () {
        loadAfterjQuery(navBar_myHost);
    });
});

