$(document).ready(function() {

// Search

	$('.button-search').bind('click', function() {

		url = $('base').attr('href') + 'shop/index.php?route=product/search';		 

		var filter_name = $('input[name=\'filter_name\']').attr('value')

		if (filter_name) {

			url += '&filter_name=' + encodeURIComponent(filter_name);

		}

		location = url;

	});

	

	$('#header input[name=\'filter_name\']').keydown(function(e) {

		if (e.keyCode == 13) {

			url = $('base').attr('href') + 'shop/index.php?route=product/search';	 

			var filter_name = $('input[name=\'filter_name\']').attr('value')

			if (filter_name) {

				url += '&filter_name=' + encodeURIComponent(filter_name);

			}

			location = url;

		}

	});

	

// Ajax Cart

	$('#cart > .heading a').bind('click', function() {

		$('#cart').addClass('active').find('.content').addClass('contentActive');

		$.ajax({

			url: 'shop/index.php?route=checkout/cart/update',

			dataType: 'json',

			success: function(json) {

				if (json['output']) {

					$('#cart .content').html(json['output']);

				}

			}

		});			

		

		$('#cart').bind('mouseleave', function() {

			$(this).removeClass('active').children('.content').removeClass('contentActive');

		});

	});

	

	$('.success img, .warning img, .attention img, .information img').live('click', function() {

		$(this).parent().fadeOut('slow', function() {

			$(this).remove();

		});

	});

});



function addToCart(product_id) {

	$.ajax({

		url: 'shop/index.php?route=checkout/cart/update',

		type: 'post',

		data: 'product_id=' + product_id,

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, .information, .error').remove();

			

			if (json['redirect']) {

				location = json['redirect'];

			}

			

			if (json['error']) {

				if (json['error']['warning']) {

					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="shop/catalog/view/theme/kick/image/close.png" alt="" class="close" /></div>');

					$('.warning').fadeIn('slow');			

					$('html, body').animate({ scrollTop: 0 }, 'slow');

				}

			}	 

						

			if (json['success']) {

				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="shop/catalog/view/theme/kick/image/close.png" alt="" class="close" /></div>');

				$('.success').fadeIn('slow');

				$('#cart_total').html(json['total']);

				$('html, body').animate({ scrollTop: 0 }, 'slow'); 

			}	

		}

	});

}



function removeCart(key) {

	$.ajax({

		url: 'shop/index.php?route=checkout/cart/update',

		type: 'post',

		data: 'remove=' + key,

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, .information').remove();

			

			if (json['output']) {

				$('#cart_total').html(json['total']);

				$('#cart .content').html(json['output']);

			}			

		}

	});

}



function removeVoucher(key) {

	$.ajax({

		url: 'shop/index.php?route=checkout/cart/update',

		type: 'post',

		data: 'voucher=' + key,

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, .information').remove();

			

			if (json['output']) {

				$('#cart_total').html(json['total']);

				

				$('#cart .content').html(json['output']);

			}			

		}

	});

}



function addToWishList(product_id) {

	$.ajax({

		url: 'shop/index.php?route=account/wishlist/update',

		type: 'post',

		data: 'product_id=' + product_id,

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, .information').remove();

						

			if (json['success']) {

				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="shop/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

				

				$('.success').fadeIn('slow');

				

				$('#wishlist_total').html(json['total']);

				

				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				

			}	

		}

	});

}



function addToCompare(product_id) { 

	$.ajax({

		url: 'shop/index.php?route=product/compare/update',

		type: 'post',

		data: 'product_id=' + product_id,

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, .information').remove();

						

			if (json['success']) {

				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="shop/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

				

				$('.success').fadeIn('slow');

				

				$('#compare_total').html(json['total']);

				

				$('html, body').animate({ scrollTop: 0 }, 'slow'); 

			}	

		}

	});

}



$('#button-cart').bind('click', function() {

	$.ajax({

		url: 'shop/index.php?route=checkout/cart/update',

		type: 'post',

		data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),

		dataType: 'json',

		success: function(json) {

			$('.success, .warning, .attention, information, .error').remove();

			

			if (json['error']) {

				if (json['error']['warning']) {

					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="shop/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

				

					$('.warning').fadeIn('slow');

				}

				

				for (i in json['error']) {

					$('#option-' + i).after('<span class="error">' + json['error'][i] + '</span>');

				}

			}	 

						

			if (json['success']) {

				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="shop/catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

					

				$('.success').fadeIn('slow');

					

				$('#cart_total').html(json['total']);

				

				$('html, body').animate({ scrollTop: 0 }, 'slow'); 

			}	

		}

	});

});		
