/*--------------------------------------------------------------------*\
| This file is part of Videocore 3.1                                  *|
|---------------------------------------------------------------------*|
| Copyright (C) 2008 Silversoft Solutions Ltd. All Rights Reserved    *|
| This file may not be redistributed in whole or significant part     *|
| Videocore is not Free Software | http://www.videocore.com           *|
|-------------------------------------------------------------------- *|
| For more information see http://www.videocore.com/license.html      *|
\*--------------------------------------------------------------------*/

var Ratings = 
{
	init: function ()
	{
		var ratings = document.getElementsByTagName('div');
		for (var x=0; x < ratings.length; x++)
		{
			if (ClassHandler.hasClass (ratings[x], 'ratings'))
			{
				var li = ratings[x].getElementsByTagName('li');
				for (var y=0; y < li.length; y++)
				{
					li[y].onmouseover = Ratings.onMouseOver;
					li[y].onmouseout = Ratings.onMouseOut;
				}
			}
		}
		return true;
	},

	Rate: function (file_id, rating)
	{
		load_ajax (BASE_URL + 'ajax.php?do=rate&file_id=' + file_id + '&rating=' + rating, 'Ratings.Returned');
	},

	Returned: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}
		
		// ###### UPDATE THE RATING #####
		var rat_list = document.getElementById(array['rating'][0]['file_id']).getElementsByTagName("li");
	  var rat = parseInt(array['rating'][1]['rate']);
    
    for (var i=0; i<rat_list.length; i++)
	  {
	   
      if (i < rat)
      {
        rat_list[i].className = "rated active";
      }
      else
      {
        rat_list[i].className = "";
      }
    }
	},

	onMouseOver: function ()
	{
		// ##### USED FOR UNACTIVE STATES #####
		var unactive = false;
	
		var lis = this.parentNode.getElementsByTagName('li');
		for (var li = 0; li < lis.length; li++)
		{			
			// ##### WHICH STATE #####
			if (unactive == true)
			{
				// ##### ADD ACTIVE STATE #####
				ClassHandler.addClass (lis[li], 'unactive');
				ClassHandler.removeClass (lis[li], 'active');
			}
			else
			{
				// ##### ADD ACTIVE STATE #####
				ClassHandler.addClass (lis[li], 'active');
				ClassHandler.removeClass (lis[li], 'unactive');
			}
			
			// ##### TEST FOR UNACTIVE #####
			if (lis[li] == this)
			{
				var unactive = true;
			}
		}
	},

	onMouseOut: function ()
	{
		var lis = this.parentNode.getElementsByTagName('li');
		for (var li = 0; li < lis.length; li++)
		{
			// ##### REMOVE STATES #####
			ClassHandler.removeClass (lis[li], 'unactive');
			ClassHandler.removeClass (lis[li], 'active');
			
			// ##### ADD ACTIVE BACK IF IT WAS RATED #####
			if (ClassHandler.hasClass (lis[li], 'rated') && !ClassHandler.hasClass (lis[li], 'active'))
			{
				ClassHandler.addClass (lis[li], 'active');
			}
			
		}
	}
}

Core.start (Ratings);
