
var Site = {
    Init: function() {
        Site.Tracking.Init();
        Site.Nav.Init();
        Site.NewsAndEvents.Init();
        Site.Redirect.Init();
    },
    Nav: {
        Init: function() {
            var flashBox = $('#flashBox');
            if (flashBox.length > 0) {

                var swfHeight;

                var SectionTo;
                //var SectionFrom = new String(jQuery.url.param("prevSect")).toLowerCase();
                var PageFrom = new String(jQuery.url.setUrl(document.referrer).attr("file")).toLowerCase().split('.')[0].split('_')[0];

                // want to remove prevSect from query string b/c it's messing with analytics
                //  would be nice if flash menu did not need it...
                // i think i shoud add a "start with"
                switch (PageFrom) {
                    case "about":
                        SectionFrom = "about";
                        break;
                    case "article":
                        SectionFrom = "newsevents";
                        break;
                    case "contactus":
                        SectionFrom = "genericState";
                        break;
                    case "coupon":
                        SectionFrom = "newsevents";
                        break;
                    case "diet":
                        SectionFrom = "nutrition";
                        break;
                    case "environmentalpolicy":
                        SectionFrom = "about";
                        break;
                    case "factsforretailers":
                        SectionFrom = "genericState";
                        break;
                    case "fageintheus":
                        SectionFrom = "about";
                        break;
                    case "howitsmade":
                        SectionFrom = "about";
                        break;
                    case "newsandevents":
                        SectionFrom = "newsevents";
                        break;
                    case "nutrition":
                        SectionFrom = "nutrition";
                        break;
                    case "nutritionchecklist":
                        SectionFrom = "nutrition";
                        break;
                    case "products":
                        SectionFrom = "products";
                        break;
                    case "recipe":
                        SectionFrom = "recipes";
                        break;
                    case "recipes":
                        SectionFrom = "recipes";
                        break;
                    case "storelocator":
                        SectionFrom = "about";
                        break;
                    case "storelocatorresults":
                        SectionFrom = "about";
                        break;
                    case "tour":
                        SectionFrom = "newsevents";
                        break;
                    case "winterfancyfoodshow2010":
                        SectionFrom = "newsevents";
                        break;
                    default:
                        SectionFrom = "home";
                        break;
                }


                //page sections
                var homeSection = $('.homeSection');
                var productSection = $('.productSection');
                var nutritionSection = $('.nutritionSection');
                var newseventsSection = $('.newseventsSection');
                var recipesSection = $('.recipesSection');
                var aboutSection = $('.aboutSection');
                var genericLinkSection = $('.genericLinkSection');

                if (homeSection.length > 0) {
                    swfHeight = "700";
                    SectionTo = "home";
                }
                if (productSection.length > 0) {
                    swfHeight = "600";
                    SectionTo = "products";
                }
                if (nutritionSection.length > 0) {
                    swfHeight = "100";
                    SectionTo = "nutrition";
                }
                if (newseventsSection.length > 0) {
                    swfHeight = "100";
                    SectionTo = "newsevents";
                }
                if (recipesSection.length > 0) {
                    swfHeight = "100";
                    SectionTo = "recipes";
                }
                if (aboutSection.length > 0) {
                    swfHeight = "100";
                    SectionTo = "about";
                }
                if (genericLinkSection.length > 0) {
                    swfHeight = "100";
                    SectionTo = "genericState";
                }


                var flashvars = {};
                flashvars.MENU_STATE_FROM = SectionFrom;
                flashvars.MENU_STATE_TO = SectionTo;
                var params = {};
                params.menu = "false";
                params.scale = "noscale";
                params.bgcolor = "#000000";
                params.wmode = "transparent";
                var attributes = {};
                swfobject.embedSWF("assets/swfs/main.swf", "flashBox", "900", swfHeight, "9.0.0", false, flashvars, params, attributes);

                $('.getFlash').css('display', 'block');
                $('.altContHead').css('display', 'block');

            }
            var contactW = $('.contactWrap');
            var factsForRetailers = $('.factsForRetailers');
            if (contactW.length > 0) {
                $('.footer ul li.contact a').addClass('active');
            }
            if (factsForRetailers.length > 0) {
                $('.footer ul li.fact a').addClass('active');
            }
        }
    },
    NewsAndEvents: {
        Init: function() {
            var newsNevents = $('.newsNevents');
            if (newsNevents.length > 0) {

                //$("#requestContent div.entry:last").addClass('lastEntry');

                function filterNewsClick() {
                    $('.filter a').removeClass('active');
                    $('#filterNews').addClass('active');
                    $('.newsNevents .content').css('height', 'auto');
                    $('.pagination').hide();
                    $("#requestContent div.entry").hide();
                    $("#requestContent div.blog").hide();
                    $("#requestContent div.news").fadeIn('slow');
                    $("#requestContent div.news:last").addClass('lastEntry');
                }
                function filterEventsClick() {
                    $('.filter a').removeClass('active');
                    $('#filterEvents').addClass('active');
                    $('.newsNevents .content').css('height', 'auto');
                    $('.pagination').hide();
                    $("#requestContent div.entry").hide();
                    $("#requestContent div.blog").hide();
                    $("#requestContent div.event").fadeIn('slow');
                    $("#requestContent div.event:last").addClass('lastEntry');
                }
                function filterAwardsClick() {
                    $('.filter a').removeClass('active');
                    $('#filterAwards').addClass('active');
                    $('.newsNevents .content').css('height', 'auto');
                    $('.pagination').hide();
                    $("#requestContent div.entry").hide();
                    $("#requestContent div.blog").hide();
                    $("#requestContent div.award").fadeIn('slow');
                    $("#requestContent div.award:last").addClass('lastEntry');
                }
                function filterBlogClick() {
                    $('.filter a').removeClass('active');
                    $('#filterBlog').addClass('active');
                    $('.newsNevents .content').css('height', 'auto');
                    $('.pagination').hide();
                    $("#requestContent div.entry").hide();
                    $("#requestContent div.blog").fadeIn('slow');
                    $("#requestContent div.blog:last").addClass('lastEntry');
                }
                function filterAllClick() {
                    //$('.filter a').removeClass('active');
                    //$('#filterAll').addClass('active');
                    $('.newsNevents .content').css('height', 'auto');
                    $('.pagination').show();
                    $("#requestContent div.entry").hide();
                    $("#requestContent div.entry").fadeIn('slow');
                    buildDivsPagination();
                    //$("#requestContent div.all").fadeIn('slow');
                    //$("#requestContent div.all:last").addClass('lastEntry');
                }

                function lookForFilter() {
                    var urlFilter = new String(jQuery.url.param("filter")).toLowerCase();

                    if (urlFilter == 'news') {
                        filterNewsClick()
                    }
                    if (urlFilter == 'events') {
                        filterEventsClick()
                    }
                    if (urlFilter == 'awards') {
                        filterAwardsClick()
                    }
                    if (urlFilter == 'blog') {
                        filterBlogClick()
                    }
                    if (urlFilter == 'all') {
                        filterAllClick()
                    }
                }


                function initFilter() {
                    var filterNews = $('#filterNews');
                    var filterEvents = $('#filterEvents');
                    var filterAwards = $('#filterAwards');
                    var filterBlog = $('#filterBlog');
                    var filterAll = $('#filterAll');
                    filterNews.click(function() {
                        filterNewsClick()
                    });
                    filterEvents.click(function() {
                        filterEventsClick()
                    });
                    filterAwards.click(function() {
                        filterAwardsClick()
                    });
                    filterBlog.click(function() {
                        filterBlogClick()
                    });
                    filterAll.click(function() {
                        filterAllClick()
                    });
                }


                function buildDivsPagination() {

                    $('.filter a').removeClass('active');
                    $('#filterAll').addClass('active');

                    $('.pagination').show();
                    //$('.newsNevents .content').css('height','800px');

                    function refreshPaginationStyles() {

                        $('.pagination .prev').hover(
                        function() { $(this).css('color', '#7d7d7d') },
                        function() { $(this).css('color', '#007db4') }
                        );
                        $('.pagination .next').hover(
                        function() { $(this).css('color', '#7d7d7d') },
                        function() { $(this).css('color', '#007db4') }
                        );

                        $('.pagination ul li').hover(
                        function() { $(this).css('color', '#7d7d7d') },
                        function() { $(this).css('color', '#007db4') }
                        );
                        $('.pagination ul li.active').hover(
                        function() { $(this).css('color', '#007db4') },
                        function() { $(this).css('color', '#7d7d7d') }
                        );
                        $('.pagination ul li:last').css('border', '0');
                        //$("#requestContent div.entry:eq("+numberShown+")").addClass('lastEntry');
                    }
                    $('.pagination .next').click(function() { showNext() });
                    $('.pagination .prev').click(function() { showPrev() });

                    $("#requestContent div.entry").hide();

                    var numberShown = 6;

                    var pageIndex = 1;
                    var eleIndex = 0;

                    var entries = $("#requestContent div.entry");
                    var numOfPages = Math.round(entries.length / numberShown);

                    function displayEntries(pIndex) {
                        if (pIndex >= numOfPages) {
                            $('.pagination .prev').show();
                            $('.pagination .next').hide();

                        }
                        else if (pIndex <= 1) {
                            $('.pagination .next').show();
                            $('.pagination .prev').hide();

                        } else {

                            $('.pagination .next').show();
                            $('.pagination .prev').show();
                        }

                        var theShown = numberShown * pIndex;
                        var theHiddenSmaller = theShown - numberShown;

                        for (var i = 0; i < entries.length; i++) {
                            var entry = entries[i];
                            if (i < theShown) {
                                $("#requestContent div.entry:eq(" + i + ")").show();
                            }
                            if (i < theHiddenSmaller) {
                                $("#requestContent div.entry:eq(" + i + ")").hide();
                            }
                            if (i >= theShown) {
                                $("#requestContent div.entry:eq(" + i + ")").hide();
                            }
                        }
                        refreshPaginationStyles();

                    }
                    displayEntries(pageIndex);

                    function showNext() {
                        pageIndex++;
                        if (pageIndex >= numOfPages) {
                            pageIndex = numOfPages;

                        }
                        var page = pageIndex - 1;
                        $('.pagination ul li').removeClass('active');
                        $('.pagination ul li:eq(' + page + ')').addClass('active');
                        displayEntries(pageIndex);
                    }
                    function showPrev() {

                        pageIndex--;
                        if (pageIndex <= 1) {
                            pageIndex = 1;

                        }
                        var page = pageIndex - 1;
                        $('.pagination ul li').removeClass('active');
                        $('.pagination ul li:eq(' + page + ')').addClass('active');

                        displayEntries(pageIndex);
                    }

                    var liDisplay = [];
                    for (var ii = 0; ii < numOfPages; ii++) {
                        var displayNum = ii + 1
                        var liMarkup = '<li>' + displayNum + '</li>';

                        liDisplay.push(liMarkup);
                    }
                    var pUL = $('.pagination ul').get(0);
                    var disjoin = liDisplay.join("");
                    pUL.innerHTML = disjoin;
                    $('.pagination ul li:eq(0)').addClass('active');
                    $('.pagination ul li:last').css('border', '0');

                    $('.pagination ul li').click(function() {
                        $('.pagination ul li').removeClass('active');
                        $(this).addClass('active');
                        displayEntries(this.innerHTML)


                    });
                }

                buildDivsPagination();
                initFilter();
                lookForFilter();
            }
        }
    },
    Redirect: {
        Init: function() {
            var homePage = $('.homeSection');
            if (homePage.length > 0) {

                var hash = (/\#.+/.test(document.location.href)) ? document.location.href.split('#')[1] : null;



                if (hash) {
                    if (hash.indexOf('/products/') > -1) {

                        if (hash.indexOf('/products/classic') > -1) {
                            document.location.href = "products.aspx#/products/classic";
                        }
                        if (hash.indexOf('/products/zero') > -1) {
                            document.location.href = "products.aspx#/products/zero";
                        }
                        if (hash.indexOf('/products/two') > -1) {
                            document.location.href = "products.aspx#/products/two";
                        }
                        if (hash.indexOf('/products/five') > -1) {
                            document.location.href = "products.aspx#/products/five";
                        }
                        //document.location.href = "products.aspx
                    }
                    if (hash.indexOf('/nutrition/') > -1) {
                        if (hash.indexOf('/nutrition/nutrition') > -1) {
                            document.location.href = "nutrition.aspx";
                        }
                        if (hash.indexOf('/nutrition/diet') > -1) {
                            document.location.href = "diet.aspx";
                        }
                        //document.location.href = "nutrition.aspx";
                    }
                    if (hash.indexOf('/recipes') > -1) {
                        document.location.href = "recipes.aspx";
                    }
                    if (hash.indexOf('/news') > -1) {
                        document.location.href = "newsandevents.aspx";
                    }
                    if (hash.indexOf('/about/') > -1) {
                        if (hash.indexOf('/about/fage') > -1) {
                            document.location.href = "about.aspx";
                        }
                        if (hash.indexOf('/about/how') > -1) {
                            document.location.href = "HowItsMade.aspx";
                        }
                        if (hash.indexOf('/about/plant') > -1) {
                            document.location.href = "FageInTheUS.aspx";
                        }
                        if (hash.indexOf('/about/envp') > -1) {
                            document.location.href = "EnvironmentalPolicy.aspx";
                        }
                        //document.location.href = "about.aspx";
                    }
                    if (hash.indexOf('/contact') > -1) {
                        document.location.href = "contactUs.aspx";
                    }
                }
            }
        }
    },
    Coupon: {
        openEcentives: function(e, fn, ln, s) {
            var url = "http://offers.e-centives.com/mcp/";
            var qstring = "?";
            if (s == null || s == '' || typeof (s) == "undefined") {
                qstring += "type=promo&id=5056a472-09dd-4a8b-8a8e-536f0b87ead7&e=" + e + "&fn=" + fn + "&ln=" + ln;
            }
            else {
                qstring += "s=" + s;
            }
            //console.log(url + qstring);
            // no longer doing pop-up
            // window.open(url + qstring, '', '');
            document.location.href = url + qstring;
        },
        openEcentivesEncrypt: function(s) {
            Site.Coupon.openEcentives('', '', '', s);
        }
    },
    Tracking: {
        Init: function() {
            // set up the tracker
            var options = {
                XmlUrl: "/assets/xml/trackItData.xml",
                Plugins: [
                    $.TrackItPlugins.CheckUrlMapping
                ]
            };
            Site.Tracking.Tracker = new $.TrackIt('GoogleAnalytics', options);

            // handle pageViews that involve .NET postback to its self
            if ($('.contactWrap').length > 0 && $('.thankyou').length == 0)
                Site.Tracking.Tracker.track('Contact Us Page Load');

            if ($('.contactWrap').length > 0 && $('.thankyou').length > 0)
                Site.Tracking.Tracker.track('Thank You Page Load');

            // weed out recipe_detail page to avoid double tracking
            if (new String(jQuery.url.attr("file")).toLowerCase() != "recipe_detail.aspx") {
                if ($('.recipesWrap').length > 0 && $('.recipeFinder').length > 0)
                    Site.Tracking.Tracker.track('Recipe Main Page Load');

                if ($('.recipesWrap').length > 0 && $('.recipeFinder').length == 0)
                    Site.Tracking.Tracker.track('Recipe Results Page Load');
            }

            // handle direct deep link to products page
            // swf doesn't fire JS function if loaded with proper deep link
            if (new String(jQuery.url.attr("file")).toLowerCase() == "products.aspx") {
                var deepLink = new String(jQuery.url.attr("anchor")).toLowerCase();
                switch (deepLink) {
                    case "/products/classic":
                        Site.Tracking.Tracker.track('Products Classic Page Load');
                        break;
                    case "/products/zero":
                        Site.Tracking.Tracker.track('Products Zero Percent Page Load');
                        break;
                    case "/products/two":
                        Site.Tracking.Tracker.track('Products Two Percent Page Load');
                        break;
                }
            }
            $('.trackerWithClientVal').live("click", function() {
                if (typeof (Page_ClientValidate) == 'function') {
                    Page_ClientValidate();
                }
                if (Page_IsValid) {
                    Site.Tracking.Tracker.track($(this).attr('tracker'));
                }
            });

        }
    }
};
function productSwfClick(id) {
    switch (id) {
        case "classic":
            Site.Tracking.Tracker.track('Products Classic Page Load');
            break;
        case "zero":
            Site.Tracking.Tracker.track('Products Zero Percent Page Load');
            break;
        case "two":
            Site.Tracking.Tracker.track('Products Two Percent Page Load');
            break;
    }; 
};
$(Site.Init);





