$(document).ready(function () {

	 	/* Search */
	$('.button-search').bind('click', function() {
		url = '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;
	});
	
	$('input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = '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;
		}
	});
	
	
// 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);
}); 

  $(".box-category > ul > li a ").hover(function () {
$(this).stop().animate({ paddingLeft: "5" }, "fast"); },
function () {
$(this).stop().animate({ paddingLeft: "0" }, "medium");
}); 

$('#cart').hover(function() {
$(this).find('.content').css("display", "block");
},function() {
$(this).find('.content').css("display", "none");
}); 

	$("#cart").hover(
  function () {
   	$('#cart .content').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});	
  },
  function () {
  $('#cart .content').removeClass('active');
  }
);

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});

/* Items */
var item = 3; // list of items on one page
$(".list-items li").css("display", "none");
$(".list-items li").slice(0, item).css("display", "block");
$('.btn li a').eq(0).addClass("active");

$('.btn li a').click(function() {
	var element_index = $('.btn li a').index(this);
	$('.btn li a').removeClass("active");
	$(this).addClass("active");
	$(".list-items li").hide();
	$(".list-items li").slice(element_index*item, element_index*item+item).fadeIn(400);
	return false;
});

/* Submenu */

$('#categories > ul > li').hover(function() {
	$("#categories li").removeClass("active");
	$(".welcome-text").html(welcome);
}); 

$('#categories > ul > li.submenu').hover(function() {
	$("#categories li.submenu").removeClass("active");
	$(this).addClass("active");
	$(".welcome-text").css("display", "block");
	$(".welcome-text").html($(this).find("div").html());
	
	$("#categories li.submenu div").css("display", "none");

}); 

$("#categories > ul > li.standard").hover(function () {
$(this).children('div').slideDown('fast', function() {
// Animation complete.
});

 },function () {
$(this).children('div').slideUp('medium', function() {
// Animation complete.
});  
 }); 


$("#categories > ul > li.standard > div > ul > li").hover(function () {
$(this).children('ul.subsubmenu').slideDown('fast', function() {
// Animation complete.
});

 },function () {
$(this).children('ul.subsubmenu').slideUp('medium', function() {
// Animation complete.
});  
 });  

/* autoclear function for inputs */
$('.autoclear').click(function() {
if (this.value == this.defaultValue) {
this.value = '';
}
});

$('.autoclear').blur(function() {
if (this.value == '') {
this.value = this.defaultValue;
}
});

});

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" /></div>');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').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="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').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="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

