/***** EASING *****/

jQuery.easing.jswing = jQuery.easing.swing; jQuery.extend(jQuery.easing, { def: "easeInOutQuad", swing: function (e, f, a, h, g) { return jQuery.easing[jQuery.easing.def](e, f, a, h, g) }, easeInQuad: function (e, f, a, h, g) { return h * (f /= g) * f + a }, easeOutQuad: function (e, f, a, h, g) { return -h * (f /= g) * (f - 2) + a }, easeInOutQuad: function (e, f, a, h, g) { if ((f /= g / 2) < 1) { return h / 2 * f * f + a } return -h / 2 * ((--f) * (f - 2) - 1) + a }, easeInQuint: function (e, f, a, h, g) { return h * (f /= g) * f * f * f * f + a }, easeOutQuint: function (e, f, a, h, g) { return h * ((f = f / g - 1) * f * f * f * f + 1) + a }, easeInOutQuint: function (e, f, a, h, g) { if ((f /= g / 2) < 1) { return h / 2 * f * f * f * f * f + a } return h / 2 * ((f -= 2) * f * f * f * f + 2) + a }, easeInExpo: function (e, f, a, h, g) { return (f === 0) ? a : h * Math.pow(2, 10 * (f / g - 1)) + a }, easeOutExpo: function (e, f, a, h, g) { return (f == g) ? a + h : h * (-Math.pow(2, -10 * f / g) + 1) + a }, easeInOutExpo: function (e, f, a, h, g) { if (f === 0) { return a } if (f == g) { return a + h } if ((f /= g / 2) < 1) { return h / 2 * Math.pow(2, 10 * (f - 1)) + a } return h / 2 * (-Math.pow(2, -10 * --f) + 2) + a }, easeInRegular: function (e, f, a, h, g) { return h * (f /= g) * f + a }, easeOutRegular: function (e, f, a, h, g) { return -h * (f /= g) * (f - 2) + a }, easeInOutRegular: function (e, f, a, h, g) { if ((f /= g / 2) < 1) { return h / 2 * f * f + a } return -h / 2 * ((--f) * (f - 2) - 1) + a } });

function mainNav() {
    $('#mainNav > ul > li').each(function () {
        if ($(this).find('ul').size() > 0) {
            $(this).find('> a').wrapInner('<span></span>');
        }
    });
    $('#mainNav > ul > li').hover(function () {
        $(this).addClass('over');
        $('#subNavInner').html($(this).find('ul:first').clone());
        if ($('#subNavInner > ul').size() > 0) {
            $('#subNavInner > ul > li:nth-child(3n)').each(function () {
                var height = $(this).prev().prev().height();
                if ($(this).prev().height() > height) {
                    height = $(this).prev().height();
                }
                if ($(this).height() > height) {
                    height = $(this).height();
                }
                if ($(this).prev().prev().nextAll().find('ul').size() > 0) {
                    $(this).prev().prev().height(height);
                }
                if ($(this).prev().nextAll().find('ul').size() > 0) {
                    $(this).prev().height(height);
                }
                if ($(this).nextAll().find('ul').size() > 0) {
                    $(this).height(height);
                }
            });
            var navHeight = $('#subNavInner').outerHeight();
            $('#subNav').stop().animate({
                height: navHeight
            }, 500, "easeOutQuint");
        }
    }, function () {
        $(this).removeClass('over');
        $('#subNav').stop().animate({
            height: 0
        }, 500, "easeOutQuint");
    });
    $('#subNav').hover(function () {
        var navHeight = $('#subNavInner').height();
        $('#subNav').stop().animate({
            height: navHeight
        }, 500, "easeOutQuint");
    }, function () {
        $('#subNav').stop().animate({
            height: 0
        }, 500, "easeOutQuint");
    });
}

function fader() {
    $('.fader').each(function () {
        if ($(this).find('li').size() > 1) {
            $(this).find('li:gt(0)').fadeTo(0, 0);
            var total = $(this).find('li').size();
            $('<ol class="faderControls"><li class="prev"><a href="#" title="Previous Item">Previous Item</a></li></ol>').insertAfter($(this));
            for (i = 0; i < total; i++) {
                $('.faderControls:last').append('<li class="btn"><a href="#" title="Item ' + (i + 1) + '">Item ' + (i + 1) + '</a></li>');
            }
            $('.faderControls:last').append('<li class="next"><a href="#" title="Next Item">Next Item</a></li>').find('.btn:first').addClass('on');
        }
    });
    $('.faderControls').each(function () {
        var $list = $(this);
        var $fader = $list.prev('.fader');
        var i = 0;
        function setFader(i) {
            $fader.find('li').stop().fadeTo(0, 0);
            $fader.css('margin-left', -(i * 357) + 'px');
            $fader.find('li:eq(' + i + ')').fadeTo(500, 1);
            $list.find('.on').removeClass('on');
            $list.find('.btn:eq(' + i + ')').addClass('on');
        }
        var intOn = true;
        var myInterval = setTimeout(nextFade, 12000);
        function nextFade() {
            if (intOn == true) {
                interval = setTimeout(nextFade, 8000);
            }
            i++;
            if (i >= $($list.find('li.btn')).size()) {
                i = 0;
            }
            setFader(i);
        }
        $(this).find('li.next a').click(function () {
            intOn = false;
            clearInterval(myInterval);
            i = $($list.find('li.btn')).index($list.find('li.on')) + 1;
            if (i >= $($list.find('li.btn')).size()) {
                i = 0;
            }
            setFader(i);
            return false;
        });
        $(this).find('li.prev a').click(function () {
            intOn = false;
            clearInterval(myInterval);
            i = $($list.find('li.btn')).index($list.find('li.on')) - 1;
            if (i <= -1) {
                i = $($list.find('li.btn')).size() - 1;
            }
            setFader(i);
            return false;
        });
        $(this).find('.btn a').click(function () {
            intOn = false;
            clearInterval(myInterval);
            i = $($list.find('li.btn')).index($(this).parent());
            setFader(i);
            return false;
        });
    });
}

function setHeights() {
    $('.homeBlock:even').each(function () {
        var height = $(this).height();
        var nextHeight = $(this).next('.homeBlock').height();
        if (nextHeight > height) {
            height = nextHeight
        }
        $(this).css('min-height', height);
        $(this).next('.homeBlock').css('min-height', height);
    });
}

function homeConcertina() {
    $('.rightNav > li:first').addClass('open');
    $('.rightNav > li:gt(0)').addClass('closed').find('ul').hide();
    $('.rightNav > li > a').click(function () {
        $('.rightNav .open').not($(this).parent()).removeClass('open').addClass('closed').find('ul').slideUp(600, "easeOutQuint", function () { ie6Fix() });
        $(this).parent().removeClass('closed').addClass('open').find('ul').slideDown(600, "easeOutQuint", function () { ie6Fix() });
        return false;
    });
}

function loginBoxes() {
    $('.gradBox label input').val('').addClass('js').fadeTo(0, 0).each(function () {
        $(this).focus(function () {
            $(this).fadeTo(0, 1);
        }).blur(function () {
            if ($(this).val().length == 0) {
                $(this).fadeTo(0, 0);
            }
        });
    });
    $('.searchBox input').val('Search').focus(function () {
        if ($(this).val() == 'Search') {
            $(this).val('');
        }
    }).blur(function () {
        if ($(this).val().length == 0) {
            $(this).val('Search');
        }
    });
    /*
    $('.searchBox .searchBtn').click(function(){
    window.location.href = "/search.aspx?searchfor=" + $('.searchBox input').val();
    return false;
    });*/
}

function banner() {
    var bannerNo = 0;
    if ($('.banner li').size() > 1) {
        $('.banner ul').addClass('js');
        $('.banner li:first').addClass('currentBanner');
        $('.banner li:gt(0)').hide();
    }
    setInterval(function () {
        bannerNo++;
        if (bannerNo > $('.banner li').size() - 1) {
            bannerNo = 0;
        }
        $('.banner li.currentBanner').removeClass('currentBanner');
        $('.banner li:eq(' + bannerNo + ')').addClass('currentBanner').fadeIn(500, function () {
            $('.banner li').not('.currentBanner').hide();
        });
    }, 8000);
}

function ie6Fix() {
    $('#btmWrap').hide().show();
}

$(function () {
    mainNav();
    banner();
    homeConcertina();
    setHeights();
    loginBoxes();
    fader();
    ie6Fix();
})

$(window).load(function () {
    setHeights();
    ie6Fix();
})

$(window).resize(function () {
    ie6Fix();
})

$(window).unload(function () { });
