$(document).ready(function() {
	/* Move some product page elements to header */
	/* $('#ProductBuy').appendTo('#Header .inner'); */
	$('#ShareProduct').appendTo('#Header .inner');
	$('#PageHeader').appendTo('#Header .inner');
	
	
	/* Product price elements */
	$('.Product').hover(function() {
		$(this).find('dd.Price').show();
	}, function() {
		$(this).find('dd.Price').fadeOut();
	}).click(function(e) {
		if(!$(e.target).is('a'))
			window.location.href = $(this).find('a.ProductImage').attr('href');
	}).each(function() {
		$(this).attr('title', $(this).find('a.ProductImage').attr('title'));
	});
	
	
	/* Product image overlays */
	var dim = $('<div id="dim"></div>').appendTo(document.body);
	var overlay = $('<div id="overlay"></div>').appendTo(document.body);
	
	dim.click(function() {
		dim.fadeOut();
		overlay.hide();
	});
	
	$('#ProductImages a').click(function() {
		dim.fadeTo(200, 0.75);
		overlay.empty().css({'margin': 0, 'width': 0, 'height': 0}).hide();
		var x = $('<a class="x"></a>').click(function() {
			dim.click();
		}).appendTo(overlay);

		var img = $('<img />').appendTo(overlay);
		
		img.load(function() {
			overlay.fadeIn().css({
				'width': this.width,
				'height': this.height,
				'margin-left': -this.width/2,
				'margin-top': -this.height/2
			});
		});
		
		img.attr('src', $(this).attr('href'));
		
		return(false);
	});
	
	
	$('#Header a.cart').each(function() {
		if($(this).text() == '0')
			$(this).hide();
	});
	
	
	/* Size selector */

	/*
	// var selector_eu = $('<ul class="sizes eu" style="display: none;"></ul>');
	var selector_us = $('<ul class="sizes us"></ul>');
	var input = $('<input type="hidden" name="variaatio" value="" id="variaatio" />');
	
	var checks = $('#ProductBuy .Checks').after(selector_us).after('<ul class="tabs"><li class="selected">US</li></ul>').after(input);
	
	checks.find('label').each(function() {
		var val = $(this).find('input').val();
		var text = $(this).text();
		
		// selector_eu.append('<li data-id="'+val+'">'+text[0]+'</li>');
		selector_us.append('<li data-id="'+val+'">'+text+'</li>');
	});
	
	checks.remove();
	
	$('ul.tabs li').click(function() {
		$(this).addClass('selected').siblings().removeClass('selected').each(function() {
			$('.sizes.'+$(this).text().toLowerCase()).hide();
		});
		
		$('.sizes.'+$(this).text().toLowerCase()).show();
	});
	
	$('.sizes li').click(function() {
		$(this).addClass('selected').siblings().removeClass('selected');
		
		input.val($(this).attr('data-id'));
	});
	
	var lis = selector_us.find('li');
	lis.eq(Math.floor(lis.length/2)).click();
	*/
	
	
	/* Fixed header */
	
	/*
	var fixed_top = $('#Header .navi').position().top;
	
	$(window).scroll(function(e) {
		if(document.documentElement.scrollTop > fixed_top) {
			$('#Header .navi, #Header .links').addClass('fixed');
		}
		else {
			$('#Header .navi, #Header .links').removeClass('fixed');
		}
	});
	*/
	
	$('#ProductImages li a').slice(1).each(function() {
		$(this).appendTo('#ProductImages');
	});
	
	
	$('#ProductImages img').each(function() {
		this.src = this.src.replace('150x100', '240x160');
	});
	
	
	$('#CartTable tbody tr').each(function() {
		$(this).find('.CartProduct h3').after('<p class="CartPrice">'+$(this).find('.CartPrice').html()+'</p>');
		$(this).find('.CartProduct h3').after($(this).find('.CartQuantity input'));
	});
});
