$(window).bind('load', function () {
	$('div.cover > a').click(function () {
		if(!$('div#fullDocument').hasClass('visible')) {
			showFullDocumentSize(this);
		}
		return false;
	});

	$('div.cover > a').each(function (i) {
		$(this).hover(function () {
			$(this).children('img:first').css('display','none');
			$(this).children('img.hover').css('display','block');
		},function () {
			$(this).children('img:first').css('display','block');
			$(this).children('img.hover').css('display','none');
		});
	});

	$(window).resize(function () {
		if($('div#fullDocument').html()) {
			resizeFullDocumentLayer($('div#fullDocument'));
		}
	
	});

});

function resizeFullDocumentLayer(fullDocument) {
	var h = Math.round($(window).height()-100);
	fullDocument.css('top',Math.round(($(window).height()-h)/2));
	fullDocument.children('div#fullDocumentHeader').css('height',30);
	fullDocument.children('div#fullDocumentContent').css('height',h-70);
	fullDocument.children('div#fullDocumentFooter').css('height',40);
	return h;
}

function showFullDocumentSize(thumbnail) {
	var thumbnail = $(thumbnail);
	var fullDocument = $('div#fullDocument');
	fullDocument.addClass('visible');
	thumbnail.parent().append('<img alt="" class="spinner" src="images/any_spinner_big.gif"/>').children('img.spinner').fadeIn('slow');
	var fullDocumentMask = $('div#fullDocumentMask');

	var meta = thumbnail.parent().children('div.meta');
	var value = thumbnail.children('img:first').attr('alt');
	fullDocument.find('h3:first').html(value);
	var fullImage = meta.children('div.fullImage').html();
	var value = meta.children('div.url').html();
	fullDocument.find('input[name="fstrPageForPurchase"]').attr('value', value);
	var value = meta.children('div.products').html();
	fullDocument.find('input[name="ss_arrUniversalProductIds"]').attr('value', value);
	var value = meta.children('div.price').html();
	fullDocument.find('span.price').html(value);
	var value = meta.children('div.documentType').html();
	fullDocument.find('span.documentType').html(value);
	
	$('div#fullDocumentContent').image(fullImage, function () {

		thumbnail.siblings('img.spinner').stop().fadeOut('slow',function () {thumbnail.siblings('img.spinner').remove();});
		h = resizeFullDocumentLayer(fullDocument);

		var offset = thumbnail.offset();
		var size = [thumbnail.children().width(),thumbnail.children().height()]
		
		$('body').append('<div id="morph" style="z-index:3;display:block;position:absolute;background-color:#fff;border:#555 1px solid;"></div>');
		var morph = $('div#morph');
		var left = ($(window).width() - $('div#fullDocumentContent').css('width').replace('px',''))/2;
		morph.css('width',size[0]-2).css('height',size[1]-2).css('left',offset.left).css('top',offset.top);
		//fullDocumentMask.fadeTo(0,0).css('display','block').fadeTo('slow',.80);
		morph.animate({
			width: $('div#fullDocumentContent').css('width').replace('px','') - 2,
			height: h - 2,
			left: left,
			top: Math.round(($(window).height()-h)/2)
		},100,null,function () {
			fullDocument.fadeIn(1000, function () {
				morph.remove();
			});
			fullDocumentMask.fadeTo(0,0).css('display','block').fadeTo(200,.80);
			$('div#fullDocumentContent').scrollTop(0);
		});
	});
}

function hideFullDocument() {
	var fullDocument = $('div#fullDocument');
	var fullDocumentMask = $('div#fullDocumentMask');

	fullDocumentMask.stop().fadeOut('slow',function () {fullDocumentMask.attr('display', 'none');});
	fullDocument.stop().fadeOut('fast', function () {
		$('div#fullDocumentContent').html('');
		fullDocument.removeClass('visible');
	});
}

// Function to load an image with support for running a callback snippet after completion
$.fn.image=function(i, f){
	return this.each(function(){
		var o=new Image();
		o.onload=f;
		o.src=i;
		this.appendChild(o);
	});
}

