/*jslint browser:true,indent:2,white:false,nomen:false,plusplus:false*/
/*global YAHOO,window,YUI */

YUI().use('node-event-delegate', function(Y) {
	
	var groupNavEl,
      groupClass,
      tableString,
      switchTabs,
      hideTabs,
      delegateTabs;
      
	// Event Listener to Switch Tabs
	switchTabs = function(e) {
		e.preventDefault();
		// Get the list element which is the parent of the anchor
		groupNavEl = e.currentTarget.get('parentNode');
		groupClass = groupNavEl.get('className');
		
		// Only initiate if the tab does not have class selected
		if( !groupNavEl.hasClass('selected') ) {
			// Remove current selected tab class and add it to the tab clicked on
			Y.one('#ygs-group_standings div.bd div ol .selected').removeClass('selected');
			groupNavEl.addClass('selected');
			// Switch Classes on the Table to show table which has the same class as tab clicked
			Y.one('#ygs-group_standings div.bd table.selected').removeClass('selected');
			// Take care of other classes used so when the selector query is made it used the group in concern
			groupClass = groupClass.replace('last ','');
			groupClass = groupClass.replace('first ','');
			tableString = '#ygs-group_standings div.bd table.'+groupClass;
			Y.one(tableString).addClass('selected');
		}
	};
	
	// Not using this for now because IE sucks at using onAvailable
	hideTabs = function() {
		// Hide Tabs first
		Y.one('body').addClass('js');
	};
	
	// To improve experience when using tabs ad js class on body
	//Y.on("available",hideTabs, 'body');
	
	delegateTabs = function() { 
		// Initiate the Listener Now
		Y.one('#ygs-group_standings').delegate("click", switchTabs, 'div.bd .ygs-tabs ol li a');
	};
	
	// Delegate Listener for tabs when dom is ready
	Y.on("domready", delegateTabs);
	
	
});
