$(document).ready(function(){
	//Звезды рейтинга
	$(".rating_stars .active").click(function(){set_rating($(this).attr("rel"),$(this).attr("value"));});
	$(".rating_stars .active").each(function(){
		var value = $(this).attr("value");
		$(this).mouseenter(function(){
			$(".rating_stars div.active[rel="+$(this).attr("rel")+"]").each(function()
			{
				if($(this).attr("value") <= value)
				$(this).css("background-position", "0 -40px");
			});
		});
	});
	$(".rating_stars .active").mouseleave(function(){
		$(".rating_stars div.active[rel="+$(this).attr("rel")+"]").css("background-position", "0 0");
	});

	//IE PNG
	$(document).pngFix();

	//Fancybox
	$("a.fancy").fancybox({
	"opacity" 		:	false,
	"autoScale"		:	true,
	"centerOnScroll":	true,
	"hideOnOverlayClick"	:	true,
	"hideOnContentClick"	:	false,
	"overlayShow"	:	true,
	"overlayOpacity"	:	0.3,
	"overlayColor"	:	"#666",
	"titleShow"		:	true,
	"titlePosition"		:	"inside",
	"transitionIn"	: 	"elastic",
	"transitionOut"	:	"elastic"
	});


	//Инпуты по кликам
	$(".text[default]").focus(function () {
		if($(this).attr("value") == $(this).attr("default"))
		{
			$(this).attr("value", "");
		}
	});

	$(".text[default]").blur(function () {
		if($(this).attr("value") == "")
		{
			$(this).attr("value", $(this).attr("default"));
		}
	});
	
	$(".iht_input").focus(function () {
		$("sup[rel="+$(this).attr('id')+"]").fadeIn('fast');
	});

	$(".iht_input").blur(function () {
		$("sup[rel="+$(this).attr('id')+"]").fadeOut('fast');
	});

	//Видео
	$(".video_player_div").each(function (i) {
		var id = $(this).attr("id");
		var file = $(this).attr("filename");
		var preview = $(this).attr("preview");
		var v_comment = $(this).attr("comment");
		var player_path = "/players/";
		swfobject.embedSWF(player_path + "uppod.swf", id, player_conf.video.width, player_conf.video.height, "9.0.115.0", player_path + "expressInstall.swf",
		{
			wmode: "transparent",
			file: file,
			poster: preview,
			comment: v_comment,
			st: player_path+"styles/"+player_conf.video.style+".txt"
		},
		{
			wmode: "transparent",
			allowFullScreen: "true",
			allowScriptAccess: "always",
			movie: player_path + "uppod.swf"
		}
		);
	});

	//Аудио
	$(".audio_player_div").each(function (i) {
		var id = $(this).attr("id");
		var file = $(this).attr("filename");
		var v_comment = $(this).attr("comment");

		var player_path = "/players/";
		swfobject.embedSWF(player_path + "uppod.swf", id, player_conf.audio.width, player_conf.audio.height, "9.0.115.0", player_path + "expressInstall.swf",
		{
			wmode: "transparent",
			file: file,
			comment: v_comment,
			st: player_path+"styles/"+player_conf.audio.style+".txt"
		},
		{
			wmode: "transparent",
			allowScriptAccess: "always",
			movie: player_path + "uppod.swf"
		}
		);
	});


	//Отслеживание submit'ов по всем формам модулей
	$("#registration_form").submit(function(){register(); return false;});
	$("#registration_form #reg_submit").click(function(){register(); return false;});
	$("#login_form").submit(function(){auth_action(1); return false;});
	$("#login_form #login_submit").click(function(){auth_action(1); return false;});
	$("#restore_form").submit(function(){restore_password(); return false;});
	$("#restore_form #restore_submit").click(function(){restore_password(); return false;});
	$("#user_edit_form").submit(function(){edit_user(); return false;});
	$("#user_edit_form #user_edit_submit").click(function(){edit_user(); return false;});
	$("#send_opinion_form").submit(function(){send_opinion(); return false;});
	$("#send_opinion_form #opinion_button").click(function(){send_opinion(); return false;});
	$("#send_question_form").submit(function(){send_question(); return false;});
	$("#send_question_form #question_button").click(function(){send_question(); return false;});
	$(".contacts_form").submit(function(){send_letter("#"+$(this).children('.contacts_form_info').attr('id'), "#"+$(this).attr('id')); return false;});
	$(".contacts_form .contacts_form_submit").click(function(){send_letter("#"+$(this).parent().parent().children('.contacts_form_info').attr('id'), "#"+$(this).parent().parent().attr('id')); return false;});
	$("#unlogin_link").click(function(){auth_action(-1); return false;});
	$("#add_bulletin_form").append("<input type=\'hidden\' name=\'script\' value=\'add_bulletin\'/>");
	$("#add_bulletin_form").jqupload({"callback":"add_bulletin"});
	$(".file_upload_form").attr("action","/ajax/");
	$(".file_upload_form").jqupload_form();
	$("#add_comment_form").submit(function(){add_comment(); return false;});
	$("#add_comment_form #add_comment_submit").click(function(){add_comment(); return false;});

	$("#add_guestbook_theme_form").submit(function(){add_guestbook_theme(); return false;});
	$("#add_guestbook_theme_form #add_guestbook_theme_submit").click(function(){add_guestbook_theme(); return false;});

	//Поведение меню
	$("div.menu_item, div.submenu_item").mouseenter(function() {
		$("#"+$(this).attr("rel")).css("display","block");
		//var elem = document.getElementById($(this).attr("id"));
		var l = 0;
		var t = 0;

		/*while (elem)
		{
			l += elem.offsetLeft;
			t += elem.offsetTop;
			elem = elem.offsetParent;
		}*/

		if($(this).hasClass("menu_item"))
		{
			t += $(this).outerHeight();
		}

		if(!$(this).hasClass("menu_item"))
		{
			l = $(this).outerWidth();
			t = $(this).position().top;
		}

		$("#"+$(this).attr("rel")).css("left",l+"px");
		$("#"+$(this).attr("rel")).css("top",t+"px");
		$("#"+$(this).attr("rel")).css("display","block");
	});

	$("div.menu_item, div.submenu_item").mouseleave(function() {
		if($(this).hasClass("submenu"))
		$(this).css("display", "none");
		$("#"+$(this).attr("rel")).css("display","none");
	});
});
