function miniCart(args)
{

	increaseQty = function(id)
	{
		qtyBox = jQuery('#qty-'+id);
		
		newValue = parseInt(qtyBox.val()) + 1;
		qtyBox.val(newValue);
		
		updateCart(id);
	},
	
	decreaseQty = function(id)
	{
		qtyBox = jQuery('#qty-'+id);
		currentVal = parseInt(qtyBox.val());

		if (currentVal > 1) {
			newValue = currentVal - 1;
			qtyBox.val(newValue);		
			
			updateCart(id);
		}
	},

	updateCart = function(productId)
	{
		var qtyBox 		= jQuery('#qty-'+productId);		
		var loaderBox 	= jQuery('#cart-mini-loader-'+productId);
		var qty			= qtyBox.val();
		var	messageBlock = jQuery('#message-'+productId);

		loaderBox.show();
		
		jQuery.ajax({
			url: args.updateUrl,
			data: 'id='+productId+'&qty='+qty,
			dataType: 'json',
			complete: function(response) {	
			
				json = jQuery.parseJSON(response.responseText);
				
				if (json.messages !== undefined) {
					messageBlock.html(json.messages).slideDown(500);
				} else {	
					qtyBox.val(json.qty);

					_parseReply(json);

					if (messageBlock.is(':visible')) {
						messageBlock.slideUp(500);
					}					
				}
				loaderBox.hide();
			}
		});
	},
	
	
	removeItem = function(productId)
	{
		var	messageBlock = jQuery('#message-'+productId);
		
		jQuery('#cart-remove-'+productId).hide();
		jQuery('#cart-small-loader-'+productId).show();
		
		jQuery.ajax({
			url: args.removeUrl,
			data: 'id='+productId,
			complete: function(response) {
				
				json = jQuery.parseJSON(response.responseText);
				
				if (json.messages !== undefined) {
					messageBlock.html(json.messages).slideDown(500);
					jQuery('#cart-small-loader-'+productId).hide();
					jQuery('#cart-remove-'+productId).show();
				} else {		
					jQuery('#cart-product-'+productId).fadeOut(1000);					
					_parseReply(json);
					
					if (messageBlock.is(':visible')) {
						messageBlock.slideUp(500);
					}							
				}
			}
		});
	},
	
	getCrosssell = function()
	{  
		crossEl = jQuery('#cart-crosssell');

		if (crossEl.is(':hidden')) {
			miniCartWidth = jQuery('.header-cart').width();
			leftPos = parseInt(crossEl.css('left')) + 15 - parseInt(miniCartWidth);
			crossEl.show().animate({
				left: leftPos + "px"
			});
		}

		jQuery('#cart-crosssell > *[id!="crosssell-loader"]').remove();
		jQuery('#crosssell-loader').show();			
	
		jQuery.ajax({
			url: args.crosssellUrl,
			//data: 'id='+productId,
			dataType: 'html',
			complete: function(response) {
				jQuery('#crosssell-loader').hide();
				crossEl.append(response.responseText);
			}
		});
	
	},
	
	_parseReply = function(json)
	{
		jQuery('.sum span').html(json.priceTotal);
		
		if (json.freeShipping == '1' && jQuery('#buy-more-block').is(':visible')) {
			jQuery('#buy-more-block').fadeOut(1000);
			jQuery('#free-shipping').fadeIn(1000);
		} else if (json.freeShipping == '0' && jQuery('#free-shipping').is(':visible')) {
			jQuery('#free-shipping').fadeOut(1000);
			jQuery('#buy-more-block').fadeIn(1000);
			jQuery('#buy-x-more').html(json.buyMore);
		} else if (json.freeShipping == '0') {
			jQuery('#buy-x-more').html(json.buyMore);
		}
		
		jQuery('#cart-no').html(json.summaryQty);
		//cufonReplaceAll();	
	}
	
	jQuery(document).ready( function() {
		
		jQuery('.support').hoverIntent( 
			function() { 
				jQuery('.header-popup').hide();
				jQuery('#shadow-overlay').show();
				jQuery('.support-dropdown').show();
			},
			function() {}
		);
		
		jQuery('.support-dropdown').mouseleave(overlayShutdown);	
		jQuery('#shadow-overlay').click(overlayShutdown);		
			
		jQuery('.wishlist').hoverIntent ( 
			function() { 
				jQuery('.header-popup').hide();
				jQuery('#shadow-overlay').show();
				jQuery('.block-wishlist').show();
			},
			function() {}
		);
		
		jQuery('.block-wishlist').mouseleave(overlayShutdown);
		
		jQuery('.top-cart').hoverIntent ( 
			function() { 
				jQuery('.header-popup').hide();
				jQuery('#shadow-overlay').show();
				jQuery('.header-cart').show();
				jQuery('.header-cart').css("visibility","visible");
				miniCartPos = jQuery('.header-cart').position();
				jQuery('#cart-crosssell').css('left',parseInt(miniCartPos.left) - 5 + "px");
				jQuery('#cart-crosssell').css('top',parseInt(miniCartPos.top) + 18 + "px");

			},
			function() {}
		);
		
		jQuery('#close-button').click(overlayShutdown);
		
		/* dynamiccart integration */
		
		document.observe('dynamiccart:update', function(e) {

			var variantWarning = jQuery('#product-choose-variant');

			if (e.memo.status == 'validation-error') {
				if (variantWarning.is(':visible')) {
					variantWarning.effect("pulsate", { times:2 }, 350);
				} else {
					variantWarning.show('fast');
				}		
			}
			else if (e.memo.status == 'success') {
				if (variantWarning.is(':visible')) {
					variantWarning.hide('fast');
				}	
				
				jQuery('#loadingmask').show();
				jQuery.get('/various/cart/rebuild/', function(data) {
					jQuery('.header-cart').html(data);
					jQuery('#loadingmask').hide();					
					jQuery('#shadow-overlay').show();
					jQuery('.header-cart').show();
					jQuery('.header-cart').css("visibility","visible");
					jQuery('.scroll-pane').jScrollPane({
						verticalDragMinHeight: 32,
						verticalDragMaxHeight: 32,
						horizontalDragMinWidth: 10,
						horizontalDragMaxWidth: 10
					});
					jQuery('#close-button').click(overlayShutdown);
					jQuery('.header-cart li').first()
						.effect("highlight", {'color':'LightGreen'}, 3000);
					setTimeout(overlayShutdown,3000);
				});
				
				jQuery.get('/various/cart/count/', function(data) {
					var json = jQuery.parseJSON(data);
					jQuery('#cart-no').html(json.cart);
					
					/* crosssells part */
					if (parseInt(json.crosssell) > 0) {
						jQuery('.mini-cart-item').mouseover(function() {
							if (jQuery('#cart-crosssell').is(':hidden')) {
								getCrosssell();
							}
						});
					}				
					/* crosssells part */
					
				});				
			}
			else {
				var message = e.memo.message.split("\n");
				variantWarning.html(message[0]);
				variantWarning.show('fast');
				jQuery('#messages_product_view').hide();
			}

		});
		
	});

}


function miniWishlist(args)
{

	removeWishlistItem = function(productId)
	{
		jQuery('#wishlist-remove-'+productId).hide();
		jQuery('#wishlist-small-loader-'+productId).show();

		jQuery.ajax({
			url: args.removeUrl,
			data: 'id='+productId,
			complete: function(response) {
				jQuery('#wishlist-product-'+productId).fadeOut(1000);
				jQuery('#wishlist-no').html("("+response.responseText+")");
				//cufonReplaceAll();	
			}
		});
	}
}

function swichSideMenu(id)
{
	jQuery('.sidenavs').hide();
	jQuery('#sidenav-'+id).show();
}

function overlayShutdown()
{
	jQuery('.header-popup').hide();
	jQuery('#shadow-overlay').hide();
}

