// main class that load event fires the init from 
var indexClass =  {
	thumbObj: null,
	init: function() {
		indexClass.setKeywords();
		indexClass.setFeatureTabs();
		try {
			indexClass.setRefresh();
			//indexClass.setNewsAnalytics();
			// indexClass.setGenreAnalytics();
			// indexClass.setDailySixAnalytics();
			// indexClass.setBFAnalytics();			
		} catch(err) {
			//fail
		}
		// remove comment to reinstate ad refresh 
		//indexClass.setAdRefresh();
	},
	setKeywords: function() {
		if($('keywords')) Event.observe($('keywords'), 'focus', function() {
			$('keywords').style.backgroundImage = 'none';
		});
	},
	setFeatureTabs: function() {
		if ($$('.features').length != 0) {
			if ($$('div.features')[0].select('div.content-target').length != 0) {
				if($$('.scroller-wrap').length != 0) {
					indexClass.thumbObj = new featureThumbScroller($$('div.scroller-wrap')[0],$$('div.scroller-wrap')[0].select('ul.stories')[0].select('li').length);
					new subTabClass($$('div.features')[0].select('div.content-target')[0]);
				}

			}
		}
		if ($$('.release-cal').length != 0) {
			new tabClass($$('.release-cal')[0]);
		}
		
	},
	setAdRefresh: function() {
		if (settings.pageType == 'platform-index' || settings.pageType == 'main-index') {
			dart.adRefreshTimer('leaderboard');
			dart.adRefreshTimer('ad');
		}
	},
	
	setRefresh: function() {
		(function(){
			window.location.reload();
		}).delay(600);
	},

	// click-tracking set and handler methods
	setNewsAnalytics: function() {
		try {
			var test = $$('.top-content')[0].select('.top');
		} catch (error) {
			return false;
		}
		var action = '';
		if (!settings.pagePlatform) {
			action = 'homepage-top-stories';
		} else if (settings.pagePlatform == 'free-games' || settings.pageType == 'gamefinder') {
			return false;
		} else {
			action = settings.pagePlatform + '-top-rated-games';
		}
		// do loop through links
		$$('.top-content')[0].select('.top')[0].select('a').each(function(linkAt) {
			linkAt.rel = linkAt.href;
			linkAt.onclick = function() {
				indexClass.newsAnalytics(linkAt.rel,action);
				return false;
			}
			linkAt.href = '#';
		});
	},
	newsAnalytics: function(urlPath,googAction) {
		goog.trackIndexClick(urlPath,googAction);
		var jumpLag = window.setTimeout(function(){
			window.location.href = urlPath;
		},100);
	},
	setDailySixAnalytics: function() {
		if (!$('top_lists')) { return false; }
		var target = $('top_lists');
		var dataStruct = {
			"cat": "index-clicks",
			"action": "daily-six",
			"label": "",
			"url": ""
		}
		// title link
		var titleData = dataStruct;
		titleData.label = 'title';
		var titleLink = target.select('h2')[0].select('a')[0];
		titleLink.rel = titleLink.href;
		titleData.url = titleLink.rel;
		titleLink.onclick = function() {
			indexClass.dailySixAnalytics(titleData);
			return false;
		}
		titleLink.href = '#';
		
		// comments
		var commentData = dataStruct;
		commentData.label = 'comments';
		var commentLink = target.select('ul.bread')[0].select('a')[0];
		commentLink.rel = commentLink.href;
		commentData.url = commentLink.rel;
		commentLink.onclick = function() {
			indexClass.dailySixAnalytics(commentData);
			return false;
		}
		commentLink.href = '#';
		
		// image
		var imageData = dataStruct;
		imageData.label = 'image';
		var imageLink = target.select('dl.first')[0].select('a')[0];
		imageLink.rel = imageLink.href;
		imageData.url = imageLink.rel;
		imageLink.onclick = function() {
			indexClass.dailySixAnalytics(imageData);
			return false;
		}
		imageLink.href = '#';
		
		// prod page links
		target.select('dl.list').each(function(listAt) {
			listAt.select('a').each(function(linkAt) {
				var linkData = {
					"cat": "index-clicks",
					"action": "daily-six",
					"label": "prod-link",
					"url": ""
				}
				linkAt.rel = linkAt.href;
				linkData.url = linkAt.rel;
				linkAt.onclick = function() {
					indexClass.dailySixAnalytics(linkData);
					return false;
				}
				linkAt.href = '#';
			});
		});
		
		// more
		var moreData = dataStruct;
		moreData.label = 'more';
		var moreLink = target.select('p.more')[0].select('a')[0];
		moreLink.rel = moreLink.href;
		moreData.url = moreLink.rel;
		moreLink.onclick = function() {
			indexClass.dailySixAnalytics(moreData);
			return false;
		}
		moreLink.href = '#';
	},
	dailySixAnalytics: function(obj) {
		goog.generalTracker(obj);
		var lagTimer = window.setTimeout(function(){
			window.location.href = obj.url;
		},100);
	},
	setGenreAnalytics: function() {
		if (settings.pagePlatform == '' || $$('.content.third').length == 0) { return false; }
		$$('.content.third')[0].select('div').each(function(divAt) {
			var action = 'genre-' + settings.pagePlatform + '-';
			action += divAt.select('h2')[0].innerHTML.strip().toLowerCase();
		    divAt.select('a').each(function(linkAt) {
		        linkAt.rel = linkAt.href;
				linkAt.onclick = function() {
					indexClass.genreAnalytics(linkAt.rel,action);
					return false;
				}
				linkAt.href = '#';
		    });
		});
	},
	genreAnalytics: function(url,action) {
		goog.trackIndexClick(url,action);
		var jumpLag = window.setTimeout(function(){
			window.location.href = url;
		},100);
	},
	setBFAnalytics: function() {
		if ($$('.network-cont').length == 0) { return false; }
		$$('.network-cont')[0].select('a').each(function(linkAt) {
			linkAt.rel = linkAt.href;
			var action = "blogfaction";
			if (!settings.pagePlatform) {
				action += '-hompage';
			} else {
				action += '-' + settings.pagePlatform;
			}
			var dataStruct = {
				"cat": "index-clicks",
				"action": action,
				"label": linkAt.title.strip() + ' - ' + linkAt.innerHTML.strip(),
				"url": linkAt.rel
			}
			linkAt.onclick = function() {
				indexClass.bfAnalytics(dataStruct);
				return false;
			}
			linkAt.href = '#';
		});
	},
	bfAnalytics: function(obj) {
		goog.generalTracker(obj);
		var lagTimer = window.setTimeout(function() {
			window.location.href = obj.url;
		}, 100);
	}
}

var tabClass = Class.create({
	tabs: [],
	contentTabs: [],
	contentStruct: null,
	ajaxPath: null,
	initialize: function(targetNode) {
		this.targetNode = targetNode;
		this.setTabs();
		starCheck.init();
	},
	setTabs: function() {
		var myClass = this;
		
		this.tabs = $A(this.targetNode.select('.tab'));
		this.ajaxPath = this.targetNode.id.replace(/_/gi, "/");
		this.tabs.each(function(tabAt) {
			Event.observe(tabAt, 'click', function() {
				myClass.changeTab(tabAt);
				goog.captureTabClick(tabAt);
			});
		});
		this.contentTabs = $A(this.targetNode.select('div.content'));
	},
	changeTab: function(obj) {
		this.tabs.each(function(tabAt) {
			tabAt.removeClassName('on');
		});
		obj.addClassName('on');
		var thisClass = this;
		this.contentTabs.each(function(contentAt) {
			if (thisClass.tabs.indexOf(obj) == thisClass.contentTabs.indexOf(contentAt)) {
				contentAt.addClassName('on');
				thisClass.checkContent(contentAt);
			} else {
				contentAt.removeClassName('on');
			}
		});
	},
	checkContent: function(cont) {
		var contentTarget = cont.select('div.content-target')[0];
		var thisClass = this;
		var nodeCap = 1;
		if (sniff.isIE) { nodeCap = 1; }
		if (contentTarget.childNodes.length > nodeCap) { return; }
		if (!this.contentStruct) {
			new Ajax.Request('/ajax/get_tabs/' + thisClass.ajaxPath + '/', {
				method: 'get',
				onSuccess: function(t) {
					thisClass.contentStruct = $H(t.responseText.evalJSON()).toObject();
					thisClass.setContent(contentTarget);
				},
				onFailure: function(t) {
					
				}
			});
		} else {
			thisClass.setContent(contentTarget);
		}
	},
	setContent: function(obj) {
		var newContent = 'this.contentStruct.node_' + (this.contentTabs.indexOf(obj.parentNode) + 1);
		obj.update(eval(newContent));
	}
});

var subTabClass = Class.create({
	tabs: [],
	contentTabs: [],
	initialize: function(targetNode) {
		this.targetNode = targetNode;
		try {
			this.setTabs();
		} catch(err) {}
	},
	setTabs: function() {
		this.tabs = $A(this.targetNode.select('ul.stories')[0].select('li'));
		this.contentTabs = $A(this.targetNode.select('div.slot'));
		var thisClass = this;
		this.tabs.each(function(tabAt) {
			Event.observe(tabAt, 'mouseover', function(){
				thisClass.changeTabs(tabAt);
			});
		});
	},
	changeTabs: function(obj) {
		// TODO: implement later when docs on mouseover tracking are better
		//indexClass.featureTabAnalytics(obj);
		var thisClass = this;
		if (indexClass.thumbObj.progress == true) {return false}
		this.tabs.each(function(tabAt) {
			tabAt.removeClassName('on');
		});
		obj.addClassName('on');
		this.contentTabs.each(function(contentTabAt) {
			contentTabAt.removeClassName('on');
		});
		this.contentTabs[this.tabs.indexOf(obj)].addClassName('on');
		indexClass.thumbObj.indicator(obj);
	}
});

// check for star ratings and set them
var starCheck = {
	list: [],
	init: function() {
		if (starCheck.list.length == 0) {
			this.list = $A($$('.user-tab-stars'));
		}
		this.setStars();
	},
	setStars: function() {
		this.list.each(function(itemAt) {
			var starImage;
			if (itemAt.parentNode.parentNode.parentNode.className.indexOf("alt") != -1) {
				starImage = 'user-review-alt.png';
			} else {
				starImage = 'default.png';
			}
			new Starbox(itemAt, itemAt.id.split("_")[1],{
				overlay: starImage,
				locked: true
			});
		});
	}
}

var tabPaginator = {
	top200List: null,
	featuredList: null,
	oberonActionList: null,
	oberonArcadeList: null,
	oberonFeaturedList: null,
	oberonNewList: null,
	oberonSportsList: null,
	oberonTopList: null,
	scroll: function(dir,pNode) {
		var curListRaw = $(pNode.id).getElementsByTagName('ul')[0].id.split('_');
		var curListName = curListRaw[0];
		if(curListName == 'top25') curListName = 'top200';
		var curListIndex = curListRaw[1];
		var curListDesc = $(pNode.id).getElementsByTagName('p')[0].innerHTML;
		switch(curListName) {
			case 'top200':
				if (!tabPaginator.top200List) {
					var url = '/lib/content/feeds/mochi_' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.top200List = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
			
			case 'featured':
				if (!tabPaginator.featuredList) {
					var url = '/lib/content/feeds/mochi_' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.featuredList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
				
			case 'oberonAction':
				if (!tabPaginator.oberonActionList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonActionList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
				
			case 'oberonArcade':
				if (!tabPaginator.oberonArcadeList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonArcadeList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;

			case 'oberonFeatured':
				if (!tabPaginator.oberonFeaturedList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonFeaturedList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
				
			case 'oberonNew':
				if (!tabPaginator.oberonNewList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonNewList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
				
			case 'oberonSports':
				if (!tabPaginator.oberonSportsList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonSportsList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
			
			case 'oberonTop':
				if (!tabPaginator.oberonTopList) {
					var url = '/lib/content/feeds/' + curListName;
					var myAjax = new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(t){
							tabPaginator.oberonTopList = t.responseText.evalJSON(true);
							tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
						}
					});
				} else {
					tabPaginator.showNewData(dir,curListName,curListIndex,pNode,curListDesc);
				}
				break;
			
			default: return false;
		}
	},
	showNewData: function(dir,curList,curIndex,pNode,curListDesc) {
		switch(dir) {
			case 'next':
				var curListLen = eval('tabPaginator.' + curList + 'List.games.length');
				var output = '<p class="desc">' + curListDesc + '</p>';
				var startLoop = 10 - (-curIndex);
				var endLoop = 19 - (-curIndex);
				if (endLoop >= curListLen) endLoop = curListLen - 1;
				if((curIndex <= curListLen)&(startLoop < curListLen)) {
					for (var z=startLoop; z<=endLoop; z++) {
						output += ''
						+ '<ul id="' + curList + '_' + z + '">'
						+ '<li class="art">';
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						}
						output += '<img src="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].thumbnail_url') + '" alt="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '"/>'
						+ '</a>'
						+ '<li class="title">'
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						}
						output += eval('tabPaginator.' + curList + 'List.games[' + z + '].name')
						+ '</a>'
						+ '<li class="desc">';
						if(eval('tabPaginator.' + curList + 'List.games[' + z + '].description.length') > 230) {
							output += eval('tabPaginator.' + curList + 'List.games[' + z + '].description.substring(0,230)')
							+ '...';
						} else {
							output += eval('tabPaginator.' + curList + 'List.games[' + z + '].description');	
						}
						output += '<br />';
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">'
							+ 'Get It Now!';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">'
							+ 'Go Play Now!'
						}
						output += '</a>'
						+ '</li>'
						+ '</ul>';
					}
					$(pNode.id).getElementsByClassName('content-target')[0].update(output);
					$(pNode.id).getElementsByClassName('prev')[0].style.display = 'block';
					if(endLoop == curListLen) {
						$(pNode.id).getElementsByClassName('next')[0].style.display = 'none';
					}
					else {
						$(pNode.id).getElementsByClassName('next')[0].style.display = 'block';
					}
					$(pNode).parentNode.scrollTo();
				} else {
					$(pNode.id).getElementsByClassName('next')[0].style.display = 'none';
					$(pNode.id).getElementsByClassName('prev')[0].style.display = 'block';
				}
				break;
				
			case 'prev':
				var curListLen = eval('tabPaginator.' + curList + 'List.games.length');
				var output = '<p class="desc">' + curListDesc + '</p>';
				var startLoop = curIndex - 10;
				var endLoop = curIndex - 1;
				if(curIndex >= 10) {
					for (var z=startLoop; z<=endLoop; z++) {
						output += ''
						+ '<ul id="' + curList + '_' + z + '">'
						+ '<li class="art">';
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						}
						output += '<img src="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].thumbnail_url') + '" alt="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '"/>'
						+ '</a>'
						+ '<li class="title">'
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">';
						}
						output += eval('tabPaginator.' + curList + 'List.games[' + z + '].name')
						+ '</a>'
						+ '<li class="desc">';
						if(eval('tabPaginator.' + curList + 'List.games[' + z + '].description.length') > 230) {
							output += eval('tabPaginator.' + curList + 'List.games[' + z + '].description.substring(0,230)')
							+ '...';
						} else {
							output += eval('tabPaginator.' + curList + 'List.games[' + z + '].description');	
						}
						output += '<br />';
						if(curList == 'oberon') {
							output += '<a href="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].link') + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">'
							+ 'Get It Now!';
						} else {
							output += '<a href="/free-games/' + curList + '-' + (z+1) + '" title="' + eval('tabPaginator.' + curList + 'List.games[' + z + '].name') + '/">'
							+ 'Go Play Now!'
						}
						output += '</a>'
						+ '</li>'
						+ '</ul>';
					}
					$(pNode.id).getElementsByClassName('content-target')[0].update(output);
					$(pNode.id).getElementsByClassName('next')[0].style.display = 'block';
					if(startLoop == 0) {
						$(pNode.id).getElementsByClassName('prev')[0].style.display = 'none';
					}
					else {
						$(pNode.id).getElementsByClassName('prev')[0].style.display = 'block';
					}
					$(pNode).parentNode.scrollTo();
				} else {
					$(pNode.id).getElementsByClassName('prev')[0].style.display = 'none';
					$(pNode.id).getElementsByClassName('next')[0].style.display = 'block';
				}
				break;
			
			default: return false;
		}
	}
}


var featureThumbScroller = Class.create({
	obj: null,
	rightArrow: null,
	leftArrow: null,
	setCount: 1,
	unitWidth: 0,
	sliderTarget: null,
	maxLeft: 0,
	slideInc: 0,
	currentSlot: 1,
	maxViews: 4,
	progress: false,
	currentThumb: 0,
	maxSlot: 0,
	initialize: function(obj,setCount) {
		
		var thisClass = this;
		this.obj = obj;
		if (setCount) {
			this.setCount = setCount;			
		}
		this.rightArrow = this.obj.select('.arrows')[0].select('.forward')[0];
		this.leftArrow = this.obj.select('.arrows')[0].select('.back')[0];
		this.rightArrow.onclick = function() {
			thisClass.scroll('forward');
			return false;
		}
		this.leftArrow.onclick = function() {
			thisClass.scroll('back');
			return false;
		}
		this.sliderTarget = this.obj.select('.stories')[0];
		this.unitWidth = this.sliderTarget.select('li')[0].getWidth() + parseInt(10);
		this.maxLeft = this.unitWidth * this.setCount;
		this.slideInc = this.unitWidth * this.maxViews;
		this.maxSlot = Math.ceil(this.setCount/this.maxViews);
		thisClass.buttonCheck(this.currentSlot);
	},
	scroll: function(type) {
		var thisClass = this;
		if (thisClass.progress == true) {return false};
		thisClass.progress = true;
		switch(type) {
			case "forward":
				if (thisClass.sliderTarget.style.left || thisClass.sliderTarget.style.left != 0) {
					var newNum = parseInt(thisClass.sliderTarget.style.left.split("px")[0]) - thisClass.slideInc;
				} else {
					var newNum = -thisClass.slideInc;
				}
				if (Math.abs(newNum) < thisClass.maxLeft) {
					new Effect.Morph(thisClass.sliderTarget, {style: 'left:' + newNum + 'px', duration: .75});
					thisClass.currentThumb = (thisClass.currentSlot * thisClass.maxViews);
					//for scroll effect to return to 1st position and highlight
					thisClass.changeTabs(thisClass.currentThumb);
					thisClass.currentSlot++;
					thisClass.buttonCheck(this.currentSlot);
				}
			break;
			default:
				var newNum = parseInt(thisClass.sliderTarget.style.left.split("px")[0]) + thisClass.slideInc;
				if (newNum <= 0) {
					new Effect.Morph(thisClass.sliderTarget, {style: 'left:' + newNum + 'px', duration: .75});
					thisClass.currentThumb = (thisClass.currentThumb) - parseInt(4);
					thisClass.changeTabs(thisClass.currentThumb);
					thisClass.currentSlot--;
					thisClass.buttonCheck(this.currentSlot);
				} 
			break;
		}
		(function() {
			thisClass.progress = false;	
		}).delay(.75);
	},
	//handles after scroll effect higlighting and positioning
	changeTabs: function(obj) {
		
		var thisClass = this;
		var tabs = thisClass.obj.select('.stories')[0].select('li');
		var slots = $$('.features')[0].select('.slot');
		$$('.indicator')[0].style.left = '-3333px';
		(function() {
			tabs.each(function(tabAt) {
				tabAt.removeClassName('on');
			});
			slots.each(function(slotAt) {
				slotAt.removeClassName('on');
			});
			tabs[obj].addClassName('on');
			slots[obj].addClassName('on');
			thisClass.indicator(tabs[obj]);
		}).delay(.75);
	},
	buttonCheck: function(num) {
		var thisClass = this;
		switch(num) {
			case 1:
				thisClass.obj.select('.back')[0].addClassName('off');
				if (thisClass.maxSlot == 1) {
					thisClass.obj.select('.forward')[0].addClassName('off');
				} else {
					thisClass.obj.select('.forward')[0].removeClassName('off');
				}
				
				break;
			case thisClass.maxSlot:
				thisClass.obj.select('.back')[0].removeClassName('off');
				thisClass.obj.select('.forward')[0].addClassName('off');
				break;
			default: 
				var arrows = thisClass.obj.select('.arrows')[0].select('li');
				arrows.each(function(arrowsAt) {
					arrowsAt.removeClassName('off');
				});
				break;
		}
		
	},
	indicator: function(obj) {
		var originPos = $$('.stories')[0].offsetLeft
		var newPos = parseInt(obj.offsetLeft + 85 + originPos);
		$$('.indicator')[0].style.left =  newPos + 'px';
	}
});

var thumbScroller = Class.create({
	obj: null,
	autoScroll: false,
	rightArrow: null,
	leftArrow: null,
	setCount: 1,
	unitWidth: 0,
	sliderTarget: null,
	maxLeft: 0,
	slideInc: 0,
	currentSlot: 1,
	initialize: function(obj,setCount) {
		var thisClass = this;
		this.obj = obj;
		if (setCount) {
			this.setCount = setCount;			
		}
		this.rightArrow = this.obj.select('.arrows')[0].select('.forward')[0];
		this.leftArrow = this.obj.select('.arrows')[0].select('.back')[0];
		this.rightArrow.onclick = function() {
			if (thisClass.autoScroll) {
				thisClass.autoScroll = false;
			}
			thisClass.scroll('forward');
			return false;
		}
		this.leftArrow.onclick = function() {
			if (thisClass.autoScroll) {
				thisClass.autoScroll = false;
			}
			thisClass.scroll('back')
			return false;
		}
		this.sliderTarget = this.obj.select('.thumb_list')[0];
		this.unitWidth = this.sliderTarget.select('li')[0].getWidth();
		this.maxLeft = this.unitWidth * this.sliderTarget.select('li').length;
		this.slideInc = thisClass.unitWidth * thisClass.setCount;
	},
	scroll: function(type) {
		var thisClass = this;
		switch(type) {
			case "forward":
				if (thisClass.sliderTarget.style.left || thisClass.sliderTarget.style.left != 0) {
					var newNum = parseInt(thisClass.sliderTarget.style.left.split("px")[0]) - thisClass.slideInc;
				} else {
					var newNum = -thisClass.slideInc;
				}
				if (Math.abs(newNum) < thisClass.maxLeft) {
					thisClass.sliderTarget.morph('left:' + newNum + 'px' );
					thisClass.currentSlot++;
				}
			break;
			default:
				var newNum = parseInt(thisClass.sliderTarget.style.left.split("px")[0]) + thisClass.slideInc;
				if (newNum <= 0) {
					thisClass.sliderTarget.morph('left:' + newNum + 'px' );
					thisClass.currentSlot--;
				}
			break;
		}
		
	}
});

var subIndex = {
	init: function() {
		subIndex.setScroller();
		subIndex.setGameFinder();
	},
	setScroller: function() {
		if ($$('.scroll-cont').length == 0) { return false ;}
		var scrollExtras = Class.create(thumbScroller, {
			scroll: function($super,type) {
				$super(type);
				if ((this.currentSlot*this.setCount) - this.setCount != 0) {
					var newText = ((this.currentSlot*this.setCount) - this.setCount) + ' - ' + this.currentSlot*this.setCount;
				} else {
					var newText = '1 - ' + this.currentSlot*this.setCount;
				}
				$('scrollCounter').update(newText);
			}
		});
		topRatedGames = new scrollExtras($$('.scroll-cont')[0],4);
		topRatedGames.autoScroll = true;
		subIndex.setTopRatedGamesTimer();
		$$('.top-rated')[0].select('.restart')[0].onclick = function() {
			subIndex.topRatedReset();
			return false;
		}
	},
	setTopRatedGamesTimer: function() {
		var scrollTimer = subIndex.topRatedScroll.delay(5);
	},
	topRatedScroll: function() {
		if (!topRatedGames.autoScroll) { return false;}
		topRatedGames.scroll('forward');
		if (topRatedGames.currentSlot <= 4) {
			subIndex.setTopRatedGamesTimer();			
		} else if (topRatedGames.currentSlot == 5) {
			subIndex.topRatedReset.delay(5);
		}
	},
	topRatedReset: function() {
		topRatedGames.autoScroll = false;
		topRatedGames.currentSlot = 1;
		topRatedGames.sliderTarget.morph('left: 0px',{duration: 2});
		var newText = '1 - 4';
		$('scrollCounter').update(newText);
		topRatedGames.autoScroll = true;
		subIndex.setTopRatedGamesTimer();
	},
	setGameFinder: function() {
		if (!$('platform')) { return false };
		$('platform').select('option').each(function(optionAt,index){
		    if (optionAt.value == settings.pagePlatform) {
		         $('platform').selectedIndex = index;
		    }
		});
	}
}

document.observe('dom:loaded',indexClass.init);
document.observe('dom:loaded',subIndex.init);