function changeZoomImage(small, large) {
	MagicZoom_stopZooms();
	var zoomLink = document.getElementById('zoom1');
	zoomLink.href = large;
	$('#zoom1').attr('class','initZoom');
	zoomLink.innerHTML = '<img src="' + small + '"/>';
}

$(document).ready(function () {
	
    $("#header-wrap").pngFix();
    
   	$('#sizeguide-pop').jqm({ toTop : true, overlay : 50});
    $('#added-to').jqm({toTop:true, overlay: 1});

    // Sizeguide popup
    $('a.product-guide').click(function(){
    	prodWidth = $("#sizeguide-pop").outerWidth();
    	$left = (($(window).width()-(prodWidth+22))/2);
   		$("#sizeguide-pop").css('left', $left);
    	$("#sizeguide-pop").css('width', (prodWidth-22));
    	$('#sizeguide-pop').jqmShow();
		return false;
    })
    // How many milliseconds until the modal window closes.
    var modal_hide_timeout = 4000;

   $.displayAddConfirm = function() {
        leftBuy = (($(window).width()-962)/2)+(962-198);
        $("#added-to").css('left', leftBuy);
    	$('#added-to').jqmShow();
    	return false;
    };
    $.raiseProductError = function(msg) {
        var errors = {};
        var pname = (($("#name").html().length > 0) ? $("#name").html() : 'this product');
        errors['stock'] = "Sorry, this product is out of stock.";
        errors['system'] = "Due to a system error, we were unable to add " + pname + " \
                            to your basket. Please try again or contact us on \
                            +44 (0)845 539 2109. Sorry for any inconvenience.";
        errors['size'] = "Please select a size.";
        $("#add_error").fadeIn("fast");
        $("#add_error").html(errors[msg]);
    };
    $('.add-product input').click(function() {
        var skuproduct = false;
        var e = false;
        
        if($("#id_size").length > 0) {
            var p = $("#id_size").val();
            skuproduct = true;
        }
        else {
            var p = $("#productname").val();
        }
    	
    	if(p.substring(0, 2) == 'xx') {
    	    e = true;
	        $.raiseProductError('stock');
    	}
    	if(p.length == 0) {
    	    e = true;
    	    $.raiseProductError('size');
    	}
    	
    	if(e == false) {
    	    $.post('/shop/cart/add/ajax/', { 'productname': p, 'quantity': 1, 'skuproduct': skuproduct }, function(i) {
    	        var data = JSON.parse(i);
    	        if(data['results'] == 'Error') {
    	            $.raiseProductError('system');
    	        }
    	        else {
    	            $("#add_error").hide();
    	            $.displayAddConfirm();
    	            $("#add_name").html(data['name']);
    	            var plural = ((data['cart_count'] == 1) ? '' : 's');
    	            $("#top-basket a").html('Shopping Bag ' + data['cart_count'] + ' item' + plural);
    	            $("#add_price").html('£' + data['unit_price']);
    	            $("#add_size").html(data['size']);
    	            setTimeout('$("#added-to").jqmHide();', modal_hide_timeout);
    	        }
    	    });
    	}
	    return false;
    })
    
    $(window).resize(function(){
    	$('#added-to').jqmHide(); 
    })
    
   	/* magic zoom */
	MagicZoom_stopZooms();

	$('.initZoom').click(function(){
		$(this).attr('class','MagicZoom');
		MagicZoom_findZooms();
		return false;
	})
	/* end magic zoom */
	
	/* Image swap */
	$("#product-images ul li a").click(function(){
		MagicZoom_stopZooms();
		$large = $(this).attr('href');
		$medium = $(this).attr('rel');

		$("#zoom1").attr({'href' : $large });
	    $("#zoom1 img").attr({'src' : $medium });
	    return false;
	});
});

