$(function(){
	//Search
	var searchInput = $('#search input');
	var value = searchInput.val();
	searchInput.click(function(){		
			$(this).val('');
	});
	searchInput.blur(function(){
		if($(this).val() == ""){
				$(this).val(value);
		}
});

$(function(){
	//Menu
	$('#categories li').mouseenter(function(){	
		$(this).find('ul').first()
			   .stop(true,true).slideDown('fast');
	}).mouseleave(function(){
		$(this).find('ul').first()
			   .stop(true,true).slideUp('fast');
	});
});
	
$(function(){
	//Slideshow
	function slider(cName,anim,move){
		$(cName).cycle({ 
			fx: anim,
			speed:  'slow',
			prev:   '.prev', 
			next:   '.next',
			timeout: move,
			pager:  '#pager',
			pagerAnchorBuilder: function(id) {
				id++;
				return '<li><a href="javascript:void(0);">' + id + '</a></li>'; 
			} 
		});	
	}
	//Homepage Slideshow
	slider(".slides",'scrollHorz',5000);
	//Product Images Slideshow
	slider(".prod-images",'scrollHorz',0);
});

$(function(){
	//Image  Fade
	$('.box img,.prod-grid img,.prod-list img').hover(function(){
		this.check = this.check || 1;
		$(this).stop().fadeTo('slow',this.check++%2==0 ? 1 : 0.5);
	});
});

$(function() {
    //Forms
   $("select, input:checkbox, input:radio, input:file,textarea,input:text,input:password").uniform();
});

$(function(){
	//Grid List Category View
	$(".display ul li").click(function(){
		$(".display ul li a").removeClass("active");
		$(this).find("a").addClass("active");
		var cName = $(this).hasClass("list");
		if(cName){
			$("#cat-products").removeClass("prod-grid").addClass("prod-list");	
			$.cookie('display', 'list');
		}else{
			$("#cat-products").removeClass("prod-list").addClass("prod-grid");	
			$.cookie('display', 'grid');
		}		
	});
	function display(view) {
		if (view == 'list') {
			$("#cat-products").removeClass("prod-grid").addClass("prod-list");	
			$(".display ul li.grid a").removeClass("active");
			$(".display ul li.list a").addClass("active");
		}else{
			$("#cat-products").removeClass("prod-list").addClass("prod-grid");
		}
	}
	view = $.cookie('display');
	if (view) {
		display(view);
	} else {
		display('grid');
	}
});

$(function(){
	//Increase Decrease QTY
	$(".qtyBtn").click(function(){
		if($(this).hasClass("increase")){
			var qty = $("#qty").val();
			qty++;
			$("#qty").val(qty);
		}else{
			var qty = $("#qty").val();
			qty--;
			if(qty>0){
				$("#qty").val(qty);
			}
		}
	});	
});

$(function(){
	//Tabs
	$(".tab").hide().first().show();
	$("ul.tabs li a,.review a.reviews").click(function(){
		$("ul.tabs li a").removeClass("active");
		$(this).addClass("active");
			var rel = $(this).attr("rel");
			$(".tab").hide();
			$("."+rel).show();		
	});
});


	$('.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;
	});
	
	$('#header 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;
		}
	});
});


$(function(){
	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});	
});

$(function(){
	/* Ajax Cart */
	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('active');
		$('#cart .content').show();
				
		$.ajax({
			url: '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');
			$('#cart .content').hide();
		});
	});
});

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/simple/image/close.png" alt="" class="close" /></div>');
					
					var notify = $('#notification').html();
						$.blockUI({ message: $(notify) });
						$('.blockOverlay').click(function(){
							$.unblockUI();
					});
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/simple/image/close.png" alt="" class="close" /></div>');
				
				var notify = $('#notification').html();
				$.blockUI({ message: $(notify) });
				$('.blockOverlay').click(function(){
					$.unblockUI();
				});
							
				$('#cart_total').html(json['total']);
			}	
		}
	});
}

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/simple/image/close.png" alt="" class="close" /></div>');
				var notify = $('#notification').html();
				$.blockUI({ message: $(notify) });
				$('.blockOverlay').click(function(){
					$.unblockUI();
				});
				$('#wishlist_total').html(json['total']); 				
			}	
		}
	});
}

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/simple/image/close.png" alt="" class="close" /></div>');
				var notify = $('#notification').html();
				$.blockUI({ message: $(notify) });
				$('.blockOverlay').click(function(){
					$.unblockUI();
				});
				$('#compare_total').html(json['total']);
			}	
		}
		
	});
}
