/*--------------------------------------------------
Initialize
--------------------------------------------------*/
$(function () {
  
    //Visualizza carrello
    $('#u_cart >  a').bind('mouseover', function() {
        $('#u_cart').addClass('active');
		
            $.ajax({
                url: '/store_cart/summary',
		dataType: 'html',
		success: function(html) {
                    $('#u_cart #cart_box .content').html(html);
		}
            });			
		
            $('#u_cart').bind('mouseleave', function() {
                $(this).removeClass('active');
            });
    });     
        
        

    //Aggiunge prodotto al carrello (Ajax)
    $('.addForm').submit(function (event) {
        
        var productIDValSplitter = (this.id).split("_");
	var productIDVal 	 = productIDValSplitter[1];

	var productX             = $("#productImageWrapID_" + productIDVal).offset().left;
	var productY             = $("#productImageWrapID_" + productIDVal).offset().top;

	var basketX              = $("#u_cart").offset().left;
	var basketY              = $("#u_cart").offset().top;

	var gotoX                = basketX - productX;
	var gotoY                = basketY - productY;

	var newImageWidth        = $("#productImageWrapID_" + productIDVal).width() / 3;
	var newImageHeight       = $("#productImageWrapID_" + productIDVal).height() / 3;

	$("#productImageWrapID_" + productIDVal + " img:first")
	    .clone()
	    .prependTo("#productImageWrapID_" + productIDVal)
	    .css({'position' : 'absolute'})
	    .animate({opacity: 0.8}, 100 )
	    .animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 900, function() {
	        $(this).remove();
               
                $('#u_cart').addClass('active');
                $.ajax({
		    type: 'post',
		    url: '/store_cart/add',
		    dataType: 'html',
                    data: $('#addForm_' + productIDVal + ' :input'),
	       	        success: function (html) {
                            $('#cart_box .content').animate({opacity: 0}, 500,function(){
		                $('#cart_box .content').html(html);
                            });
                            $('#cart_box .content').animate({opacity: 1}, 500);

                            setTimeout(function(){
                                $("#u_cart").delay(80000).removeClass('active');
                            },4000);
                            
			}
		});

	    });
        event.preventDefault();
    });   
    
    
    
    //Remuove prodotto dal carrello (Ajax)
    $('.cart_remove').live('click', function () {
        //if (!confirm('Confermi la rimozione del prodotto dal carrello?')) {
	//    return false;
	//}
	$(this).removeClass('cart_remove').addClass('cart_remove_loading');
	$.ajax({
	    type: 'post',
	    url: '/store_cart/remove',
	    dataType: 'html',
	    data: 'productId=' + this.id,
	    success: function (html) {
                $('#cart_box .content').animate({opacity: 0}, 500,function(){
	            $('#cart_box .content').html(html);
                });
                $('#cart_box .content').animate({opacity: 1}, 500);
	    }
	});
    });    
    
    //prevent confirm double submit;
    var counter = 1; 
    $('#confirm').submit(function() {
        if(counter > 1) return false;
        $('.button span').removeClass().css({background: 'none', opacity: '10'})
        .append('<img id="theImg" src="/frontend/img/loading/loading16x16.gif" />');
        counter++;
    });

});






