$(document).ready(function(){
	
	//display large images:
	$("a.fancybox").fancybox({titleShow: false, cyclic: true});
	$('.product-image').image_zoom($('#zoom-container'));
	$('#product-main-image').parent('a').fancybox({titleShow: false});
	//rollovers:
	$('.product-thumbs img').each(function() {
		
		//preload larger images: 
		var new_url = $(this).parent().attr('href');//.replace('_300', '_700');
		$.preloadImages(new_url);
		
		var main_image = $('#product-main-image');
		$(this).hover(function() {
			$(this).css({cursor: 'pointer', position: 'relative', top: '-2px'});
			main_image.attr('src', new_url);
			main_image.parent('a').attr('href', new_url).fancybox({titleShow: false});
		}, function () {
			$(this).css('top', '0');
		});
		
	});
	
	//add to cart:
	$('#add-to-cart').validate({
		submitHandler: function (form) {
			$(form).ajaxSubmit({
				url: $(form).attr('action') + '.json',
				dataType: 'json',
				success: function (data) {
					if (data.error.length == 0) {
						$('#feedback').html('<p class="highlight">item added<br /><br /><a class="button checkout" href="/view-bag">view bag &raquo;</a></p>').fadeIn('fast');
						//no error:
						var count = parseInt($('#cart-count').html()) + 1;
						if (isNaN(count)) count = 1;
						update_cart_count(count);
					}
				}
			});
			return false;
		}
	});

    // adjust product nav if this is a 'you may also like' clickthrough
    if (document.location.hash == '#also-like') {
        $('#product-nav-prev').replaceWith('<img src="/images/spacer.gif" id="product-nav-spacer"  width="47" height="24" />');
        $('#product-nav-next').replaceWith('<img src="/images/spacer.gif" id="product-nav-spacer"  width="47" height="24" />');
    }

    //email to a friend
    $("#email_friend").fancybox({
        'scrolling'        : 'no',
        'titleShow'        : false,
        'onClosed'        : function() {
            $("#error_message").hide();
        }
    });

    $("#email_friend_form").bind("submit", function() {

        if ($("#to_email").val().length < 1 || $("#from_email").val().length < 1) {
            $("#error_message").show();
            $.fancybox.resize();
            return false;
        }

        $.fancybox.showActivity();

        $.ajax({
            type        : "POST",
            cache    : false,
            url        : "/email-to-friend",
            data        : $(this).serializeArray(),
            success: function(data) {
                $.fancybox(data);
            }
        });

        return false;
    });
	
});
