$(document).ready(function() {
	
	$('.accordion').liteAccordion({
	    onActivate : function() {
	        this.find('figcaption').fadeOut();
	    },
	    slideCallback : function() {
	        this.find('figcaption').fadeIn();
	    },
	    containerWidth : 935,
	    containerHeight : 235,
	    headerWidth : 48,
	    autoPlay : true,
	    pauseOnHover : true,
	    theme : 'hop',
	    rounded : true,
	    enumerateSlides : false
	});

	$('.footercontent ul:first').after($('.sidenav ul:first').clone());
	$('.footercontent ul:eq(1) li ul').remove();
	
	$('a.newwindow').attr('target', '_blank');
	
	$('li.search input').focus(function(){
		var data = $(this).data('defaulttext');

		if (typeof data == 'undefined' || !data.length || $(this).val() == data) {
			$(this).data('defaulttext', $(this).val());
			$(this).val('');
		}
    }).blur(function(){
		if (!$(this).val().length) {
			$(this).val($(this).data('defaulttext'));
		}
    });

	$('li.search input:first').keydown(function(e) {
		if (e.keyCode == 13) {
			$(this).next().click();
		}
	});

	$('li.search span').click(function() {
		pathArray = location.pathname.split( '/' );

		url = baseUrl;

		url += 'index.php?route=product/search';

		var filter_keyword = $('li.search input:first').val();

		if (filter_keyword) {
			url += '&filter_name=' + encodeURIComponent(filter_keyword);
		}

		// var filter_category_id = $('#filter_category_id').attr('value');

		// if (filter_category_id) {
		// 	url += '&category_id=' + filter_category_id;
		// }

		location = url;
	});

	$('#sidebar div.sidenav').after('<fb:fan profile_id="179363468204" stream="0" connections="8" logobar="0" width="235" height="325"></fb:fan>');
	
	$('.headingshadow:first').prepend('<div id="notification"></div>');
	
	/* Ajax Cart */
	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('active');
		
		$.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');
		});
	});
	
	var emadr = ['b', 'c', 'e', 'g', 'i', 'k', 'n','s','t'];
	emadr = emadr[6] + emadr[4] + emadr[1] + emadr[5] + '@' + emadr[3] + emadr[4] + emadr[7] + emadr[0] + emadr[3] + '.' + emadr[6] + emadr[2] + emadr[8];
	
	$('ul.usernav li.grey a:last').attr('href', 'mail' + 'to://' + emadr)
								  .attr('title', emadr);
	
	$('ul.usernav li.grey a').tipsy({gravity: 'n',
								 html: true,
								 opacity: 0.5,
								 offset: 10}); 

	
});

$('.success, .warning, .attention, .information').live('click', function() {
	$(this).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="error" style="display: none;">' + json['error']['warning'] + '</div>');
					$('.error').fadeIn('slow');
					$('html, body').animate({ scrollTop: 0 }, 'slow');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] +'</div>');
				
				$('.success').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="success" style="display: none;">' + json['success'] + '</div>');
				
				$('.success').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="success" style="display: none;">' + json['success'] + '</div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

