//var php = new Array();
//php["lib"] = 'php/details.php';

var tmpLay = new Array();

function comment_put(){
	tmp_res = JsHttpRequest.query(
		php["lib"],
		{
			action : 'putComent',
			ordnum : thisLayget,
			nick : getId('nickField').value,
			text : getId('textField').value
		},
		function(result,errors){
//			getId('stars').innerHTML='Comment was added';
//			document.location = 'details.php?id='+thisLayget;
			coment_get(thisLayget);
		},
		true
	);
};

function stars_get(){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'getVote',
			ordnum : thisLayget
		},
		function(result,errors){
			var tmpDiv = getId("stars");
			tmpDiv.innerHTML = '';
			var tmpNum = result[0];
			var tmpStar = '';
			if (result[0]>5) tmpNum = 5;
			for (var i=1; i<=result[0]; i++){
				tmpStar = '';
				tmpLay[i] = result[i];
				if (result[i]['voted']==0){
					for (j=0; j<Math.floor(result[i]['vote']); j++){
						tmpStar+= '<div class="star_on" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
						if (j>5)alert(1);
					};
					for (j=Math.floor(result[i]['vote']); j<5; j++){
						tmpStar+= '<div class="star_off" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
					};
				}
				else{
					for (j=0; j<Math.floor(result[i]['vote']); j++){
						tmpStar+= '<div class="star_on"></div>';
					};
					for (j=Math.floor(result[i]['vote']); j<5; j++){
						tmpStar+= '<div class="star_off"></div>';
					};
				};
            };
			if (tmpStar!='')tmpDiv.innerHTML = tmpStar;
            tmpLay = result;
		},
		true
	);
};

function voteIt(layId, starId, act, tmpI){
	if (tmpLay[tmpI]['voted']=='0')
	{
	 	var tmpTrig = (act==1?starId:0);
	 	if (act==1){
			for (var ts=0; ts<tmpTrig; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_on';
			for (var ts=tmpTrig; ts<5; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_off';
		}
		else{
			for (var ts=0; ts<Math.floor(tmpLay[tmpI]['vote']); ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_on';
			for (var ts=Math.floor(tmpLay[tmpI]['vote']); ts<5; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_off';
		};
	};
};

function voteThis(ordin, voteRate){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'voteThis',
			ordnum : ordin,
			vote : voteRate
		},
		function(result,errors){
			stars_get();
		},
		false
	);
};