//show a div centered on the screen
function showCentered(obj)
{	
	$(obj).style.left = (document.documentElement.clientWidth / 2 - parseInt($(obj).style.width) / 2) + 'px';		
	if(parseInt($(obj).style.left) < 5)
		$(obj).style.left = '150px';
	Element.show(obj);
}

//popup
function openPopup(url, name)
{
	window.open(url, name, 'left=200, top=200, width=500, height=150, resizable=1');
}

//cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function secondsToTime(s)
{
	var seconds = s;
	var minutes = 0;
	var hours = 0;
	var extra = 0;
	
	//check seconds
	if(seconds > 59)
	{
		extra = Math.floor(seconds / 60);
		minutes = minutes + extra;
		seconds = seconds % 60;
	}
	
	//check minutes
	if(minutes > 59)
	{
		extra = floor(minutes / 60);
		hours = hours + extra;
		minutes = minutes % 60;
	}
	
	//check hours
	if(hours > 23)
	{
		//I don't know what to do.
	}
	
	//add zeros
	if(seconds < 10) seconds = '0' + seconds;
	if(minutes < 10) minutes = '0' + minutes;
	if(hours < 10 && hours > 0) hours = '0' + hours;
	
	var total = minutes + ":" + seconds;
	if(hours) total = hours + ":" + total;
	
	return total;
}

//switching client control tabs
function switchControlTab(tab, channel, script)
{
	view = tab;
	var tabs = document.getElementsByClassName('tab');
	for(var i = 0; i < tabs.length; i++)
	{
		$(tabs[i]).className = 'tab';
	}	
	
	$('tab_'+tab).className = 'tab selected';
	
	if(script > 0)
	{	
		updateURLTableForScript(tab, script, lastp);
	}
	else
	{
		updateURLTable(tab, channel);
	}
}


//hiding the client
function hideClientControls()
{
	//update this so that it remembers
	
	if($('td_tools').style.display == 'none')
	{
		parent.document.getElementById('innerFrame').cols="100%, 300";
		jQuery('table,#td_tools').hide().show();
		$('controlstoggle').src = "/images/hideclient.gif";
		$('controlstoggle').alt = "Hide Player";
	}
	else
	{
		parent.document.getElementById('innerFrame').cols="100%, 10";
		jQuery('#td_tools').hide();
		$('controlstoggle').src = "/images/showclient.gif";
		$('controlstoggle').alt = "Show Player";
	}
}

//muting
function muteSound()
{	
	//update this so that it remembers	
	if($('mutebutton').alt == 'Turn Sound Off')
	{
		mute = true;
		$('mutebutton').src = '/images/button_unmute.gif';
		$('mutebutton').alt = 'Turn Sound On';
		createCookie('CynoCast_sound','off',30);
	}
	else
	{
		mute = false;
		$('mutebutton').src = '/images/button_mute.gif';
		$('mutebutton').alt = 'Turn Sound Off';
		createCookie('CynoCast_sound','on',30);
	}
}

//toggleScriptPassword
function toggleScriptPassword()
{
	if($('private').checked)
	{
		$('spassword_frame').style.display = "";
	}
	else
	{
		$('spassword').value = "";
		$('spassword_frame').style.display = "none;";
	}
}

//toggleDialogs
function toggleDialogs(channel, checkbox)
{
	createCookie('CynoCast_dialogs_'+channel, checkbox.checked, 30);
}

//show/hide buttons
function toggleContent(target, opened, closed, textonly)
{
	//find target
	var root = target;
	var eSwitch = $('switch_' + target);
	var target = $('content_' + target);
	
	//todo: add code to handle images for opened and closed
		
	//hide/show
	if (target.style.display == 'none')
	{
		if(textonly)
			eSwitch.innerHTML = opened;
		else
			eSwitch.innerHTML = '<img src="/images/' + opened + '.gif" border="0" />';
		$(target).style.display = "";
		createCookie('CynoCast_toggle_'+root,'hide',30);		
	}
	else
	{
		if(textonly)
			eSwitch.innerHTML = closed;
		else
			eSwitch.innerHTML = '<img src="/images/' + closed + '.gif" border="0" />';
		$(target).style.display = "none";
		createCookie('CynoCast_toggle_'+root,'show',30);
	}
}

//swap sections
function swapSections(a, b)
{
	var temp = $('section_'+a).innerHTML;
	
	$('section_'+a).innerHTML = $('section_'+b).innerHTML;
	fixSwapLinks(a);

	$('section_'+b).innerHTML = temp;
	fixSwapLinks(b);
}

function fixSwapLinks(s)
{	
	var section = $('section_'+s);
	var controls = section.childNodes[0];
	if(!controls.innerHTML) controls = section.childNodes[1];

	var parts = controls.innerHTML.split('&nbsp;');	

	s = parseInt(s);
	prev = s - 1;
	next = s + 1;

	//move up
	if(s > 1)
		parts[0] = '<a href="javascript:swapSections(' + prev + ', ' + s + ');"><img src="/images/moveup.gif" border="0" /></a>'
	else
		parts[0] = '<img src="/images/moveup_diabled.gif" border="0" />';
	
	//move down
	if(s < 4)
		parts[1] = '<a href="javascript:swapSections(' + s + ', ' + next + ');"><img src="/images/movedown.gif" border="0" /></a>'
	else
		parts[1] = '<img src="/images/movedown_disabled.gif" border="0" />';
		
	//alert(parts[0] + '&nbsp;' + parts[1] + '&nbsp;' + parts[2]);		
	controls.innerHTML = parts[0] + '&nbsp;' + parts[1] + '&nbsp;' + parts[2];
}

//function for the menu like on the scripts page
function buttonMenu(d)
{
	//hide all submenu items
	var divs = document.getElementsByClassName('submenu');
	for(var i = 0; i < divs.length; i++)
	{
		$(divs[i]).style.display = 'none';
	}
	
	//show the one
	if(d)
		$(d).style.display = '';
}

function show(e)
{
	$(e).style.display = '';
}
	
function doPopup(url, width, height) {
   var l = (screen.width - width) /2, t = (screen.height - height) /2;
   var w = window.open(url, '_blank', 'height='+height+',width='+width+',top='+t+',left='+l+',toolbar=no,location=no,menubar=no,resizable=no,status=no,scrollbars=no');
}

function maxlen(str, len) {
   if (str.length > len)
      str = str.substr(0, len - 3) + '...';
   return str;
}