$(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;
	});

	var fadeDuration = 200; //time in milliseconds
      	
	$('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;
		}
	});
	
	$("#write_button").click(function () {
		$("#write_review").toggle();
	});	
	
	
	/* Ajax Cart */
	$('#cart a').bind('click', function() {
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').toggle().html(json['output']);
				}
			}
		});			
	});
	
});

$('.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" /></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 }, 'fast'); 
				$("#cart").animate({opacity:0}, 400)
								.animate({opacity:1}, 400);
				

			}	
		}
	});
}

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'); 
			}	
		}
	});
}

$(function(){
	//search
	var searchInput = $('.searchBox input');
	var value = searchInput.val();
	searchInput.click(function(){		
			$(this).val('');
	});
	searchInput.blur(function(){
		if($(this).val() == ""){
				$(this).val(value);
		}
	});
	//All Categories
	$(".categories > li").mouseenter(function(){
		$(".categories ul.first").stop(true,true).slideDown('fast');
	}).mouseleave(function(){
		$(".categories ul.first").stop(true,true).slideUp('fast');
	});		

	$(".access .links > li").mouseenter(function(){
		$(".links ul.first").stop(true,true).slideDown('fast');
	}).mouseleave(function(){
		$(".links ul.first").stop(true,true).slideUp('fast');
	});	

});


$(function(){
	//Homepage Slideshow
	$('.slides').cycle({ 
		speed: 200, 
		timeout: 3000, 
		fx : 'scrollLeft',
		pager: '.slideTab', 
		pagerEvent: 'mouseover', 
		pause: 1 
	});	
	$('.slideTab a').wrap("<li></li>");
	$('.slides li').mouseenter(function(){
		$(this).find('p').stop(true,true).animate({bottom: '0px'},200);
	}).mouseleave(function(){
		$(this).find('p').stop(true,true).animate({bottom: '-36px'},200);
	});
});


$(function(){
	//Best Sellers
	$(".bestSeller li").mouseenter(function(){			
		$(this).find(".details").show();		
	}).mouseleave(function(){
		$(this).find(".details").hide();
	});
});


$(function(){
	//Product Tabs
	var li = $(".productTabs li");
		$(".tabWrapper").hide().first().show();
		li.click(function(){
			li.removeClass("active");
			var attr = $(this).attr("class");			
			$(this).addClass("active");
			$(".tabWrapper").hide();
			$("div."+attr).show();			
		});
	$("li a.reviews").click(function(){
		var attr = $(this).attr("class");		
		$(".productTabs li").removeClass("active");
		$(".productTabs li.reviews").addClass("active");
		$(".tabWrapper").hide();
		$("div."+attr).show();	
	});
});


