function getCookie(Name){ //get cookie value
    var re=new RegExp(Name+"=[^;]+", "i");
    if (document.cookie.match(re))
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

function setCookie(name, value, days){ //set cookie value
    var expireDate = new Date()
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}

function updateContent(html) {
    var content = document.getElementById("contents");
    document.getElementById('contents').innerHTML = html;
    appearContent(content);
    while (html.match(/(<script[^>]+javascript[^>]+>\s*(\/\/*<!\[CDATA\[)?(<!--)?\s*)/i)) {
        html = html.substr(html.indexOf(RegExp.$1) + RegExp.$1.length);

        if (!html.match(/((\/\/ *\]\]>)?(-->)?\s*<\/script>)/)) break;

        block = html.substr(0, html.indexOf(RegExp.$1));
        html = html.substring(block.length + RegExp.$1.length);
        (new Function(block))();
    }
}

function htmlContentAnimateOut(){
    var content = document.getElementById("contents");
    disappearContent(content);
}

function disappearContent(content,value){
    if (typeof value == 'undefined') value = 100;
    var property = content.filters ? 'filter' : 'opacity';
    if (value == 0) {
        thisMovie("civ-v").htmlContentAnimateOutComplete();
        if (content.style.removeAttribute) {
            content.style.removeAttribute(property);
        } else {
            content.style[property] = 0;
        }
		document.getElementById('contents').innerHTML = ""; //woah there cowboy
    } else {
        content.style[property] = content.filters ? 'alpha(opacity=' + value + ')' : value/100;
        setTimeout(function () {
            disappearContent(content, value - 10)
            }, 50);
    }
}

function appearContent(content, value) {
    if (typeof value == 'undefined') value = 0;
    var property = content.filters ? 'filter' : 'opacity';
    if (value == 100) {
        thisMovie("civ-v").htmlContentAnimateInComplete();
        if (content.style.removeAttribute) {
            content.style.removeAttribute(property);
        } else {
            content.style[property] = 1;
        }
    } else {
        content.style[property] = content.filters ? 'alpha(opacity=' + value + ')' : value/100;
        setTimeout(function () {
            appearContent(content, value + 10)
            }, 50);
    }
}


function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            updateContent(xhr.responseText);

        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function sectionObject(param){
    this.sub_section = '';

    if(param == '/' || param == 'home'){
        this.main_section = 'home';
    }
    else{
        var section_split = param.split('/');
        this.main_section = section_split[0];

        if(section_split.length > 1){
            if(section_split[1] !='/' && section_split[1] !=''){
                for(var i = 1; i < section_split.length; i++){
                    if(i == section_split.length - 1){
                        this.sub_section = this.sub_section + section_split[i];
                    }else{
                        this.sub_section = this.sub_section + section_split[i] + '_';
                    }
                }
            }
        }
    }
}

//Handle Change for Country
function handleChange(section,  lang) {
    var passed_lang = lang;
    switch(passed_lang){
        case "us":
        case "de":
        case "uk":
        case "fr":
        case "it":
        case "au":
        case "es":
        break;
        default:
            passed_lang = "us";
    }

    var xhr = getTransport();
    var sub_url = new sectionObject(section);
    var subsection_index;

    updateContent('');

    xhr.onreadystatechange = function() {
        updateChange(xhr);
    }

//Add Subpage
    switch(sub_url.main_section){

        //Top Add Page
       //case 'pagename';
            //addSubpage(true);
           //If page has tabs add:
           //subsection_index = checkInformationSubsections(sub_url.sub_section);
           //if page does not have tabs add:
           //subsection_index = -1;
           //break;

        case 'home':
            addSubpage(false)
            subsection_index = -1;
            break
        case 'media':
            addMediaPage(true);
            subsection_index = -1;
            break;
        case 'information':
            addSubpage(true);
            subsection_index = checkInformationSubsections(sub_url.sub_section);
            break;
        case 'civilizations':
            addSubpage(true);
            subsection_index = checkCivilizationsSubsections(sub_url.sub_section);
            break;
        case 'community':
            addSubpage(true);
            subsection_index = checkCommunitySubsections(sub_url.sub_section);
            break;
        default:
            addSubpage(true);
            subsection_index = -1;
            break;
    }

    setCookie('currentSection', sub_url.main_section, 1);
    setCookie('currentSubsection', subsection_index, 1);
	
    //  if( (sub_url.main_section!= 'news')){
    if(subsection_index == -1 && sub_url.sub_section != ''){
        xhr.open('get', 'xml/' + passed_lang + '/' + sub_url.main_section + '/' + sub_url.sub_section + '/index.php', true);
    }else{
        xhr.open('get', 'xml/' + passed_lang + '/' + sub_url.main_section + '/index.php', true);
    }
    // }
    //else{
    //read news/index.xml
    //output nodes based on sub_url.sub_section
    // }
    xhr.send('');
}

function checkInformationSubsections(subsection){
    switch(subsection){
        case 'overview':
            return 0;
        case 'features':
            return 1;
        default:
            return 0;
    }
}

//Handles tabs for Civilizations
function checkCivilizationsSubsections(subsection){
    switch(subsection){
        case 'america':
            return 0;
        case 'arabia':
            return 1;
        case 'aztecs':
            return 2;
        case 'china':
            return 3;
        case 'egypt':
            return 4;
        case 'england':
            return 5;
        case 'france':
            return 6;
        case 'germany':
            return 7;
        case 'greece':
            return 8;
        case 'india':
            return 9;
        case 'iroquois':
            return 10;
        case 'japan':
            return 11;
        case 'ottoman':
            return 12;
        case 'persia':
            return 13;
		case 'rome':
            return 14;
		case 'russia':
            return 15;
		case 'thailand':
            return 16;
		case 'songhai':
            return 17;
        default:
            return 0;
    }
}

//Handles Community Tabs
function checkCommunitySubsections(subsection){
    switch(subsection){
        case 'overview':
            return 0;
        case 'features':
            return 1;
        case 'podcasts':
            return 2;
        case 'fansites':
            return 3;
        case 'goodies':
            return 4;
		case 'bios':
    		return -1;
		case 'podcast_transcript_1':
    		return -1;
        case 'podcast_transcript_2':
            return -1;
        case 'podcast_transcript_3':
            return -1;
        case 'podcast_transcript_4':
            return -1;
        case 'podcast_transcript_5':
            return -1;
        case 'podcast_transcript_6':
            return -1;
        case 'podcast_transcript_7':
            return -1;
        case 'feature_people_and_places_of_civilization_v':
            return -1;
        case 'feature_strategic_view':
            return -1;
        case 'feature_all_about_cities':
            return -1;
        case 'feature_technology_quotes':
            return -1;
        case 'feature_box_art_reveal':
            return -1;
        case 'feature_culture':
            return -1;
        case 'feature_gold':
            return -1;
        case 'feature_happiness':
            return -1;
        case 'feature_strategic_resources':
            return -1;
        case 'feature_worker_improvements':
            return -1;
        case 'feature_gdr':
            return -1;
        case 'feature_socialresponsibility':
            return -1;
        case 'specialedition':
            return -1;
        case 'civilization_v_day':
            return -1;
        case 'system_requirements':
            return -1;
        case 'demo':
            return -1;
        case 'demo_release_date':
            return -1;
        case 'live_stream':
            return -1;
		default:
            return 0;
    }
}
function addSubpage(val){
    if(val == true){
        $('body').addClass('subpage');
    }
    else{
        $('body').removeClass('media');
        $('body').removeClass('subpage');
    }
}

function addMediaPage(val){
    if(val == true){
        $('body').addClass('media');
    }
    else{
        $('body').removeClass('subpage')
        $('body').removeClass('media');
    }
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

function notifyFlashURL(path){
    thisMovie("civ-v").updateShellURL(path);
}


jQuery(function($) {

    var selected_index = 1;

});