var items = new Array();
var currentId = null;
var counter;

function Item(image, text, button) {
	this.image = image;
	this.text =  text;
	this.button = button;
	this.show = function() {
		$(this.image).css({"display":"inline", "z-index": 1});
		$(this.image).fadeIn(300);
		$(this.button).addClass("active");
		$("div#carousel div.controls p").html(this.text);
	};
	this.hide = function() {
		$(this.image).css("z-index", 2);
		$(this.image).fadeOut(300);
		$(this.button).removeClass("active");
	};
}

function showImage(id) {
	clearTimeout(counter);
	if(currentId != null) items[currentId].hide();
	if(id > 4) id = 0;
	items[id].show();
	currentId = id;
	counter = setTimeout(function(){showImage(currentId + 1)}, 4000);
}

function init() {
	$("div#carousel").html('<h3 class="access">Promotions</h3>'+"\n"+
		'<ul class="images">'+"\n"+
		'<li><img src="images/homepage/carousel/bigbang.jpg" alt="Snowboard Coach at the TSA Big Bang Show" /></li>'+"\n"+
		'<li><img src="images/homepage/carousel/hillside_lesson.jpg" alt="Instructor Training - Teaching on the Steeps" /></li>'+"\n"+
		'<li><img src="images/homepage/carousel/stiffy.jpg" alt="Mountain Freestyle - Old Skool Stiffy" /></li>'+"\n"+
		'<li><img src="images/homepage/carousel/soularchmethod.jpg" alt="Freestyle @ MK - Soul Arch Method" /></li>'+"\n"+
		'<li><img src="images/homepage/carousel/variables.jpg" alt="Instructor Training - Riding Variables" /></li>'+"\n"+
		'</ul>'+"\n"+
		'<div class="controls">'+"\n"+
		'<p>Lorem ipsum</p>'+"\n"+
		'<ul>'+"\n"+
		'<li><a href="" class="1">Snowboard Coach at the TSA Big Bang Show</a></li>'+"\n"+
		'<li><a href="" class="2">Instructor Training - Teaching on the Steeps</a></li>'+"\n"+
		'<li><a href="" class="3">Mountain Freestyle - Old Skool Stiffy</a></li>'+"\n"+
		'<li><a href="" class="4">Freestyle @ MK - Soul Arch Method</a></li>'+"\n"+
		'<li><a href="" class="5">Instructor Training - Riding Variables</a></li>'+"\n"+
		'</ul>'+"\n"+
		'</div>'+"\n"
	);	
}

$(document).ready(function() {	
	init();
						   
	$("div#carousel ul.images li").each(function(i) {
		var item = new Item($(this), $(this).children("img").attr("alt"), $("div#carousel div.controls ul li a."+(i+1)));
		items.push(item);		
	});
	
	$("div#carousel div.controls ul li a").click(function() {
		showImage(parseInt($(this).attr("class") - 1));
		return false;
	});
	
	showImage(0);
});
