var $j = jQuery.noConflict();
$j(document).ready(function(){
	initFadeSlider();
	initSliders();
	initPopup();
	initTierPrice();
	$j('ul.tabs').delegate('li:not(.current)', 'click', function() {
		$j(this).addClass('current').siblings().removeClass('current')
			.parents('div.section').find('div.box').hide().eq($j(this).index()).fadeIn(150);
	})

	formHeight = $j('#discount-coupon-form div.discount').innerHeight();

	tableHeight = $j("#shopping-cart-table tfoot tr td table tr td.shopping_cart_totals").height();

	$j("#shopping-cart-table tfoot tr td table tr td:first").height(formHeight + 10);

});
function initFadeSlider(){
	if ($j('.box-slider').length == 0){
		return false;
	}
	$j('.box-slider .fader')
	.cycle({
		fx: 'fade',
		pager:  '#nav-slider'
	});
};

function initPopup(){
	$j("a[rel=example_group]").fancybox({
		'title' : this.title
	});
}

function initSliders(){
	if ($j("#gallery .gallery-items ul li").size()<=4) $j('#gallery .next,#gallery .prev').hide();

	$j("#gallery .gallery-items").jCarouselLite({
		btnNext: "#gallery  .next",
		btnPrev: "#gallery  .prev",
		circular: false,
		visible: 4
	});
	if ($j("#gallery .gallery-items ul li").size()<=4) {$j('#gallery .gallery-items ul').css("marginLeft",'4px');}

	$j("div.gallery-items ul li a").click(function(e) {
		e.preventDefault(e);
		$j("div.product-img-box p.product-image img#image").attr("src", $j(this).attr("name_popup"));
		$j("div#data img#image_popup").attr("src", $j(this).attr("name"));
	});
}

function initTierPrice(){

	$j('#qty').keyup(function() {
		qty = parseInt($j(this).val());
		if(qty > 0){
			if(typeof(tier_prices) !== 'undefined')
			{
				total_price = false;
				
				if(tier_prices[qty])
				{
					prev_qty = qty;
					total_price = tier_prices[qty]*qty;
				}
				else
				{
					qty_less_price = false;
					for(key in tier_prices)
					{
						if( key < qty )
						{
							qty_less_price = tier_prices[key];
						}
					}
					
					if(qty_less_price != false)
					{
						total_price = qty_less_price*qty;
					}
				}
				
				if( total_price != false )
				{
					out_price = formatCurrency(total_price, {'pattern':tier_prices_pattern, 'decimal': 2}, false);
					
					$j('#current-price').css('display', 'none');
					$j('#tier-price-message').css('display', 'none');
					
					$j('#tier-price-total').css('display', 'block');
					$j('#tier-price-total strong').text(out_price);
				}
			}
		}
		else{
			$j('#tier-price-total').css('display', 'none');
			
			$j('#tier-price-message').text(tier_prices_error_message);
			$j('#tier-price-message').css('display', 'block');
		}
	});
}

