var GamezRating_currentRating             =   null;

function GamezRating_initRating(initialValue)
{
	try
	{
	    GamezRating_setNumber(initialValue);
	    new Control.Slider('handle','plaintrack',
    		{
    			onSlide:function(v)
    			{
    				GamezRating_setNumber(v);
    			}
    		}
    	);
    	// initial value
    	$('handle').style.left	=	(initialValue * 2)+ "px"; // must be AFTER new Control.Slider;
        GamezRating_setColorGraph(initialValue);
        GamezRating_currentRating = initialValue;
	}
	catch(e)
	{
	}
	return true;
}

function GamezRating_setNumber(num)
{
	var n		=	parseInt(num * 100);
	GamezRating_currentRating    =   n;
	var cssprefix	=	'ns';
	var space		=	cssprefix + 'nothing';
	var digits	=	(n + "").split('');

	var honderdtal	= $('honderdtal');
	var tiental	=	$('tiental');
	var getal	=	$('getal');

	if(n == 100)
	{
		// show extra digit for 100
    	GamezRating_setClass(honderdtal, (cssprefix + '1'));
    	GamezRating_setClass(tiental, (cssprefix + '0'));
    	GamezRating_setClass(getal, (cssprefix + '0'));
	}
	else if (n == 0)
	{
    	GamezRating_setClass(honderdtal, space);
    	GamezRating_setClass(tiental, space);
    	GamezRating_setClass(getal, (cssprefix + '0'));
		n = '0';
	}
	else if (n < 10)
	{
		// hide tientallen
    	GamezRating_setClass(honderdtal, space);
    	GamezRating_setClass(tiental, space);
    	GamezRating_setClass(getal, (cssprefix + digits[0]));
	}
	else
	{
		// show numbers between 10 and 99
		// hide honderd
    	GamezRating_setClass(honderdtal, space);
    	GamezRating_setClass(tiental, (cssprefix + digits[0]));
    	GamezRating_setClass(getal, (cssprefix + digits[1]));
	}
	GamezRating_setColorGraph(n);
}
function GamezRating_setColorGraph(n)
{
	if(n)
	{
		var el        =   $('colortrack');
		var n         =  n * 2;
		try
		{
		  el.clip.right   =   n;
		}
		catch(e)
		{
		  var newrect	=	"rect(0px,"+n+"px,18px,0px)";
		  el.style.clip   =   newrect;
		}
	}
	// note : rect() does fuck-all when given a rect bigger than the source image
}
function GamezRating_setClass(el,className)
{
	el.setAttribute('class',className);
	el.className	=	className;
	return true;
}

function GamezRating_submitRating()
{
    var newaction   =   '/vote/'+$('ratingvideoid').value+'/'+GamezRating_currentRating;
    new Ajax.Request(newaction,{method:'get',asynchronous:true});

    Effect.SlideUp('ratingvoteformwrapper');
    Effect.SlideDown('ratingthankyou',{queue:'end'});

    return false;
}
