/**
 * albumBrowser.js v1.0
 * http://www.fabienpinardon.com/
 * Freely distributable under MIT-style license.
 */
 
var run = 0;
var albums = new Array();
var firstAlbumLeft = 0;
var lastAlbumLeft = 0;
var selectedImage = null;
var acceleration = 0;
var decceleration = 0;
var dir = 0;
var collision = 0;
var prevMouseX = 0;
var mouseX = 0;
var distance = 0;

var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) 
	document.captureEvents(Event.MOUSEMOVE)

function startMouseMove()
{
	if (run)
	{
		clearInterval(run);
		run = 0;
	}
	
	distance = 0;
	prevMouseX = 0;
	document.onmousemove = getMouseX;
	run = setInterval(mouseMove,10);
}

function stopMouseMove()
{
	document.onmousemove = null;
	
	if (run)
	{
		clearInterval(run);
		run = 0;
	}
}

function getMouseX(e)
{
	if (IE)
	{ // grab the x-y pos.s if browser is IE
        mouseX = event.clientX + document.body.scrollLeft;
  	} 
	else
	{  // grab the x-y pos.s if browser is NS	
    	mouseX = e.pageX;
  	}  
}

function mouseMove()
{
	var offsetL = document.getElementById("browser").offsetLeft + document.getElementById("main").offsetLeft;
	
	if (prevMouseX == 0)
	{
		prevMouseX = mouseX;
		return;
	}
	else
		distance = prevMouseX - mouseX;
		
	if (distance == 0)
	{
		if (mouseX > (offsetL + 450))
			distance = -4;
		else
		if (mouseX < (offsetL + 50))
			distance = 4;
	}
	else
		distance = distance*2;
			
	moveAlbumsWithMouse(distance);
	
	prevMouseX = mouseX;	
}

function startMovingAlbums(direction)
{
	acceleration=0;
	decceleration=0;
	distance = 0;
	
	if (run)
	{
		clearInterval(run);
		run = 0;
	}
		
	dir = direction;
	run = setInterval("moveAlbums(dir)", 50);
}

function stopMovingAlbums()
{
	if (run)
	{
		clearInterval(run);
		run = 0;
		decceleration = acceleration;
		run = setInterval("slowStopMovingAlbums(dir)", 50);
	}
}

function moveAlbumsWithMouse(distance)
{
	var move = false;
	
	if (distance > 0 && firstAlbumLeft < 280)
	{
		if (firstAlbumLeft + distance > 280)
		{
			distance = 280 - firstAlbumLeft;
		}
		
		move = true;
	}
	else
	if (distance < 0 && lastAlbumLeft > 210)
	{
		if (lastAlbumLeft + distance < 210)
		{
			distance = lastAlbumLeft - 210;
		}
		
		move = true;
	}
		
	if (move)
	{
		for(var i = 0;i<albums.length;i++)	
		{
			album = albums[i];
			var image = document.getElementById(albums[i].id);
			var w=parseInt(image.offsetLeft)
			
			w=w+distance;
			image.style.left=w+"px";
			
			album.left=w;
		}
		
		firstAlbumLeft = albums[0].left;
		lastAlbumLeft = albums[albums.length-1].left + 100; // + image size ;)
	}
}

function moveAlbums(direction)
{
	if (run == 0)
	{
		return;
	}

	var move = true;
	
	if (direction == 0)
	{
		if (firstAlbumLeft < 280)
		{
			if (firstAlbumLeft + acceleration > 280)
			{
				collision = 1;
				acceleration = 280 - firstAlbumLeft;
			}
			else
			{
				if (acceleration < 50) // 50 is maximum
				{
					acceleration+=2; 
				}
			}
		}
		else
			move = false;
	}
	else
	{
		if (lastAlbumLeft > 210)
		{
			if (lastAlbumLeft - acceleration < 210)
			{
				collision = 1;
				acceleration = lastAlbumLeft - 210;
			}
			else
			{
				if (acceleration < 50) // 50 is maximum
				{
					acceleration+=2; 
				}
			}
		}
		else
			move = false;
	}
		
	if (move)
	{
		for(var i = 0;i<albums.length;i++)	
		{
			album = albums[i];
			var image = document.getElementById(albums[i].id);
			var w=parseInt(image.offsetLeft)
			
			if (direction == 0)
			{	
				w=w+(acceleration);
				image.style.left=w+"px";
			}
			else
			{
				w=w-(acceleration);
				image.style.left=w+"px";		
			}
			
			album.left=w;
		}
		
		firstAlbumLeft = albums[0].left;
		lastAlbumLeft = albums[albums.length-1].left + 100; // + image size ;)
	}
}

function slowStopMovingAlbums(direction)
{
	if (run == 0)
	{
		return;
	}

	var move = true;
	
	if (direction == 0)
	{
		if (firstAlbumLeft < 280 && decceleration > 0)
		{
			if (firstAlbumLeft + decceleration > 280)
			{
				collision = 1;
				decceleration = 280 - firstAlbumLeft;
			}
			else
			{
				decceleration-=2; 
			}
		}
		else
			move = false;
	}
	else
	{
		if (lastAlbumLeft > 210 && decceleration > 0)
		{
			if (lastAlbumLeft - decceleration < 210)
			{
				collision = 1;
				decceleration = lastAlbumLeft - 210;
			}
			else
			{
				decceleration-=2; 
			}
		}
		else
			move = false;
	}
		
	if (move)
	{
		for(var i = 0;i<albums.length;i++)	
		{
			album = albums[i];
			var image = document.getElementById(albums[i].id);
			var w=parseInt(image.offsetLeft)
			
			if (direction == 0)
			{	
				w=w+(decceleration);
				image.style.left=w+"px";
			}
			else
			{
				w=w-(decceleration);
				image.style.left=w+"px";		
			}
			
			album.left=w;
		}
		
		firstAlbumLeft = albums[0].left;
		lastAlbumLeft = albums[albums.length-1].left + 100; // + image size ;)
	}
	else
	{
		if (run)
		{
			clearInterval(run);
			run = 0;
		}
	}
}

function enableSelection(id)
{
	var image = document.getElementById(id);
	
	if (selectedImage)
	{
		selImage = document.getElementById(selectedImage.id);
		selImage.style.padding = 0;
	}
	
	selectedImage = image;
	selImage = document.getElementById(selectedImage.id);
	selImage.style.padding = 4;
}

function disableSelection()
{
	if (selectedImage)
	{
		selImage = document.getElementById(selectedImage.id);
		selImage.style.padding = 0;
	}
	
	selectedImage = null;
}

function Album(id, title, artist, artist_id, price, pic, left, visible, order, active)
{
	this.title = title;
	this.id = id;
	this.artist = artist;
	this.artist_id = artist_id;
	this.price = price;
	this.pic = pic;
	this.left = left;
	this.visible = visible;
	this.order = order;
	this.active = active;
	this.imageDiv = "";
}

function displayInfo()
{
	with (this) text = "<b><a href=\"#\" onClick=\"changeContent('permwhale_album2.php?id="+id+"','')\">"+title+"</a></b> by <a href=\"#\" onClick=\"changeContent('permwhale_artist2.php?id="+artist_id+"','')\">"+artist+"</a><br>"+price;
	
	showtextex(text, "album_browser_info");
}

function clearInfo()
{
	hidetext();
}

Album.prototype.displayInfo = displayInfo;

function getAlbumsOnSale() 
{
	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null)
  	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	 
	var url="albums_on_sale.php";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		// Save all info
		var xmlDoc = xmlHttp.responseXML;
		
		var albumsXML = xmlDoc.getElementsByTagName("album");
		albums.length = 0;
		
		for (var i=0;i<albumsXML.length;i++)
		{ 
			
			var alb_id = albumsXML[i].getElementsByTagName("alb_id")[0].childNodes[0].nodeValue;
			var title = albumsXML[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;	
			var price = "";

			if (albumsXML[i].getElementsByTagName("price")[0].childNodes[0].nodeValue != null)
				price = albumsXML[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
		
			var pic = albumsXML[i].getElementsByTagName("pic")[0].childNodes[0].nodeValue;
			var artist_name = albumsXML[i].getElementsByTagName("artist_name")[0].childNodes[0].nodeValue;
			var artist_id = albumsXML[i].getElementsByTagName("artist_id")[0].childNodes[0].nodeValue;	
		
			var alb;
			 
			alb = new Album(alb_id, title, artist_name, artist_id, price, pic, (i*110), 1, i, 0);
			
			if (alb.imageDiv == "")
			{
				var imageLnk = alb.pic;
				
				if (alb.visible)
				{
					var html = "<img src=\"/albums/"+imageLnk+"\" alt=\" \" name=\" \" id=\"alb_"+alb.id+"\" width=\"100\" height=\"100\" class=\"reflect rheight25 ropacity50\"/>";	
					createDiv(alb.id, html, 100, 100, alb.left, 2, alb);
				}
				else
				{
					var html = "<img src=\"/albums/"+imageLnk+"\" alt=\" \" name=\" \" id=\" \" width=\"100\" height=\"100\" />";
					createDiv(alb.id, html, 0, 0, alb.left, 10, alb);
				}
			}
			
			albums[i] = alb;
			
			if (i==0)
				firstAlbumLeft = alb.left;
			if (i==albums.length-1)
				lastAlbumLeft = alb.left+100; // + image size ;)
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try
  	{
  		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
	{
		// Internet Explorer
		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
		catch (e)
    	{
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
	}
	
	return xmlHttp;
}

function createDiv(id, html, width, height, left, top, album)
{
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', id);
   
   	newdiv.style.width = "100px";
   	newdiv.style.height = "100px";
   
   	newdiv.style.position = "absolute";
    newdiv.style.left = left+"px";
    newdiv.style.top = top+"px";
    
   	newdiv.style.background = "#00C";
   	newdiv.style.border = "4px solid #FFFFFF";
	newdiv.style.zIndex = 10;
	newdiv.onmouseover = function (event)
	{
      event = event?event:window.event;
      this.style.border = "4px solid #000000";
	  this.style.cursor='pointer';	
	  album.displayInfo();
	}
	newdiv.onmouseout = function (event)
	{
		event = event?event:window.event;
 		this.style.border = "4px solid #FFFFFF";
		this.style.cursor='default';
	
		clearInfo()
	}
	newdiv.onmousedown = function (event)
	{
		event = event?event:window.event;
 		this.style.padding = "0";
		changeContent("permwhale_album2.php?id="+this.id,"");
	}
	
   	var container = document.getElementById('browser');
 	container.appendChild(newdiv);
	
	if (html)
	{
    	newdiv.innerHTML = html;
   	}
	else
	{
    	newdiv.innerHTML = "nothing";
   	}
	
	addReflections();
} 

var previousOnload2 = window.onload;
window.onload = function () 
{ 
	if (previousOnload2)
		previousOnload2();
	
	getAlbumsOnSale();
}