// check query string is_seo parameter
//console.log(window.location.search.toString().replace(/\?is_seo=(.+)&?/,'$1'));
var is_seo = window.location.search.toString().replace(/\?is_seo=(.+)&?/,'$1');
if (is_seo == '1') {
    createCookie('is_seo',1,1); // cookie expires in 1 day
} else if (is_seo == '0') {
    eraseCookie('is_seo');
} else {
    is_seo = readCookie('is_seo');
    if (is_seo == '' || is_seo == null || is_seo == undefined) {
        is_seo = 0;
    }
}

// if no hash found then try to find it from the path
if (!location.hash && !is_seo) {
    hash = '';

    // check language (first lang param, second cookie)
    lang = SWFAddress.getBaseURL().replace('http:\/\/'+location.host,'').replace(/((\w){2}(_(\w){2})?)\//,'$1').toLowerCase();
    if (lang == null) {
       lang = readCookie('renault_tv_lang'); 
    }
    //console.log(lang);

    path = SWFAddress.getBaseURL().replace('http:\/\/'+location.host,'').replace(/(\w){2}(_(\w){2})?\//,'').toLowerCase();
    //console.log(path);
    
    segments = path.substr(1).split('/');
    //console.log(segments);
    
    if (segments.length > 0 && path != '') {
        
        // TODO: make the channel selections dynamic
                                            
        // english channels
        if (segments[0] == 'channels' || segments[0] == 'canaux') {
            if (typeof segments[1] == 'string') {
                hash = segments[1];
                //console.log(hash);
                var start = 2;
            } else {
                hash = segments[2];
                var start = 3;
            }
            //console.log(hash);
            // check for more segments
            if (segments.length > start+1) {
                for (i=start; i<segments.length; i++) {
                    hash += '/'+ segments[i];
                }
            }
        } else if (segments[0] == 'car') {
            hash = 'cars';
        } else if (segments[0] == 'cars') {
            hash = 'cars';
            if (segments.length > 1) {
                hash += '/' + (lang.search('en') != -1 ? 'cars' : 'voitures');
                for (i=1; i<segments.length; i++) {
                    hash += '/'+ segments[i];
                }
            }
            //console.log(hash);
        }

        // write path directly as hash
        else if (segments.length > 2) {
            hash = path.substr(1);  
        }
                      
    }
    //console.log(hash);
    //alert(hash);
    
    if (hash != '') {
        SWFAddress.setValue('/'+hash);     
    } 
    
    // spotlight urls
    else if (segments[0] == 'megane') {
        SWFAddress.setValue('/motorsport/videos/138082/wsr-overview-2008');
    } else if (segments[0] == 'clio') {
        SWFAddress.setValue('/motorsport/videos/137759/wsr-a-guide-to-the-series');
    }
}
