//public vars set these to change the look and feel
var div_class = 'TabbedPanelsContentGroup';//this is the wrapper
var ul_class = 'TabbedPanelsTabGroup';//ul
var li_class_s = 'TabbedPanelsTabOn';//li selected
var li_class = 'TabbedPanelsTab';//li
var div_content = 'TabbedOuter';//content hidden
var div_content_s = 'TabbedInner';//content display
var _max_word = 1;//maximum number of words to show


//start private vars
var ie = false;//this gets over written if ie
var thelist = new Array();//fill this with tabs
var thelistsub  = new Array();
var thecontent = new Array();//fill this with content to match the tabs
var _holdid = 0;
var _oldid = 0;
var int_id = '';//used to mark the timer
var upto =1;//used to change the opacity of active
var downto = 0;//used to change the opacity of the old link
function get_h()
{
	
	if(document.getElementById(mainholder))//check we can find the container
	{
		
		var container = document.getElementById(mainholder);//set the container	
		var listh = container.getElementsByTagName(theh);//make an array of headings
		var i = 0;
		//alert(listh.length);
		for(i=0; i<listh.length; i++)
		{
			thelist[i] = listh[i].innerHTML;//collect all the heading text
			if(listh[i].getAttribute('title'))
			{
				thelistsub[i] = listh[i].getAttribute('title');
			}
			else
			{
				thelistsub[i] = listh[i].innerHTML;
			}
			listh[i].innerHTML = '';//clear the inner html
		}
		
		var insidestuff = container.innerHTML;//grab everything from inside the container
		var the_x = new RegExp("<" + theh + ">(?:&nbsp;)?</" + theh + ">","ig");//create a reg X to match <h>&nbsp;</h> or <h></h> any case globaly
		insidestuff = insidestuff.replace(the_x ,"[~|~]");//convert the reg X to an easy to use string that you dont think would be in the string
		make_content(insidestuff);//split the content on the new string and collect from 1 not 0 as 0 will be empty
		container.innerHTML = '';//clear the container
		
		var thediv = document.createElement('div');//make a content div
		thediv.setAttribute('id', div_class);//give it an id we can use
		thediv.setAttribute((ie ? 'className' : 'class'), div_class);//set the class to for the div to use
		
		var hold_list = new Array();
		
		for(var c=0; c<thecontent.length; c++)//loop through the content array
		{
			var _index = document.createElement( 'ul' );//start making the tab list
		//_index.setAttribute('id', 'main_menu');//make it possible to access this element
			_index.setAttribute((ie ? 'className' : 'class'),ul_class);//set its class
			var tempdiv = document.createElement('div');//make a div to put the content in
			var theid = 'cont_' + c;//its id will be
			tempdiv.setAttribute('id',theid);//set its id
			
			var _li = document.createElement( 'li' );//add some list elemnts to the ul
			_li.setAttribute('id', 'menu_' + c);
			if(c == _active)//look at the global _active  to see if this is the selected content
			{
				tempdiv.setAttribute((ie ? 'className' : 'class'), div_content_s);
				_li.setAttribute((ie ? 'className' : 'class'), li_class);//we have to switch it to on
				if(!ie)
				{
					_li.style.opacity = 0.2;
				}
				else
				{
					try
					{
						_li.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 20;
					} catch (e) { 
						// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet. 
						_li.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=20)';
					}
				}
				
			}
			else
			{
				tempdiv.setAttribute((ie ? 'className' : 'class'), div_content);
				_li.setAttribute((ie ? 'className' : 'class'), li_class);//we need to set it to off
			}
			
			tempdiv.innerHTML = '<' + theh + '>' + thelist[c] + '</' + theh + '>' + thecontent[c];//place the content in divs
			//need a function to chop off some text without killing the html mark up
			_li.innerHTML = '<a href="javascript:do_tab(' + c + ');"><' + theh + '>' + thelistsub[c] + '</' + theh + '>' + chunk_it_2(thecontent[c]) + '</a>';//place content in the boxchunk_it(tempstring)
			//alert('<a href="javascript:do_tab(' + c + ');"><' + theh + '>' + thelistsub[c] + '</' + theh + '>' + chunk_it(thecontent[c]) + '</a>');
			thediv.appendChild(tempdiv);//stick the div in the div
			_index.appendChild(_li);//stick the li in th ul
			hold_list[c] = _index;
		}		
			
		container.appendChild(thediv);//stick the div in the container
		for(var l=0; l<hold_list.length; l++)
		{
			container.appendChild(hold_list[l]);//stick the ul in the container div
		}
	}
}

function make_content(insidestuff)
{
	var acount = 0;//count from 0
	insidestuff = insidestuff.split('[~|~]');//explode into array on the [~|~] thanks to evans reg X
	for(var i=1; i<insidestuff.length; i++)//start from 1 as we should have '' in 0
	{
		thecontent[acount] = insidestuff[i];//stick it in the content array			
		acount++;//increment our array marker
	}
}
//id is the selected tab
function do_tab(id)
{	
	
	_oldid = _holdid;//copy old id from active
	_holdid = id;//trap active
	upto = 1;//make sure upto is set to full
	var box = 'cont_' + _oldid;
	var _div = document.getElementById(box);//grab a ref to old link
	_div.setAttribute((ie ? 'className' : 'class'), div_content);//set it to unactive
	var box = 'cont_' + _holdid;
	var _div = document.getElementById(box);//grab a ref to the active link
	_div.setAttribute((ie ? 'className' : 'class'), div_content_s);//set it to selected
	fader();//call the fader
	setTimeout(scroll_it,500);
		
}


function scroll_it()
{
	var nam='top';
	$target = $('[name=' + nam + ']');
	var targetOffset = $target.offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 1000);
}

function chunk_it_2(tempstring)
{
	var _temp = tempstring.split(' ');
	var our_count = 0;//number of words we currently have
	var our_tags = 0;//number of open tags
	var our_string ='';//string to send back
	var checker='';//checks if the tag needs closing
	var very_temp='';//holds the string + tag
	var more_temp='';//holds the broken string
	//we need to get round tags 
	//explode on the >
	tempstring = tempstring.split(">");
	for(var i= 0; i<tempstring.length; i++)//loop through poss form is string[0] tag[1]
	{
		very_temp = tempstring[i].split("<");//should have content in 0 and tag in 1
		if(very_temp[1])
		{
			checker= very_temp[1].substr(0,2);//see what sort of tag we have
			//alert(checker);
			if(checker == 'im' || checker == 'IM')//check for none closing tags
			{
				//its OK to close and forget
				return("<" + very_temp[1] + ">");//must be a none closer
			}
		}
	}
}

function chunk_it(tempstring)
{
	var _temp = tempstring.split(' ');
	if(_temp.length > _max_word)
	{
		var our_count = 0;//number of words we currently have
		var our_tags = 0;//number of open tags
		var our_string ='';//string to send back
		var checker='';//checks if the tag needs closing
		var very_temp='';//holds the string + tag
		var more_temp='';//holds the broken string
		//we need to get round tags 
		//explode on the >
		tempstring = tempstring.split(">");
		for(var i= 0; i<tempstring.length; i++)//loop through poss form is string[0] tag[1]
		{
			very_temp = tempstring[i].split("<");//should have content in 0 and tag in 1
			if(very_temp[1])
			{
				checker= very_temp[1].substr(0,2);//see what sort of tag we have
				//alert(checker);
				if(checker == 'im')//check for none closing tags
				{
					//its OK to close and forget
					alert('found ' + very_temp[1]);
					return("<" + very_temp[1] + ">");//must be a none closer
				}
			}
			else
			{
				checker = '';//set some defaults
				very_temp[1] = '';
			}
			more_temp = very_temp[0].split(" ");//break content on white space
			for(var ikle = 0; ikle<more_temp.length; ikle++)//loop through the content
			{
				if(our_count < _max_word)//make sure we still need words
				{
					our_count++;//inc our counter
					our_string+= more_temp[ikle] + " ";//add the word to our string
				}
				else
				{
					ikle = more_temp.length;//get out of this loop
				}
			}
			if(checker == 'im' || checker == 'hr' || checker == 'br')//check for none closing tags
			{
				//its OK to close and forget
				our_string += "<" + very_temp[1] + ">";//must be a none closer
			}
			else
			{
				checker = checker.substr(0,1);//see if its a close
				if(checker == "/")
				{
					//must be closing
					if(our_tags > 0)
					{
						our_tags--;//reduce our tag count
					}
					our_string += "<" +very_temp[1] + ">";//replace the tag
				}
				else
				{
					if(very_temp[1] != '')
					{
						our_tags++;//increment our tag count
						our_string += "<" +very_temp[1] + ">";//replace the tag
					}
				}
			}
			if(our_count >= _max_word && our_tags == 0)//see if we have done enough
			{
				//alert(our_string);
				return(our_string);//get out and return the string
			}
			
		}
		return(our_string);
	}
	else
	{
		return(tempstring);
	}
}


function fader()
{
	if(document.getElementById('menu_' + _holdid))
	{
		upto -= 0.1;//reduce the opacity setting
		downto = 1 - upto;//make the opposite
		//alert(upto);
		var _li1 = document.getElementById('menu_' + _holdid);//grab the new
		var _li2 = document.getElementById('menu_' + _oldid);//grab the old
		if (ie) // internet explorer
		{
			var ieupto = upto * 100;//ie does this in 100's so X 100
			var iedownto = downto * 100; //same for the downer
			try
			{
				_li1.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieupto;
			} catch (e) { 
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet. 
				_li1.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + ieupto + ')';
			}
			try
			{
				_li2.filters.item("DXImageTransform.Microsoft.Alpha").opacity = iedownto;
			} catch (e) { 
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet. 
				_li2.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + iedownto + ')';
			}
		}
		else // other browsers
		{
			_li1.style.opacity = upto;
			_li2.style.opacity = downto;
		}
		if(upto <= 0.3)//that should be enough
		{
			clearTimeout(int_id);//stop it from going again
			upto=1;//reset upto(opacity)
			if(!ie)
			{
				_li2.style.opacity = upto;//set the  opacity back to full
			}
			else
			{
				_li2.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;//set the  opacity back to full
			}
			
		}
		else
		{
			int_id = setTimeout(fader, 100);//do that again
		}
	}
}

