var getUniqueID = Math.floor(Math.random()*1000001);
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
		$('#good_food').mouseover(function() {
			$('#nestle_logo_rollover').css('display', 'block');
			$('#nestle_logo').css('display', 'none');
		});
	
		$('#good_food').mouseout(function() {
			$('#nestle_logo_rollover').css('display', 'none');
			$('#nestle_logo').css('display', 'block');
		});
	
        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/season3_home_left.swf';
            var swfR = 'assets/swfs/season3_home_right.swf';
            $('.flashWrap').eq(0).addClass('characterWide');
            // $('.flashWrap').eq(1).addClass('characterNudge');

            FlashCharacterController.loadSwfs(swfL, swfR, $.cookie('landedCookie'));
            $('.flashWrap').eq(0).addClass('characterWide');
            $('.flashWrap').eq(1).addClass('characterNudge');
            EmbedVideo.Init();
            ControlVideo.Init('landing');



            //cookie
            $.cookie('landedCookie', 'landed');


        }
        if (productsPage.length > 0) {
            ScrollHproducts.Init();
            Products.Init();
            products.addClass('active');
        }
        if (webisodesPage.length > 0) {
            vidName = 'Episode 1 - Famous Fruits!';
            ScrollHwebisodes.Init();
            Webisodes.Init();
            webisodes.addClass('active');
            $('.sMove ul li a').removeClass('active');
            $('#BLQc-Uu3ESQ').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");

            }

        }

        if (contactUsPage.length > 0) {
            ContactUs.Init();
        }

    },
    triggerFunc: function() {
        var showModalDetails = {
            clickEvents: function() {
                $('.share-email-modal').click(function(e) {
                    $('.share-modal-wrap,#at_email,#at_email form,#share-modal-wrap-holder').show('slow', function(){
                        $('.st_to_modal').focus();
                    });
                    $('#at_email').css('visibility', 'visible');
                    $('#at_success').hide();
                    $('#at16ptc').html('Email a Friend');
                    Recaptcha.reload();
                    e.preventDefault();
                });
                $('.text-link-share').click(function() {
                    $('#at_email,#at_email form').hide();
                });
                $('#at16ptx,.cancel_modal').click(function() {
                    var errorMsgs = $('#ctl00_Content_EmailSharePopup1_req_st_to,#ctl00_Content_EmailSharePopup1_req_st_from');
                    $('span[id$="labelError"]').html("");
                    $('.st_to_modal,.st_from_modal').val('');
                    $('.st_msg').val('');
                    $('.st_to_modal+span,.st_from_modal+span').hide();
                    $(errorMsgs).hide();
                });
                $('#CurrentSeason a').click(function(){
                    $('.share-modal-wrap').hide();
                });
            },
            focusOutlineForm: function() {
                var $modalInput = $('.share-modal-wrap input,.share-modal-wrap textarea'),
                err = 'st_error_field',
		        foc = 'focus-input';
                $($modalInput).focus(function() {
                    $(this).addClass(foc);
                });
                $($modalInput).blur(function() {
                    $(this).removeClass(foc);
                });
                $('form').submit(function() {
                    function validateEmail(id) {
                        var emailPattern = /^[-+.\w]{1,64}@[-.\w]{1,64}\.[-.\w]{2,6}$/;
                        return emailPattern.test(id);
                    }
                    function outlineInvalidInput() {
                        var $emailTo = $('.st_to_modal').val(),
                            $emailFrom = $('.st_from_modal').val(),
                            emailToValid = validateEmail($emailTo),
                            emailFromValid = validateEmail($emailFrom);

                        if (!emailToValid) {
                            $('.st_to_modal').addClass(err);
                        } else {
                            $('.st_to_modal').removeClass(err);
                        }
                        if (!emailFromValid) {
                            $('.st_from_modal').addClass(err);
                        } else {
                            $('.st_from_modal').removeClass(err);
                        }
                        if (emailToValid && emailFromValid) {
                            // both mails valid
                            $('#at_error').hide();
                            $('.st_to_modal,.st_from_modal').val('');
                        } else {
                            $('#at_error').show();
                        }
                    }
                    //outlineInvalidInput();
                });
            },
            shareVideoAgain: function() {
                $('.share_video_again').live('click', function() {
                    $('#at_success').hide();
                    $('.share-modal-wrap,#at_email,#at_email form,#share-modal-wrap-holder').show();
                    Recaptcha.reload();
                    $('.st_to_modal,.st_from_modal,.st_msg').val('');
                    return false;
                });
            },
            fixFirefoxOSx: function(){
                $('.st_to_modal').click(function(){
                  $('.st_from_modal').focus();
                  $('.st_to_modal').focus();
                  var toVal = $('.st_to_modal').val();
                  $('.st_to_modal').val(toVal);    
                  $('.st_from_modal').removeClass('focus-input');
                });
                $('.st_from_modal').click(function(){
                  $('.st_to_modal').focus();
                  $('.st_from_modal').focus();
                  var fromVal = $('.st_from_modal').val();
                  $('.st_from_modal').val(fromVal);
                  $('.st_to_modal').removeClass('focus-input');
                });
            }
        };
        showModalDetails.fixFirefoxOSx();
        showModalDetails.clickEvents();
        showModalDetails.shareVideoAgain();
        showModalDetails.focusOutlineForm();
        var pgName = window.location.pathname,
        newName = pgName.replace('/','').replace('.aspx',''),
        newName = newName + 'Pg';
        $('body').addClass(newName);
    }
};
Site.triggerFunc();

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 = "BLQc-Uu3ESQ";
        var videoURL = "http://www.youtube.com/v/{id}&hl=en&fs=1&rel=0&enablejsapi=1&iv_load_policy=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);
        }
        videoPaused = false;
        window.onYouTubePlayerReady = function(playerId) {
            window.youPlayer = document.getElementById("youPlayer");
            window.youPlayer.addEventListener('onStateChange', "trackItTracking.handleYouTubeStateChange");
            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;
        }
    );
};


