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/WEB Cran02.04.swf'
            var swfR = 'assets/swfs/WEB Rachel03-2.swf'
            FlashCharacterController.loadSwfs(swfL, swfR, $.cookie('landedCookie'));
            EmbedVideo.Init();
            ControlVideo.Init('landing');



            //cookie
            $.cookie('landedCookie', 'landed');


            $('.cranMod .modBtn a').click(function() { _hbPageView('[cranMod]', 'landing'); });
            $('.facebookMod .modBtn a').click(function() { _hbPageView('[facebook]', 'landing') });
            $('.promoMod .modBtn a').click(function() { _hbPageView('[womans day Mod]', 'landing') });




        }
        if (productsPage.length > 0) {
            ScrollHproducts.Init();
            Products.Init();
            products.addClass('active');
        }
        if (webisodesPage.length > 0) {
            vidName = 'Episode 1 Talking Bout Cranberry';
            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");

            }
            $('.promoBtn a').click(function() { _hbPageView('[womans day]', 'promotions') });
            $('.youtubeMod .modBtn a').click(function() { _hbPageView('[youtube]', 'promotions') });
            $('.facebookMod .modBtn a').click(function() { _hbPageView('[facebook]', 'promotions') });

        }

        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 = "mCiO-xwHFNs";
        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;
      
        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) {
            
            //Returns the state of the player. 
            //Possible values are 
            //unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).

            if (state === 0) {//ended
                videoPaused = false;
                _hbPageView('[' + vidName + ']:[complete]', 'webisodes');
            }

            if (state === 1) {//playing
                FlashCharacterController.AudioOff();

                if (videoPaused === false) {
                    _hbPageView('video:[' + vidName + ']:[start]', 'webisodes');
                } else {
                    _hbPageView('[' + vidName + ']:[restart]', 'webisodes');
                }
            }

            if (state === 2) {//paused
                videoPaused = true;
                _hbPageView('[' + vidName + ']:[pause]', 'webisodes');
            }

            if (state === 3) {//buffering

            }

            if (state === 5) {//cued
                videoPaused = false;
                _hbPageView('[' + vidName + ']:[load]', 'webisodes');
            }
        }

        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;
        }
    );
};

