﻿
$(document).ready(function () {
    //global vars
    var url = location.pathname;
    $('.cartNav a').css({ backgroundPosition: '0px -36px' });
    $('.cartNav span').css({ backgroundPosition: '0px -36px' });
    $('.cartBtn').css({ backgroundPosition: '0px 0px' });
    $('.labelColumn').fadeTo(0, 0);
    $('.ingredients tr td:first-child').css('text-align', 'left');
    $('.ingredients tr:last td').css('border-bottom', 'none');
    //adds padding to the last row of the cart
    $('.cartControl tr:last td').css('padding-bottom', '30px');

    //change color of every td in a product row in shopping cart
    $('.itemRow').hover(function () {
        $(this).css('background-color', '#f7f5ea');
    }, function () {
        $(this).css('background-color', '#fffdf3');
    });

    //Contact Page
    $(".contactRow").each(function () {
        var lbl = $('> .labelColumn', this);
        var fld = $('> .fieldColumn', this);
        var inp = $('> .fieldColumn', this).children();
        inp.attr('value', lbl.html());
        $(inp).focus(function () {
            inp.css("background-color", "#eee");
            inp.css("border", "1px solid #aaa");
            if (this.value == lbl.html()) {
                this.value = '';
                lbl.fadeTo('fast', 1.0);
            }
        });
        $(inp).blur(function () {
            inp.css("background-color", "#fff");
            inp.css("border", "1px solid #CCC");
            if (this.value == '') {
                this.value = lbl.html();
                lbl.fadeTo('fast', 0);
            }
        });



    });


    //yellow button fader
    $(".superBtn").fadeTo("fast", 0.7);
    $(".superBtn").hover(function () {
        $(this).fadeTo("fast", 1.0);
    }, function () {
        $(this).fadeTo("fast", 0.7);
    });

    $(".buy").fadeTo("fast", 0.8);
    $(".buy").hover(function () {
        $(this).fadeTo("fast", 1.0);
    }, function () {
        $(this).fadeTo("fast", 0.8);
    });

    //product detail left nav effects
    $(".prdNav li:last").css("border-bottom", "none");

    //product detail tab section
    var $tabz = $('ul.tabs li');
    var $cont = $('.tabContent ul li');
    $($cont).hide();
    var $activeTab = $('.tabContent ul li:first');
    $($activeTab).fadeIn("fast");
    hei = $($activeTab).height();
    $('.tabContent').stop().animate({ height: hei }, 'slow');
    $('ul.tabs li').click(function () {
        $($cont).hide();
        $($tabz).removeClass("selected");
        $(this).addClass("selected");
        $activeTab = $(this).find("a").attr("rel");
        $($activeTab).fadeIn("slow");
        hei = $($activeTab).height();
        $('.tabContent').stop().animate({ height: hei }, 'slow');
    });

    $('.cartBtn').hover(function () {
        $(this).stop().animate({ backgroundPosition: '(0px -100px)' }, 500);
    }, function () {
        $(this).stop().animate({ backgroundPosition: '(0px 0px)' }, 300);
    });

    $('.cartNav span').each(function () {
        $(this).addClass("off");
        var aLnk = $(this).attr("rel");
        aLnk = "/" + aLnk;

        var cont = url.indexOf(aLnk);

        if (cont > 0) {
            $(this).addClass("on");
            $(this).removeClass("off");
        }
        if ($(this).hasClass("on")) {
            $(this).css({ backgroundPosition: '0px 0px' });
        }
        else {
            $(this).css({ backgroundPosition: '0px -36px' });
        }
    });

    $('div.accord > div').hide();
    $('div.accord > h4').click(function () {
        var closeHi, openHi, hi;
        $(this).next('div').slideToggle('fast', function () {
            openHi = $(this).height();
            if ($(this).siblings('div:visible').length) {
                $(this).siblings('div:visible').slideUp('fast', function () {
                    closeHi = $(this).height();
                    hi = $('.tabContent').height() + openHi - closeHi;
                    $('.tabContent').stop().animate({ height: hi }, 'slow');
                })
            } else {
                hi = $('.tabContent').height() + openHi;
                $('.tabContent').stop().animate({ height: hi }, 'slow');
            }
        });

    });


    //homepage slider
    var intervalInSeconds = 5;
    var timeBetween = intervalInSeconds * 1000;
    $('.nFWrap .slideContainer > div:first').css('display', 'block').addClass('active');
    var slideTimer = setInterval("nextSlide()", timeBetween);

    /*
    $('.notFlash').after('<div id="thumbNav"></div>').cycle({
    fx: 'fade',
    pager: '#thumbNav',
    timeout: 6000,
    before: onBefore,
    pagerAnchorBuilder: function (idx, slide) {
    return '<a><div class="thumbOn">&nbsp;</div><span></span></a>';
    }
    });

    $('#thumbNav a').each(function () {
    $(this).css('background-position', $(this).index() * -143 + "px -75px");
    $(this).find('.thumbOn').css('background-position', $(this).index() * -143 + "px 0px");
    $(this).find('.thumbOn').fadeTo(0, 0);
    $(this).find('span').html($('.notFlash li').eq($(this).index()).find('h2 img').attr('alt'));
    $('.activeSlide').find('.thumbOn').fadeTo('slow', 1);
    });
  
    //  $('#thumbNav a').hover(function () {
    //     if ($(this).index() == $('.activeSlide').index()) {
    //   } else {
    //     $(this).find('.thumbOn').stop().fadeTo('normal', 1);
    //    }
    // }, function () {
    //   if ($(this).index() == $('.activeSlide').index()) {
    //     $(this).css("cursor", "default");
    // } else {
    //    $(this).find('.thumbOn').stop().fadeTo('normal', 0);
    // }
    //});
    $('#thumbNav a:last').css('margin-right', '0');
    */
});

function nextSlide() {
    $currentSlide = $('.nFWrap .slideContainer > .active');
    $currentSlide.fadeOut(1000).removeClass('active');

    $nextSlide = $currentSlide.next();
    if (!$nextSlide.is(':last')) {
        $('.nFWrap .slideContainer > div:first').fadeIn(1000).addClass('active');
    } else {
        $nextSlide.fadeIn(1000).addClass('active');
    }
}

//function onBefore() {
//    $('.activeSlide').find('.thumbOn').fadeTo('slow', 0);
//}

(function ($) {
    $.fn.dropDownNav = function (options) {
        var defaults = {
            speed: 'slow'
        };
        var options = $.extend(defaults, options);

        $(this).hoverIntent(function () {
            var $this = $(this);
            $this.find('ul').slideDown(300);

        }, function () {
            var $this = $(this);
            if ($this.find('ul').length == 0) { //if there isn't anything to slide up, we still want to remove the hovering class
                $this.removeClass('hovering');
            }
            else {
                $this.find('ul').slideUp(300, function () {
                    $this.removeClass('hovering');
                });
            }
        });
    };
})(jQuery);
