/*
Usage: 
	<div class="tb-container">
	<div id="Sisältö 1" class="tb-tab">
	hölpöti,hölpötihölpöti,hölpötihölpöti,hölpöti
	</div>
	<div id="2" class="tb-tab">
	hölpöti,hölpötihölpöti,hölpötihölpöti,hölpöti
	</div>
	</div>
*/
var tb_tabs = null;
var tb_container = null;

if(!appurl) 
{
	var appurl = '';
}

if (tb_onselect) {} else var tb_onselect = function (tab) {}

function tb_cookieName() {
	if (tb_container.id) return 'tb_'+tb_container.id;
	var name = window.location.href;
	var i = name.indexOf("#");
//	i = (name.indexOf("?")<i)?name.indexOf("?"):i;
	i = (i>0)?i:name.length;
	return escape(name.substring(0, i));
}

function tb_onClick(index)
{
	var tab_el=tb_tabs[index];
	if (tab_el && tab_el.getAttribute('url'))
	{
		tb_selectTab(index);
		window.location=tab_el.getAttribute('url');
		return true;
	}
	tb_selectTab(index);
}

function tb_selectTab(index, force) {
	if (tb_tabs) ; else return false;
	for (var i=0; i<tb_tabs.length; i++) {
		if (i != index) { tb_tabs[i].style.display = "none"; document.getElementById("tb_control"+i).className="tb-control";}
		else { 
			tb_tabs[i].style.display = ""; 
			document.getElementById("tb_control"+i).className="tb-control-active"; 
		}
	}
	http_set_cookie(tb_cookieName(), index);
	tb_onselect(tb_tabs[index]);
}

function tb_init() {
	var divs = document.getElementsByTagName("DIV");
	var i, s="", js = "";
	tb_tabs = new Array()

	for (i=0; i<divs.length; i++) switch (divs[i].className) {
		case "tb-tab": 
			tb_tabs.push(divs[i]); 
			//divs[i].style.width='100%';
			break;
		case "tb-container": 
			tb_container = divs[i]; 
			//tb_container.style.width='100%';
			break;
		default: break;
	}

	if ((tb_tabs.length < 1) || (tb_container==null)) { tb_tabs = null; if (tb_onload) tb_onload(); return; }
//	tb_container.appendChild(tb_tabs[0]);
	var j = http_get_cookie(tb_cookieName());
	j = ((j=="")||(j==null))?0:j*1;
	j = (j>tb_tabs.length-1)?0:j;
	// ei koskaan valita sellaista jolla on url
	var tab_el=tb_tabs[j];
	if (tab_el && tab_el.getAttribute('url'))
	{
		j=0;
		for (var i=0; i<tb_tabs.length; i++) 
		if (!tb_tabs[i].getAttribute('url'))
		{
			j=i;
			break;
		}
	}
	for (i=0; i<tb_tabs.length; i++) {
		if (i != j) tb_tabs[i].style.display = "none";
		else tb_onselect(tb_tabs[i]);
//		tb_container.appendChild(tb_tabs[i]);
	}

	var newDiv = document.createElement("DIV");
	newDiv.id = "tb-controls";
	newDiv.className = "tb-controls";
	newDiv.style.width='100%';
	js =  'onmouseover="if (this.className!=\'tb-control-active\') this.className=\'tb-control-hover\'" onmouseout="if (this.className!=\'tb-control-active\') this.className=\'tb-control\'"';
	s = '<table border="0" cellpadding="0" cellspacing="0"><tr><td style="width:3px;"></td>';
	for (i=0; i<tb_tabs.length; i++)
	{
		s+= '<td id="tb_control'+ i +'" class="tb-control'+((i==j)?"-active":"")+'" onclick="tb_onClick('+ i +');" '+ js +'><div style="position:relative;">';
		if(appurl)
		{
			s+= '<img src="'+(appurl?appurl+"/":"")+'images/k_vasenyla.gif" style="position:absolute;left:-5px;top:-3px;" alt="">';
	   		s+= '<img src="'+(appurl?appurl+"/":"")+'images/k_oikeayla.gif" style="position:absolute;right:-'+(browser.isIE?"2":"5")+'px;top:-3px;" alt="">';
		}
		s+= tb_tabs[i].id;
		s+= '</div></td><td style="width:3px;"></td>';
	}
	s += "</tr></table>";

	if (document.createRange) {
		var r = document.createRange();
		r.selectNode(document.getElementsByTagName("div").item(0));
		var f = r.createContextualFragment(s);
		newDiv.appendChild(f);
	}
	else newDiv.innerHTML = s;
//	newDiv.innerHTML = s;

	tb_container.parentNode.insertBefore(newDiv, tb_container);
	tb_container.style.display = "block";
	if (tb_onload) tb_onload();
}

var tb_onload = window.onload;
window.onload = tb_init;

