$(function(){

	// Функция предварительной загрузки изображений
	function preload(images) {
		if (typeof document.body == "undefined") return;
	    try {
			var div = document.createElement("div");
			var s = div.style;
			s.position = "absolute";
			s.top = s.left = 0;
			s.visibility = "hidden";
			document.body.appendChild(div);
			div.innerHTML = "<img src=\"" + images.join("\" /><img src=\"") + "\" />";
			var lastImg = div.lastChild;
			lastImg.onload = function() { document.body.removeChild(document.body.lastChild); };
		}
		catch(e) {
			// Error. Do nothing.
		}
	}

	// Предварительная загрузка изображений
	preload(["/i/loading.gif", "/i/load_b.gif", "/i/load_l.gif", "/i/load_l.gif", "/i/e_o.gif", "/i/e_b.gif", "/i/e_b_1.gif", "/i/e_b_2.gif", "/i/e_b_3.gif", "/i/e_b_4.gif", "/i/e_b_5.gif", "/i/e_b_6.gif", "/i/e_b_7.gif", "/i/e_b_8.gif", "/i/e_b_9.gif", "/i/e_g.gif", "/i/e_o.gif"]);

	// Вывод модального окна после нажатия кнопки отправки формы
    $("input[name=submit]").bind("click", function(){		$.modal("<div><img align='middle' style='margin-right: 10px;' src='/i/load_b.gif' height='26px' width='26px'><span style='position: relative; top: 4px;'>сохранение данных...</span></div>", {escClose: false, opacity: 70, overlayCss: {backgroundColor:"#000"}, containerCss: {backgroundColor:"#fff"}, dataCss: {fontWeight:"bold", padding:"20px"}});
	});

	// Подсветка пунктов меню
    $("table.m div, table.n td, table.ph td, table.phb td").bind("mouseover", function(){
		$(this).addClass("over");
	}).bind("mouseout", function(){
		$(this).removeClass("over");
	});

	$.ex = function(par){
	}

	// Оценки


	$.fn.estimations = function(){		var cur_est = $(this);
		var cur_est_av = cur_est.find("span.av_est");		var cur_est_img = cur_est.find("span.cur_user_rate img");
		var cur_photo = $("meta[name='photo_id']").attr("content");
		var cur_user_rate_num = cur_est.find("span.cur_user_rate_num");
		var cur_user_rate_num_title = cur_user_rate_num.attr("title");
		var cur_user_rate_num_html = cur_user_rate_num.html();
		return this.each(function(){			cur_est_img.bind("mouseover", function(){				$(this).css("cursor", "pointer");
				$(this).attr("src", "/i/e_o_d.gif");
				var cur_img = $(this).attr("title");
				cur_est_img.slice(0, cur_img).attr("src", "/i/e_o_d.gif");
				cur_user_rate_num.attr("title", cur_img).html(cur_img);
			}).bind("mouseout", function(){
				cur_est_img.slice(cur_user_rate_num_title, 5).attr("src", "/i/e_g.gif");
				cur_est_img.slice(0, cur_user_rate_num_title).attr("src", "/i/e_o.gif");
				cur_user_rate_num.attr("title", cur_user_rate_num_title).html(cur_user_rate_num_html);
			}).bind("click", function(){				var cur_img = $(this).attr("title");
				cur_user_rate_num_title = cur_img;
				cur_user_rate_num_html = cur_img;
		   		cur_est_img.slice(cur_img, 5).attr("src", "/i/e_g.gif");
				cur_est_img.slice(0, cur_img).attr("src", "/i/e_o.gif");
				cur_user_rate_num.attr("title", cur_user_rate_num_title).html(cur_user_rate_num_html);
				cur_est_av.html("<img width='15px' height='15px' src='/i/load_l.gif'>");
				$.post("/scripts/estimations_post.php", {cur_estimation_type: cur_est.attr("class"), cur_estimation: cur_img, cur_photo: cur_photo}, function(data){
					cur_est_av.html(data.rates_sum);
					rates_q = "количество голосов: " + data.rates_q;
					cur_est.find("span.add_info").html(rates_q);
			    }, "json");			});		});
	};

	$("div.rate_art").estimations();

});
