(function($) {
	$.fn.TabControl = function(p_options) {
		m_selectedTab = null;
		m_displayedTabID = null;
		m_tabcontentIDs = new Object()	
	
		initTabControl("mainTab");
	}
	
	function initTabControl(){

		for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
			//alert(.html("wow:"))
			
			var test = $("this.ul:first").css("display", "none");
			
			
			var clickedontab = getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any

			var ulobj = document.getElementById(arguments[i])
			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]

				// Default Tab To Not Show
				document.getElementById(ulistlink.getAttribute("rel")).style.display = "none"

				if (ulistlink.getAttribute("rel")){
					savetabcontentIDs(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs


/*
					ulistlink.onclick=function(){
						changeTab(this)
						return false
					}
*/
					ulistlink.onmouseover = function(){
						changeTab(this)
						return false
					}


					if (ulist[x].className=="selected" && clickedontab==""){ //if a tab is set to be selected by default
						showTab(ulistlink, true) //auto load currenly selected tab content
					}
				}
			} //end inner for loop

			if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
				var culistlink=getullistlinkbyId(arguments[i], clickedontab)

				if (typeof culistlink!="undefined"){ //if match found between tabcontent id and rel attribute value
					showTab(culistlink, true)
					setCookie(ulobj.id, culistlink.getAttribute("rel")) // Save Cookie Value	
				}else{ //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
					showTab(ulist[0].getElementsByTagName("a")[0], true) //just auto load first tab instead
				}
			}
		} //end outer for loop
	}
	
	function changeTab(linkobj){
		// Ensure Selected Tab Is Not Chosen
		if(m_displayedTabID != linkobj.getAttribute("rel")){
			m_selectedTab = linkobj;
			fadeTabOut();
		}
	}

	function showTab(p_linkobj, p_init){

		var linkobj = p_linkobj //(arguments.length == 1) ?  : m_m_selectedTab;	// Determine If Set Hard OR Came From Focus Out		
		var ulid = linkobj.parentNode.parentNode.id //id of UL element
		var resourceContainerID;

		var ullist = document.getElementById(ulid).getElementsByTagName("a") //get list of LIs corresponding to the tab contents

		for (var i=0; i<ullist.length; i++){

			if(ullist[i] == linkobj) resourceContainerID = i;

			ullist[i].parentNode.className= "tabInactive"  //deselect all tabs

			if (typeof m_tabcontentIDs[ulid][i]!="undefined"){
				document.getElementById(m_tabcontentIDs[ulid][i]).style.display = "none" //hide all tab contents
			}
		}

		// Set Tab Nav Background : on UL
		linkobj.parentNode.parentNode.className = linkobj.getAttribute("rel") + "Selected";
		linkobj.parentNode.className = "tabActive";

		document.getElementById(linkobj.getAttribute("rel")).style.display = "block" //expand corresponding tab content

		m_displayedTabID = linkobj.getAttribute("rel") // Save Tab ID
		setCookie(ulid, linkobj.getAttribute("rel")) // Save Cookie Value


		if(!p_init) fadeTabIn();

		// Update Resources Right Column If Needed
		if(typeof($.showResource) == "function"){
			$.showResource(resourceContainerID);
		}
	}

	function savetabcontentIDs(ulid, relattribute){// save ids of tab content divs
		if (typeof m_tabcontentIDs[ulid]=="undefined"){ //if this array doesn't exist yet
			m_tabcontentIDs[ulid]=new Array()
		}
		m_tabcontentIDs[ulid][m_tabcontentIDs[ulid].length]=relattribute
	}

	function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
		var ullist=document.getElementById(ulid).getElementsByTagName("li")

		for (var i=0; i<ullist.length; i++){
			if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
				return ullist[i].getElementsByTagName("a")[0]
				break
			}
		}
	}

	//*******************************************************************************************
	function fadeTabIn() {
		$("#" + m_displayedTabID).fadeIn(250);
	}

	function fadeTabOut() {
		$("#" + m_displayedTabID).fadeOut(250, function(){ showTab(m_selectedTab) });
	}
		

})(jQuery);