var tvTabButtons = new Array();
var tvTabDivs = new Array();
var properties = new Array();
if (!srcs) {
	var srcs = null;
}
//===================================================================
//  
//  startPage: run onload
//  
//===================================================================
function startPage(queryString) {
	setIframes('all'); 
	setProperties(queryString);
	var tvChannels = document.getElementById("tvChannels");
//	tvChannels.style.height = (tvChannels.parentNode.parentNode.clientHeight +100)+"px";
	
	var channels = tvChannels.childNodes;
	var idx = 0;
	for (var ch = 0; ch<channels.length; ch++) {
		if (channels[ch].id && (channels[ch].id.search(/channel(\d+)/) ==0)) {
			idx = RegExp.$1;
			tvTabDivs[tvTabDivs.length] = document.getElementById("channel"+idx);
			tvTabButtons[tvTabButtons.length] = document.getElementById("button"+idx);
		}
	}
		
	if (!properties['opentab']) {
		properties['opentab'] = 0;
	}
	showChannel(properties['opentab']); 
}	
//===================================================================
//  
//  setProperties: analize search query line
//  
//===================================================================
function setProperties(queryString) {
	if (!queryString) {
		queryString = decodeURIComponent(location.search.slice(1));
	}
	
	if (queryString.indexOf("=") != -1) {
		var pairs = queryString.split(/&/);
		for (var p = 0;p < pairs.length; p++) {
			if (pairs[p].search(/=/) != -1) {
				key_value = pairs[p].split(/=/,2);
				properties[key_value[0]] = key_value[1];
			}
		}
	}
}	
//===================================================================
//  
//  setIframes: set iframes src
//  
//===================================================================
function setIframes(idx) {
	if (!srcs) {
		return;
	}
	var all_iframes = document.getElementsByTagName("iframe");
	var ifrs = new Array();
	for (var i = 0; i < all_iframes.length; i++) {
		if (all_iframes[i].name.search(/ifr\d+/) == 0) {
			ifrs[ifrs.length] = all_iframes[i];
		}
	}
	
	if (idx != "all") {
		if (srcs[idx]) {
			var delim = (srcs[idx].search(/\?/)== -1) ? "?" : "&";
			var rand = Math.floor(Math.random() * 1000000);
			srcs[idx] = srcs[idx].replace(/[&\?]rand=[^\&]*/,"");
			srcs[idx] += delim + "rand=" + rand;
			ifrs[idx].src = srcs[idx];
		}
		return;
	}
	
	for (var i = 0; i < ifrs.length; i++) {
		if (srcs[i] && (ifrs[i].src != srcs[i])) {
			ifrs[i].src = srcs[i];
		}
	}
	
	
}	
//===================================================================
//  
//  showChannel: switch between channels
//  
//===================================================================
function showChannel(idx) {
	var channelButton = tvTabButtons[idx];
	var channelDiv = tvTabDivs[idx];
	for (var b=0;b<tvTabButtons.length;b++) {
		if (b == idx) {
			if (channelButton.className == "buttonOn") {
				setIframes(b);
			}
			onButton(b);
			onChannel(b);
		}
		else {
			outButton(b);
			outChannel(b);
		}
	}
}	
//===================================================================
//  
//  onButton: button is on
//  
//===================================================================
function onButton(idx) {
	var td = tvTabButtons[idx];
	td.className = "buttonOn";
	var cells = (firstTableChild(td)).rows[0].cells;
	for (var c=0;c<cells.length;c++) {
		if (cells[c].className) {
			cells[c].className = cells[c].className.replace(/Out$/,"On");
		}
	}
}	
//===================================================================
//  
//  outButton: button is on
//  
//===================================================================
function outButton(idx) {
	var td = tvTabButtons[idx];
	td.className = "buttonOut";
	var cells = (firstTableChild(td)).rows[0].cells;
	
	for (var c=0;c<cells.length;c++) {
		if (cells[c].className) {
			cells[c].className = cells[c].className.replace(/On$/,"Out");
		}
	}
}	
//===================================================================
//  
//  onChannel: channel is on
//  
//===================================================================
function onChannel(idx) {
	var div = tvTabDivs[idx];
	div.style.display = "block";
	div.style.zIndex = 0;
}	
//===================================================================
//  
//  outChannel: channel is out
//  
//===================================================================
function outChannel(idx) {
	var div = tvTabDivs[idx];
	div.style.zIndex = -1;
	div.style.display = "none";
}	
//===================================================================
//  
//  firstTableChild: find the first child table in object
//  
//===================================================================
function firstTableChild(obj) {
	for (var c=0; c< obj.childNodes.length; c++) {
		if (obj.childNodes[c].tagName == "TABLE") {
			return obj.childNodes[c];
		}
	}
	return null;
}	

