$(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().show();
	}).mouseleave(function(){
		$(this).find('ul').first().hide();
	});
});
	
$(function(){
	//Slideshow
	$('.pagerWrap').append('<ul id="pager"></ul><div class="clear"></div>');
	$('.slides').cycle({ 
			fx: 'scrollHorz',
			speed:  'slow',
			timeout: 3000,
			pager:  '#pager',
			pagerAnchorBuilder: function(id) {
				id++;
				return '<li><a href="javascript:void(0);">' + id + '</a></li>'; 
			} 
		});	
	
	//Featured Slide
	$('.featured_slides').cycle({ 
		fx:     'scrollHorz', 
		speed:  'slow', 
		timeout: 0, 
		next:   '.next', 
		prev:   '.prev' 
	});
});

$(function(){
	//Image  Fade
	$('.box img,.prod-grid img,.prod-list img,.thumbWrap img').hover(function(){
		this.check = this.check || 1;
		$(this).stop().fadeTo('slow',this.check++%2==0 ? 1 : 0.8);
	});
});

$(function() {
    //Forms
   $("select, input:checkbox, input:radio, input:file,textarea,input:text,input:password").uniform();
});

$(function(){
	//Carousel Thumbnails
	$(".thumbWrap").each(function() {
		var all = $(this).find("div");
		for(i=0; i < all.length; i += 4) {
			all.slice(i, i + 4).wrapAll('<div class="thumbSlide"></div>'); 
		}
	});	
	$('.thumbWrap').cycle({
		fx:     'scrollVert', 
    	prev:   '.down', 
    	next:   '.up',
		timeout: 0
	});
});

$(function(){
	//Color & Size product page
	$(".colorOp").parent('.selector').hide();
	$(".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).removeClass("active");
			$(this).css('opacity',1).addClass("active");
	});	
	$("ul.size li").click(function(){		
		$("ul.size li").removeClass("active");
		$(this).addClass("active");
			$("ul.size li").css('opacity',0.5);
			$(this).css('opacity', 1)
	});	
});

$(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']);
			}	
		}
		
	});
}