function updateDiv(div,url)
{
	var div = document.getElementById(div);
	
	var req = newXMLHttpRequest();
	
	req.onreadystatechange = function()
	{
		if(req.readyState == 4)
		{
			// Check that a successful server response was received
			if(req.status == 200)
			{
				div.innerHTML = req.responseText;
				//alert(req.responseText);
			}
			else
			{
        		// An HTTP problem has occurred
        		alert("HTTP error: " + req.statusText);
      		}
		}
	}
	
	req.open("GET", url, true);
	req.send(null);
}

function showLayer(div)
{
	var div = document.getElementById(div);

	if(div.style.display == 'block')
	{
		div.style.display = 'none';
	}
	else
	{
		div.style.display = 'block';
		div.style.opacity = 0;
		div.style.filter = 'alpha(opacity=0)';
	}
	
	startFade(div);
}

function switchLayer(div,layerClass)
{	
	var collect = document.getElementsByTagName("DIV");
	
	var temp = new Array();
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].className == layerClass)
		{
			temp.push(collect[x]);
		}
	}
	
	collect = temp;
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].id == div)
		{
			collect[x].style.display = 'block';
			collect[x].style.opacity = 0;
			fader = collect[x];
		}
		else
		{
			collect[x].style.display = 'none';
		}
	}
	
	startFade(fader);
}

function startFade(div)
{
	fader = div;
	
	for(var x = 0; x < 11; x++)
	{
		setTimeout('fadeMe(' + x + ')',30*x);
	}
}

function fadeMe(value)
{	
	fader.style.opacity = value/10;
	fader.style.filter = 'alpha(opacity='+ (value * 10) +')';
}

function findParent(thenode,tag)
{
	// Keep bubbling up until we find the tag that we need
	var findp = thenode;
	while(findp.nodeName != tag)
	{
		findp = findp.parentNode;
	}
	
	return findp;
}

function setBox(box,theClass)
{
	box.value = "";
}

function next(div,classname)
{
	var div = document.getElementById(div);
	
	var collect = div.getElementsByTagName("DIV");
	
	var temp = new Array();
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].className == classname)
		{
			temp.push(collect[x]);
		}
	}
	
	collect = temp;
	
	var current;
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].style.display == 'block')
		{
			current = x;
		}
	}
	
	if((current + 1) < collect.length)
	{
		current += 1;
		
		for(var x = 0; x < collect.length; x++)
		{
			if(x == current)
			{
				collect[x].style.display = 'block';
			}
			else
			{
				collect[x].style.display = 'none';
			}
		}
	}
	else
	{
		current = 0;
		
		for(var x = 0; x < collect.length; x++)
		{
			if(x == current)
			{
				collect[x].style.display = 'block';
			}
			else
			{
				collect[x].style.display = 'none';
			}
		}
	}
}

function prev(div,classname)
{
	var div = document.getElementById(div);
	
	var collect = div.getElementsByTagName("DIV");
	
	var temp = new Array();
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].className == classname)
		{
			temp.push(collect[x]);
		}
	}
	
	collect = temp;
	
	var current;
	
	for(var x = 0; x < collect.length; x++)
	{
		if(collect[x].style.display == 'block')
		{
			current = x;
		}
	}
	
	if((current - 1) < 0)
	{
		current = collect.length - 1;
		
		for(var x = 0; x < collect.length; x++)
		{
			if(x == current)
			{
				collect[x].style.display = 'block';
			}
			else
			{
				collect[x].style.display = 'none';
			}
		}
	}
	else
	{
		current -= 1;
		
		for(var x = 0; x < collect.length; x++)
		{
			if(x == current)
			{
				collect[x].style.display = 'block';
			}
			else
			{
				collect[x].style.display = 'none';
			}
		}
	}
}

/* JQuery stuff */
var slider_slideELems = ''; // List of 'Slides' (li elements)
var slider_outNext = false; // If there are any slides out of the box to the right
var slider_outPrev = false; // If there are any slides out of the box to the left
var slider_sliding = false;	// Control to stop multiple clicks on the controls
var slider_rightBound = '';	// Right-most boundary of the slider
var slider_slideAmount = ''; // Amount to slide by. Defined by the width of the slides
var slider_zeropoint = ''; // For the calculation of where the slides are

$(document).ready(function(){
	// Update the main holder offset
	slider_startOffset();

	// Get a array of all of the list elements or "Slides"
	slider_slideElems = $('.popular li');
	
	// Max width of the holding box
	slider_rightBound = $('#slidehood').innerWidth();
	
	slider_slideAmount = slider_slideElems.eq(0).outerWidth(true);
	
	$('#slider_next').click(function(event){
		slider_next();
	});
	
	$('#slider_prev').click(function(event){
		slider_prev();
	});
	
	$('#slider_next').addClass('ghosted');
	$('#slider_prev').addClass('ghosted');
	
	slider_checkSlides();
});

function slider_next()
{
	if(slider_outNext && !slider_sliding)
	{
		slider_sliding = true;
		$('.popular').animate({left: '-='+slider_slideAmount},1000, function(){slider_checkSlides(); slider_sliding = false;});
	}
}

function slider_prev()
{
	if(slider_outPrev && !slider_sliding)
	{
		slider_sliding = true;
		$('.popular').animate({left: '+='+slider_slideAmount},1000, function(){slider_checkSlides(); slider_sliding = false;});
	}
}

function slider_checkSlides()
{
	slider_outNext = false;
	slider_outPrev = false;
	
	slider_slideElems.each(function(){
		var liPos = $(this).offset();
		
		if((liPos.left - slider_zeropoint) >= slider_rightBound)
		{
			slider_outNext = true;
		}
		
		if((liPos.left - slider_zeropoint) <= 0)
		{
			slider_outPrev = true;
		}
	});
	
	if(slider_outNext)
	{
		$('#slider_next').removeClass('ghosted');
	}
	else
	{
		$('#slider_next').addClass('ghosted');
	}
	
	if(slider_outPrev)
	{
		$('#slider_prev').removeClass('ghosted');
	}
	else
	{
		$('#slider_prev').addClass('ghosted');
	}
}

function slider_startOffset()
{
	// Get the #slidehood's offset
	slider_zeropoint = $('#slidehood').offset();
	
	slider_zeropoint = slider_zeropoint.left;
	
	
}
