YAHOO.namespace("gamesScroller");
var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

YAHOO.gamesScroller.topDownloadable = function(scollerElem) {
	this.init(scollerElem);
};

YAHOO.gamesScroller.topDownloadable.prototype = {
	highlightClass : 'outline',
	gameNames : new Array(),
	curIndex : 1,
	prevIndex : 1,
	featuredGameElem : null,
	
	autoPlay : null,
	scrollDelay : 7000,
	
	init: function(divContainer) {
		var me = this;
		games = DOM.getElementsBy(function(el) { return true; }, 'li', divContainer);
		
		for(var i = 0; i < games.length; i++) {
			this.gameNames[i] = games[i].firstChild.lastChild.nodeValue;
			//var count = i + 1;
			//ele = DOM.get('free-game-' + count).firstChild;
			//ele = games[i].firstChild;
			
			//function callbackGameClick(e) { EVT.preventDefault(e); alert(count); me.stopAutoScroll(); me.scrollGame(count), me; }
			//EVT.on(ele, 'click', callbackGameClick);
		}

		function callbackGameClick1(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(1), me; }
		EVT.on(DOM.get('free-game-1').firstChild, 'click', callbackGameClick1);
		function callbackGameClick2(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(2), me; }
		EVT.on(DOM.get('free-game-2').firstChild, 'click', callbackGameClick2);
		function callbackGameClick3(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(3), me; }
		EVT.on(DOM.get('free-game-3').firstChild, 'click', callbackGameClick3);
		function callbackGameClick4(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(4), me; }
		EVT.on(DOM.get('free-game-4').firstChild, 'click', callbackGameClick4);
		function callbackGameClick5(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(5), me; }
		EVT.on(DOM.get('free-game-5').firstChild, 'click', callbackGameClick5);
		function callbackGameClick6(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(6), me; }
		EVT.on(DOM.get('free-game-6').firstChild, 'click', callbackGameClick6);
		function callbackGameClick7(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollGame(7), me; }
		
        function callbackNext(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollNext(), me; }
        function callbackPrev(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollPrev(), me; }
        EVT.on('next', 'click', callbackNext);
        EVT.on('prev', 'click', callbackPrev);
		
		this.startAutoScroll();
	},
	
	startAutoScroll: function() {
		if(this.autoPlay !== null)
			return;
		
		var me = this;
		this.autoPlay = setInterval(function() { me.scrollNext(); }, this.scrollDelay); //setInterval
	},
	
	stopAutoScroll: function() {
		if(this.autoPlay !== null) {
			clearTimeout(this.autoPlay);
			this.autoPlay = null;
		}
	},
	
	scrollNext: function() {
		var me = this;
		this.prevIndex = this.curIndex;
		this.curIndex++;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		if(this.curIndex != 1 && this.curIndex > this.gameNames.length) {
			this.curIndex = 1;
		}
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex-1]); }, 350);
	},
	
	scrollPrev: function() {
		var me = this;
		this.prevIndex = this.curIndex;
		this.curIndex--;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		if(this.curIndex != this.gameNames.length && this.curIndex < 1) {
			this.curIndex = this.gameNames.length;
		}
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex-1]); }, 350);
	},
	
	scrollGame: function(gameIndex) {
		var me = this;
		this.prevIndex = this.curIndex;
		
		if(gameIndex < 1) this.curIndex = this.gameNames.length;
		else if(gameIndex > this.gameNames.length) this.curIndex = 1;
		else this.curIndex = gameIndex;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex-1]); }, 350);
	},
	
	removeHighlight: function(index, nodeClass) {
		DOM.removeClass('free-game-' + index, nodeClass);
		
		attributes = { opacity: { from: 1, to: 0 } };
		anim = new YAHOO.util.Anim(DOM.get('featuredDownloadGame').parentNode, attributes, 0.6, YAHOO.util.Easing.backOut);
		anim.animate();
	},
	
	addHighlight: function(index, nodeClass) {
		DOM.addClass('free-game-' + index, nodeClass);
	},
	
	changeFeaturedGame: function(gameName) {
		var featuredGameDiv = DOM.get('featuredDownloadGame');
		var featuredGameDivChildren = DOM.getChildren(featuredGameDiv);
		var playBtn = DOM.getChildren(featuredGameDivChildren[3])[0];

		featuredGameDivChildren[0].innerHTML = gameArray.real[this.curIndex-1].name;
        featuredGameDivChildren[1].innerHTML = '<span class="joystickMini' + gameArray.real[this.curIndex-1].rating + '">Current Rating: ' + gameArray.real[this.curIndex-1].rating + '</span>'; //rating
        featuredGameDivChildren[2].childNodes[0].nodeValue  = gameArray.real[this.curIndex-1].desc; //desc
        playBtn.href = gameArray.real[this.curIndex-1].link; //playBtn
		
        featuredGameDiv.previousSibling.previousSibling.href = gameArray.real[this.curIndex-1].link; // gameImg link
        featuredGameDiv.previousSibling.previousSibling.firstChild.src = gameArray.real[this.curIndex-1].imgbig; //gameImg
        featuredGameDiv.previousSibling.previousSibling.firstChild.alt = 'Download ' + gameArray.real[this.curIndex-1].name; //gameImg alt

        attributes = { opacity: { from: 0, to: 1 } };
		anim = new YAHOO.util.Anim(DOM.get('featuredDownloadGame').parentNode, attributes, 0.6, YAHOO.util.Easing.backOut);
        anim.animate();
	}
};
