var stars_array = new Array();

window.addEvent('domready', function(){
	$$('#content .rating').each(function(el_div, id_div)
	{
		var idp=el_div.get('id').substr(2);
		stars_array[id_div]=new Array();
		stars_array[id_div][0]=idp;
		el_div.getChildren('span.stars').each(function(el_span)
		{			
			el_span.getChildren('a.star').each(function(el, id)
			{
				stars_array[id_div][id+1]=el;
				el.set({'events': {'mouseover':
					function(event){
						AddHover(event, id_div, id, true);
					}}
				});
				el.set({'events': {'mouseout':
					function(event){
						AddHover(event, id_div, id, false);
					}}
				});
			});
		});
	});
	
	$$('#menu a.sub').each(function(el)
	{
        el.set({'events': {'click': function(event){
                new Event(event).stop();
                OpenSub(event);
        }}, 'href':'javascript:void(0)'});
        
        if (!el.hasClass('open'))
        {
            var nextSibling = el.getNext('ul');
            nextSibling.addClass('ishide');
        }
    });
    
	//if ($('form_reg'))
	//{
	//}	
});

function OpenSub(event)
{
    var el=event.target;
    if (el.get('tag')=='a')
    {
        var nextSibling = el.getNext('ul');
        if (nextSibling.hasClass('ishide')) nextSibling.removeClass('ishide');
         else nextSibling.addClass('ishide');
    }
}

function AddHover(event, id_div, id, flag)
{
	if (flag)
	{
		for (var i=0; i<=4; i++)
		{
			if (i<=id) $(stars_array[id_div][i+1]).addClass('hover');
			else $(stars_array[id_div][i+1]).removeClass('hover');
		}
	} else {
		for (var i=0; i<=4; i++)
		{
			$(stars_array[id_div][i+1]).removeClass('hover');
		}
	}
}

function rate(product_id, score)
{
	var mark=Array(1, 2, 3, 4, 5);
	AjaxRequest('/rate.php', 'mark='+mark[score]+'&productID='+product_id, callback_rate)	
}
function callback_rate(obj)
{
	
	var l=stars_array.length;
	for(j=0; j<l; j++)
	{
		if (stars_array[j][0]==obj.idp)
		{
			for (var i=0; i<=4; i++)
			{
				$(stars_array[j][i+1]).removeClass('hover');
				$(stars_array[j][i+1]).removeClass('on');
				$(stars_array[j][i+1]).removeClass('off');
				$(stars_array[j][i+1]).removeEvents();
			}
			if (obj.stars>5) obj.stars=5;
			for (var i=1; i<=obj.stars; i++)
			{
				$(stars_array[j][i]).addClass('on');
			}
			$('checkmark_'+obj.idp).removeClass('checkmark');
			break;
		}
	}
}

//=======================================================================================================
function AjaxRequest(url, param, callback)
{
	var SC_Request = new Request({method: 'post', url: url,
		onRequest: function()
		{
		},
		onSuccess: function(response)
		{
			obj=eval("(" +response+")");
			if (callback) callback(obj);
		},
		onFailure: function()
		{
		}
	});
	if (param.length>0) param+='&';
	SC_Request.send(param+'rnd='+Math.random());
}
