$(document).ready(function() {
	
	$('#content #notification').slideDown('200').delay('1000').slideUp('200');
	$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
	  while(x<c.length){var m=r.exec(c.substr(x));
	    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
	    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
	    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
	URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
	  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
	  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
	});
	
	$('.tab-move').click( function ()
	{
	    $('html, body').animate({scrollTop: $('#tabs').offset().top+'px'});
	});
	

	/* 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;
	});
	
	$('#header 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;
		}
	});
	
	$('#cart > .heading a').click( function() {
		if($('#cart').hasClass('active'))
		{
			$('#cart').removeClass('active');
		}
		else
		{
			
			$('#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');
		});
	});
	
	// IE6 & IE7 Fixes
	/*
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
	*/
	
	$('#facebook-product-like').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+$.URLEncode($(location).attr('href'))+'&amp;send=false&amp;layout=box_count&amp;width=47&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=60" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:47px; height:60px;" allowTransparency="true"></iframe>');

	$('.nav-item').click(function() {
		parent = $(this).parent();
		sibling = $(this).siblings();
		if($(parent).hasClass('li-active') || !$(sibling).html()) window.location = $(this).attr('title');
		else
		{
			other_menus = $(parent).parent();
			$('ul',other_menus).hide();
			$('.li-active',other_menus).toggleClass('li-active');
			$(sibling).slideToggle("slow");
			$(parent).toggleClass('li-active');
			$('#content').css('min-height', $('#container').height()-226);
		}
	});
});

$(document).ready(function() {
	var height = $('#column-left').height() > $('#column-right').height() ? $('#column-left').height() : $('#column-right').height();<?php echo $content_top; ?>
	$('#content').css('min-height', height);
});

$(window).load(function() {
	var height = $('#column-left').height() > $('#column-right').height() ? $('#column-left').height() : $('#column-right').height();
	$('#content').css('min-height', height);
});


$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().slideUp('200', 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/fullVolume/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/fullVolume/image/close.png" alt="" class="close" /></div>');
				
				$('.success').slideDown('200').delay('2000').slideUp('200');
				
				$('#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'] + '<img src="catalog/view/theme/fullVolume/image/close.png" alt="" class="close" /></div>');
				
				$('.success').slideDown('200').delay('2000').slideUp('200');
				
				$('#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'] + '<img src="catalog/view/theme/fullVolume/image/close.png" alt="" class="close" /></div>');
				
				$('.success').slideDown('200').delay('2000').slideUp('200');
				
				$('.compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
