$(document).ready(function(){
	$('#product-details .secondary-info:first').prepend('<ul id="product-details-tabs"></ul>')
		.children('.sub-content:gt(0)').hide();
	
	function isAvailable(id, label)	{	
		if ($(id).length > 0) {
			$(id + ' > h2').remove();
			return '<li><a href="' + id + '">' + label + '</a></li>';
		} else {
			return '<li><span>' + label + '</span></li>';
		}
	}
	$('#product-details-tabs').append(
		isAvailable('#product-details-product-description', txtProductDescription)
	  + isAvailable('#product-details-technical-data', txtTechnicalData)
	  + isAvailable('#product-details-accessories', txtAccessories)
	);
	$('#product-details-tabs a:first').parent().addClass('active');
	$('#product-details-tabs a').click(function() {
		var tabId = $(this).attr("href").match(/#product-details-.*$/);
	         	$("div " + tabId).show().siblings('.sub-content').hide();
	         	$(this).parent('li').addClass('active').siblings().removeClass('active');
		return false;
	       	});			
	$('.recently-viewed h2').css('cursor','pointer').addClass('hideMe').toggle(function(){
		$('.recently-viewed .product').hide('fast');
		$('.recently-viewed h2').removeClass('hideMe').addClass('showMe');
	}, function(){
		$('.recently-viewed .product').show('fast');
		$('.recently-viewed h2').addClass('hideMe').removeClass('showMe');
	});
	$('#product-details .skus li').hover(function(){
		$(this).css('background', '#fff').css('cursor','pointer');
	},function(){
		$(this).css('background', '#f8f8f8');
	});
	$('#product-details .skus li').click(function(){
		var imgSrc = $(this).children('.sku-thumb').attr('href');
		var productName = $(this).find('label').text();
		var productDescription = $(this).children('.sku-description').text();
		var productDescription2 = $(this).children('.sku-description2').text();
		$(this).find('input').attr('checked', 'checked');
		$('#product-details h1').text(productName);
		$('#product-image').attr('src', imgSrc + '&width=200&height=200&padding=0').parent().attr('href', imgSrc);
		$('.info-area-description p').text(productDescription);
		$('.primary-info h2').text(productDescription2);
	});

	var productImage = $('#product-image');
	var originalImageSrc = productImage.attr('src');
		
	$('#product-details .product-alt-thumb').hover(function() {
		var newSrc = $(this).find('img').attr('src').replace('width=60&height=60', 'width=200&height=200');
		productImage.attr('src',newSrc);
	}, function() {
		productImage.attr('src',originalImageSrc);
	});
	$("#product-details .image-area a").click(function(){
		var imageUrl = $(this).attr('href');
		if ($('#slickbox').size() == 0) {
			$('body').append('<div id="slickbox"></div>');
			$('#slickbox').append('<img src="' + imageUrl +'" alt="" />')
			.click(function(){ $(this).remove(); });
		}
		$('body').click(function(){ $('#slickbox').remove(); });
		return false;	
	});
});