$(document).ready(function() {

    /* ------------- Proxy links -------------- */
    // When the link is pressed it "clicks" the button.
    // The initial HTML looks like this:
    // 
    //    <input class="proxy" type="submit" value="Save">
    // 
    // After jQuery has done it's thing it look like this:
    // 
    //    <input type="submit" class="proxy" value="Save" style="display:none">
    //    <a href="#" class="proxy">Save</a>
    $('input.proxy').each(function() {
        var input = $(this);
        $('<p><a href="#">' + this.value + '</a></p>')
                       .attr('class', this.className)
                       .insertBefore(this)
                       .click(function() {
                           input.click();
                           return false;
                       });
        input.hide();
    });


    /* ------------- Hover states -------------- */
    /* Header - hover over '0 items' highlights 'shopping bag' text*/
    $('.no-items').hover(
		function() {
		    $('.s-bag').addClass('white-text');

		}, function() {
		    $('.s-bag').removeClass('white-text');
		});

    // do the same for Look to buy - hover over view details, highlight title and price
    $('.look-to-buy .product-single .details-link').hover(
		function() {
		    $(this).siblings('h4').addClass('white-text');
		    $(this).siblings('.price').addClass('white-text');
		}, function() {
		    $(this).siblings('h4').removeClass('white-text');
		    $(this).siblings('.price').removeClass('white-text');
		});

    /* ------------- Homepages -------------- */
    //hide product details
    $("#main-season .details").hide();
    $("#secondary-season .details").hide();

    //on hover, display product details	
    $("#main-season a#main-season-image").hover(
		function() {
		    $(this).children('.details').fadeIn('normal');
		},
		function() {
		    $(this).children('.details').fadeOut('normal');
		});

    $("#secondary-season a#secondary-season-image").hover(
		function() {
		    $(this).children('.details').fadeIn('normal');
		},
		function() {
		    $(this).children('.details').fadeOut('normal');
		});

    //Hover over images, to give chevron rollovers 
    $("#main-season .sidebar-promos a.promo-image").hover(
		function() {
		    $(this).parent().children('p').children('a').addClass('hover');
		},
		function() {
		    $(this).parent().children('p').children('a').removeClass('hover');
		});


    /* ------------- Reveal hidden content -------------- */
    /* Search - reveal search box */
    $('#search').hide();
    $('.search-results').show();

    $("a#nav-search").toggle(function() {
        $('#search').slideDown('slow');
        $('.search-results').slideUp('slow');
    }, function() {
        $('#search').slideUp('slow');
        $('.search-results').slideDown('slow');
    });
    $('a#search-close').click(function() {
        $('#search').slideUp('slow');
        return false;
    });

    /* Add personalised message - click checkbox, reveal textarea*/
    $('textarea.pm').hide();
    $('input.pm').removeAttr("checked");

    $('input.pm').click(function() {
        if ($(this).attr("checked")) {
            $(this).siblings('.pm').show('slow');

        } else {
            $(this).siblings('.pm').hide('slow');
        }
    });

    /* ------------- Product list pages -------------- */
    //hide product details
    $(".list-page .details").hide();

    // BUG : Animation in FF Mac causes rendering modes to flicker
    //http://groups.google.com/group/jquery-en/browse_thread/thread/fca06b2e9d596b3e/
    //$("body").css('opacity','0.999');

    // on hover, display product details
    $("#main-content .list-page li").hover(
		function() {
		    $(this).children('.detailswrapper').show();
		    $(this).children('.detailswrapper').children('.details').fadeIn();
		    $(this).children('a').children('.details').fadeIn('normal');
		},
		function() {
		    $(this).children('.detailswrapper').children('.details').fadeOut();
		    $(this).children('a').children('.details').fadeOut('normal');
		});

    /* ------------- Homepages -------------- */
    /* Hide 'GO' input fallback */
    $("#main-overlay .proxy-simple").hide();

    /* Jump menu */
    function jump(targ, selObj, restore) {
        eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
        if (restore) selObj.selectedIndex = 0;
    }

    /* Sale bar hover state */
    $("#sale-overlay").hover(
		function() {
		    $(this).css('background-position', 'top left');
		}, function() {
		    $(this).css('background-position', 'bottom left');
		});

    /* ------------- Product page -------------- */
    /* Reveal Scene7 zoom functionality */
    $('#product-image-wrapper #ensure-content-width').hide(); // hide div to force content if js is disabled
    $('#product-image-wrapper #zoom-in').show();
    $('#product-image-wrapper #click-to-reset').show();

    /* ------------- Store locator -------------- */
    /* Hide fallback map images */
    $('#map-canvas img').hide();

});
