//###    For the time being - we're only dealing with snap to half - and click to select ratings - rather than smooth hovers
//###    if we ever decide to put this back in, we need to re-use the toggleRating function (and default it to false)

var rateApproved = true;
var ratingStarWidth = 10;

function toggleRating( ) { rateApproved = !rateApproved; document.body.style.cursor = rateApproved?'hand':''}

function ratingChange( star, container, prefix, starIndex, count, e )
{
	
    var snapToHalf = true;
    var event = window.event || e;
    if( !rateApproved ) return;

    var oCurrentNode = container;
    var left = 0, top = 0;
    var obj, onIcon, offIcon;
   
    try
    {
       while(oCurrentNode.tagName!='BODY' ){

//	top+=parseInt(oCurrentNode.offsetTop,10);
            left+=parseInt(oCurrentNode.offsetLeft,10);

            var oCurrentNode=oCurrentNode.offsetParent;
        }
    }
    catch(err)
    {}

    if ( rateApproved )
    {
        // First we need to correct all of the current stars
        for( var i = 1; i <= count; i++ )
        {
            obj = document.getElementById('rate' + prefix + i );
            onIcon = document.getElementById('rate' + prefix + i + 'iconon' );
            offIcon = document.getElementById('rate' + prefix + i + 'iconoff' );
            if( obj )
            {
                if( i < starIndex )
                {
                    onIcon.style.display = '';
                    offIcon.style.display = 'none';
                    obj.style.visibility = '';
                    obj.style.width = ratingStarWidth;
                }
                else if( i > starIndex )
                {
                    onIcon.style.display = 'none';
                    offIcon.style.display = '';
                    obj.style.width = 0;
                    obj.style.visibility = 'hidden';
                }

            }
        }

        // Get references to the on and off icons
        onIcon = document.getElementById('rate' + prefix + starIndex + 'iconon' );
        offIcon = document.getElementById('rate' + prefix + starIndex + 'iconoff' );


if((event.clientX-left) > 2 && (event.clientX-left) < (ratingStarWidth*2)+2)
        {

            onIcon.style.display = '';
            offIcon.style.display = 'none';
            if( snapToHalf )
                star.style.width = ( (event.clientX-(left)) < ratingStarWidth ) ? ratingStarWidth/2 : ratingStarWidth;
            else
                star.style.width = (event.clientX-(left)+1);

            star.style.visibility = '';

        }
        else if( (event.clientX-left) <= 2 )
        {
            onIcon.style.display = 'none';
            offIcon.style.display = '';
            star.style.visibility = 'hidden';
            star.style.width = 0;
        }
    }
    var value = '';
    var value = calcRatingSummary( prefix, count );

    try
    {
        // Trigger the fields onchange event
        eval( prefix + '_change(' + value + ')' );
    }
    catch( e ){ }

}

function calcRatingSummary(prefix, starCount) {
    var obj,
    onIcon;
    var count = 0;

    for (var i = 1; i <= starCount; i ++ ) {
        obj = document.getElementById('rate' + prefix + i );
        onIcon = document.getElementById('rate' + prefix + i + 'iconon' );
        //alert(obj.style.width);	
        if (onIcon.style.display == '') 
	{
	count += 1;
	//alert('found another');
	}
    }

    var fieldValue = document.getElementById(prefix );
    if (fieldValue)
{
     fieldValue.value = (count / starCount) * 100;
}
    //var summary = document.getElementById('rateSummary' + prefix );
    //var english = translateNumberToEnglish(count);
    //summary.innerHTML = english + (english == 'One' || english == 'Half a' ? ' star': ' stars');

    return count;

}

function translateNumberToEnglish(number) {
    var english;

    switch(Math.floor(number)) {
        case 0: english = 'No';
        break;
        case 1: english = 'One';
        break;
        case 2: english = 'Two';
        break;
        case 3: english = 'Three';
        break;
        case 4: english = 'Four';
        break;
        case 5: english = 'Five';
        break;
        case 6: english = 'Six';
        break;
        case 7: english = 'Seven';
        break;
        case 8: english = 'Eight';
        break;
        case 9: english = 'Nine';
        break;
        case 10: english = 'Ten';
        break;
    }
    try {
        if (number % 1 > 0) {
            if (english == 'No')
                english = 'Half a';
            else english += ' and a half';
        }
    }
    catch(e) {}

    return english;
}