

var rating_save_state = new Array();


function get_rating(photo_id,score,votes,already_sent)
{
	var div = document.getElementById("rating_stars[" + photo_id + "]");
	var mark = 0;
	var out = "";
	var img_path = "";
	rating_save_state[photo_id] = new Array();
	if (votes > 0) mark = Math.round(score/votes);
	for (var i = 1; i <= 5; i++)
	{
		if (i <= mark)
		{
			img_path = "/im/star_filled.gif";
			rating_save_state[photo_id][i] = 1;
		}
		else
		{
			img_path = "/im/star_empty.gif";
			rating_save_state[photo_id][i] = 0;
		}
		out += "<img src='" + img_path + "' onMouseOver='select_rating_star(" + photo_id + ",this)' onMouseOut='unselect_rating_star(" + photo_id + ")' onClick='send_rating_mark(" + photo_id + "," + i + "," + score + "," + votes + "," + already_sent + ")'>";
	}
	div.innerHTML = out;

	if (already_sent)
	{
		$("#rating_stars[" + photo_id + "]").append("<div class=event>Ваш голос принят</div>");
		setTimeout("rating_hideEvent(" + photo_id + ")",1000);
	}
}


function select_rating_star(photo_id,obj)
{
	var div = document.getElementById("rating_stars[" + photo_id + "]");
	var ar = div.getElementsByTagName("img");
	var b_found = 0;
	if (ar && div)
        for (var i = 0; i < ar.length; i++)
	{
		if (obj == ar[i])
		{
			b_found = 1;
			ar[i].src = "/im/star_selected.gif";
		}
		else if (b_found == 0) ar[i].src = "/im/star_selected.gif";
		else if (rating_save_state[photo_id][i+1] == 1) ar[i].src = "/im/star_filled.gif";
		else ar[i].src = "/im/star_empty.gif";
	}
}


function unselect_rating_star(photo_id)
{
	var div = document.getElementById("rating_stars[" + photo_id + "]");
	var ar = div.getElementsByTagName("img");

	for (var i = 0; i < ar.length; i++)
	{
		if (rating_save_state[photo_id][i+1] == 1) ar[i].src = "/im/star_filled.gif";
		else ar[i].src = "/im/star_empty.gif";
	}
}


function send_rating_mark(photo_id,mark,score,votes,already_sent)
{
	if (already_sent)
	{
		$("#rating_stars[" + photo_id + "]").append("<div class=event>Вы уже голосовали</div>");
		setTimeout("rating_hideEvent(" + photo_id + ")",1000);
	}
	$.post("/rating.php","photo_id="+photo_id+"&mark="+mark,get_rating(photo_id,score+mark,votes+1,1));
}


function rating_hideEvent(photo_id)
{
	$("#rating_stars[" + photo_id + "] > div.event"). remove();
}

function add_photo_score(id)
{
	var s = GetCookie("photo_score");
	s=s+",*"+id;
	SetCookie("photo_score",s);
	alert("Ваш голос принят");
	return false;
}
function min_photo_score(id)
{
	var s = GetCookie("photo_score");
	s=s+",-"+id;
	SetCookie("photo_score",s);
	alert("Ваш голос принят");
	return false;
}
function GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return "";
}

function SetCookie(sName, sValue)
{
  var date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "";
}
			

function switch_div(div)
{
	var d = document.getElementById(div);
	var i = document.getElementById('img_'+div);
	if (d.style.display != 'block')
	{
		if (d!=null) d.style.display = 'block';
		if (i!=null) i.src = '/im/folder_minus.gif';
	}
	else
	{
		if (d!=null) d.style.display = 'none';
		if (i!=null) i.src = '/im/folder_plus.gif';
	}
	return false;
}


