var Semto_Rating =
{
	Locked: false,
	init: function(szAction, szBaseUrl)
	{
		Semto_Rating.Action = szAction;
		Semto_Rating.Base 	= szBaseUrl;
		
		var aBlocks = $A(document.getElementsByClassName('semto-rating-stars'));
		var pStyle 	= 
		{
			cursor: 'pointer',
			background: 'none'
		}
		
		aBlocks.each(function (pContainer)
		{
			$A(pContainer.childElements()).each(function (pChild)
			{
				if (pChild.nodeName.toLowerCase() != 'img')
				{
					return;
				}
				
				pChild.onmouseover = function()
				{
					Semto_Rating.over(this, false);
				}
				
				pChild.onclick = function()
				{
					Semto_Rating.save(this);
				}
				
				pChild.onmouseout = function()
				{
					Semto_Rating.over(this, true);
				}
			});
		});
	},
	
	over: function(pImage, bReset)
	{
		var pParent = pImage.up('.semto-rating');
		
		if (!pParent)
		{
			return;
		}
		
		var pContainer 		= pImage.up('.semto-rating-stars');
		var iOriginalRating = pParent.down('.semto-rating-value').getAttribute('value');
		var iRating 		= 0;
		
		var szStarBluePath 	= VidsTv.getBaseUrl() + 'images/star-blue.png';
		var szStarGreyPath 	= VidsTv.getBaseUrl() + 'images/star-grey.png';
		var szStarGreenPath = VidsTv.getBaseUrl() + 'images/star-green.png';
		
		var bShowBlue 		= bReset ? false : true;
		var iRating 		= 0;
		var iIndex 			= 0;
		
		$A(pContainer.childElements()).each(function (pChild)
		{
			if (pChild.nodeName.toLowerCase() != 'img')
			{
				return;
			}
			
			var szStarPath = szStarGreenPath;
			iIndex++;
			
			if (iIndex > iOriginalRating)
			{
				szStarPath = szStarGreyPath
			}
			
			if (bShowBlue)
			{
				var szStarPath = szStarBluePath;
			}
			
			pChild.setAttribute('src', szStarPath);
			
			if (pChild == pImage)
			{
				bShowBlue = false;
				iRating = iIndex;
			}
		});
	},
	
	save: function(pImage)
	{
		var iIndex 			= 0;
		var iRating 		= 0;
		var pParent 		= pImage.up('.semto-rating');
		var pContainer 		= pImage.up('.semto-rating-stars');
		var iId 			= pParent.down('.semto-rating-id').getAttribute('value');
		var iOriginalRating = pParent.down('.semto-rating-value').getAttribute('value');
		var szAction		= Semto_Rating.Action;
		var szType 			= '';
		
		if (Semto_Rating.Locked)
		{
			alert('Please wait for your previous rating to finish.');
			return;
		}
		
		Semto_Rating.Locked = true;
		
		if (pParent.down('.semto-rating-type'))
		{
			szType = pParent.down('.semto-rating-type').getAttribute('value');
		}
		
		if (pParent.down('.semto-rating-action'))
		{
			szAction = pParent.down('.semto-rating-action').getAttribute('value');
		}
		
		var szAddress = Semto_Rating.Base + 'ajax/' + szAction + '/';
		
		$A(pContainer.childElements()).each(function (pChild)
		{
			if (pChild.nodeName.toLowerCase() != 'img')
			{
				return;
			}
			
			iIndex++;
			
			if (pChild == pImage)
			{
				iRating = iIndex;
			}
		});
		
		Semto_Rating.Message = document.createElement('span');
		$(Semto_Rating.Message).update(Semto_Wait.getAsHtml('One moment...'));
		pParent.appendChild(Semto_Rating.Message);
		pContainer.hide();
		
		var pAjaxOptions =
		{
			asynchronous:	true, 
			method:			'post',
			postBody: 		'id=' + iId + '&rating=' + iRating + '&type=' + szType, 
			onSuccess:		function(pTransport)
			{
				var pJson = $(pTransport).responseText.evalJSON(true);
				var szMessage = 'Thank you!';
				
				if (!pJson.status)
				{
					szMessage = 'Whoops...';
				}
				
				pParent.down('.semto-rating-value').setAttribute('value', pJson.status ? pJson.rating : iOriginalRating);
				
				$(Semto_Rating.Message).update(szMessage);
				
				setTimeout(function()
				{
					$(Semto_Rating.Message).remove();
					pContainer.show();
					Semto_Rating.over(pImage, true);
					Semto_Rating.Locked = false;
				}, 	1500);
			}
		}
		
		new Ajax.Request(szAddress, pAjaxOptions);
	}
}