/****************************************************************
 * Permet de retourner l'url root du site
 * @param : urlHref doit etre du type http://www.aptonia.com ou http://aptonia.preview.dktetrix.net ou http://www.decathlon.fr ou http://fr.preview.dktetrix.net
 * @return : www.aptonia.com ou aptonia.preview.dktetrix.net ou decathlon.fr ou fr.preview.dktetrix.net
*/
function getRootUrl(urlHref){
    if(urlHref){
        var rootUrl = "" ;
        var urlSplit = urlHref.split("/") ;
        if(urlSplit.length > 1 && urlSplit[2]){
            rootUrl = urlSplit[2] ;
        }
        return rootUrl ;
    }
    else{
        return "" ;
    }
}

/*****************************************************************/
function setCentralCountry(pays, lang, urlRedirect){
    if(urlRedirect != "undefined"){
        //on en cree un nouveau avec une date d'expiration das 3 mois
        setCookie(lang, "country", pays, getExpirationDateCookie()) ;
        setCookie("", "redirect", lang+"_"+pays+"_"+urlRedirect, getExpirationDateCookie()) ;

        window.location.href = urlRedirect ;
    }
}

/*****************************************************************/
function initCountry(){
    var isChangeCountry = false ;
    
    //si des pays sont rajoutes dans itool, ajouter une ligne en plus
    var params = getParameters() ;
    if(params != "" && params.length == 1){
        if(params[0][0] && params[0][0].indexOf("changepays") > -1 && params[0][1] == "1"){
            isChangeCountry = true ;
        }
    }
    
    if(isChangeCountry){
        setCookie("FR", "country", "FR", -1) ;
        setCookie("EN", "country", "EN", -1) ;
        setCookie("ES", "country", "ES", -1) ;
        setCookie("IT", "country", "IT", -1) ;
        setCookie("DE", "country", "IT", -1) ;        
    }
    else{
        redirectSiteCountry() ;
    }
}

/*****************************************************************/
function redirectSiteCountry(){
    var countryCookie = getCookie("redirect") ;

    if(countryCookie && countryCookie != ""){
        //recuperation de la langue et du pays
        var langue = countryCookie.split("_")[0] ;
        var country = countryCookie.split("_")[1] ;
        var urlRedirect = countryCookie.split("_")[2] ;
        
        setCentralCountry(country, langue, urlRedirect) ;
    }
}
