var loadedobjects=""
var defaultcontentarray=new Object()
var bustcacheparameter=""


function ajaxpage(url, containerid, targetobj)//Create XMLHttpRequest object
{
var page_request = false

	if (window.XMLHttpRequest) // if Mozilla, Safari etc
	page_request = new XMLHttpRequest()
	
		else if (window.ActiveXObject)
		{ // if IE
			try 
			{
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e)
			{
				try
				{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e)
				{
				}
			}
		}
		else
		return false
	
		var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li")
		
		for (var i=0; i<ullist.length; i++) ullist[i].className=""  //deselect all tabs
		targetobj.parentNode.className="selected"  //highlight currently clicked on tab
		
			if (url.indexOf("#default")!=-1)//if simply show default content within container (verus fetch it via ajax)
			{ 
			document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
			return
			}
		
		
			var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
			var loadstatustext="<br />&nbsp <img src='http://www.danhall.co.uk/blog/skins/custom/ajaxtabs/loading.gif' /><br /><br />"
			
			document.getElementById(containerid).innerHTML=loadstatustext
		
		
			page_request.onreadystatechange=function()
			{
			loadpage(page_request, containerid)
			}
		
		
			if (bustcachevar) //if bust caching of external page
			bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
			page_request.open('GET', url+bustcacheparameter, true)
			page_request.send(null)

}



function loadpage(page_request, containerid)
{
	
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
	document.getElementById(containerid).innerHTML=page_request.responseText
	
}

function expandtab(tabcontentid, tabnumber)//interface for selecting a tab (plus expand corresponding content)
{ 
var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]

	if (thetab.getAttribute("rel"))
	{
	ajaxpage(thetab.getAttribute("href"), thetab.getAttribute("rel"), thetab)
	}
}


function savedefaultcontent(contentid)// save default ajax tab content
{
	
	if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
	defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML

}


function startajaxtabs()
{
	
	for (var i=0; i<arguments.length; i++)//loop through passed UL ids
	{ 
	
	var ulobj=document.getElementById(arguments[i]) //get next UL ID on pass of loop and pass into var
	var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
	
		for (var x=0; x<ulist.length; x++) //loop through each LI element
		{
		var ulistlink=ulist[x].getElementsByTagName("a")[0] //find the link tag in the LI element
		
			if (ulistlink.getAttribute("rel")) //if there is a 'rel' attribute in the link tag
			{
			var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/") //make link ajax friendly
			
			ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
			
			savedefaultcontent(ulistlink.getAttribute("rel")) //save default ajax tab content
			
				ulistlink.onclick=function() //create an on-click trigger for each link
				{
				ajaxpage(this.getAttribute("href"), this.getAttribute("rel"), this)//when clicked, pass the link properties into the ajax page call
				return false
				}
			
				if (ulist[x].className=="selected")
				{
				ajaxpage(ulistlink.getAttribute("href"), ulistlink.getAttribute("rel"), ulistlink) //auto load/reload currenly selected tab content
				}
			}
		}
	}
}