var Void=function(){};if(!window.console){window.console={}};$(["log","warn","group","groupEnd","groupCollapsed","dir","error","info"]).each(function(){if(!window.console[this]){window.console[this]=Void;};});
var vidName;
var Site = {
    Init: function() {
        //pages
        var landingPage = $('.landingPage');
        var productsPage = $('.productsPage');
        var webisodesPage = $('.webisodesPage');
        var promotionsPage = $('.promotionsPage');
        var contactUsPage = $('.contactus');
        //navItems
        var products = $('.products a');
        var webisodes = $('.webisodes a');
        var promotions = $('.promotions a');

        if (landingPage.length > 0) {

            vidName = 'webisode1_snippet';

            // var swfL = 'assets/swfs/Cran10.03.swf';
            // var swfR = 'assets/swfs/Rachel07-3.swf';

            var swfL = 'assets/swfs/cherry.v03.cherry.swf';
            var swfR = 'assets/swfs/cherry.v03.rachel.swf';

            FlashCharacterController.loadSwfs(swfL, swfR, $.cookie('landedCookie'));
            EmbedVideo.Init();
            ControlVideo.Init('landing');



            //cookie
            $.cookie('landedCookie', 'landed');


            $('.cranMod .modBtn a').click(function() { _hbPageView('[cherry]', 'landing'); });
            $('.facebookMod .modBtn a').click(function() { _hbPageView('[facebook]', 'landing') });
            $('.promoMod .modBtn a').click(function() { _hbPageView('[youtube]', 'landing') });




        }
        if (productsPage.length > 0) {
            ScrollHproducts.Init();
            Products.Init();
            products.addClass('active');
        }
        if (webisodesPage.length > 0) {
            vidName = 'Episode 1 Bye Bye Berries';
            ScrollHwebisodes.Init();
            Webisodes.Init();
            webisodes.addClass('active');
            EmbedVideo.Init();
            ControlVideo.Init('webisodes');


        }
        if (promotionsPage.length > 0) {
            promotions.addClass('active');


            function youTubeModClick() {

                window.open($('.youtubeMod .modBtn a').attr('href'), "newWin");

            }
            function facebookModClick() {

                window.open($('.facebookMod .modBtn a').attr('href'), "newWin");

            }

            // HitBox doesn't have time to load PageView track.  Other links on this page open in new window
            // that gives time for PageView to load.  Quick hack is to wait for 1/2 second before following 
            // link URL
            function moveOn(loc) { location.href = loc; };
            $('.trkEnterSweeps').click(function(e) {
                var loc = $(this).attr('href');
                e.preventDefault();
                _hbPageView('enter sweeps', 'promotions');
                window.setTimeout(function() { moveOn(loc) }, 500);
            });
            $('.trkGrandPrize').click(function(e) {
                var loc = $(this).attr('href');
                e.preventDefault();
                _hbPageView('details', 'promotions')
                window.setTimeout(function() { moveOn(loc) }, 500);
            });
            $('.trkDailyPrizes').click(function(e) {
                var loc = $(this).attr('href');
                e.preventDefault();
                _hbPageView('daily prizes', 'promotions')
                window.setTimeout(function() { moveOn(loc) }, 500);
            });
            $('.youtubeMod .modBtn a').click(function() { _hbPageView('[youtube]', 'promotions') });
            $('.facebookMod .modBtn a').click(function() { _hbPageView('[facebook]', 'promotions') });

            DD_belatedPNG.fix('.promoHero');

        }

        if (contactUsPage.length > 0) {
            ContactUs.Init();
        }

    }
}

var EmbedVideo = {
    Init: function() {
        function hideIEhack() {
            var youPlay = $('#youPlayer').get(0);
            if (youPlay) {
                $('.ieFlashHack').hide();
            }

        }

        //URL needs " &enablejsapi=1 " appended to it in order to access vid by script;
        var id = "p_KYiQ3xQWk";
        var videoURL = "http://www.youtube.com/v/{id}&hl=en&fs=1&rel=0&enablejsapi=1".supplant({ id: id });
        addthis_url = addthis_url_template.supplant({ id: id });
        //var flashvars = {};
        var params = { allowScriptAccess: "always", allowFullScreen: "true", wmode: "transparent" };
        var atts = { id: "youPlayer" };
        swfobject.embedSWF(videoURL, "youtubeBox", "354", "223", "9.0.0", null, null, params, atts, hideIEhack);

    },
    loadNewVideo: function(id, vidTitle) {
        addthis_url = addthis_url_template.supplant({ id: id });
        addthis_title = vidTitle;
        vidName = vidTitle;
        _hbPageView('[' + vidName + ']:[load]', 'video');
        window.youPlayer.cueVideoById(id, 0);
    }
}


var ControlVideo = {
    Init: function(pageName) {
        window.onPlayerError = function(errorCode) {
            alert("An error occured: " + errorCode);
        }

        //TRACKING
        var videoPaused = false;
        window.onytplayerStateChange = function(state) {

            //console.log(window.vidName, state);
            //Returns the state of the player. 
            //Possible values are 
            //unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).

            if (state===-1){//unstarted
                //alert('unstarted');
            }
            
            if (state === 0) {//ended
                videoPaused = false;
                //console.log('end', vidName);
                _hbPageView('[' + vidName + ']:[end]', 'video');
            }

            if (state === 1) {//playing
                FlashCharacterController.AudioOff();

                //console.log('play', vidName);
                if (videoPaused === false) {
                    _hbPageView('video:[' + vidName + ']:[play]', 'video');
                } else {
                    _hbPageView('[' + vidName + ']:[restart]', 'video');
                }
            }

            if (state === 2) {//paused
                videoPaused = true;
                //console.log('pause', vidName);
                _hbPageView('[' + vidName + ']:[pause]', 'video');
            }

            if (state === 3) {//buffering
                //alert('buffer');
            }

            if (state === 5) {//cued
                videoPaused = false;
                //alert(vidName);
                //console.log('load', vidName);
                //_hbPageView('[' + window.vidName + ']:[load]', 'video');
            }
        }

        window.onYouTubePlayerReady = function(playerId) {
            window.youPlayer = document.getElementById("youPlayer");
            window.youPlayer.addEventListener("onStateChange", "onytplayerStateChange");
            window.youPlayer.addEventListener("onError", "onPlayerError");
        }
    }
};

$(Site.Init);


// string extensions

String.prototype.supplant = function(o) {
    return this.replace(/{([^{}]*)}/g,
        function(a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};

