$(document).ready(function () {

	/* Search */
	$('.button-search').bind('click', function() {
		url = $('base').attr('href') + '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;
	});
	
	$('#content-top input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') + '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').hover(function() {
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});			
		
		$(this).find('.content').css("display", "block");
		
		$('#cart').bind('mouseleave', function() {
			$(this).find('.content').css("display", "none");
		});
		
	});

	 if ($.browser.msie && ($.browser.version == 8 || $.browser.version == 7 || $.browser.version == 6)) {
	 }
	 else {
	 
	$(".box-product > div").hover(
  function () {
 
   $(this).siblings().stop().animate({
  opacity: .8

}, 500)
  },
  function () {
  
      $(this).siblings().stop().animate({
  opacity: 1

}, 500)
   
  }
);  
  	$(".product-grid").hover(
  function () {
	 
   $(this).siblings().stop().animate({
  opacity: .8
}, 500)
  },
  function () {
  
      $(this).siblings().stop().animate({
  opacity: 1
}, 500)
   
  }
);
  	
	 }
	 
	// Animation for the languages and currency dropdown
	$('.switcher').hover(function() {
	$(this).find('.option').stop(true, true).slideDown(300);
	},function() {
	$(this).find('.option').stop(true, true).slideUp(150);
	}); 
		 		 
	/* Categories */
		 
	$("#categories > ul > li ").hover(function () {

		if ($.browser.msie && ($.browser.version == 8 || $.browser.version == 7 || $.browser.version == 6)) {
		
			$(this).find("ul.sub-menu").eq(0).show();
		
	 	} else {
		
			$(this).find("ul.sub-menu").eq(0).slideToggle(300);
		
		}
								
	},function () {

		if ($.browser.msie && ($.browser.version == 8 || $.browser.version == 7 || $.browser.version == 6)) {
		
			$(this).find("ul.sub-menu").eq(0).hide();		
		
		} else {
	
			$(this).find("ul.sub-menu").eq(0).slideToggle(300);
		
		}
		

	}); 
	
		$("#categories > ul > li > ul.sub-menu > li ").hover(function () {

			if ($.browser.msie && ($.browser.version == 8 || $.browser.version == 7 || $.browser.version == 6)) {
			
		  		$(this).find("ul.sub-menu").eq(0).show();
			
			} else {

		  		$(this).find("ul.sub-menu").eq(0).slideToggle(300);
			
			}

		},function () {
		
			if ($.browser.msie && ($.browser.version == 8 || $.browser.version == 7 || $.browser.version == 6)) {
			
				$(this).find("ul.sub-menu").eq(0).hide();
			
			} else {

				$(this).find("ul.sub-menu").eq(0).slideToggle(300);
			
			}

		}); 

	/* autoclear function for inputs */
	$('.autoclear').click(function() {
	if (this.value == this.defaultValue) {
	this.value = '';
	}
	});

	$('.autoclear').blur(function() {
	if (this.value == '') {
	this.value = this.defaultValue;
	}
	});

	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});	
	
});

function addToCart(product_id) {
	$.ajax({
		url: '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="catalog/view/theme/default/image/close.png" alt="" class="close" /><p class="clear"></p></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="catalog/view/theme/default/image/close.png" alt="" class="close" /><p class="clear"></p></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function removeCart(key) {
	$.ajax({
		url: '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: '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: '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="catalog/view/theme/default/image/close.png" alt="" class="close" /><p class="clear"></p></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: '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="catalog/view/theme/default/image/close.png" alt="" class="close" /><p class="clear"></p></div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
