var cf1;
var cf2;
var cf3;
var cf4;
var cf5;

var selectorGroup01 = new Array
(
	'image-01',
	'image-02',
	'image-03'
);

var selectorGroup02 = new Array
(
	'image-05',
	'image-06',
	'image-07'
);

var selectorGroup03 = new Array
(
	'image-09',
	'image-10',
	'image-11'
);

var selectorGroup04 = new Array
(
	'image-13',
	'image-14',
	'image-15'
);

var selectorGroup05 = new Array
(
	'hero-01',
	'hero-02'
);

startSlideshow1 = function()
{
	cf1 = new Crossfader
	(
		selectorGroup01,
		450,   // transition speed
		9000,  // pause
		true
	);
}

startSlideshow2 = function()
{
	cf2 = new Crossfader
	(
		selectorGroup02,
		450,   // transition speed
		9000,  // pause
		true
	);
}

startSlideshow3 = function()
{
	cf3 = new Crossfader
	(
		selectorGroup03,
		450,   // transition speed
		9000,  // pause
		true
	);
}

startSlideshow4 = function()
{
	showCallsToAction.delay(1);
	cf4 = new Crossfader
	(
		selectorGroup04,
		450,   // transition speed
		9000,  // pause
		true
	);
}

startSlideshow4Alternate = function()
{
	cf4 = new Crossfader
	(
		selectorGroup04,
		450,   // transition speed
		9000,  // pause
		true
	);
}

startSlideshow5 = function()
{
	cf5 = new Crossfader
	(
		selectorGroup05,
		1200,   // transition speed
		12000,  // pause
		true
	);
}

// -----------------------------------------------
var expirationValue = 4; // hours the cookie should last

GetCookie = function(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

// -----------------------------------------------
SetCookie = function(name,value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

// -----------------------------------------------
DeleteCookie = function(name)
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// -----------------------------------------------
var exp = new Date();
//exp.setTime(exp.getTime() + (expirationValue*24*60*60*1000));
exp.setTime(exp.getTime() + (expirationValue*60*60*1000));

// -----------------------------------------------
function amt()
{
	var count = GetCookie('count')
	if(count == null) {
	SetCookie('count','1')
	return 1
}
else
{
	var newcount = parseInt(count) + 1;
	DeleteCookie('count')
	SetCookie('count',newcount,exp)
	return count
   }
}

// -----------------------------------------------
getCookieVal = function(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

// -----------------------------------------------
checkCount = function()
{	
	var count = GetCookie('count');
	if (count == null)
	{
		count=1;
		SetCookie('count', count, exp);

		startSlideshow1();
			window.setTimeout("startSlideshow2()",1000);
			window.setTimeout("startSlideshow3()",2000);
			window.setTimeout("startSlideshow4()",3000);
		startSlideshow5();
		
		initSlideshow();
	}
	else
	{
		count++;
		SetCookie('count', count, exp);
		
		startSlideshow1();
		startSlideshow2();
		startSlideshow3();
		startSlideshow4Alternate();
		startSlideshow5();
		
		var selectorA = $("shop-call-to-action-01");
		var selectorB = $("shop-call-to-action-02");
		var selectorC = $("shop-call-to-action-03");
		var selectorD = $("shop-call-to-action-04");
		
		selectorA.setStyle
		(
			{
				display: "block"
			}
		);
		selectorB.setStyle
		(
			{
				display: "block"
			}
		);
		selectorC.setStyle
		(
			{
				display: "block"
			}
		);
		selectorD.setStyle
		(
			{
				display: "block"
			}
		);
		
		Stop(cf1);
		Stop(cf2);
		Stop(cf3);
		Stop(cf4);
		
		Start(cf1);
		window.setTimeout("Start(cf2)",10000);
		window.setTimeout("Start(cf3)",10500);
		window.setTimeout("Start(cf4)",11000);
		
		initSlideshow();
	}
}

// -----------------------------------------------

showCallsToAction = function()
{
	var selectorA = $("shop-call-to-action-01");
	var selectorB = $("shop-call-to-action-02");
	var selectorC = $("shop-call-to-action-03");
	var selectorD = $("shop-call-to-action-04");
	
	new Effect.Appear(selectorA,
	{
		duration: 0.2,
		afterFinish:function()
		{		
			new Effect.Appear(selectorB,
			{
				duration: 0.2,
				afterFinish:function()
				{		
					new Effect.Appear(selectorC,
					{			
						duration: 0.2,
						afterFinish:function()
						{		
							new Effect.Appear(selectorD,
							{			
								duration: 0.2,
								afterFinish:function()
								{		
									//alert('done');
								}
							});
						}
					});
				}
			});
		}
	});
}

initSlideshow = function()
{	
	$("hero").observe
	(
		"mouseover",function()
		{
			Stop(cf5);	
		}
	);
	
	$("hero").observe
	(
		"mouseout",function()
		{
			Start(cf5);	
		}
	);
	
	$("top-left").observe
	(
		"mouseover",function()
		{
			Stop(cf1);	
		}
	);
	
	$("top-left").observe
	(
		"mouseout",function()
		{
			Start(cf1);	
		}
	);
	
	$("top-right").observe
	(
		"mouseover",function()
		{
			Stop(cf2);	
		}
	);
	
	$("top-right").observe
	(
		"mouseout",function()
		{
			Start(cf2);	
		}
	);
	
	$("bottom-left").observe
	(
		"mouseover",function()
		{
			Stop(cf3);	
		}
	);
	
	$("bottom-left").observe
	(
		"mouseout",function()
		{
			Start(cf3);	
		}
	);
	
	$("bottom-right").observe
	(
		"mouseover",function()
		{
			Stop(cf4);	
		}
	);
	
	$("bottom-right").observe
	(
		"mouseout",function()
		{
			Start(cf4);	
		}
	);
}

Stop = function(selectorGroup)
{
	selectorGroup.StopFade();
}

Start = function(selectorGroup)
{
	selectorGroup.StartFade();
}
	
document.observe("dom:loaded",function()
{
	//preLoadImages();
	new Rollover('top-left',
	{
		suffixSeperator: '-',
		rolloverSuffix: 'hover',
		extension: '.gif'
	});
	new Rollover('top-right',
	{
		suffixSeperator: '-',
		rolloverSuffix: 'hover',
		extension: '.gif'
	});
	new Rollover('bottom-left',
	{
		suffixSeperator: '-',
		rolloverSuffix: 'hover',
		extension: '.gif'
	});
	new Rollover('bottom-right',
	{
		suffixSeperator: '-',
		rolloverSuffix: 'hover',
		extension: '.gif'
	});
});

checkCount();