$(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 = $(this).attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});

	/* Ajax Cart */
	$('#cart > 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');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// 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');
			});	
		}
	}

$('.drop_lang div').live('click', lang_close)
$('.lmenu').live('click', lang_open)
$('.drop_money div').live('click', money_close)
$('.moneymenu').live('click', money_open)
$('.top_right').live('mouseout', jsddm_timer)

var mtimeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function menu_open()
{  menu_canceltimer();
   menu_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function menu_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function menu_timer()
{  closetimer = window.setTimeout(menu_close, mtimeout);}

function menu_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('.menu > li').bind('mouseover', menu_open)
   $('.menu > li').bind('mouseout',  menu_timer)});

document.onclick = menu_close;

});

$(".add_to_chart").live("mouseover",function(){
 $(".add_to_chart").addClass("add_to_chart_hover");
 $(".add_to_chart_logo").addClass("add_to_chart_logo_hover");

}).live("mouseout",function(){
 $(".add_to_chart").removeClass("add_to_chart_hover");
 $(".add_to_chart_logo").removeClass("add_to_chart_logo_hover");

});


///////////////Product tabs///////////////////////////
	$(".tab_content").hide(); //Hide all content
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$(".tab").live("click",function() {
		$(".tab").removeClass("tabactive"); //Remove any "active" class
		$(this).addClass("tabactive"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("id").replace("p_",""); //Find the href attribute value to identify the active tab + content
        $("#"+activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
///////////////Product tabs END///////////////////////




$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});

var timeout = 4000;
function jsddm_timer()
{
  window.setTimeout(lang_close, timeout);
  window.setTimeout(money_close, timeout);
  }

function lang_open()
{
   $(".drop_lang").css('visibility', 'visible');
   //alert("test");
   }
function lang_close()
{  $(".drop_lang").css('visibility', 'hidden');
}


function money_open()
{
   $(".drop_money").css('visibility', 'visible');
  // alert("test");
   }
function money_close()
{  $(".drop_money").css('visibility', 'hidden');
}



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']) {
					$('#menu').after('<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']) {
				$('#menu').after('<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']);
			}
		}
	});
}


$(".pcolse").live("click",function(){
 $("#pbox").remove();
})

$(".pcart").live("click",function(){
  location.href = "/index.php?route=checkout/cart";
})

function ptimer(){
$("#pbox").delay(1500).fadeOut(400).queue(function() {$(this).remove();});
}
function addToCart_2(product_id, qunat) {

$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id+'&quantity='+qunat,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			}

			if (json['error']) {
				if (json['error']['warning']) {
					$('#menu').after('<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']) {
				$('#menu').after('<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']);
			}
		}
	});
}

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']) {
				$('#menu').after('<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']);
			}	
		}
	});
}

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']) {
				$('#menu').after('<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']);
			}	
		}
	});
}

