// JavaScript Functions
function buttonStateCheck(button, submitbutton, text)
{
	var string = text.value; 
	if (string.length > 0)
	{
		button.disabled = false;
	} 
	else
	{
		button.disabled = true;
	}
}

function showHideLogin()
{
	document.getElementById('site-desc').style.display='none';
	document.getElementById('loginButton').style.display='none';
	document.getElementById('registerButton').style.display='none';
	document.getElementById('browseButton').style.display='none';
	document.getElementById('mobileButton').style.display='none';
	document.getElementById('loginForm').style.display='block';
	document.getElementById('registerButtonRight').style.display='block';
}

function hoverImg(imgid, newimg)
{
	document.getElementById(imgid).src=newimg;
}

function openid_check(login)
{
	//alert(login);
	if (login.match("\\."))
	{
		//alert("found a dot!");
		// Source: http://www.hscripts.com/tutorials/javascript/dom/textfield-events.php - 28 January 2007
		document.loginForm.password.value = '';
		document.loginForm.password.readOnly = true;
		//document.loginForm.rememberme.checked = false;
		//document.loginForm.rememberme.disabled = true;
	}
	else
	{
		document.loginForm.password.readOnly = false;
		//document.loginForm.rememberme.disabled = false;
	}
}

function openid_nopw(isreadOnly)
{
	if (isreadOnly)
	{
		alert('OpenID passwords are processed via your identity provider');
	}
}

function new_freecap()
{
	// loads new freeCap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent browser/isp caching
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
	}
}

function showHideAdvance()
{
	if(document.getElementById("advanced").style.display=='block')
	{
		document.getElementById("advanced").style.display='none';
		document.getElementById("advanced-arrow").src='images/icon_arrow.gif';		
	}
	else
	{
		document.getElementById("advanced").style.display='block';
		document.getElementById("advanced-arrow").src='images/icon_arrow_down.gif';
	}
}

// Open a popup window for the Mini Player
function openMiniPlayer(url)
{
	window.open(url, "MiniPlayer", 'resizable,width=600,height=500') 
}

// Open a popup window for the Demo Player
function openDemoPlayer(url)
{
	window.open(url, "DemoPlayer", 'width=790,height=610') 
}

function showHideWeather(sid)
{
	if (sid == 4)
	{
		//alert('you selected weather');
		document.getElementById("weatherid").style.display='block';
		document.getElementById("feedurl").style.display='none';
		document.getElementById("weatherdesc").style.display='block';
		document.getElementById("weatherunit").style.display='block';
		document.getElementById("advancedBlock").style.display='none';
	}
	else
	{
		document.getElementById("weatherid").style.display='none';
		document.getElementById("feedurl").style.display='block';
		document.getElementById("weatherdesc").style.display='none';
		document.getElementById("weatherunit").style.display='none';
		document.getElementById("advancedBlock").style.display='block';
	}
}

function addText(text, input_id)
{
	var txtarea = document.getElementById(input_id);
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) 
	{
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} 
	else 
	{
		txtarea.value  += text;
		txtarea.focus();
	}
}

// AJAX subscribe with tags
function subscribe(feed_id)
{
	document.getElementById('tags'+feed_id).style.display='none';
	document.getElementById('ajax-tags'+feed_id).style.display='block';
	document.getElementById('list-tags'+feed_id).style.display='block';
}

function unsubscribe(feed_id)
{
	document.getElementById('edit'+feed_id).style.display='none';
	document.getElementById('ajax-tags'+feed_id).style.display='none';
	document.getElementById('tags'+feed_id).style.display='none';
	document.getElementById('users'+feed_id).style.display='none';
	document.getElementById('reorder'+feed_id).style.display='none';
}

function tag_form(feed_id)
{
	document.getElementById('tags'+feed_id).style.display='block';
	document.getElementById('ajax-tags'+feed_id).style.display='none';
	document.getElementById('list-tags'+feed_id).style.display='none';
}

function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	{
	   //alert(feed_id, tags);
	   //alert('hello world');
	   //myfield.form.submit();
	   return false;
	}
	else
	   return true;
}

// Dreamweaver Jump Menu Code
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/*************************************************************************
  menuLayers from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/
viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}

var menuLayers = {
  timer: null,
  activeMenuID: null,
  offX: -20,   // horizontal offset 
  offY: -30,   // vertical offset 
  show: function(id, e) {
    var mnu = document.getElementById? document.getElementById(id): null;
    if (!mnu) return;
    this.activeMenuID = id;
    if ( mnu.onmouseout == null ) mnu.onmouseout = this.mouseoutCheck;
    if ( mnu.onmouseover == null ) mnu.onmouseover = this.clearTimer;
    viewport.getAll();
    this.position(mnu,e);
  },
  
  hide: function() {
    this.clearTimer();
    if (this.activeMenuID && document.getElementById) 
      this.timer = setTimeout("document.getElementById('"+menuLayers.activeMenuID+"').style.visibility = 'hidden'", 200);
  },
  
  position: function(mnu, e) {
    var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
    var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
    
    if ( x + mnu.offsetWidth + this.offX > viewport.width + viewport.scrollX )
      x = x - mnu.offsetWidth - this.offX;
    else x = x + this.offX;
  
    if ( y + mnu.offsetHeight + this.offY > viewport.height + viewport.scrollY )
      y = ( y - mnu.offsetHeight - this.offY > viewport.scrollY )? y - mnu.offsetHeight - this.offY : viewport.height + viewport.scrollY - mnu.offsetHeight;
    else y = y + this.offY;
    
    mnu.style.left = x + "px"; mnu.style.top = y + "px";
    this.timer = setTimeout("document.getElementById('" + menuLayers.activeMenuID + "').style.visibility = 'visible'", 200);
  },
  
  mouseoutCheck: function(e) {
    e = e? e: window.event;
    // is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
    var mnu = document.getElementById(menuLayers.activeMenuID);
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( mnu != toEl && !menuLayers.contained(toEl, mnu) ) menuLayers.hide();
  },
  
  // returns true of oNode is contained by oCont (container)
  contained: function(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) 
      if ( oNode == oCont ) return true;
    return false;
  },

  clearTimer: function() {
    if (menuLayers.timer) clearTimeout(menuLayers.timer);
  }
  
}
/*************************************************************************
  End menuLayers 
*************************************************************************/  