function template_success(text)
{
	if(text != null)
	{
		return '<span style="color:#33dd33;font-weight:bold;">'+text+'</span>';
	}
	else
	{
		return '';
	}
}
function template_fail(text)
{
	if(text != null)
	{
		return '<span style="color:#dd3333;font-weight:bold;">'+text+'</span>';
	}
	else
	{
		return '';
	}
}


function loginPopup()
{
	document.getElementById('loginBox').className = 'ShowLoginBox';
	document.getElementById('loginNotifier').className = 'login_info_Hide';
	return false;
}
function loginRetry(content)
{
	document.getElementById('loginBox').className = 'HideLoginBox';
	document.getElementById('loginNotifier').className = 'login_info';
	document.getElementById('loginBox').innerHTML = content;
}
function checkEnter(e, inpu)
{ //e is event object passed from function invocation
	var characterCode;

	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		inpu.submit();
		return false;
	}
	else
	{
		return true;
	}
}
/*
Deprecated, now the image opens in the window
*/
/*function imageStretch(id, width, backto)
{
	if(document.getElementById(id).style.width == width+'px')
	{
		document.getElementById(id).style.width = backto+'px';
	}
	else
	{
		document.getElementById(id).style.width = width+'px';
	}
}*/

//this is used specificly in the add file to album page
function add_file_form_TYPE(type)
{
	if(type == 'img')
	{
		document.getElementById('ThumbText').style.visibility = 'hidden';
		document.getElementById('ThumbInput').style.visibility = 'hidden';
	}
	else if(type == 'swf')
	{
		
		document.getElementById('ThumbText').style.visibility = 'visible';
		document.getElementById('ThumbInput').style.visibility = 'visible';
	}
}

function textCount(total, textArea, leftID)
{
	
	document.getElementById(leftID).innerHTML = (total-textArea.value.length);
}




originalPercent = 0.4;
lastPercent = 0;
tempFreeze = false;
recentValue = '';
recentText = '';
function movePercent(barID, barHolder, original, myvalue, mytext)
{
	recentValue = myvalue;
	recentText = mytext;
	if(tempFreeze == false)
	{
		originalPercent = original/100;
		var width = barHolder.offsetWidth;
		var backdiv_w = document.getElementById('back_'+barID).offsetWidth;
		var frontdiv_w = document.getElementById('front_'+barID).offsetWidth;
		
		
		objx = 0;
		objy = 0;
		do
		{
			objx += barHolder.offsetLeft;
			objy += barHolder.offsetTop;
		}while (barHolder = barHolder.offsetParent);
		
		var total = mousex - objx;
		var percent = (total/width);
		if(percent <= .05)
		{
			percent = 0;
		}
		else if(percent >= .97)
		{
			percent = 1;
		}
		lastPercent = percent;
		
		var newSize = percent*width;
		var oldSize = originalPercent*width;
		
	
		//%'s work better than pixel sizes, make sure it works on all browsers, and if it does, fix the 'new size' and 'width' junk above
		if(newSize >= oldSize)
		{
			var frontpercent = originalPercent/percent;
			document.getElementById('back_'+barID).style.width= (percent*100)+'%';
			document.getElementById('front_'+barID).style.width= (frontpercent*100)+'%';
	//		document.getElementById('back_'+barID).style.width= newSize+'px';
	//		document.getElementById('front_'+barID).style.width= oldSize+'px';
			document.getElementById('back_'+barID).className = 'percentBlue';
			document.getElementById('front_'+barID).className = 'percentGreen';
		}
		else
		{
			var frontpercent = percent/originalPercent;
			document.getElementById('back_'+barID).style.width= (originalPercent*100)+'%';
			document.getElementById('front_'+barID).style.width= (frontpercent*100)+'%';
			//document.getElementById('front_'+barID).style.width= newSize+'px';
			//document.getElementById('back_'+barID).style.width= oldSize+'px';
			document.getElementById('back_'+barID).className = 'percentRed';
			document.getElementById('front_'+barID).className = 'percentGreen';
		}
		
		if(myvalue != 0)
		{
			document.getElementById('toolTip').innerHTML = myvalue - Math.floor(myvalue*percent)+' '+mytext;
			document.getElementById('toolTip').style.left =mousex+10+'px';
			document.getElementById('toolTip').style.top = (mousey+10)+'px';
			
		}
		
	}
}


function clickPercent(text, barID)
{
	tempFreeze = true;
	document.getElementById('toolTip').style.left = '-1000px';
	
	var p = Math.floor((lastPercent*100));
	var bh = document.getElementById('holder_'+barID);
	document.getElementById('holder_'+barID).onmouseout = function(){outPercent(barID, p)};
	document.getElementById('holder_'+barID).onmousemove = function(){movePercent(barID, bh, p, recentValue, recentText)};
	
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Opera, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var r = new String (Math.random()*1000);
	xmlhttp.onreadystatechange=state_Change;
	xmlhttp.open("GET", URL_Root+"/users/setStat.php?stat="+text+"&percent="+p+"&barID="+barID+"&r="+r, true);
	xmlhttp.send(null);
}
function outPercent(barID, percent)
{
	document.getElementById('back_'+barID).style.width= percent+'%';
	document.getElementById('front_'+barID).style.width= '0%';
	document.getElementById('back_'+barID).className = 'percentGreen';
	document.getElementById('toolTip').style.left = '-1000px';
}
var xmlhttp=null;
function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
	{// 200 = "OK"
	var ptext = xmlhttp.responseText;
	var a = ptext.split(',');
	document.getElementById('back_'+a[0]).className = 'percentGreen';
	document.getElementById('back_'+a[0]).style.width = a[1]+'%';
	
	document.getElementById('front_'+a[0]).style.width = '0%';
	
	document.getElementById('holder_'+a[0]).className = 'percentHolder';
	tempFreeze = false;
//	alert(ptext);
	}
  else
	{
	alert("Problem retrieving XML data:" + xmlhttp.statusText + " "+xmlhttp.status);
	}
  }
}



/* MOUSE REAL POSITION CODE */
/* MOUSE REAL POSITION CODE */
var mousex = 0;
var mousey = 0;
function updateMousePos()
{
  document.onmousemove = getMouseXY; // update(event) implied on NS, update(null) implied on IE
  getMouseXY();
}
updateMousePos();
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if ((e.clientX || e.clientY) && document && document.body)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
	  if (document.documentElement != null && document.documentElement.scrollTop != null)
	  {//if doc type is newer, then use this type
	  mousey = e.clientY + document.documentElement.scrollTop;
	  }
	  else if(document.body != null && document.body.scrollTop != null)
	  {//if doc type is 3.2 or under it should use this style
      mousey = e.clientY + document.body.scrollTop;
	  }
	  else
	  {
		mousey = e.clientY;
	  }
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }  
  }
}
/* END OF MOUSE REAL POSITION CODE */

//will display a YUI content box (with close), which will display contents on success, and close screen on failure
function showPopup(htmlTitle, htmlContent, newWidth)
{	
	theWidth = 320;
	if(newWidth != null && newWidth != '')
	{
		theWidth = newWidth;
	}
	
	var rr = Math.random();
	var useNodes = false;
	if(typeof(htmlContent) == 'object')
	{
		useNodes = true;
	}
	
	YAHOO.fga.container.dynamicPopup = new YAHOO.widget.Panel('dynamicPopup', { width:theWidth+'px', visible:true, constraintoviewport:true, fixedcenter:true, draggable:true, modal:true } ); 
	YAHOO.fga.container.dynamicPopup.setHeader(htmlTitle);
	YAHOO.fga.container.dynamicPopup.revertOld = updateOld;
	YAHOO.fga.container.dynamicPopup.revertOld();
	YAHOO.fga.container.dynamicPopup.setBody('');
	if(useNodes)
	{
		YAHOO.fga.container.dynamicPopup.oldChild = htmlContent;
		YAHOO.fga.container.dynamicPopup.oldHolder = htmlContent.parentNode;
		YAHOO.fga.container.dynamicPopup.appendToBody(htmlContent);
	}
	else
	{
		YAHOO.fga.container.dynamicPopup.setBody(htmlContent);
	}
	YAHOO.fga.container.dynamicPopup.render(document.body);
	YAHOO.fga.container.dynamicPopup.cfg.setProperty('fixedcenter', false);//turn off centering (for scrolling)
	
	YAHOO.fga.container.dynamicPopup.hideEvent.subscribe(updateOld);
	return false;
}
function updateOld()
{
	if(YAHOO.fga.container.dynamicPopup.oldHolder != null)
	{
		YAHOO.fga.container.dynamicPopup.oldHolder.appendChild(YAHOO.fga.container.dynamicPopup.oldChild);
		YAHOO.fga.container.dynamicPopup.oldHolder = null;
	}
}

var preReplacement = new Array();
var progressBar = '<img src="http://l.yimg.com/a/i/us/per/gr/gp/rel_interstitial_loading.gif" />';
function postData(formName, usePopup, replaceId, successText, failText)
{
	if(usePopup == null)
	{
		usePopup = true;
	}
	
	
	if(usePopup && !YAHOO.fga.container.dynamicPopup)
	{
		showPopup('TITLE', 'CONTENT');
	}
	
	YUI().use('io-form', function(Y)
	{
		var handleSuccess = function(ioId, o)
		{
			if(usePopup)
			{
				YAHOO.fga.container.dynamicPopup.revertOld();
				YAHOO.fga.container.dynamicPopup.setBody(o.responseText);
			}
			else if(successText != null)
			{
				document.getElementById(replaceId).innerHTML = preReplacement[replaceId]+' '+template_success(successText);
			}
			else
			{
				document.getElementById(replaceId).innerHTML = o.responseText;
			}
			var ar1 = o.responseText.split('<script>');//divide the text into before script and after script
			if(ar1.length >= 2)
			{
				var ar2 = ar1[1].split('</script>');// grab the after script, and divide into before end script and after end script
				eval(ar2[0]);//run the after script -> before end script
			}
		}
		
		var handleFailure = function(ioId, o)
		{
			if(usePopup)
			{
				YAHOO.fga.container.dynamicPopup.revertOld();
				YAHOO.fga.container.dynamicPopup.setBody("Sorry, there was an error in the connection.");
			}
			else if(replaceId != null)
			{
				document.getElementById(replaceId).innerHTML = preReplacement[replaceId]+' '+template_fail(failText);
			}
		}
		
		Y.on('io:success', handleSuccess);
		Y.on('io:failure', handleFailure);
		var formObject;
		if(typeof(formName) == 'string')
		{
			formObject = document.getElementById(formName);
		}
		else
		{
			formObject = formName;
		}
		var cfg = {form: {id: formObject}};
		var sUrl = formObject.action;
		
		Y.io(sUrl, cfg);//initiate
		if(usePopup)
		{
			YAHOO.fga.container.dynamicPopup.revertOld();
			YAHOO.fga.container.dynamicPopup.setBody(progressBar);
		}
		else
		{
			if(preReplacement[replaceId] == null && replaceId != null)
			{
				preReplacement[replaceId] = document.getElementById(replaceId).innerHTML;
			}
			document.getElementById(replaceId).style.height = document.getElementById(replaceId).offsetHeight+'px';
			document.getElementById(replaceId).innerHTML = progressBar;
			
		}
		
	});
	
	return false;
}

//Click to leave site code
var linkElement;
function linkOut(elementA)
{
	//linkElement = elementA;
//	popupAsk('Cancel', 'javascript://a=0', 'Confirm', elementA.href);
//	elementA.href = 'javascript://a=0';
}


function popupAsk(question1, action1, question2, action2)
{
	var firstTime = false;
	if (!YAHOO.fga.container.askQuestion)
	{
		firstTime = true;
	}
		YAHOO.fga.container.askQuestion = 
			new YAHOO.widget.Panel("wait",  
				{ width: "260px", 
					fixedcenter: true, 
					close: false, 
					draggable: false, 
					zindex:4,
					modal: true,
					visible: false
				}
			);
		
		YAHOO.fga.container.askQuestion.setHeader("This link will take you to an external site.");
		YAHOO.fga.container.askQuestion.setBody('<table width="100%"><tr><td><a style="outline:none;" id="askQuestion1" href="'+action1+'">'+question1+'</a></td><td><a style="outline:none;" id="askQuestion2" href="'+action2+'">'+question2+'</a></tr></table>');
		YAHOO.fga.container.askQuestion.render(document.body);
		
		
		YAHOO.util.Event.on("askQuestion1", "click", closeQuestion);
		
//	}
	YAHOO.fga.container.askQuestion.show();
}
function closeQuestion()
{
	linkElement.href = document.getElementById('askQuestion2').href;
	YAHOO.fga.container.askQuestion.hide();
}


////
////FGL Percent Bar
////
//min
//max
//startAt
//sendTo
//toolTip

//holder

//backBar
//frontBar

//originalPercent
//currentPercent
function FGL_Percent()
{
	this.render = function()
	{
		this.canSelect = true;
		this.noInput = true;
		this.holder.FGL_Percent = this;
		
		//Find the back and front bars
		var m = this.holder.childNodes;
		var i = 0;
		while(i < m.length)
		{
			if(m.item(i).id == 'back')
			{
				this.backBar = m.item(i);
				this.frontBar = this.backBar.childNodes.item(0);
			}
			if(m.item(i).id == 'FGL_Percent_input')
			{
				this.nput = m.item(i);
				this.noInput = false;
			}
			i++;
		}
		
		
//		if(this.inputName != '')
//		{
			// = document.createElement('INPUT');
//			this.holder.appendChild(this.nput);
//			this.nput.setAttribute('value', 'HAHA');
//		}
		
		
		this.originalPercent = this.startAt / (this.maxValue - this.minValue);
		
		
		YAHOO.util.Event.addListener(this.holder, 'click', function(e)
		{
			this.FGL_Percent.currentPercent = this.FGL_Percent.findPercentX();
			this.FGL_Percent.sentPercent = Math.round(this.FGL_Percent.currentPercent*100)/100;
			
			if(this.FGL_Percent.canSelect && this.FGL_Percent.noInput)
			{
				this.FGL_Percent.updateBars();

				
				var holderQuick = this;
				YUI().use('io-base', function(Y)
				{
					var success = function(ioId, o)
					{
						var zz = new Tween(holderQuick.FGL_Percent, 'originalPercent', Tween.elasticEaseOut, holderQuick.FGL_Percent.originalPercent, holderQuick.FGL_Percent.sentPercent, 0.5, '');
						zz.start();
						
						holderQuick.FGL_Percent.canSelect = false;
						zz.onMotionChanged = function(){
							holderQuick.FGL_Percent.updateBars()
							};
						zz.onMotionFinished = function(){
							holderQuick.FGL_Percent.originalPercent = holderQuick.FGL_Percent.sentPercent
							holderQuick.FGL_Percent.canSelect = true;
							};

						holderQuick.FGL_Percent.updateBars();
					}
					
					var failure = function(ioId, o)
					{
					}
					
					Y.on('io:success', success);
					Y.on('io:failure', failure);
					
					
					var p = 0;
					if(holderQuick.FGL_Percent.currentPercent != 0)
					{
						p = Math.round((holderQuick.FGL_Percent.currentPercent * 100))/100;
					}
					
					var cfg = 
					{
						method: "GET",
						data: 'currentPercent='+ p +'&iframePreventKey='+iframePreventKey
					}
					
					var sUrl = holderQuick.FGL_Percent.sendTo+'?'+ cfg['data'];
					
					var request = Y.io(sUrl, cfg);
					
				});
			}
			else if(this.FGL_Percent.canSelect && !this.FGL_Percent.noInput)
			{
				var holderQuick = this;
				holderQuick.FGL_Percent.nput.setAttribute('value', holderQuick.FGL_Percent.sentPercent);
				var zz = new Tween(holderQuick.FGL_Percent, 'originalPercent', Tween.elasticEaseOut, holderQuick.FGL_Percent.originalPercent, holderQuick.FGL_Percent.sentPercent, 0.5, '');
				zz.start();
				
				holderQuick.FGL_Percent.canSelect = false;
				zz.onMotionChanged = function(){
					holderQuick.FGL_Percent.updateBars()
					};
				zz.onMotionFinished = function(){
					holderQuick.FGL_Percent.originalPercent = holderQuick.FGL_Percent.sentPercent
					holderQuick.FGL_Percent.canSelect = true;
					};
	
				holderQuick.FGL_Percent.updateBars();
			}
			
		}, false);
		
		//this.holder.addEventListener('mousemove', mouseMoved, false);
		YAHOO.util.Event.addListener(this.holder, 'mousemove', mouseMoved, false);
		function mouseMoved(e)
		{
			if(this.FGL_Percent.canSelect)
			{
				this.FGL_Percent.currentPercent = this.FGL_Percent.findPercentX();
				this.FGL_Percent.updateBars();
			}
		}
		
		//this.holder.addEventListener('mouseout', function (e)
		YAHOO.util.Event.addListener(this.holder, 'mouseout', function(e)
		{
			if(	findMouseRelativeX(this) <= 0 ||
				findMouseRelativeX(this) >= this.offsetWidth ||
				findMouseRelativeY(this) <= 0 ||
				findMouseRelativeY(this) >= this.offsetHeight
			)
			{
				this.FGL_Percent.currentPercent = this.FGL_Percent.originalPercent;
				this.FGL_Percent.updateBars();
			}
			else
			{
				//MAJOR HACK, this is NOT OOP FRIENDLY!
				//This fixes that problem where you can quickly leave a box, and it not register it correctly.
				var self = this;
				setTimeout(function()
				{
					self.FGL_Percent.checkAgain();
				}, 10);
				setTimeout(function()
				{
					self.FGL_Percent.checkAgain();
				}, 100);
			}
		}, false);
	}
	
	this.checkAgain = function()
	{
		if(	findMouseRelativeX(this.holder) <= 0 ||
			findMouseRelativeX(this.holder) >= this.holder.offsetWidth ||
			findMouseRelativeY(this.holder) <= 0 ||
			findMouseRelativeY(this.holder) >= this.holder.offsetHeight
		)
		{
			this.currentPercent = this.originalPercent;
			this.updateBars();
		}
	}
	
	this.findPercentX = function()
	{
		//grab the holder's X, width, check mouse's X, return 0.5 if in the middle
		
		//return (mousex - findObjectX(this.holder)) / this.holder.offsetWidth;
		var p = 0;
		if(findMouseRelativeX(this.holder) != 0)
		{
			p = findMouseRelativeX(this.holder) / this.holder.offsetWidth;
		}
		if(p < 0.01)
		{
			p = 0;
		}
		if(p > 0.99)
		{
			p = 1;
		}
		return p;
	}
	
	this.updateBars = function()
	{
		
		if(!this.canSelect)
		{
			this.currentPercent = this.originalPercent;
		}
		var frontpercent = 0;
		if(this.originalPercent > 1)
			this.originalPercent = 1;
		if(this.currentPercent > 1)
			this.currentPercent = 1;
		if(this.currentPercent < 0)
			this.currentPercent = 0;
		
		if(this.currentPercent >= this.originalPercent)
		{
			frontpercent = 0;
			if(this.originalPercent != 0)
			{
				frontpercent = this.originalPercent/this.currentPercent;
			}
			this.backBar.style.width= this.currentPercent*100+'%';
			this.frontBar.style.width= frontpercent*100+'%';
			this.backBar.className = 'percentBlue';
			this.frontBar.className = 'percentGreen';
		}
		else
		{
			frontpercent = 0;
			if(this.currentPercent != 0)
			{
				frontpercent = this.currentPercent/this.originalPercent;
			}
			this.backBar.style.width= this.originalPercent*100+'%';
			this.frontBar.style.width= frontpercent*100+'%';
			this.backBar.className = 'percentRed';
			this.frontBar.className = 'percentGreen';
			
		}
	}
}

function findMouseRelativeX(obj)
{
	return mousex - findObjectX(obj);
}
function findMouseRelativeY(obj)
{
	return mousey - findObjectY(obj);
}

function findObjectX(obj)
{
	objx = 0;
	do
	{
		objx += obj.offsetLeft;
	}while (obj = obj.offsetParent);
	return objx;
}
function findObjectY(obj)
{
	objy = 0;
	do
	{
		objy += obj.offsetTop;
	}while (obj = obj.offsetParent);
	return objy;
}
function tryMe()
{
	alert('argg');	
}





