window.featureList = new Object;
featureList.currentIndex = 0;
featureList.features = [];
featureList.fadeOutStrength = 1;
//for when the content is implemented -- root node for the information about the featured items
featureList.baseHref = '';

function spawnFeature(image,url) {
	var feature = new Object;
	feature.image = image;
	feature.url = url;
	featureList.features.push(feature);
}

//==== TESTING ====================
//==== TODO: PRELOAD IMAGES =======
spawnFeature("/images/home_large_4.jpg","/news-and-events/IndulgeDadtheTowers.cfm");
spawnFeature("/images/home_large_2.jpg","/vip/kids-club.cfm");
spawnFeature("/images/home_large_3.jpg","http://www.apple.com/au/retail/castletowers/");

j(document).ready(function() {
	currentImage = featureList.currentIndex;
	timeout = null;
	j('.featureBottomListItem img').hover(featureListHoverIn,featureListHoverOut);
	j('.featureBottomListItem img').fadeTo('slow',featureList.fadeOutStrength);
	loadContent(featureList.currentIndex);
	j(j('.featureBottomListItem img')[featureList.currentIndex]).stop(true);
	j(j('.featureBottomListItem img')[featureList.currentIndex]).fadeIn(0);
	j('.featureBottomListItem img').each(function(i,el) {
		j(el).click(function() {
			j('.featureBottomListItem img').fadeTo('slow',featureList.fadeOutStrength);
			j(this).stop(true);
			j(this).fadeTo(0,1.0);
			featureList.currentIndex = getCurrentFeatureIndex(this);
			loadContent(featureList.currentIndex);
			clearTimeout(timeout);
			timeout = setTimeout('cycleImage(featureList.currentIndex);',8000);
		});
	});
	timeout = setTimeout('cycleImage(featureList.currentIndex);', 8000);
});

function getCurrentFeatureIndex(element) {
	var index = -1;
	j('.featureBottomListItem img').each(function(i,el) {
		if (el === element) {
			index = i;
		}
	});
	return index;
}

function loadContent(index) {
	var speed = 400;
	j('.featureTopImage').fadeOut(speed,function() {	
		j('.featureTopImage img').attr('src',featureList.features[index].image);	
		j('.featureTopImage').fadeIn(speed);
		j('.featureTopImage a').attr('href',featureList.baseHref + featureList.features[index].url);
	});	
}

function featureListHoverIn(el) {
	//j('.featureLeftListInfoPopup').stop(true);
	j(el.target).stop(true,true);
	j(el.target).fadeTo('slow',1.0);
	/*j('.featureLeftListInfoPopup').animate({ left: j(el.target).position().left },
													{ complete: function() {
														j(this).html(featureList.features[getCurrentFeatureIndex(el.target)].name);
													}});*/
}

function featureListHoverOut(el) {
	if (!(el.target === j('.featureBottomListItem img')[featureList.currentIndex]))
	{
		j(el.target).fadeTo('fast',featureList.fadeOutStrength);
	}

	/*j('.featureLeftListInfoPopup').animate({ left: j(j('.featureLeftListItem img')[featureList.currentIndex]).position().left },
													{ complete: function() {
														j(this).html(featureList.features[featureList.currentIndex].name);
													}});*/
}

function cycleImage(currentIndex) {   
	nextImage = currentIndex + 1;
	if(nextImage > 2) nextImage = 0;
	j(j('.featureBottomListItem img')[currentImage]).fadeTo('fast',featureList.fadeOutStrength);
	j(j('.featureBottomListItem img')[nextImage]).fadeTo('slow',1.0);
	rotate(nextImage);
	currentImage=nextImage;
	timeout = setTimeout('cycleImage(currentImage);',8000);
}

function rotate(nextImg) {
	loadContent(nextImg);
}
