var currentWallpaper = 1;
var totalWallpapers = 0;
var wallpaperThumbWidth = 190;
var wallpaperThumbHeight = 120;
var wallpaperIMGs = new Array();
var wallpaperULs = new Array();
var wallpaperClicked = false;
var wallpaperRolledOver = false;
var wallpaperInit = false;

//function loadWallpapers()
//{
//	myxr.connect('post', wallpaperURL, false);
//	myxr.sendAndLoad('', loadWallpapersComplete);
//}
//
//function loadWallpapersComplete(myxr)
//{
//	if (myxr.responseText)
//	{
//		var jr = eval("(" + myxr.responseText + ")");
//	
//		if (jr.success)
//		{
//			var w = document.getElementById('wallpapers-output');
//			w.innerHTML = jr.output;
//
//			w.onmouseover = showWallpaperDownloadLinks;
//			w.onclick = onWallpaperClicked;
//			//w.onmouseout = hideWallpaperDownloadLinks;
//			
//			totalWallpapers = jr.count;
//
//			aw = document.getElementById('all-wallpapers');
//			aw.style.width = (totalWallpapers * wallpaperThumbWidth) + 'px';
//			
//			wallpaperIMGs = aw.getElementsByTagName('img');
//			wallpaperULs = aw.getElementsByTagName('ul');
//
//			// ----------------------------
//			// centers all the little dudes
//			// ----------------------------
//
//			//for (i = 0; i < wallpaperULs.length; i++)
//			//{
//			//	ul = wallpaperULs[i];
//			//	wimg = wallpaperIMGs[i];
//			//	
//			//	ul.onmouseover = null;
//			//	ul.onmouseout = null;
//			//	wimg = onmouseover = null;
//			//	wimg = onmouseout = null;
//            //
//			//	lis = ul.getElementsByTagName('li');
//			//	liw = 86;
//			//	lih = 22;
//			//	l = 6;
//			//	t = 6;
//			//	rc = Math.ceil(lis.length / 2);
//			//	ul.style.top = (((4 - rc) * ((lih + t) / 2)) + t) + 'px';
//			//	if (lis.length % 2 == 1)
//			//	{
//			//		lis[lis.length - 1].style.marginLeft = ((liw + l) / 2) + 'px';
//			//	}
//			//}
//		}
//		else
//		{
//			//trace('wallpaper returned, but with errors');
//		}
//	}
//	else
//	{
//		//trace('error occured with wallpapers');
//	}
//}

function doWallpaperInit()
{
	wallpaperInit = true;

	var w = document.getElementById('wallpapers-output');
	var aw = document.getElementById('all-wallpapers');

	w.onmouseover = showWallpaperDownloadLinks;
	w.onclick = onWallpaperClicked;
	//w.onmouseout = hideWallpaperDownloadLinks;
	
	wallpaperIMGs = aw.getElementsByTagName('img');
	wallpaperULs = aw.getElementsByTagName('ul');

	totalWallpapers = wallpaperIMGs.length;

	aw.style.width = (totalWallpapers * wallpaperThumbWidth) + 'px';
}

function nextWallpaper()
{
	if (!wallpaperInit)
		doWallpaperInit();
	
	if (currentWallpaper < totalWallpapers)
	{
		currentWallpaper++;

		aw = document.getElementById('all-wallpapers');
		cl = (aw.style.left) ? parseInt(aw.style.left.replace(/[^\-0-9]/g, '')) : 0;
		if (typeof(window['awt']) != 'undefined') awt.stop();
	 	awt = new Tween(aw.style, 'left', Tween.strongEaseInOut, cl, -wallpaperThumbWidth * (currentWallpaper - 1), 1, 'px');
		awt.start();
		displayWallpaperArrows()
	}
}

function previousWallpaper()
{
	if (!wallpaperInit)
		doWallpaperInit();

	if (currentWallpaper > 1)
	{
		currentWallpaper--;

		aw = document.getElementById('all-wallpapers');
		cl = (aw.style.left) ? parseInt(aw.style.left.replace(/[^\-0-9]/g, '')) : 0;
		if (typeof(window['awt']) != 'undefined') awt.stop();
	 	awt = new Tween(aw.style, 'left', Tween.strongEaseInOut, cl, -wallpaperThumbWidth * (currentWallpaper - 1), 1, 'px');
		awt.start();
		displayWallpaperArrows()
	}
}

function displayWallpaperArrows()
{
	document.getElementById('previous-wallpaper').style.display	= (currentWallpaper == 1) 				? 'none' : 'block';
	document.getElementById('next-wallpaper').style.display		= (currentWallpaper == totalWallpapers)	? 'none' : 'block';
}


function showWallpaperDownloadLinks(e)
{
	if (!wallpaperRolledOver && !wallpaperClicked && wallpaperIMGs.length > 0 && wallpaperULs.length > 0)
	{
		wallpaperRolledOver = true;
		document.onmousemove = checkForWallpaperMouseOut;
		wallpaperIn();
	}
}

function checkForWallpaperMouseOut(e)
{
	var tx = (e.pageX) ? e.pageX : e.clientX + document.body.scrollLeft;
	var ty = (e.pageY) ? e.pageY : e.clientY + document.body.scrollTop;
	var op = findPos(document.getElementById('wallpapers-output'));

	if (wallpaperRolledOver && (tx < op[0] || tx > op[0] + 190 || ty < op[1] || ty > op[1] + 120))
	{
		wallpaperRolledOver = false;
		document.onmousemove = null;
		wallpaperClicked = false;
		wallpaperOut();
	}
}

function onWallpaperClicked()
{
	wallpaperClicked = true;
	if (!wallpaperDownloadsShowing && wallpaperRolledOver)
	{
		wallpaperDownloadsShowing = true;
		wallpaperIn();
	}
	else if (wallpaperRolledOver)
	{
		wallpaperDownloadsShowing = false;
		wallpaperOut();
	}
}

function wallpaperIn()
{
	wallpaperDownloadsShowing = true;
	ci = wallpaperIMGs[currentWallpaper - 1];
	cu = wallpaperULs[currentWallpaper - 1];
	//setOpacity(ci, 100);
	setOpacity(cu, 0);
	//cio = parseFloat(ci.style.opacity.replace(/[^0-9\-\.]/g, '')) * 100;
	//cuo = parseFloat(cu.style.opacity.replace(/[^0-9\-\.]/g, '')) * 100;
	if (typeof(window['cuot']) != 'undefined') cuot.stop();
	if (typeof(window['ciot']) != 'undefined') ciot.stop();
	cu.style.display = 'block';
	ciot = new OpacityTween(ci, Tween.strongEaseInOut, 100, 60, .5);
	ciot.start();
	cuot = new OpacityTween(cu, Tween.strongEaseInOut, 0, 100, .5);
	cuot.start();
}

function wallpaperOut()
{
	wallpaperDownloadsShowing = false;
	ci = wallpaperIMGs[currentWallpaper - 1];
	cu = wallpaperULs[currentWallpaper - 1];
	cio = parseFloat(ci.style.opacity.replace(/[^0-9\-\.]/g, '')) * 100;
	cuo = parseFloat(cu.style.opacity.replace(/[^0-9\-\.]/g, '')) * 100;
	if (typeof(window['ciot']) != 'undefined') ciot.stop();
	ciot = new OpacityTween(ci, Tween.strongEaseInOut, cio, 100, .5);
	ciot.start();
	if (typeof(window['cuot']) != 'undefined') cuot.stop();
	cuot = new OpacityTween(cu, Tween.strongEaseInOut, cuo, 0, .5);
	cuot.start();
	cuot.onMotionStopped = function()
	{
		cu.style.display = 'none';
	}
}