
$(document).ready(function(){

	$('a.add_to_cart').click(function(event) {
		event.preventDefault();
		var hostname = window.document.location.hostname;

		var $add = $( this ),
			id = $add.attr( 'id' ),
			$img = $add.find('img'),
			src = $img.attr('src'),
			qty = '1',
			action = 'add';
		
		$img.attr({ src: 'http://' + hostname + '/modules/plugins/cart/img/loader.gif' });
		$('.item-block-content').css('opacity','0.5');
		
		$.post( 'index.php?page=cart_handler', { action: action, id: id, qty: qty, ajax: 'true'} ,
						function(res) {
							if(res){	
								$img.attr({ src: 'http://' + hostname + '/modules/plugins/cart/img/added.png' });
								$('.item-block-content').css('opacity','1');
											
								if($('#supperAjaxCart').css('display') == 'none') {		
									$('.tipVisible').trigger('click');
								}	
								$('#supperAjaxCart').html(res);							
								$.get('index.php?page=cart_handler', { ajax: 'true'}, function(res) {$('#supperAjaxCart').html(res);} );
																	
								$add.delay(1000).queue(function() {
									$img.attr({ src: src });
								});
								
							}else{
								$img.attr({ src: 'http://' + hostname + '/modules/plugins/cart/img/denied.png' });
								$('.item-block-content').css('opacity','1');
								$add.delay(1000).queue(function() {
									$img.attr({ src: src });
								});
							}
					}
				);
	});

	$('#cart_quantity').submit(function(event) {
			event.preventDefault();
			
			var $form = $( this ),
				qty = $form.find( 'input[name="products_qty"]' ).val(),
				id = $form.find( 'input[name="products_id"]' ).val(),
				atr = '',
				$btn = $form.find( 'input[type="image"]' ),
				src = $btn.attr('src'),
				action = 'add';
			
			$form.find('input[name="id"]').each(function(){
				atr += $(this).val();
			});
			
			$form.find('textarea[name="id"]').each(function(){
				atr += $(this).val();
			});
			
			$form.find('select[name="id"]').each(function(){
				atr += $(this).val();
			});
			
			$btn.attr({ src: '/modules/plugins/cart/img/loader.gif' });
			$form.css('opacity','0.5');
			
			$.post( 'index.php?page=cart_handler', { action: action, id: id, qty: qty, atr: atr, ajax: 'true'} ,
							function(res) {
								if(res){	
									$btn.attr({ src: '/modules/plugins/cart/img/added.png' });
									$form.css('opacity','1');
									
									if($('#supperAjaxCart').css('display') == 'none') {		
										$('.tipVisible').trigger('click');
									}
									$('#supperAjaxCart').html(res);							
									$.get('index.php?page=cart_handler', { ajax: 'true'}, function(res) {$('#supperAjaxCart').html(res);} );
									
									$form.delay(1000).queue(function() {
										$btn.attr({ src: src });
									});
									
								}else{
									$btn.attr({ src: '/modules/plugins/cart/img/denied.png' });
									$form.delay(1000).queue(function() {
										$btn.attr({ src: src });
									});
								}
						}
					);
  	});
	
	$('a.remove_from_cart').live('click', function(event) {
			event.preventDefault();
			var hostname = window.document.location.hostname;

			var $remove = $( this ),
				id = $remove.attr( 'id' ),
				url = $remove.attr( 'href' ),
				action = 'remove';
			
			$remove.css('background','url("../img/loader.gif") no-repeat center center');
			$('.product_in_cart').css('opacity','0.5');
			
			$.post('index.php?page=cart_handler', { action: action, id: id, ajax: 'true'} ,
							function(res) {
								if(res){
									$('#supperAjaxCart').html(res);
								}	
						}
					);
	});
});
