$(document).ready(function() {
	
	/* box content scroll - first page */
	if ($('.box-content-scroll').length > 0)
		{
		var divWiddth = $('.box-content-scroll .itemScroll').size() * 161;
		$('.box-content-scroll').css("width",divWiddth+"px");
		}
	
	/* rel external link in target */
	$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});	
	
	
	/* Search */
	$('#header #search .button').bind('click', function() {
		if ($('#header #search .input').val()!='Cauta prin bacanie') {
			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 = $('input[name=\'filter_name\']').attr('value')
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			location = url;
		}
	});
	*/


	/* Products opacity grid */
  if (!$.browser.msie || parseInt($.browser.version, 10) > 8) {
      var onMouseOutOpacity = 1;
      $('div.product-grid > div').css('opacity', onMouseOutOpacity)
        .hover(
          function () {
            $(this).prevAll().stop().fadeTo('slow', 0.40);
            $(this).nextAll().stop().fadeTo('slow', 0.40);
          },
          function () {
            $(this).prevAll().stop().fadeTo('slow', onMouseOutOpacity);
            $(this).nextAll().stop().fadeTo('slow', onMouseOutOpacity);
          }
      );
  }

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

	/* smooth scroll */
$('a[href*=#]').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
		{
		var $target = $(this.hash);
		$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		if ($target.length)
			{
			var targetOffset = $target.offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
			}
		}
	});

});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().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']) {
					$('#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']);
				$('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']) {
				$('#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']);
			}	
		}
	});
}

function contentAdso() {
	url = 'index.php?route=product/adso';

	var filter_name = $('#box_search #filter_name').attr('value');

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

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

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

	var filter_sub_category = $('#box_search input[name=\'filter_sub_category\']:checked').attr('value');
  
	if (filter_sub_category) {
		url += '&filter_sub_category=true';
	}

	if ($('#box_search #filter_description').attr('checked')) {
		url += '&filter_description=1';
	}

	if ($('#box_search #model').attr('checked')) {
		url += '&model=1';
	}

       var filter_manufacturer_id = $('#box_search #filter_manufacturer_id').attr('value');

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

      var filter_pricemin = $('#box_search #filter_pricemin').attr('value');

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

       var filter_pricemax = $('#box_search #filter_pricemax').attr('value');

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

       var filter_groups = $('#box_search #filter_groups').attr('value');

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

       var filter_attribute = $('#box_search #filter_attribute').attr('value');

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

	location = url;
}

