var mouseOffset = (document.all?true:false) ? 0 : 12; //how far the mouse is from the center position of the slider you want it to be at
var origWidth = -1;
var mouseX = -1;
var mouseY = -1;
var xmlhttp = null;


onLoad(initScrollers);
function initScrollers()
{
	var objs = document.getElementsByTagName('div');
	for (var i=0; i<objs.length; i++)
	{
		if ( objs[i].className.match(/slider/i) && !objs[i].className.match(/notRateable/) )
		{
			addRatingEventListeners(objs[i]);			
			var bar = getSliderBar(objs[i]);
			bar.setAttribute('origwidth',bar.style.width);
		}
	}
	
	if ( ! document.all?true:false ) { document.captureEvents(Event.MOUSEMOVE); };
	document.onmousemove = getMouseCoords;
}

function getMouseCoords(e)
{
        if ( !e ) var e = window.event;
        if ( document.all?true:false )
        {
                mouseX = e.clientX + document.body.scrollLeft;
                mouseY = e.clientY + document.body.scrollTop;
        }
        else
        {
                mouseX = e.pageX;
                mouseY = e.pageY;
        }
}

function getSliderParentEl(e)
{
	var targ;
	if ( !e ) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // workaround for Safari bug
		
	
	var parent = targ;
	var className = targ.className;
	while ( !className.match(/slider/) && parent != document.body )
	{
		parent = parent.parentNode;
		className = parent.className;
	}
	
	if ( parent == document.body ) return false; //didn't get the parent and went right up to document root
	else return parent;
}

function getSliderBar(parent)
{
	var el;
	for (var i=0; i<parent.childNodes.length; i++)
    {
		if ( typeof parent.childNodes[i].tagName != 'undefined' )
		{
			el = parent.childNodes[i];
			break;
		}
	}
	
	if ( el ) return el;
	else return false;
}

function scrollIt(e)
{	
	var parent = getSliderParentEl(e);
	var bar = getSliderBar(parent);
	if ( !bar ) return false;
	
	if ( !bar || mouseX < 0 || mouseY < 0 ) return;
	else bar.style.width = (mouseX - objPos(bar)[0] + mouseOffset) + 'px';
}

function resetBar(e)
{
	var parent = getSliderParentEl(e);
	var bar = getSliderBar(parent);
	if ( !bar ) return false;
	bar.style.width = bar.getAttribute('origwidth');
}

function setRating(e)
{	
	var parent = getSliderParentEl(e);
	var bar = getSliderBar(parent);
	if ( !bar ) return false;
	
	
	var rating = calcRating(bar,parent);
	
	snapRating(bar,rating);
	removeRatingEventListeners(parent);
	if ( 1==2 /*!confirm('Are you sure you want give a rating of ' + rating + '/5?\nRemember, you can only rate once!')*/ )
	{
		resetBar(e);
		addRatingEventListeners(parent);
	}
	else
	{
		//save the rating
		type = parent.getAttribute('assetType');
		id = parent.getAttribute('assetID');
		
		if ( !type || !id )
		{
			alert('Error occured');
			resetBar(e);
			addRatingEventListeners(parent);
		}

		sendRating(rating,type,id,function()
		{
			if (xmlhttp.readyState==4)
			{// 4 = "loaded"
				if (xmlhttp.status==200)
				{// 200 = "OK"
					response = xmlhttp.responseText;
					if ( response.match('Rating failed') )
					{
						resetBar(e);
						addRatingEventListeners(parent);
					}
					
					alert(response);
    			}
  				else alert("Problem retrieving data:" + xmlhttp.statusText);
			}
		});
	}
}

function snapRating(bar,rating)
{
	bar.style.width = (rating * 100 / 5) + '%';
	//bar.style.width = bar.offsetWidth + mouseOffset + 'px';
	bar.style.width = bar.offsetWidth + 'px';
}

function calcRating(bar,parent)
{
	var rating = Math.round((bar.offsetWidth / parent.offsetWidth * 100) * 5 / 100);
	if ( rating < 0 ) rating = 0;
	if ( rating > 5 ) rating = 5;
	return rating;
}

function addRatingEventListeners(parent)
{
	/*
	parent.addEventListener('mousemove',scrollIt,false);
	parent.addEventListener('mouseout',resetBar,false);
	parent.addEventListener('click',setRating,false);
	*/
	addSimpleEventListener(parent,'mousemove',scrollIt);
	addSimpleEventListener(parent,'mouseout',resetBar);
	addSimpleEventListener(parent,'click',setRating);
}

function removeRatingEventListeners(parent)
{
	/*
	parent.removeEventListener('mousemove',scrollIt,false);
	parent.removeEventListener('mouseout',resetBar,false);
	parent.removeEventListener('click',setRating,false);
	*/
	removeSimpleEventListener(parent,'mousemove',scrollIt);
	removeSimpleEventListener(parent,'mouseout',resetBar);
	removeSimpleEventListener(parent,'click',setRating);
}

function sendRating(rating,type,id,func)
{
	xmlhttp=null;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

 	if (xmlhttp != null)
	{
		var url = '/?action=rate&rating='+rating+'&'+type+'='+id;
		xmlhttp.onreadystatechange=func;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else alert("Your browser does not support XMLHTTP.");
}
