$(function() {
	$("div.small-feed").children("ul").children("li").mouseover(function() { $(this).addClass("selected"); });
	$("div.small-feed").children("ul").children("li").mouseout(function() { $(this).removeClass("selected"); });

	$("#small-feed-smi").children("ul").children("li").mouseover(function() {
		$("#small-feed-smi").children("img").attr("src", "/media/msport_block_mainpage/" + $(this).attr("imageId"));
		$("#small-feed-smi").children("img").attr("alt", $(this).attr("imageTitle"));
	})

	$("#small-feed-near").children("ul").children("li").mouseover(function() {
		$("#small-feed-near").children("img").attr("src", "/media/msport_block_mainpage/" + $(this).attr("imageId"));
		$("#small-feed-near").children("img").attr("alt", $(this).attr("imageTitle"));
	});
	$.ajaxSetup({
		// Disable caching of AJAX responses */
		cache: false
	});
//	$("a.highslide").click(function() {
//		$("div.highslide-body").addClass("loading");
//		$("div.highslide-body").empty();
//		var id = $(this).attr("documentID");
//		/*$.getJSON('/doc/' + id, function(data) {
//		if (data != null) {
//				
//		}
//		});*/
//		$("div.highslide-body").load('/doc/' + id,
//		 function() {
//		 	$("div.highslide-body").removeClass("loading");
//		 });
//	});
	$("a.highslide").click(function(у) {
		hs.htmlExpand(this, { contentId: 'document-popup-container' });
		$("div.highslide-body").addClass("loading");
		$("div.highslide-body").empty();
		
		$("div.highslide-body").load(
			$(this).attr("href"),
			function() {
				$("div.highslide-body").removeClass("loading");
			}
		);
		return false;
	});
	$(".highslide-close").click(function() {
		$("div.highslide-body").empty();

	});

	$('#c-image').live('click', function() { updateCaptcha(); });
	$('#reg-form').submit(function() {
		$('label', this).each(function() {
			$(this).removeClass("invalid");
		});
		$(this).ajaxSubmit({
			type: 'POST', dataType: 'json', iframe: true, data: { "___AjaxForm": true },
			success: function(data) {
				Sys.Debug.traceDump(data);
				if (data.Errors) {
					var errors = "";
					for (var prop in data.Errors) {
						var key = String(prop).replace("_", "\\.");
						$("span#" + key).nextAll("label:first").addClass("invalid");
						var messages = data.Errors[prop];
						for (var j = 0; j < messages.length; j++) {
							errors += "<li>" + messages[j] + "</li>";
						}
					}
					if (data.Errors["captchaText"])
						updateCaptcha();
				}
				else {
					$("#reg-form").css("display", "none");
					$('#registration-complite').css("display", "block");
					$('#reg-form input').each(function() {
						$(this).val("");
					});
				}

			}
			, error: function(xhr, status, e) { }
		});

		return false;
	});
	$('#cabinet-save').click(function() {
		$.post($("#cab-form").attr('action'), $("#cab-form").serialize(), function(data) {
			data = eval("[" + data + "]");
			if (data.length > 0 && data[0].Errors) {

				var errors = "";
				for (var prop in data[0].Errors) {
					var key = String(prop).replace("_", "\\.");
					$("span#" + key).nextAll("label:first").addClass("invalid");
					var messages = eval("data[0].Errors." + prop);
					for (var j = 0; j < messages.length; j++) {
						errors += "<li>" + messages[j] + "</li>";
					}
				}
			}
			else {
				location.reload();
			}
		})
		return false;
	});
	$('#cabinet-cancel').click(function() {
		$(this).parent().parent().parent().toggleClass('active');
		if ($(this).parent().parent().parent().hasClass('form-cabinet')) {
			$(this).parent().parent().parent()
				.toggleClass('cabactive')
		}
		return false;
	});
	function updateCaptcha() {
		i = $('#c-image');
		s = i.attr("src");
		i.attr("src", "");
		if (String(s).indexOf("?") == -1)
			s = s + "?";
		else {
			s = s.substring(0, String(s).indexOf("?") + 1);
		}
		s = s + Math.random();
		i.attr("src", s);

	}

	$('#recovery-submit').click(function() {
		$.post($("#recovery-form").attr('action'), $("#recovery-form").serialize(), function(data) {
			data = eval("[" + data + "]");
			if (data.length > 0 && data[0].Errors) {
				var errors = "";
				for (var prop in data[0].Errors) {
					var key = String(prop).replace("_", "\\.");
					$("span#" + key).nextAll("label:first").addClass("invalid");
					var messages = eval("data[0].Errors." + prop);
					for (var j = 0; j < messages.length; j++) {
						errors += "<li>" + messages[j] + "</li>";
					}
				}

				$('#recovery-result ul.errors').css('display', '');
				$('#recovery-result ul.errors').html(errors);
				$('#recovery-back').css('display', '');
				$('#recovery-complite').css('display', 'none');
			}
			else {
				$('#recovery-result ul.errors').css('display', 'none');
				$('#recovery-back').css('display', 'none');
				$('#recovery-complite').css('display', '');
			}
			$("#recovery-form").css('display', 'none');
			$('#recovery-result').css('display', '');
		});
		return false;
	});
	$('#recovery-back').click(function() {
		$('#recovery-result').css('display', 'none');
		$("#recovery-form").css('display', '');
	});

	$('#form-login button').click(function() {
		if (!$(this).parent().hasClass('active')) {
			$(this).parent()
				.addClass('active')
		}
		$('#login-errors').empty();
		$.post($("#form-login").attr('action'), $("#form-login").serialize(), function(data) {
			data = eval("[" + data + "]");
			if (data.length > 0 && data[0].Errors) {
				var errors = "";
				for (var prop in data[0].Errors) {
					var key = String(prop).replace("_", "\\.");
					$("span#" + key).nextAll("label:first").addClass("invalid");
					var messages = eval("data[0].Errors." + prop);
					for (var j = 0; j < messages.length; j++) {
						errors += "<li>" + messages[j] + "</li>";
					}
				}
				$('#login-errors').html(errors);
			}
			else {
				location.reload();
			}
		});
		return false;
		return false;
	});
});
