YAHOO.namespace("newGameScroller");
var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

YAHOO.newGameScroller.featuredGame = function(featuredGameElementID) {
	this.init(featuredGameElementID);
};

YAHOO.newGameScroller.featuredGame.prototype = {
	highlightClass : 'outline',
	gameNames : {},
	curIndex : 1,
	prevIndex : 1,
	loading : false,
	
	autoPlay : null,
	scrollDelay : 7000,
	dataUrl : '/Includes/getGameInfo.php',
	data : null,

	init: function(featuredGameElementID) {
		this.data = new Object();
		var me = this;
		var games = DOM.getElementsBy(function(el) { return true; }, 'li', featuredGameElementID);

		for(var i = 0; i < games.length; i++) {
			this.gameNames[i+1] = 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 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);
	},

	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);
		if(this.curIndex != 1 && this.curIndex > 6) {
			this.curIndex = 1;
		}

		this.stopAutoScroll();
		this.addHighlight(this.curIndex);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex]); }, 360);
	},

	scrollPrev: function() {
		var me = this;
		this.prevIndex = this.curIndex;
		this.curIndex--;
		
		this.removeHighlight(this.prevIndex);
		if(this.curIndex != 6 && this.curIndex < 1) {
			this.curIndex = 6;
		}
		
		this.stopAutoScroll();
		this.addHighlight(this.curIndex);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex]); }, 360);
	},

	scrollGame: function(gameIndex) {
		var me = this;
		this.prevIndex = this.curIndex;
		
		this.removeHighlight(this.prevIndex);
		if(gameIndex < 1) this.curIndex = 6;
		else if(gameIndex > 6) this.curIndex = 1;
		else this.curIndex = gameIndex;

		this.stopAutoScroll();
		this.addHighlight(this.curIndex);
		setTimeout(function() { me.changeFeaturedGame(me.gameNames[me.curIndex]); }, 360);
	},

	removeHighlight: function(index) {
		DOM.removeClass('free-game-' + index, this.highlightClass);
		
		attributes = { opacity: { from: 1, to: 0 } };
		anim = new YAHOO.util.Anim(DOM.get('featuredNewGame').parentNode, attributes, 0.6, YAHOO.util.Easing.backOut);
		anim.animate();
	},

	addHighlight: function(index) {
		DOM.addClass('free-game-' + index, this.highlightClass);
	},

	changeFeaturedGame: function(gameName) {
		if(!this.loading) {
			featuredGameDiv = DOM.get('featuredNewGame');
			
			var callback = {
				success : this.handleSuccess,
				failure : this.handleFailure,
				argument: { name: gameName },
				scope : this
			}

			this.loading = true;
			if(!this.data[this.curIndex])
				var request = YAHOO.util.Connect.asyncRequest('POST', this.dataUrl, callback, 'game=' + gameName);
			else
				this.updatePage();
		}
	},

	updatePage: function() {
		var featuredGameDiv = DOM.get('featuredNewGame');
		var featuredGameDivChildren = DOM.getChildren(featuredGameDiv);
		var playBtn = DOM.getChildren(featuredGameDivChildren[2])[0];
		var currentGameIndex = this.curIndex;

		featuredGameDivChildren[0].innerHTML = this.data[currentGameIndex]['name'];
		featuredGameDivChildren[1].innerHTML = '<span class="joystickMini' + this.data[currentGameIndex]['rating'] + '">Current Rating: ' + this.data[this.curIndex]['rating'] + '/' + this.data[this.curIndex]['count'] + ' Votes</span>'; //rating
		featuredGameDivChildren[2].childNodes[0].nodeValue  = this.data[currentGameIndex]['desc']; //desc
		playBtn.href = this.data[currentGameIndex]['link'] + '?s=new'; //playBtn

		featuredGameDiv.previousSibling.previousSibling.href = this.data[currentGameIndex]['link'] + '?s=new'; // gameImg link
		featuredGameDiv.previousSibling.previousSibling.firstChild.src = 'http://assets.freearcade.com/thumbnails/250x180/' +this.data[currentGameIndex]['image']; //gameImg
		featuredGameDiv.previousSibling.previousSibling.firstChild.alt = 'Play ' + this.data[currentGameIndex]['name']; //gameImg alt

		this.loading = false;

		attributes = { opacity: { from: 0, to: 1 } };
		anim = new YAHOO.util.Anim(featuredGameDiv.parentNode, attributes, 0.8, YAHOO.util.Easing.easeBoth);
		anim.animate();
		this.startAutoScroll();
	},

	handleSuccess: function(o) {
		if(o.responseText !== undefined) {
			var data = eval('(' + o.responseText + ')');
			if(data.game[0].error == 'false') {
				this.data[this.curIndex]			= new Object();
				this.data[this.curIndex]['name']	= o.argument.name;
				this.data[this.curIndex]['link']	= data.game[0].link;
				this.data[this.curIndex]['rating']	= data.game[0].rating;
				this.data[this.curIndex]['count']	= data.game[0].count;
				this.data[this.curIndex]['image']	= data.game[0].img;
				this.data[this.curIndex]['desc']	= data.game[0].desc;

				this.updatePage();
			}
		}
	},

	handleFailure: function(o) {
		if(o.responseText !== undefined) {
			alert('Whoops, something went amiss.\nWe\'re working on it though, try again soon.');
		}
	}
};

