var videoPlayer = "/swfs/video_player.swf";
function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name){
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
function handleAgeGate(){
    $('#checkAge').submit(function(){
        if(min_age==0){
            min_age=16;
        }
        var dates = {};
        dates.year = parseInt($("#checkAge select[name=year]").val())+min_age;
        dates.day = $("#checkAge select[name=day]").val();
        dates.month = $("#checkAge select[name=month]").val();
        dates.getFullDate = function(){
            return this.month+'/'+this.day+'/'+this.year;
        }
        var birth = new Date(dates.getFullDate());

        var today = new Date();
        var numCheck = true;

        /*for(k in userInput){
            if(userInput[k]==''){
                numCheck = false;
                break;
            }
        }
        console.log(userInput);*/
        //if(numCheck==true){
            if((birth=='NaN') || (birth=='Invalid Date') || ((today.getTime()-birth.getTime())<0)){
            
                $('#checkAge').fadeOut('fast', function() {

                        $('#failedAgeGate').fadeIn('fast');
                    });
                
            } else {
                setCookie('agate', 'pass_'+locale, 30);

                window.location = '/'+locale+'/home/';
            }
        //}
        return false;
    });
}
function handleAgeGateLinks(){
    $('#langSelList').find('a').each(function(){
        $(this).click(function(){
            var link = $(this).attr('href');
            if(link=='http://www.uncharted3-playstation.fr'){
                window.location=link;
                return true;
            }
            var chunks = link.split('/');
            link = link.replace('lang/', '')+'mini';
            locale = chunks[1];
            /*if(getCookie('agate') == ('pass_'+locale)){
                window.location = '/'+chunks[1]+'/home/';
                
            } else {*/
                $('#agateTarget').load(link, function(){
                
                    $('#langSelList').fadeOut('fast', function() {
                            handleAgeGate();

                        $('#agateTarget').fadeIn('fast');
                    });
                });
            /*}*/

        return false;

        });
    });
}
function checkFlash(val){
    console.log(val);
}

//console.log(gD);
var vidWrapper = {1: {prefix: 'Uncharted_3_'}, 2: {prefix: 'Katherine_Marlowe_'}};
function loadVideo(){
    //var flashvars = {videoPath: cdn+'flv/'+gD[locale].home+'_U3_Announce_Trailer_720p_480x272_WEB.flv'};

    var flashvars = {videoPath: cdn+'flv/Uncharted3_E32011Trailer_720_'+gD[locale].home+'_1.f4v'};
    //console.log(flashvars);
    var params = {allowscriptaccess: 'always'};
    var attributes = {};
    swfobject.embedSWF(videoPlayer, "homeVideo", "480", "270", "9.0.0",null, flashvars, params, attributes);
}
var gallData = {};

function galleryPrep(){

    var screens = $('#screens').children('li').length;
    gallData.screens = {};
    for(var i=1;i<=screens;i++){
        gallData.screens[i] = {big: cdn+'images/screenshot_'+i+'large.jpg', dwn: 'screenshot_'+i+'large'};
    }
}
var ele = [];
var eleList = ['hd', 'num', 'leftContent', 'dwnSingle'];
//var ele = {hd: {}, num: {}, leftContent: {}, dwnSingle: {}};

if(mode=='wallpaper'){
                  gallData.wallpaper = wallList;
                  //console.log(gallData);
    eleList = ['dwn1024','dwn1280', 'dwn1920', 'dwnPSP'];
    //eleList = wallpaperTypes.concat(eleList);
}
if(mode==''){
    mode = 'screens';
}
function onAfter() { 
    $('#num').html($(this).data('num'));
    if(page == 'gallery'){

        

        if(mode=='wallpaper'){
            for(k in eleList){
            //console.log(k);
                ele[k].attr('href', '/download.php?type='+mode+'&file='+gallData[mode][$(this).data('num')][k]);
            }    
        } else {
            ele.hd.attr('href', gallData[mode][$(this).data('num')].big);
            ele.dwnSingle.attr('href', '/download.php?type='+mode+'&file='+gallData[mode][$(this).data('num')].dwn);
        }
    }


}

function sliders(){
    var slideNum = 1;

    $('#screens').children('li').each(function(){
        $(this).data('num', slideNum);
        slideNum++;
    });
    $('#total').html(slideNum-1); 

            $('#screens').cycle({
        fx:     'scrollHorz', 
        easing: 'easeInCubic',
        timeout: 0,
        speed:   300, 
        startingSlide: 0,
        next:   '#next', 
        prev:   '#prev',
        after:   onAfter,
        nowrap: 1

    });
    if(page != 'gallery'){
        $('#storyText').cycle({
            fx:     'fade', 
            easing: 'easeInCubic',
            timeout: 0,
            speed:   300, 
            startingSlide: 0,
            next:   '#next', 
            prev:   '#prev',
            nowrap: 1,
            cleartypeNoBg: 1,
            cleartype: 1

        });
    } else {
        
        $("#hd").click(function(){
            $(".paneMask > ul").children('li').each(function(){

                if($(this).css('display') != 'none'){
                    $(this).children('a').trigger('click');
                    //break the loop;
                    return false;
                }
            });
            return false;
        });
        
    }   
}
var curNum = 1;
function updateContent(){
            var activeVid = gD[locale].vids[curNum];

    var flashvars = {videoPath: cdn+'flv/'+vidWrapper[curNum].prefix+gD[locale].vids[curNum]+'_480x272_WEB.flv'};
        //console.log(flashvars);

    var params = {};
    var attributes = {};
    swfobject.removeSWF("homeVideo");
    ele.leftContent.html('<div id="homeVideo"></div>');
    swfobject.embedSWF(videoPlayer, "homeVideo", "480", "270", "9.0.0",null, flashvars, params, attributes);
    ele.num.html(curNum);
            ele.hd.attr('href', cdn+'mov/'+vidWrapper[curNum].prefix+activeVid+'_1.mp4');
            ele.dwnSingle.attr('href', cdn+'zip/'+vidWrapper[curNum].prefix+activeVid+'_1.zip');

}
var gD = {'en_GB': {home:'UK_ENGLISH',vids: {1: 'English'}}};
gD.en_AU = {home:'AUSTRALIA',vids: {1: 'Australia'}};
gD.nl_NL = {home:'DUTCH',vids: {1: gD.en_GB.vids[1], 2: 'Netherlands'}};
gD.fr_FR = {home:'FRANCE',vids: {1: 'French', 2: 'France'}};
gD.de_DE = {home:'GERMAN',vids: {1: 'German'}};
gD.en_NZ = gD.en_AU;
gD.en_IE = gD.en_GB;
gD.nl_BE = gD.nl_NL;
gD.fr_BE = gD.fr_FR;
gD.da_DK = {home:'DANISH',vids: {1: 'Danish',2: 'Denmark'}};
gD.el_GR = {home:'GREEK',vids: {1: gD.en_GB.vids[1],2: 'Greek'}};
gD.es_ES = {home:'SPANISH',vids: {1: 'Spanish',2: 'Spain'}};
gD.it_IT = {home:'ITALIAN',vids: {1: 'Italian', 2: 'Italy'}};
gD.fr_LU = gD.fr_FR;
gD.de_LU = gD.de_DE;
gD.no_NO = {home:'NORWAY',vids: {1: gD.en_GB.vids[1], 2: 'Norway'}};
gD.de_AT = gD.de_DE;
gD.pl_PL = {home:'POLISH',vids: {1: 'Poland', 2: gD.en_GB.vids[1]}};
gD.pt_PT = {home:'PORTUGAL',vids: {1: 'Portugal', 2: gD.en_GB.vids[1]}};
gD.ru_RU = {home:'RUSSIAN',vids: {1: 'Russian'}};
gD.de_CH = gD.de_DE;
gD.fr_CH = gD.fr_FR;
gD.it_CH = gD.it_IT;
gD.fi_FI = {home:'FINNISH',vids: {1: gD.en_GB.vids[1],2: 'Finland'}};
gD.sv_SE = {home:'SWEDISH',vids: {1: 'Swedish', 2: 'Sweden'}};
gD.tr_TR = {home:'TURKISH',vids: {1: gD.en_GB.vids[1], 2: 'TURKEY'}};

function videoGall(){
//galleryData

//set vid2
// the names are different, some videos are in some languages for one video, and not in another
// so we have to allow for this
if(typeof(gD[locale].vids[2]) == 'undefined'){
    gD[locale].vids[2] = gD[locale].vids[1];

}
//console.log(gD);
    gD[locale].vids[2] = gD[locale].vids[2].toUpperCase();

    var slideNum = 2;
        updateContent();

    $('#total').html(slideNum);
    $('#next').click(function(){
        if(curNum < slideNum){
            curNum++;
                    updateContent();

        }
    });
    $('#prev').click(function(){
        if((curNum <= slideNum) && (curNum > 1)){
            curNum--;
                    updateContent();

        }
    });     
}

$(document).ready(function() {
    if(page=='lang'){
        handleAgeGate();
        handleAgeGateLinks();
    }
    if(page=='home'){
        loadVideo();

    }
        for (key in eleList){
        ele[eleList[key]] = $('#'+eleList[key]);
    }
    if((page == 'gallery') || (page=='story') || (page == 'multiplayer')){
        
        switch(mode){
            case 'videos':
            videoGall();

            break;
            case 'wallpaper':
            for(k in eleList){
                ele[eleList[k]].attr('href', '/download.php?type='+mode+'&file='+gallData.wallpaper[1][eleList[k]]);
            } 
            break;
            default:
                        galleryPrep();
            sliders();
            $("a[rel='lightbox']").colorbox({current: "{current} / {total}"});
            break;
        }
        
    }
    $('#packShot').click(function(){
        $('#shops').slideToggle(400, 'easeInQuad', function(){});
    
    });

});

