$(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;
		}
	});
	
	/* 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(){
//Menu
$('#menu li').mouseenter(function(){	
		$(this).find('ul').first()
			   .stop(true,true).slideDown('fast');
	}).mouseleave(function(){
		$(this).find('ul').first()
			   .stop(true,true).slideUp('fast');
	});
});

$(function(){
	//Search	
	$(".searchBtn").click(function(){
		var close = $(this).hasClass("close");
			if(!close){
				$(this).addClass("close")
						.attr("title" , "close");
			}else{
				$(this).removeClass("close")
						.attr("title" , "search");
			}
		$(".searchForm").slideToggle("fast");	
	});
});

$(function(){
	//Slideshow	
	var count = $(".slides").children().size();
	var slide = 930;
	var pos = 0;	
	$(".prev").addClass("disableP");
	$(".slides").css("width", count * slide);	
	$(".np li").click(function(){
		var btn = $(this).attr("class");
		if(btn=="next" && pos<count-1){
			pos++;
			$(".slides").animate({marginLeft:slide*(-pos)});
		}else if(btn=="prev" && pos>=1){
			pos--;
			$(".slides").animate({marginLeft:"-"+slide*(pos)});			
		}
	ctrl(pos);
	function ctrl(index){
		if(index==0){
			$(".prev").addClass("disableP");
		}else{
			$(".prev").removeClass("disableP");	
		}
		if(index==count-1){
			$(".next").addClass("disableN");
		}else{
			$(".next").removeClass("disableN");	
		}
	}
	});
});

$(function(){
	//Featured
	var li = $(".featuredBox ul.btn li");
		$(".featuredBox ul.items li").hide().first().show();
		li.first().addClass("active");
		li.click(function(){
			li.removeClass("active");
			var attr = $(this).attr("class");			
			$(this).addClass("active");
			$(".featuredBox ul.items li").hide();
			$(".featuredBox ul.items li."+attr).fadeIn("slow");			
		});
});


$(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();
			$(".right div."+attr).show();			
		});
	$("li.reviewTab").click(function(){
		var attr = $(this).attr("class");		
		$(".productTabs li").removeClass("active");
		$(".productTabs li.reviews").addClass("active");
		$(".tabWrapper").hide();
		$(".right div."+attr).show();	
	});
});

$(function(){
	//Color & Size
	$(".op li").click(function(){
		var value = $(this).attr("id");
		var title = $(this).find("a").attr("title");
		$(".option").find("option[value='"+value+"']").attr("selected","selected");
	});						
	$("ul.color li").click(function(){
			$("ul.color li").css('opacity',0.5);
			$(this).css('opacity', 1);
	});	
	$("ul.size li").click(function(){		
		$("ul.size li").css('opacity',0.5);
		$(this).css('opacity', 1);
	});	
});
