
var Site = {
    Init: function() {
        Site.Nav.Init();
        Site.NewsAndEvents.Init();
        Site.Redirect.Init();
    },
    Nav: {
        Init: function() {
            var flashBox = $('#flashBox');
            if (flashBox.length > 0) {

                //console.log(document.referrer)
                var swfHeight;

                var SectionTo;
                var SectionFrom;

                try {
                    SectionFrom = document.location.search.split('prevSect=')[1].split('&')[0].toLowerCase();
                    //console.log(SectionFrom + ' this is SectionFrom')
                } catch (e) {
                    //console.log(e)
                }


                //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.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.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.award").fadeIn('slow');
                    $("#requestContent div.award:last").addClass('lastEntry');
                }
                
                
                function lookForFilter(){
                    var urlFilter;

                    try {
                        urlFilter = document.location.search.split('filter=')[1].split('&')[0].toLowerCase();
                        //console.log(urlFilter + ' this is urlFilter')
                    } catch (e) {
                        //console.log(e)
                    }
                    if (urlFilter == 'news') {
                        filterNewsClick()
                    }
                    if (urlFilter == 'events') {
                        filterEventsClick()
                    }
                    if (urlFilter == 'awards') {
                        filterAwardsClick()
                    }
                
                
                }

                

                
                function initFilter() {
                    var filterNews = $('#filterNews');
                    var filterEvents = $('#filterEvents');
                    var filterAwards = $('#filterAwards');
                    filterNews.click(function() {
                       filterNewsClick()
                    });
                    filterEvents.click(function() {
                        filterEventsClick()
                    });
                    filterAwards.click(function() {
                        filterAwardsClick()
                    });
                }
                
                
                function buildDivsPagination() {
                    $('.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";
                    }                                      
                }
            }
        }
    }
}
$(Site.Init);