$(function() {
	$("body.game").each(function() {
		var main = new SWFObject("http://game.ohard.ru/800x600_ohard.swf", 'game', 800, 600, 6, '#000');
		main.addParam("wmode", "window");
		main.addParam("quality", "low");
		main.write('game');
	});
});


$(function() {
	$("body.root").each(function() {
		var main = new SWFObject("/swf/root/preloader.swf", 'paint', '100%', '100%', 6, '#000');
		main.addParam("wmode", "window");
		$('#mainarea span.paint').each(function() {
			main.addVariable('paint', $(this).html());
		});

		$('#mainarea span.random').each(function() {
			main.addVariable('random', $(this).html());
		});
		main.addVariable('host', window.location.host);

		main.write('mainarea');
	});
});

$(function() {
	$('body.blog, body.dt-post').each(function() {
		var main = new SWFObject("/swf/button2.swf", 'btn-blog-add-flash', 200, 150, 6, '#000');
		main.addParam("wmode", "transparent");
		main.write('btn-blog-add-flash');
	});
});

function ValidateNotice(input) {
	input.blur();
	$('span.inp-wrap').addClass('bademail');
};

$(function() {
	$(".stars b").mouseover(function() {
		if (!$(this).parent("span").hasClass("inactive")) {
			$(this).addClass("hover").prevAll("b").addClass("hover");
		}
	})

	$(".stars b").mouseout(function() {
		if (!$(this).parent("span").hasClass("inactive")) {
			$(this).removeClass("hover").prevAll("b").removeClass("hover");
		}
	})
	$(".stars b").click(function() {
		var span = $(this).parent("span");
		if (!$(this).parent("span").hasClass("inactive")) {
			$(this).removeClass("hover").prevAll("b").removeClass("hover");
			$(this).addClass("active").prevAll("b").addClass("active");
			$(this).parent("span").addClass("inactive");

			var vote  = $(this).attr('count');
			var postId = $(this).parent("span").attr('id');


			$.getJSON(
				'/vote.action',
				{ id: postId, vote: vote},

				function(data) {
					if (data.status == 'ok') {
						$('strong', span.parent()).html(data.count);

						$('b', span).each(function() {
							if (data.rate >= $(this).attr('count')) {
								$(this).addClass("active");
							} else {
								$(this).removeClass("active");
							}
						});
					}
				}
			);
		}
		return false;
	})
});

$(function() {
	if ($("#cloud3").length > 0) {
		var params = $("#cloud3").attr('params');

		var tagcloud = new SWFObject("/swf/tagcloud.swf", "tagcloud", "280", "150", "9", "#336699");
		tagcloud.addParam("wmode", "transparent");
		tagcloud.addVariable("tcolor", "0x333333");
		tagcloud.addVariable("tcolor2", "0x009900");
		tagcloud.addVariable("hicolor", "0x000000");
		tagcloud.addVariable("tspeed", "100");
		tagcloud.addVariable("distr", "true");
		tagcloud.addVariable("xmlpath", "/xml/tags/?params=" + params);
		tagcloud.write("cloud3");
	}
});

$('div.video div.play').each(function(){
	var so = new SWFObject('/swf/player.swf', 'video', 425, 344, 6, "#000000");

	so.addVariable('videoid', $(this).attr('play:id'));

	so.addParam('allowscriptaccess', 'always');
	so.addParam("wmode", "opaque");
	so.addParam('allowfullscreen',  'true');

	so.write($(this).attr('id'));
});

$(function() {
	$.each(
	[
		"body.download div#phone ul#mb",
		"body.download div#tv ul#ss",
		"body.download div#tv ul#wp"
	], function(n, object) {
		if($(object).children("li").length > 0) {
			var firstItem = $(object).children("li:first");
			firstItem.show().addClass("show");
			if (object != "body.download div#phone ul#mb") {
				firstItem.children("a").clone().appendTo(firstItem.parents("div").children("span.dwn-link").empty());
			} else {
				var currentId = firstItem.children("b").html();
				$("div.form input[name=id]").attr("value", currentId);
				$("div.form img.preview-popup").attr("src", firstItem.children("img.preview-popup").attr("src"));
			}
			if (object == "body.download div#tv ul#wp" && $(object).children("li").length < 2) {
				firstItem.parents("ul").parents("div").children("span.next").children("img").addClass("disabled");
			}
		}
		if (object == "body.download div#tv ul#ss") {
			$(object).addClass("hidden");
			if ($(object).children("li").length == 0) {
				$("span.ss").hide();
			}
		}
	})

	$("body.download .next, body.download .prev").click(function() {
		if(!$(this).children("img").hasClass("disabled")) {
			var current = $(this).parents("div").children("ul").not("ul.hidden").children("li.show");
			var next = current.next("li");
			var prev = current.prev("li");

			current.fadeOut("100").removeClass("show");

			if($(this).hasClass("next")) {
				next.fadeIn("100").addClass("show");

				if ($(this).parents("div").children("ul#mb").length == 0) {
					next.children("a").clone().appendTo($(this).parents("div").children("span.dwn-link").empty());
				} else {
					var currentId = next.children("b").html();
					$("div.form input[name=id]").attr("value", currentId);
					$("div.form img.preview-popup").attr("src", $(next).children("img.preview-popup").attr("src"));
				}

				$(this).parents("div").children(".prev").children("img").removeClass("disabled");

				if (next.next("li").length == 0) {
					$(this).children("img").addClass("disabled");
				}
				if (next.hasClass("screen")) {
					$("span.ss img").removeClass("disabled");
					$("span.wp img").addClass("disabled");
					$(this).parents("div").children("span.dwn-link").css({top : "30px", bottom : "auto", width : "120px", height: "65px", right : "100px"}).children("a").css({display: "block", width : "100%", height : "100%", margin: "0"});
				}
			} else if ($(this).hasClass("prev")) {
				prev.fadeIn("100").addClass("show");

				if ($(this).parents("div").children("ul#mb").length == 0) {
					prev.children("a").clone().appendTo($(this).parents("div").children("span.dwn-link").empty());
				} else {
					var currentId = prev.children("b").html();
					$("div.form input[name=id]").attr("value", currentId);
					$("div.form img.preview-popup").attr("src", $(prev).children("img.preview-popup").attr("src"));
				}

				$(this).parents("div").children(".next").children("img").removeClass("disabled");

				if (prev.prev("li").length == 0) {
					$(this).children("img").addClass("disabled");
				}
				if (current.hasClass("screen")) {
					$("span.wp img").removeClass("disabled");
					$("span.ss img").addClass("disabled");
					$(this).parents("div").children("span.dwn-link").css({bottom : "10px", top : "auto", width : "auto", height : "auto", right : "70px"}).children("a").css({display: "inline"});
				}
			}
			if (!$("ul#ss").hasClass("hidden")) {
				$("span.dwn-link a").css({display: "block", width : "100%", height : "100%", margin: "0"})
			}
		}
		return false;
	})
	$.each(
	{
		"span.wp" : "wp",
		"span.ss" : "ss"
	}, function(element, name) {
		$(element).click(function() {
			if($(this).children("img").hasClass("disabled")) {
				var currentItems = $("ul#" + name);
				currentItems.removeClass("hidden");
				$(this).parents("div").children("ul").not(currentItems).addClass("hidden");
				currentItems.children("li").hide().removeClass("show");
				currentItems.children("li:first").show().addClass("show");
				currentItems.children("li:first").children("a").clone().appendTo($(this).parents("div").children("span.dwn-link").empty());
				if(currentItems.children("li").length < 2) {
					currentItems.parents("div").children("span.next").children("img").addClass("disabled");
				} else {
					$(this).parents("div").children(".next").children("img").removeClass("disabled");
				}
				$(this).parents("div").children(".prev").children("img").addClass("disabled");
				$(this).siblings("span.label").children("img").addClass("disabled");
				$(this).children("img").removeClass("disabled");
				if(name == "wp") {
					$(this).parents("div").children("span.dwn-link").css({bottom : "10px", top : "auto", width : "auto", height : "auto", right : "70px"});
				} else if (name == "ss") {
					$(this).parents("div").children("span.dwn-link").css({top : "30px", bottom : "auto", width : "120px", height: "65px", right : "100px"}).children("a").css({display: "block", width : "100%", height : "100%", margin: "0"});
				}
				if(name == "ss") {
					$(this).parents("div").children(".next, .prev").hide();
				} else {
					$(this).parents("div").children(".next, .prev").show();
				}
			}
		})
	})
	$("span.mb").click(function() {
		$("div#form-wrapper").show();
		return false;
	})
	$("div#form-wrapper span.close").click(function() {
		var oid = $("div#form-wrapper form input[name=id]").attr("value");

		$("div#form-wrapper").hide();
		$("body.download div.form form .form-content").show();
		$("body.download div.form form h3").remove();

		$("input#captcha").attr("value", '');
		$("div#form-wrapper form input[name=id]").attr("value", oid);


		$("body.download div.form form span.error").hide();
		$("body.download div.form form li.phone label").css("visibility", "visible");
	})
})

$('body.download div.form form').submit(function() {
	var size = $("li.size input:checked", this).attr("id");
	var pre = $("li.phone select", this).val();
	var number = $("li.phone input.text", this).val();
	$("span.error", this).hide();
	$("li.phone label", this).css("visibility", "visible");
	var params = $(this).serialize();

	var form = this;

	$.post('/download.action', params, function(data) {
		$("li.captcha img").attr("src", "/captcha/"+data.newkey+".jpg");
		$("input#codekey").attr("value", data.newkey);

		var oid = $("input[name=id]", form).attr("value");

		if (data.status != "ok") {
			if(data.status == "badphone") {
				$("li.phone span.error", form).show();
				$("li.phone label", form).css("visibility", "hidden");
				$("li.captcha input.text", form).val("");
			} else if(data.status == "badcode") {
				$("li.captcha span.error", form).show();
			} else {
			}

		} else if (data.status == "ok") {
			$(".form-content", form).hide();
			$('<h3>Изображение размера ' + size +
			' успешно <a title="проверить статус доставки" href="' + data.url +
			'" target="blank">отправлено</a> на номер +7' + pre + number +
			'</h3>').appendTo($("body.download div.form form"));

			$("input#captcha").attr("value", '');
		}

		$("input[name=id]", form).attr("value", oid);

	}, 'json');

	return false;
});

$(function() {
	$("body.dt-post .comments .comment").each(function() {
		var rnd = Math.floor(Math.random() * 4) + 1;
		$(this).css("background", "url(/i/blog/fun/" + rnd + ".gif) 15px 20px no-repeat");
	})
})

$(function() {
	$('#cloud2 a.rules').click(function(){
		$('body').addClass('veiled');
		$('.veil-popup').addClass('veil-popup-rules');
		return false;
	});

	$('a.closeveil').click(function(){
		$('body').removeClass('veiled');
		$('.veil-popup').removeClass('veil-popup-rules');
		return false;
	});
});
