function TabGroup2( configUrl )
{
	this.configUrl = configUrl;
	this.conf = null;
	
	this.style = Array();
	
	this.requestParams = Array();
}
TabGroup2.prototype.init = function ()
{
	var q = String(document.location.search).substr(1).split('&');
	var p = null;
	for(var i=0; i<q.length; i++)
	{
		p = q[i].split('=');
		this.requestParams[p[0]] = p[1];
	}

	if( ajax || (this.conf == null) )
		return ajax.ownedQuery( this.configUrl, this, "setTabs", true );
}
TabGroup2.prototype.setTabs = function ( configXML )
{
	this.conf = configXML;
	
	var styles = this.conf.getElementsByTagName("tabStyle");
	var l = styles.length;

	for( var i=0; i<l; i++ )
	{
		var id = styles.item(i).getAttribute('styleId');
		this.style[id] = {activeClass:styles.item(i).getAttribute("activeClassName"),inactiveClass:styles.item(i).getAttribute("inactiveClassName")};
	}
	var tabgroups = this.conf.getElementsByTagName("tabGroup");
	l = tabgroups.length;
	for( i=0; i<l; i++ )
	{
		var divId = tabgroups.item(i).getAttribute("groupId");
		var resultDiv = document.getElementById( divId );
		if( !resultDiv )
			continue;
			
		var tabs = tabgroups.item(i).getElementsByTagName("tab");
		var styleId = tabgroups.item(i).getAttribute("styleId");
		var l2 = tabs.length;
		var div = null;
		for( var k = 0; k<l2; k++ )
		{
			div = document.getElementById( tabs.item(k).getAttribute("tabId") );
			if( div )
			{
				div.caller = this;
				div.onclick = this.getData;
				div.activeClass = this.style[styleId].activeClass;
				div.inactiveClass = this.style[styleId].inactiveClass;

				var lnk = div.getElementsByTagName("A");
				for( var j=0; j<lnk.length; j++ )
					lnk.item(j).onclick = this.Stub;
				
				var scripts = tabs.item(k).getElementsByTagName("script");
				var sc2 = Array();
				if( scripts.length > 0 )
				{
					for(var j=0; j<scripts.length;j++)
					{
						sc2[j] = scripts.item(j).getAttribute("src");
					}
				}
				
				var params = tabs.item(k).getElementsByTagName("param");
				var pr = "&";
				if( params.length > 0 )
					for(j=0;j<params.length;j++)
					{
						if( this.requestParams[params.item(j).getAttribute('name')] != "" )
							pr = pr + (params.item(j).getAttribute('name')) + "=" + (this.requestParams[params.item(j).getAttribute('name')])+"&";
					}
				
				div.confObj = { self:div, 
								parentNode:resultDiv,
								tabs:tabs,
								href:tabs.item(k).getAttribute('href') + pr,
								scripts:sc2
							};
				if( div.className.indexOf(div.activeClass) == 0 )
				{
					this.updateLinks( resultDiv.getElementsByTagName("A"), div.confObj );
				}
			}
		}
	}
}
TabGroup2.prototype.Stub = function()
{
	return false;
}
TabGroup2.prototype.getData = function( respText )
{
	var div = this.confObj.parentNode;

	if( this.is_clicked && respText )
	{
		div.innerHTML = respText;
		var tabs = this.confObj.tabs;
		var l = tabs.length;
		var tab = null;
		for (var i=0;i<l; i++)
		{
			if( tab = document.getElementById(tabs.item(i).getAttribute('tabId')) )
				if( tab.id != this.id )
					tab.className = this.inactiveClass;
		}
		this.className = this.activeClass;
		this.is_clicked = false;
		
		this.caller.updateLinks( div.getElementsByTagName("A"), this.confObj );
		if( this.confObj.scripts.length > 0 )
			for( i=0; i<this.confObj.scripts.length; i++ )
			{
				var sc = document.createElement("SCRIPT");
				sc.setAttribute("type","text/javascript");
				sc.setAttribute("src",this.confObj.scripts[i] );
				div.appendChild( sc );
			}
		this.caller.setTabs( this.caller.conf );
		return true;
	}
	else if( !this.is_clicked )
	{
		var url = this.confObj.href;

		if( ajax.is_busy )
			return false;
		
		if( ajax.ownedQuery(url, this, "onclick", false) )
		{
			this.is_clicked = true;
			div.innerHTML = "<div style='text-align: center; padding: 4px;'><img src='images/loading2.gif' width='23' height='23' alt='Loading...'/></div>";
		}
	}
}
TabGroup2.prototype.updateLinks = function( lnks, confObj )
{
	var l = lnks.length;
	var href = confObj.href;
	for(var i=0; i<l; i++ )
	{
		if( lnks.item(i).className.match(/.*ajaxLink.*/) )
		{
			var params = lnks.item(i).href.split("?");
			if( params[1] )
			{
				var url = href+"&"+params[1];
				lnks.item(i).ajaxUrl = url;
				lnks.item(i).onclick = function () {
														if( ajax.ownedQuery(this.ajaxUrl,this.caller,"onclick",false) )
															this.caller.is_clicked = true;
														return false;
													}
				lnks.item(i).caller = confObj.self;

			}
		}
	}
}
TabGroup2.prototype.setData = function( respText )
{
	return false;
}


function tabObject( objId )
{
	this.div = document.getElementById( objId );
	this.tabs = null;
}
tabObject.prototype.tab = function ( id, url )
{
	var tab = document.getElementById(id);
	if( !tab )
		return false;
	
}


var Sections = {forum_hash:"", league_hash:"", horses_hash:""};
function checkUpdate( respXML )
{
	var location = document.location.href;
	if( location.match(/.*(login=1).*/) )
		location = location.replace("login=1","1");
	if( location.match(/.*(logout=yes).*/) )
		location = location.replace("logout=yes","1");

	if( respXML )
	{
		var fp = respXML.getElementsByTagName("forumPosts");
		var lg = respXML.getElementsByTagName("league");
		var hr = respXML.getElementsByTagName("horsesTopTipped");
		var attr = "";
		
		if( fp.length > 0 )
		{
			attr = fp.item(0).getAttribute("hash");
			if( Sections.forum_hash != "" && Sections.forum_hash != attr ){
				if( location != document.location.href )
					return document.location.href = location;
				else
					return document.location.reload();
			}
			Sections.forum_hash = attr;
		}
		if( lg.length > 0 )
		{
			attr = lg.item(0).getAttribute("hash");
			if( Sections.league_hash != "" && Sections.league_hash != attr ){
				if( location != document.location.href )
					return document.location.href = location;
				else
					return document.location.reload();
			}
			Sections.league_hash = attr;
		}
		if( hr.length > 0 )
		{
			attr = hr.item(0).getAttribute("hash");
			if( Sections.horses_hash != "" && Sections.horses_hash != attr ){
				if( location != document.location.href )
					return document.location.href = location;
				else
					return document.location.reload();
			}
			Sections.horses_hash = attr;
		}
	}	

	setTimeout( "ajax.query('sections_stats.php',checkUpdate, true)", (2*60*1000) );
}