var partnerRedirectMatrix = {
    "known_locations": ["gb", "uk", "de", "fr", "it", "es", "mx", "br"],
    "outsourced_editions": ["uk", "de", "eu", "fr", "it", "es"],
    "partner_edition": {
        "uk,de,eu,it,fr,es": [
            {
                "partner": "aol",
                "country_matches": ["gb","uk"]
            },
            {
                "partner": "videovalis",
                "country_matches": ["de"]
            }
        ]
    },
    "redirect_urls": {
        "aol_homepage": "http://www.aol.co.uk/?src=real&ncid=real-player",
        "videovalis_homepage": "http://germany.real.com/partners/videovalis.html/",
        "zylom": "http://www.zylom.com/",
        "real_uk_guide": "http://uk.real.com/",
        "real_de_guide": "http://de.real.com/",
        "real_eu_guide": "http://eu.real.com/"
    },
    "editions": {
        "uk": {
            "realplayer": {
                "subscriber":[
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{real_uk_guide}{url_match}"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["de"],
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "non-subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{real_uk_guide}/signin/signup"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     },
                     {
                         "url_matches": ["/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{zylom}/uk"
                     }
                 ]
             },
             "browser": {
                 "non-realplayer": [
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_uk_guide}/realplayer"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["de"], 
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     },
                     {
                         "url_matches": ["/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{zylom}/uk"
                     }
                 ]
             }
         },
         "de": {
             "realplayer": {
                 "subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{real_de_guide}{url_match}"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["de", "fr", "it", "es", "mx", "br", "eu"], 
                         "redirect_to": "{videovalis_homepage}"
                     },
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["gb","uk"], 
                         "redirect_to": "{aol_homepage}"
                     }
                 ],
                 "non-subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{real_de_guide}/signin/signup"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     },
                     {
                         "url_matches": ["/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{zylom}/de"
                     }
                 ]
             },
             "browser": {
                 "non-realplayer": [
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_de_guide}/realplayer"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["de", "fr", "it", "es", "mx", "br", "eu"],
                         "redirect_to": "{videovalis_homepage}"
                     },
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["gb","uk"], 
                         "redirect_to": "{aol_homepage}"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     },
                     {
                         "url_matches": ["/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{zylom}/de"
                     }
                 ]
             }
         },
         "eu": {
             "realplayer": {
                 "subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["eu"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["gb","uk"], 
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["de"], 
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "non-subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}/signin/signup"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews","/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     }
                 ]
             },
             "browser": {
                 "non-realplayer": [
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}/realplayer"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["gb","uk"], 
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["de"], 
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews","/games"],
                         "country_matches": ["*"],
                         "redirect_to": "{real_eu_guide}{url_match}"
                     }
                 ]
             }
         },
         "all_other_editions": {
             "realplayer": {
                 "subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{real_de_guide}{url_match}"
                     },
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{real_uk_guide}{url_match}"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "non-subscriber": [
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{real_de_guide}/signin/signup"
                     },
                     {
                         "url_matches": ["/music","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{real_uk_guide}/signin/signup"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{url_match}"
                     }
                 ]
             },
             "browser": {
                 "non-realplayer": [
                     {
                         "url_matches": ["/","/video"],
                         "country_matches": ["*"],
                         "redirect_to": "/realplayer"
                     }
                 ],
                 "partner": [
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["gb","uk"],
                         "redirect_to": "{aol_homepage}"
                     },
                     {
                         "url_matches": ["/","/music","/video","/video/livenews"],
                         "country_matches": ["de"],
                         "redirect_to": "{videovalis_homepage}"
                     }
                 ],
                 "everyone": [
                     {
                         "url_matches": ["/","/video","/music","/video/livenews"],
                         "country_matches": ["*"],
                         "redirect_to": "{url_match}"
                     }
                 ]
             }
         }
     }
 };
 if(self.pageEnv.env == 'development' || self.pageEnv.env == 'staging') {
    var host = window.location.host.split('.');
    var getEnvHost = function(livehost) {
        var edition = livehost.match(/http:\/\/([^\.]*)/)[1];
        var envhost = window.location.host.split('.');
        envhost[0] = edition;
        return 'http://' + envhost.join('.') + '/';
    }
    partnerRedirectMatrix.redirect_urls.videovalis_homepage = "http://" + window.location.host +  "/partners/videovalis.html";
    partnerRedirectMatrix.redirect_urls.real_uk_guide = getEnvHost(partnerRedirectMatrix.redirect_urls.real_uk_guide);
    partnerRedirectMatrix.redirect_urls.real_de_guide = getEnvHost(partnerRedirectMatrix.redirect_urls.real_de_guide);
    partnerRedirectMatrix.redirect_urls.real_eu_guide = getEnvHost(partnerRedirectMatrix.redirect_urls.real_eu_guide);
};

var getGuideSessionCookieParams = function() {
    var cookieParams;
    // if cookie already set, get params from cookie
    var rnSession = document.cookie.match(/RN[A-Z]{2}sess=([^;]*)/);
    if(rnSession) {
        cookieParams = {};
        var values = decodeURIComponent(rnSession[1]).split(';');
        if(values.length > 1) {
            for(var i in values) {
                var value = values[i].split('=');
                cookieParams[value[0]] = value[1];
            }
        }
    // if cookie is not set, set default cookie params
    } else {
        cookieParams = {
            partnerpage: 0,
            playerpage: 0,
            redirected: 0,
            popup: 0,
            edition: self.pageEnv.edition
        }
    }
    return cookieParams;    
};

var updateGuideSessionCookie = function(cookieParams) {
    // remove cookies if edition is changed
    if(cookieParams.edition != self.pageEnv.edition) {
        guideSessionCookies = document.cookie.match(/RN[A-Z]{2}sess=/g);
        for(var i in guideSessionCookies) {
            document.cookie = guideSessionCookies[i] + ";expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;domain=real.com";
        }
        cookieParams.redirected = 0;
        cookieParams.edition = self.pageEnv.edition;
    }
    var params = [];
    if(cookieParams) {
        for(var key in cookieParams) {
            params.push(key + '=' + cookieParams[key]);
        }
    }
    params = params.join(';');
    // update or set new cookie values
    document.cookie = "RN" + cookieParams.edition.toUpperCase() + "sess=" + encodeURIComponent(params) + ";path=/;domain=real.com";
    return cookieParams;
};

var openRedirectPage = function(cookieParams) {
    var url = '/signin/redirect/?path=' + cookieParams.path;
    var userLocation = (partnerRedirectMatrix.outsourced_editions.join('#').match(cookieParams.edition)) ? true : false;
    switch(true) {
        case userLocation:
            window.location = url;
        break;
    }
};

var cookieParams = getGuideSessionCookieParams();
// reset cookie param popup to 0, because there is only a popup if user came from partner page - checked in the next document.referrer if-clause
cookieParams.popup = 0;
if(document.referrer) {
// if user comes from partner page, set partnerpage flag on 1, redirected flag must be set to 0 to init redirect.tt
    for(var i in partnerRedirectMatrix.redirect_urls) {
        if(i.match(/_homepage$/) && (document.referrer.indexOf(partnerRedirectMatrix.redirect_urls[i]) > -1)) {
            cookieParams.partnerpage = 1;
            cookieParams.popup = 1;
            cookieParams.redirected = 0;
        }
    }
    var referrerPathname = document.referrer.replace(window.location.protocol + '//' + window.location.host, '');
    switch(referrerPathname) {
// if user comes from player page, set playerpage flag on 1, redirected flag must be set to 0 to init redirect.tt
        case '/realplayer':
            cookieParams.playerpage = 1;
            cookieParams.redirected = 0;
        break;
    }
}

// set cookie param path and section if page pathname is not redirect.tt, signup.tt page
if(!window.location.pathname.match(/\/signin\/redirect/)) {
        cookieParams.path       = encodeURIComponent(window.location.pathname.replace(/\/$/, '') + window.location.hash);
        cookieParams.section    = encodeURIComponent(decodeURIComponent(cookieParams.path).split('#')[0]);
}
// do not redirect, if a station or video is triggered
if(window.location.hash.match('#act.')) {
    cookieParams.redirected = 1;
}
// if user clicked on signin link in browser the player opens the url with hash init=player
if(window.location.hash == '#init=player') {
    window.open(window.location.protocol + '//' + window.location.host, '_rphome');
    window.open(window.location.protocol + '//' + window.location.host + '/signin/signup', 'signup', 'width=800,height=400,menubar=no,scrollbars=yes,status=no,resizable=yes');
    cookieParams.path = encodeURIComponent(decodeURIComponent(cookieParams.path).replace('#init=player', ''));
}

cookieParams = updateGuideSessionCookie(cookieParams);
// init redirect
if(cookieParams.redirected == 0) {
    cookieParams.redirected = 1;
    cookieParams = updateGuideSessionCookie(cookieParams);
    openRedirectPage(cookieParams);
}

