$(document).ready(function(){init();});

var initialised = false;
var currentItem = 0;
var currentRow = 0;

var fadeIn = 1;
var fadeOut = .5;
var fadeOver = .75;

function setCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function wipeCookie(name)
{
	createCookie(name,"",-1);
}

function init()
{
}

function showProductRow(row)
{
	if(!initialised)
	{
		cutout = ($('#sfDetails').css('background-image'));
		initialised = true;
	}
	
	$('#sfDetails').css('background-image', cutout.replace('cutout', 'cutout-' + row));
	
	$('#mhImages li').hide(); $('#mhImages li:eq(' + row + ')').show();
	$('#mhImages li img').stop().fadeTo(0, fadeOut);
	$('#mhImages li:eq(' + row + ') img').each(function(i){ $(this).data('count', i); }).mouseover(function(i){ productMouseOver(row, $(this).data('count') ); }).mouseout(function(i){ productMouseOut(row, $(this).data('count')); }).click(function(i){ productMouseClick(row, $(this).data('count')); });
	
	$('#mhDescriptions li').hide();
	
	productMouseClick(row, 0);
}

function productMouseOver(row, count)
{
	if(count != currentItem) $('#mhImages li:eq(' + row + ') img:eq(' + count + ')').stop(true).fadeTo('normal', fadeOver).css('cursor', 'pointer');
}

function productMouseOut(row, count)
{
	if(count != currentItem) $('#mhImages li:eq(' + row + ') img:eq(' + count + ')').stop(true).fadeTo('normal', fadeOut);
}

function productMouseClick(row, count)
{
	currentItem = count;
	$('#mhImages li:eq(' + row + ') img:eq(' + count + ')').stop().fadeTo('normal', fadeIn);
	$('#mhImages li:eq(' + row + ') img:not(:eq(' + count + '))').stop().fadeTo('normal', fadeOut);
	showProductDetail(row, count);
}

function showProductDetail(row, count)
{
	$('#mhDescriptions li').hide(); $('#mhDescriptions li:eq(' + (row-1) + ')').show();
	$('#mhDescriptions li:eq(' + (row-1) + ') div').hide();
	$('#mhDescriptions li:eq(' + (row-1) + ') div:eq(' + count + ')').show();
}