﻿$(document).ready(function() {

    $("ul.topnav li a").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        //$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
        $(this).parent().find("ul.subnav").fadeIn('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function() {
        }, function() {
            $(this).parent().find("ul.subnav").fadeOut('fast'); //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function() {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    //PROMO SLIDER

    totalPromo = $('#promo_slider div').size();
    xPos = 0;
    currentPromo = 1;
    moose = true;
    autoMoose = true;
    promoDelay = 7000;
    easeIn = 'easeInBack';
    easeOut = 'easeOutBack';
    inSpeed = 400;
    outSpeed = 400;

    $('#promo_nav .next a').click(function() {
        if (moose == true && totalPromo > currentPromo) {
            xPos = xPos - 925;
            currentPromo = currentPromo + 1;
            slideTo(xPos, 'right');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ') a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 2) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
        } else if (moose == true) {
            currentPromo = 1;
            infiniteLoop('right');
            $('#bannerRotationFrame div:eq(0) a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            $('#bannerRotationFrame div:eq(' + (totalPromo - 1) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
        }
        return false;
    });

    $('#promo_nav .prev a').click(function() {
        if (moose == true && currentPromo > 1) {
            xPos = xPos + 925;
            currentPromo = currentPromo - 1;
            slideTo(xPos, 'left');
        } else if (moose == true) {
            currentPromo = totalPromo;
            infiniteLoop('left');
        }
        $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ')').html('<img src="images/bannerRotationButton_up.png" border="0" />');
        return false;
    });

    $('#bannerRotationFrame div:eq(0) a').click(function() {
        if (currentPromo != 1) {
            xPos = 0;
            slideTo(xPos, 'left');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
            $('#bannerRotationFrame div:eq(0) a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            currentPromo = 1;
            $.clearTimer(promoTimer);
        }
        return false;
    });

    $('#bannerRotationFrame div:eq(1) a').click(function() {
        if (currentPromo == 3) {
            xPos = -925;
            slideTo(xPos, 'left');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
            $('#bannerRotationFrame div:eq(1) a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            $.clearTimer(promoTimer);
        } else if (currentPromo == 1) {
            xPos = -925;
            slideTo(xPos, 'right');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
            $('#bannerRotationFrame div:eq(1) a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            $.clearTimer(promoTimer);
        }
        currentPromo = 2;
        return false;
    });

    $('#bannerRotationFrame div:eq(2) a').click(function() {
        if (currentPromo != 3) {
            xPos = 2 * -925;
            slideTo(xPos, 'right');
            $('#bannerRotationFrame div:eq(' + (currentPromo - 1) + ') a').html('<img style="margin-top:1px" src="images/bannerRotationButton_down.png" border="0" />');
            $('#bannerRotationFrame div:eq(2) a').html('<img src="images/bannerRotationButton_up.png" border="0" />');
            currentPromo = 3;
            $.clearTimer(promoTimer);
        }
        return false;
    });

    function infiniteLoop(dir) {

        moose = false;

        totalX = 925 - (925 * totalPromo);

        if (dir == 'left') {
            $('#promo_slider').prepend('<div class="promo_content">' + $('#promo_slider div.promo_content:last').html() + '</div>').css({ left: -925 }).animate({ left: 419 }, inSpeed, easeIn, function() {
                $(this).animate({ left: 0 }, outSpeed, easeOut, function() {
                    $('#promo_slider').css({ left: totalX });
                    $('#promo_slider div.promo_content:first').remove();
                    moose = true;
                })
            });
            xPos = totalX;
        } else if (dir == 'right') {
            $('#promo_slider').append('<div class="promo_content">' + $('#promo_slider div.promo_content:first').html() + '</div>').animate({ left: totalX - 419 }, inSpeed, easeIn, function() {
                $(this).animate({ left: totalX - 925 }, outSpeed, easeOut, function() {
                    $('#promo_slider').css({ left: 0 });
                    $('#promo_slider div.promo_content:last').remove();
                    moose = true;
                });
            });
            xPos = 0;
        }
    }

    $('#promo_slider').mouseenter(function() {
        $.clearTimer(promoTimer);
    });
    $('#promo_slider').mouseleave(function() {
        autoMagical();
    });

    var promoTimer = {};

    function autoMagical() {
        promoTimer = $.timer(promoDelay, function() {
            $.clearTimer(promoTimer);
            $('#promo_nav .next a').click();
            autoMagical();
        });
    }

    autoMagical();

    function slideTo(newX, dir) {
        moose = false;
        if (dir == 'left') {
            halfX = newX - 419;
        } else {
            halfX = newX + 419;
        }
        $('#promo_slider').animate({ left: halfX }, inSpeed, easeIn, function() {
            $(this).animate({ left: newX }, outSpeed, easeOut, function() {
                moose = true;
            });
        })
    }

    try {
        $('#promo_slider').touchwipe({
            wipeLeft: function() { $.clearTimer(promoTimer); $('#promo_nav .next a').click(); autoMagical(); },
            wipeRight: function() { $.clearTimer(promoTimer); $('#promo_nav .prev a').click(); autoMagical(); },
            min_move_x: 20,
            preventDefaultEvents: true
        });
    } catch (err) { }

});

/*
* jQuery Timer Plugin
* http://www.evanbot.com/article/jquery-timer-plugin/23
*
* @version      1.0
* @copyright    2009 Evan Byrne (http://www.evanbot.com)
*/

jQuery.timer = function(time, func, callback) {
    var a = { timer: setTimeout(func, time), callback: null }
    if (typeof (callback) == 'function') { a.callback = callback; }
    return a;
};

jQuery.clearTimer = function(a) {
    clearTimeout(a.timer);
    if (typeof (a.callback) == 'function') { a.callback(); };
    return this;
};

