﻿//Tips Landing Page
//Enable tab switching and set the lifestyle as the default tab with CSS active class
function changeTabsLandingPage() {

    $('li.tip-overview-tab#fitness-tab').addClass('active');
    $('div.tip-description#fitness-tips').addClass('active');
    $('div.tips-images#fitness-images').addClass('active');

    $('li.tip-overview-tab').click(function() {
        if (!$(this).is('.active')) {

            var new_active = $(this).attr('id').substring(0, $(this).attr('id').indexOf('-'));

            $('li.tip-overview-tab.active').removeClass('active');
            $('div.tip-description.active').removeClass('active');
            $('div.tips-images.active').removeClass('active');

            $('li.tip-overview-tab#' + new_active + '-tab').addClass('active');
            $('div.tip-description#' + new_active + '-tips').addClass('active');
            $('div.tips-images#' + new_active + '-images').addClass('active');

        }

    });

}

//This should cycle the content of the current active tip tab
function cycleTipsLandingPage() {
    $('.tip-description').cycle({
        fx: 'fade',
        slideExpr: 'div.rotating-tip',
        cleartypeNoBg: true,
        timeout: 12000
    });
    $('div.tips-images').cycle({
        fx: 'fade',
        slideExpr: 'img.tip-top-image',
        timeout: 12000
    });
}

function cycleImageTips() {
    $('#tip-left-col').cycle({
        fx: 'fade',
        pager: '#left-rotating-image-nav',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + idx + '</a></li>';
        },
        slideExpr: 'div.rotate-container',
        timeout: 8000
    });
}

function cycleImageIdeas() {
    $('#rotating-food-image-ideas').cycle({
        fx: 'fade',
        pager: '#rotating-food-nav-ideas',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + idx + '</a></li>';
        },
        slideExpr: 'div.rotating-image',
        timeout: 8000
    });
}

function cycleImageRecipes() {
    $('#rotating-food-image').cycle({
        fx: 'fade',
        pager: '#rotating-food-nav',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + idx + '</a></li>';
        },
        slideExpr: 'div.rotating-image'
    });
}


//Recipes overview page, show all/vegetarian options
function toggleVegetarian() {

    $('a#sort-vegetarian-meals').click(function() {

        if ($(this).attr('class').search('current') == -1) {

            $(this).addClass('current');
            $('a#sort-all-meals').removeClass('current');

            //first remove all the items that are not vegetarian
            $('li.recipes-list-recipe:not(.vegetarian)').slideUp('slow');

            //next, redo the alt striping to take into account the new list position
            $('li.recipes-list-recipe.alt').removeClass('alt');
            $('li.recipes-list-recipe:even').addClass('alt');

        }

    });

    $('a#sort-all-meals').click(function() {

        if ($(this).attr('class').search('current') == -1) {

            $(this).addClass('current');
            $('a#sort-vegetarian-meals').removeClass('current');

            //first show all the items that are not vegetarian
            $('li.recipes-list-recipe:not(.vegetarian)').slideDown('slow');

            //next, redo the alt striping to take into account the new list position
            $('li.recipes-list-recipe.alt').removeClass('alt');
            $('li.recipes-list-recipe:even').addClass('alt');

        }

    });

    //we're also going to inject the vegetarian icon into those recipes here
    $('li.recipes-list-recipe.vegetarian div.recipe-text p.recipe-name').append('<span class="vegetarian"></span>');

}

$(document).ready(function() {
    changeTabsLandingPage();
    cycleTipsLandingPage();
    cycleImageTips();
    cycleImageIdeas();
    cycleImageRecipes();
    toggleVegetarian();
});