
$(function () {
    //Billboard Slideshow
    $('#slideshow').cycle({
        fx: 'fade',
        speed: 250,
        timeout: 3500,
        pause: 1
    });



    //Give the products a big hover state
    $(".featProduct a").bigTarget({
        hoverClass: 'over',
        clickZone: 'div:eq(0)'
    });

    $(".item a").bigTarget({
        hoverClass: 'over',
        clickZone: 'div:eq(0)'
    });



    //Slideshow
    $('#slideshow').css('overflow-y', 'hidden');

    //Specials slideshow
    var currentPosition = 0;
    var slideWidth = 220;
    var slides = $('.slide');
    var numberOfSlides = slides.length;

    $('#slidesContainer').css('overflow', 'hidden');

    slides.wrapAll('<div id="slideInner"></div>').css({
        'float': 'left',
        'width': slideWidth
    });

    $('#slideInner').css('width', slideWidth * numberOfSlides);

    $('#specialSlides').prepend('<span class="control" id="leftControl">Move left</span>').append('<span class="control" id="rightControl">Move right</span>');

    manageControls(currentPosition);

    $('.control').bind('click', function () {
        currentPosition = ($(this).attr('id') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;

        manageControls(currentPosition);
        $('#slideInner').animate({
            'marginLeft': slideWidth * (-currentPosition)
        });
    });

    function manageControls(position) {
        if (position == 0) {
            $('#leftControl').hide()
        }
        else {
            $('#leftControl').show()
        }
        if (position == numberOfSlides - 2) {
            $('#rightControl').hide()
            $('.slidesWrapper').hide()
        } else {
            $('#rightControl').show()
            $('.slidesWrapper').show()
        }
    }



    //Dropdowns

    timeout = 0;
    closetimer = 0;
    ddmenuitem = 0;

    function jsddm_open() {
        jsddm_canceltimer();
        jsddm_close();
        ddmenuitem = $(this).find('ul').css('visibility', 'visible');
    };

    function jsddm_close() {
        if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');
    };

    function jsddm_timer() {
        closetimer = window.setTimeout(jsddm_close, timeout);
    };

    function jsddm_canceltimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    };

    document.onclick = jsddm_close;

    //	Drop down list 

    listStyle = {	//	Gracefully degrade if user has js turned off
        'position': 'absolute',
        'visibility': 'hidden'
    }

    $('.dropDown li ul').css(listStyle);

    listelement = '.dropDown'; //	Define the droplist
    totalULs = $(listelement).length; //	Get the total number of UL's

    for (var i = 0; i < totalULs; i++) {	// For each dropDown list, bind the open/close events
        $(listelement).bind('mouseover', jsddm_open);
        $(listelement).bind('mouseout', jsddm_timer);
    }

    $(".selectLink, .dropDown ul li a").click(function (event) {	//	Change the default behavior of the arrow
        event.preventDefault();
        $(this).css('cursor', 'default');
    });

    /* Allow "Enter" key to submit current form. Since the entire page is wrapped in a .NET form 
    * this will prevent it from submitting the first form on the page by default.
    */
    $('input').keyup(function (e) {
        if (e.keyCode == 13) {
            $(this).siblings('input[type="image"],input[type="submit"]').click();
        }
    });

    /*
     * Allows embedded forms (mailing list signup, search forms) to use a 
     * second form even though the entire site is wrapped in an ASP.NET form.
     * Otherwise the child form element is removed by browsers.
     */
    $('.formSwitch input[type="image"]').click(function () {
        var form = $('#aspnetForm');
        var switchForm = $(this).parent('.formSwitch');
        form.attr("id", switchForm.attr("id"));
        form.attr("action", switchForm.attr("action"));
        form.attr("method", switchForm.attr("method"));
        form.attr("name", switchForm.attr("name"));

        return true;
    });

});
