HERSHEYS.Twizzlers = {};

HERSHEYS.Twizzlers.initIndex = 0;
HERSHEYS.Twizzlers.dates = ['1845', '1850', '1870', '1902', '1968', '1969', '1977', '1994', '1998', '2000', '2004', '2005', '2006', '2009'];
HERSHEYS.Twizzlers.isOurStoryEmbedded = false;

HERSHEYS.Twizzlers.GetSWF = function (appName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[appName];
    }
    else {
        return document[appName];
    }
}

HERSHEYS.Twizzlers.GetIndex = function () {
    HERSHEYS.Twizzlers.UpdateFlash(HERSHEYS.Twizzlers.initIndex);
}

HERSHEYS.Twizzlers.GetDate = function () {
    date = HERSHEYS.Twizzlers.dates[Twizzlers.initIndex];
    HERSHEYS.Twizzlers.UpdateFlash(date);
}

HERSHEYS.Twizzlers.GetAllDates = function () {
    HERSHEYS.Twizzlers.GetSWF('flash_wrap').setAllDates(Twizzlers.dates);
}

HERSHEYS.Twizzlers.UpdateFlash = function (id) {
    try {
        document.getElementById("flash_wrap").goToIndex(id);
    } catch (e) { }

}

HERSHEYS.Twizzlers.UpdateOurStoryTabs = function (id) {
    var pageCount = 6;
    var page = Math.ceil((id + 1) / pageCount);
    var start = (page - 1) * pageCount;
    var end = start + pageCount;
    var len = HERSHEYS.Twizzlers.dates.length;

    for (i = 0; i < start; i++) {
        $("#nav_" + HERSHEYS.Twizzlers.dates[i]).hide();
    }

    for (i = start; i < end; i++) {
        $("#nav_" + HERSHEYS.Twizzlers.dates[i]).show();
    }

    for (i = end; i < len; i++) {
        $("#nav_" + HERSHEYS.Twizzlers.dates[i]).hide();
    }
}

HERSHEYS.Twizzlers.initializations = {

    /* PROMOTIONS */
    'twizzlers/promotions': function () {
        $('.learn.modal_popup').fancybox({
            'autoScale': false,
            'onStart': function () {
                var headerH = document.getElementById('header').offsetHeight,
            footerH = document.getElementById('footer').offsetHeight,
            wrapperH = window.innerHeight - headerH - footerH;
                document.getElementById('fancybox-overlay').style.height = '100%';
            },
            'onComplete': function () {
                $("#pr_Official_Rules .pr_jscroll").jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 20, showArrows: false });
                $("#pr_Prizes .pr_jscroll").jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 20, showArrows: false });
                $("#pr_No_Purchase .pr_jscroll").jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 20, showArrows: false });
                $("#pr_EnterNow .pr_jscroll").jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 20, showArrows: false });
            }
        });
    },


    /* HOMEPAGE */
    'twizzlers': function () {

        var flashvars = {};
        var attributes = {};
        var param = { wmode: 'transparent' };

        swfobject.embedSWF("/assets/swf/twizzlers/twizzlersHome.swf",
              "flash_wrap", "100%", "100%", "10.0.0",
              "/assets/swf/expressInstall.swf", flashvars, param, attributes);


        $(HERSHEYS.Rotator).bind('NewItem', function (e, data) {
            var current = $('.rotator_wrapper').find('.active');
            current.hide().fadeIn(1000);
            var index = current.attr('id').replace('home', '');
            HERSHEYS.Twizzlers.UpdateFlash(Number(index) - 1);
        });

        HERSHEYS.Rotator.init();

    },

    /* PRODUCTS */
    'twizzlers/products/twizzlers-twists': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#product-div-description-left .nav a',
            contentSelector: '#product-tabbed-content .tabbedContent'
        });

        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });

    },
    'twizzlers/products/twizzlers-filled-twists': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#product-div-description-left .nav a',
            contentSelector: '#product-tabbed-content .tabbedContent'
        });
        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });

    },
    'twizzlers/products/twizzlers-pull-n-peel': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#product-div-description-left .nav a',
            contentSelector: '#product-tabbed-content .tabbedContent'
        });
        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });

    },
    'twizzlers/products/twizzlers-bites': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#product-div-description-left .nav a',
            contentSelector: '#product-tabbed-content .tabbedContent'
        });
        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });
    },
    'twizzlers/products/twizzlers-nibs': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#product-div-description-left .nav a',
            contentSelector: '#product-tabbed-content .tabbedContent'
        });
        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {
            var productName = $(data).attr('title');
            HERSHEYS.Analytics.doProductView(productName);
        });

    },

    /* OUR STORY */
    'twizzlers/our-story': function () {


        $(HERSHEYS.TabbedBrowsing).bind('DeepLinked', function (e, data) {
            HERSHEYS.Twizzlers.initIndex = data;
            HERSHEYS.Twizzlers.UpdateOurStoryTabs(data);
            if (!HERSHEYS.Twizzlers.isOurStoryEmbedded) {
                var flashvars = { 'startDate': HERSHEYS.Twizzlers.dates[HERSHEYS.Twizzlers.initIndex] };
                var attributes = {};
                var param = { wmode: 'transparent', allowscriptaccess: 'always' };
                swfobject.embedSWF("/assets/swf/twizzlers/ourStoryRope.swf",
                  "flash_wrap", "100%", "100%", "10.0.0",
                  "/assets/swf/expressInstall.swf", flashvars, param, attributes, function (e) {

                  });

                HERSHEYS.Twizzlers.isOurStoryEmbedded = true;
            }

        });

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#our-story-content .nav a',
            contentSelector: '#our-story-content .tabbedContent',
            nextSelector: '.tabbedNext',
            previousSelector: '.tabbedPrevious'
        });

        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {

            var name = $(data).attr('title');
            HERSHEYS.Analytics.doPageView('our story', name);
            HERSHEYS.Twizzlers.UpdateFlash(name);
            HERSHEYS.Twizzlers.UpdateOurStoryTabs(HERSHEYS.Twizzlers.dates.indexOf(name));
        });



        HERSHEYS.Twizzlers.resizeRope = function () {
            $('#rope').css('width', $(window).width());
        };

        $(window).bind('resize', function () {

            HERSHEYS.Twizzlers.resizeRope();
        });

        HERSHEYS.Twizzlers.resizeRope();

    },

    /* TOP 10 */
    'twizzlers/experience/top-10': function () {

        HERSHEYS.TabbedBrowsing.init({
            navSelector: '#top10-nav .nav a',
            contentSelector: '#top10-tabbed-content .tabbedContent'
        });

        $(HERSHEYS.TabbedBrowsing).bind('NewItemShowing', function (e, data) {

            var name = $(data).attr('title');
            HERSHEYS.Analytics.doPageView('experience:top-10', name);

        });


    },

    /* WHAT WOULD YOU DO */
    'twizzlers/experience/tell-us': function () {
        $("#scroller").simplyScroll({
            className: 'vert',
            horizontal: false,
            frameRate: 30,
            speed: 5
        });

        $('#submit_dob').bind('click', function () {

            var _this = $(this);

            HERSHEYS.Services.agecheck({
                year: $('#dob_year').attr('value'),
                month: $('#dob_month').attr('value'),
                day: $('#dob_day').attr('value'),

                success: function () {
                    $('.form_step').hide();
                    $('.form_step:eq(1)').show();
                    $('#dob-wrap, #submit_dob, #tell-us-form-label').css('visibility', 'hidden');
                },
                failure: function () {
                    $('#enter-your-bday').html('Sorry, you must be 18!');
                    $('#dob-wrap, #submit_dob, #tell-us-form-label').css('visibility', 'hidden');
                }
            });
            return false;

        });

        $('#tell-us-step2 .next-but').bind('click', function () {

            if ($('#first-name-wrap input:first').attr('value') == '') return false;


            $('.form_step').hide();
            $('.form_step:eq(2)').show();

            return false;
        });


        $('#tell-us-step3 .submit-but').bind('click', function () {

            if ($('#tell-us-step3 textarea').attr('value') == '') return false;

            var post = {

                'Message': $('#tell-us-step3 textarea').attr('value'),
                'UserName': $('#first-name-wrap input:first').attr('value'),
                'State': $('#state').attr('value')
            }


            $.post('/AjaxServices.ashx?method=twizzlerTellUs', post, function (data) {

                if (data == "1") {
                    $('.form_step').hide();
                    $('.form_step:eq(3)').show();
                }

            });

            return false;

        });

        $('#tell-us-complete .submit-but').bind('click', function () {

            $('.form_step textarea').attr('value', '');
            $('.form_step').hide();
            $('.form_step:eq(2)').show();

            return false;
        });




    }

    /* ETC */
};

HERSHEYS.Utility.doPageInits(HERSHEYS.Twizzlers.initializations);

$(document).ready(function () {

    /* Wire up search result button to field and search result page. */
    HERSHEYS.Utility.stealSearchButton({
        'site': 'twizzlers'
    });

    /* Enable leaving notice binding */
    HERSHEYS.Utility.leavingNotice({
        'transparency': HERSHEYS.transparency,
        'title': 'TWIZZLERS'
    });
    HERSHEYS.ShareThis.init();

    /* To track Twizzlers Tell Us Text Input */
    $('.tell-us #tell-us-step1 input#submit_dob').bind('click', function () {
        s.events = 'event15';
        s.pageName = s.prop4 = s.prop1 = 'hersheys:twizzlers:experience:tell us:text input';
        s.channel = s.prop2 = 'hersheys:twizzlers:experience';
        s.prop3 = 'hersheys:twizzlers:experience:tell us';
        s.prop26 = s.eVar36 = 'twizzlers:app:tell us';

        s.t();
    });

    /* To Track Twizzlers User Info */
    $('.tell-us #tell-us-step2 input.next-but').bind('click', function () {
        s.events = 'event18';
        s.pageName = s.prop4 = s.prop1 = 'hersheys:twizzlers:experience:tell us:user info';
        s.channel = s.prop2 = 'hersheys:twizzlers:experience';
        s.prop3 = 'hersheys:twizzlers:experience:tell us';
        s.prop26 = s.eVar36 = 'twizzlers:app:tell us';

        s.t();
    });

    /* To Track Twizzlers Text Input */
    $('.tell-us #tell-us-step3 input.submit-but').bind('click', function () {
        s.events = 'event16';
        s.pageName = s.prop4 = s.prop1 = 'hersheys:twizzlers:experience:tell us:complete';
        s.channel = s.prop2 = 'hersheys:twizzlers:experience';
        s.prop3 = 'hersheys:twizzlers:experience:tell us';
        s.prop26 = s.eVar36 = 'twizzlers:app:tell us';

        s.t();
    });
});
