var oPage = new function()
{
	this.defvar = function(mVal,mDef)
		{return typeof mVal != 'undefined'?mVal:mDef;}
	this.exists = function(oObj,sProperty)
		{return typeof oObj[sProperty] != 'undefined';}
	this.open = function(oA,iWidth,iHeight,sResize,sScroll,sTarget)
	{// Not for use with XHTML 1.0 Strict, depends on the target attribute.
		iWidth = this.defvar(iWidth,540);
		iHeight = this.defvar(iHeight,550);
		sResize = this.defvar(sResize,'yes');
		sScroll = this.defvar(sScroll,'yes');
		sTarget = this.defvar(sTarget,null);
		if(null == sTarget) sTarget = (''==oA.target?'_blank':oA.target);
		
		oPopUpWin = window.open(oA.href,sTarget,'height='+iHeight+',width='+iWidth+',resizable='+sResize+',scrollbars='+sScroll);
		oPopUpWin.focus();
		return false;
	}
	
	this.img = new function(oBase)
	{
		this.oBase = oBase;
		this.aImg = new Object();
		this.sBaseDir = IMG_HOST;
		this.sName = '.';
		this.base = function(sBaseDir)
		{
			this.sBaseDir = sBaseDir;
		}
		this.addTab = function(sName)
		{
			return this.add(sName,'/tabs/'+sName+'.gif','/tabs/over/'+sName+'.gif');
		}
		this.addFloater = function(sName)
		{
			return this.add(sName,'/tabs/floater/'+sName+'.gif','/tabs/floater/over/'+sName+'.gif');
		}
		this.add = function(sName)
		{
			this.sName = sName;
			this.aImg[sName] = new Object();
			
			switch(arguments.length)
			{
				case 4:
					this.addstate('down',arguments[3]);
				case 3:
					this.addstate('over',arguments[2]);
				case 2:
					this.addstate('out',arguments[1]);
			}
		}
		this.addstate = function(sState,sPath,sBaseDir)
		{
			this.aImg[this.sName][sState] = new Image();
			this.aImg[this.sName][sState].src = this.oBase.defvar(sBaseDir,this.sBaseDir)+sPath;
			return this.addStateTo(this.sName,sState,sPath,sBaseDir)
		}
		this.addStateTo = function(sName,sState,sPath,sBaseDir)
		{
			this.aImg[sName][sState] = new Image();
			this.aImg[sName][sState].src = this.oBase.defvar(sBaseDir,this.sBaseDir)+sPath;
			return true;
		}
		this.state = function(sName,sState)
			{window.document.images['img_'+sName].src = this.aImg[sName][sState].src;}
		this.out = function(sName)
			{this.state(sName,'out');}
		this.over = function(sName)
			{this.state(sName,'over');}
		this.down = function(sName)
			{this.state(sName,'down');}
		return this;
	}(this);
	
	this.set_class = function(oElement,sClass)
	{
		aClasses = oElement.className.split(' ');
		aClasses.push(sClass);
		oElement.className = aClasses.join(' ');
	}
	
	this.unset_class = function(oElement,sClass)
	{
		aOldClasses = oElement.className.split(' ');
		aNewClasses = new Array();
		
		for(i in aOldClasses)
		{
			if('' == aOldClasses[i] || sClass == aOldClasses[i]) continue;
			aNewClasses.push(aOldClasses[i]);
		}
		
		oElement.className = aNewClasses.join(' ');
	}
	
	this.disableForm = function(sID,sText)
	{
		oButton = $(sID);
		oButton.disabled = true;
		if(typeof sText != 'undefined')
		{
			oButton.value = sText;
		}
	}
	
	
	return this;
}


function is_home_page(oSetHomepage)
{
	var aHomePages = ['http://www.'+WINZY_DOMAIN+'/','http://home.'+WINZY_DOMAIN+'/','http://home.'+WINZY_DOMAIN+'/i'];
	for(iKey in aHomePages)
	{
		if(oSetHomepage.isHomePage(aHomePages[iKey])) return true;
	}
	return false;
}
function set_home_page(oSetHomepage)
{
	oSetHomepage.setHomePage('http://home.'+WINZY_DOMAIN+'/');
}
function show_help(sID)
{
	$('help_img_'+sID).src = IMG_HOST+'/help_green.gif';
	$('help_div_'+sID).style.display = 'block';
}
function hide_help(sID)
{
	$('help_img_'+sID).src = IMG_HOST+'/help_blue.gif';
	$('help_div_'+sID).style.display = 'none';
}
function show_element(sElement)
{
	for(var iArg = 0; iArg < arguments.length; ++iArg) 
		$(arguments[iArg]).style.display = '';
	return false;
}
function hide_element(sElement)
{
	for(var iArg = 0; iArg < arguments.length; ++iArg) 
		$(arguments[iArg]).style.display = 'none';
	return false;
}
function redirect(sURL,iSeconds)
{
	if(typeof iSeconds == 'undefined') iSeconds = 0;
	if(iSeconds > 0)
	{
		window.setTimeout("location.href = '"+sURL+"'", iSeconds * 1000);
	}
	location.href = sURL;
	return true;
}

function number_format(fNumber,iDecimals,sDecPoint,sThousandsSep)
{
	iDecimals = oPage.defvar(iDecimals,0);
	sDecPoint = oPage.defvar(sDecPoint,'.');
	sThousandsSep = oPage.defvar(sThousandsSep,',');
	
	iMultiplier = Math.pow(10,iDecimals);
	
	fNumber = Math.round(fNumber * iMultiplier);
	fNumber /= iMultiplier;
	
	fNumber += '';
	iDecPoint = fNumber.indexOf('.');
	if(iDecPoint != -1) sFraction = fNumber.substring(iDecPoint + 1);
	else sFraction = '';
	
	for(var i=sFraction.length; i < iDecimals; ++i) sFraction += '0';
	
	if(sFraction.length > 0) sFraction = sDecPoint+sFraction;
	
	fNumber = '' + Math.floor(fNumber);
	oRegExp = /(\d+)(\d{3})/; // Reqrite this later?
	while(oRegExp.test(fNumber)) fNumber = fNumber.replace(oRegExp,"$1"+sThousandsSep+"$2");
	return fNumber + sFraction;
}

function update_points(iPoints)
{
	$('userdata_points').innerHTML = number_format(iPoints);
}

var aWinzyGetTimeUnits = [[604800,'week','weeks'],[86400,'day','days'],[3600,'hour','hours'],[60,'minute','minutes'],[1,'second','seconds']];
function get_time(iSeconds,iDisplayUnits)
{
	var aUnits = aWinzyGetTimeUnits;
	var iUnits = aUnits.length;
	iDisplayUnits = oPage.defvar(iDisplayUnits,2);
	
	if(0 == iSeconds) return '0 seconds ago';
	
	var bNegative = iSeconds < 0;
	if(bNegative) iSeconds = -iSeconds;
	
	var aCalculated = [];
	for(var iUnit=0; iUnit < iUnits; ++iUnit)
	{
		var iUnitSeconds = aUnits[iUnit][0];
		if(iSeconds < iUnitSeconds)
		{
			aCalculated[iUnit] = 0;
			continue;
		}
		aCalculated[iUnit] = Math.floor(iSeconds / iUnitSeconds);
		iSeconds -= aCalculated[iUnit] * iUnitSeconds;
	}
	
	var aOutput = [];
	for(var iUnit in aCalculated)
	{
		iCalculatedUnits = aCalculated[iUnit];
		if(iCalculatedUnits < 1) continue;
		aOutput.push(iCalculatedUnits+' '+aUnits[iUnit][1 == iCalculatedUnits?1:2]);
		if(aOutput.length >= iDisplayUnits) break;
	}
	
	if(aOutput.length < 1) return false;
	
	return (bNegative?'-':'')+aOutput.join(', ');
}

function load_iframe(oParent,sURL)
{
	var oIFrame = window.document.createElement('iframe');
	
	oIFrame.setAttribute('src', sURL);
	oIFrame.style.height = '300px';
	oIFrame.style.width = '100%';
	oIFrame.style.border = '1px solid rgb(128,128,128)';
	
	oParent.appendChild(oIFrame);
	return true;
}
function toggle_preview(iSearchKey)
{
	var oDiv = $('preview_div'+iSearchKey);
	var oOpen = $('preview_open'+iSearchKey);
	var oClose = $('preview_close'+iSearchKey);
	
	if(!oPage.exists(oDiv,'bShowingIFrame'))
	{
		load_iframe(oDiv,oOpen.href);
		oDiv.bShowingIFrame = true;
	}
	
	
	if(oDiv.bShowingIFrame)
	{
		oDiv.style.display = '';
		oOpen.style.display = 'none';
		oClose.style.display = '';
		oDiv.bShowingIFrame = false;
	}
	else
	{
		oDiv.style.display = 'none';
		oOpen.style.display = '';
		oClose.style.display = 'none';
		oDiv.bShowingIFrame = true;
	}
	
	return false;
}
function bookmark_url(sURL,sTitle)
{
	if(window.sidebar) window.sidebar.addPanel(sTitle,sURL,'');
	else if(window.external) window.external.AddFavorite(sURL,sTitle);
	return false;
}

