$(document).ready(function(){
	$('.add-to-cart').submit(function(){
		var idsplits   = (this.id).split('-');
		var product_id = idsplits[3];
		var productX   = $("#product-image-" + product_id).offset().left;
		var productY   = $("#product-image-" + product_id).offset().top;
		var basketX    = $("#shop-cart-side").offset().left + $("#shop-cart-side").width()/2;
		var basketY    = $("#shop-cart-side").offset().top + 20;
		var url        = $(this).attr('action');
		var data       = $(this).serialize();
		
		var gotoX      = basketX - productX;
		var gotoY      = basketY - productY;
		
		var newImageWidth 	= $("#product-image-" + product_id).width() / 2;
		var newImageHeight	= $("#product-image-" + product_id).height() / 2;
		$.scrollTo("#shop-cart-side", {duration:1000, margin: true});
		$("#product-image-" + product_id)
		.clone()
		.prependTo("#product-wrap-item-" + product_id)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.05, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
			$(this).remove();
			$('#cart-loader').addClass('ajax-loader-cart').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
			
			$.ajax({  
				type: "POST",  
				url: url,
				dataType: 'json',
				data: data,
				success: function(theResponse) {
					if( $("#cart-item-" + product_id).length > 0){
						$("#cart-item-" + product_id).animate({ opacity: 0 }, 500);
						$("#cart-item-" + product_id).before(theResponse.new_item).remove();
						$("#cart-item-" + product_id).animate({ opacity: 0 }, 500);
						$("#cart-item-" + product_id).animate({ opacity: 1 }, 500);
						
					} else {
						$("#shop-cart-side-item li:last").before(theResponse.new_item);
						$("#shop-cart-side-item li:last").hide();
						$("#shop-cart-side-item li:last").show("slow"); 	
					}
					if ($('#cart-empty').length > 0)
					{
						$('#cart-empty').remove();
					}
					if ($('#cart-total').length > 0)
					{
						$("#cart-total").animate({ opacity: 0 }, 500);
						$("#cart-total").before(theResponse.total).remove();
						$("#cart-total").animate({ opacity: 0 }, 500);
						$("#cart-total").animate({ opacity: 1 }, 500);
					}
					else
					{
						$("#shop-cart-side-item li:last").after(theResponse.total);
						$("#shop-cart-side-item li:last").hide();
						$("#shop-cart-side-item li:last").show("slow"); 
					}
					
					$('#checkout-btn').show('slow');
					$('#cart-loader').removeClass('ajax-loader-cart');
				}  
			});
		});
		
		return false;
	});
	
	$('#shop-cart-side-item li a').live('click', function(event){
		var splits     = (this.id).split('-');
		var product_id = splits[2];
		
		$('#cart-loader').addClass('ajax-loader-cart').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		
		$.ajax({  
			type: "POST",  
			url: baseurl + 'shoppingcart/remove',  
			data: {product_id: product_id},
			success: function(theResponse){
				$("#cart-item-" + product_id).hide('slow', function(){
					$("#cart-item-" + product_id).remove();
				});
				
				if ($('#cart-total').length > 0)
				{
					$("#cart-total").animate({ opacity: 0 }, 500);
					$("#cart-total").before(theResponse).remove();
					$("#cart-total").animate({ opacity: 0 }, 500);
					$("#cart-total").animate({ opacity: 1 }, 500);
				}
				
				if ($('#shop-cart-side-item ul > li').length == 2)
				{
					var emp = $('<li id="cart-empty" class="center">Belum ada item<\/li>').hide();
					$("#cart-total").remove();
					$('#checkout-btn').hide();
					$(emp).appendTo('#shop-cart-side-item ul').fadeIn("slow");
				}
				
				$('#cart-loader').removeClass('ajax-loader-cart');
			}
		});
		
		return false;
	});
	
	$('#remove-all-item').click(function(){
		$('#cart-loader').addClass('ajax-loader-cart').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		
		$.ajax({  
			type: "POST",  
			url: baseurl + 'shoppingcart/remove',  
			data: {product_id: 'all'},
			success: function(theResponse){
				//if (theResponse == 'success')
				//{
					$("#shop-cart-side-item ul > li").hide('slow', function(){
						$(this).remove();
					});
					var emp = $('<li id="cart-empty" class="center">Belum ada item<\/li>').hide();
					$('#checkout-btn').hide();
					$(emp).appendTo('#shop-cart-side-item ul').fadeIn("slow");
				//}
				
				$('#cart-loader').removeClass('ajax-loader-cart');
			}
		});
		return false;
	});
	
	$('#testimonial-slider').slides({
			preload: false,
			generateNextPrev: false,
			generatePagination: false,
			play: 5000,
			pause: 3000,
			hoverPause: true,
			container: 'testimonial-entry',
			slideSpeed: 800,
			crossfade: true,
			effect: 'slide, fade'
	});
	
	$('.frontpage-item').equalHeights();
});

var fixSoldOut = function(){
	$('.sold-out').each(function(i,el){
		var pid = el.id.split('-')[2];
		
		var li = $('#product-image-'+pid).position().left;
		var ti = $('#product-image-'+pid).position().top;
		$(el).css({'left': li+'px', 'top': ti+'px'});
	});
}

$(window).load(fixSoldOut).resize(fixSoldOut);

