(function($) {
	$.fn.frmenu = function(options) {
		var options = $.extend({
		}, options);

		return this.each(function() {
			var th = $(this);
			$(this).find("li").mouseover(function() {
				var index = th.find("li").index($(this));
				$(this).addClass("active").siblings().removeClass("active");
				$("#submenu ul").hide().eq(index).show();
			});
		});
	};


	$.fn.topmedia = function(o) {
		var o = $.extend({
			speed:200,
			count:3,
			actTop:10,
			actWidth:210,
			actHeight:147,
			noactTop:42,
			noactWidth:120,
			noactHeight:83,
			containerWidth:221,
			borderWidth:6,
			containerShift:41
		}, o);

		return this.each(function() {
			var th = $(this);

			$(this).parent().find(".topmedia_rubric span").click(function() {
				var items = $(this).parent().parent().parent().find("ul li");
				if ($(this).hasClass("active")) {
					$(this).removeClass("active");
					$(this).parent().css({background:"url('/i/mm_tab.png') right top no-repeat"} );
					items.show();
				} else {

					$(this).parent().parent().find("div").css({background:"url('/i/mm_tab.png') right top no-repeat"} );
					$(this).parent().parent().find("span").removeClass("active");
					$(this).addClass("active");
					$(this).parent().css({background:"url('/i/mm_tab_active.png') right top no-repeat"} );
					var type = $(this).parent().attr("class");

					items.each(function() {
						if ($(this).hasClass(type)) {
							$(this).show();
						} else {
							$(this).hide();
						}
					});
				}

				topmediaReset(th);

				return false;
			});

			var complete = true;
			$(this).find("ul li .topmedia_img a").click(function() {
				var li = $(this).parent().parent();
				var curCount = $("ul li:visible", th).length;

				if (!li.hasClass("topmedia_active") && curCount > o.count && complete) {
					complete = false;
					var ul = li.parent();
					var thIndex = $("li:visible", ul).index(li);
					var actIndex = $("li:visible", ul).index($("li.topmedia_active:visible", ul));
					var dir = thIndex > actIndex ? 'forvard' : 'back';

					$("li.topmedia_active:visible .topmedia_text", ul).fadeOut(1, function() {
						//$(".topmedia_text", ul).hide();

						if (dir == 'back' && thIndex == 0) {
							ul.prepend($("li:visible", ul).eq(curCount-1)).css({left:parseInt(ul.css("left").replace('px', '')) - o.containerWidth});
						}
						if (dir == 'forvard' && thIndex == curCount-1) {
							ul.append($("li:visible", ul).eq(0)).css({left:parseInt(ul.css("left").replace('px', '')) + o.containerWidth});
						}

						li.animate({top:o.actTop,width:o.actWidth+o.borderWidth,height:o.actHeight+o.borderWidth}, o.speed+5, function() {
							$(this).addClass("topmedia_active");
							//$(".topmedia_text", $(this)).fadeIn(o.speed);
							complete = true;
						}).find("img").animate({width:o.actWidth, height:o.actHeight}, o.speed);

						li.siblings("li.topmedia_active:visible").animate({top:o.noactTop,width:o.noactWidth+o.borderWidth,height:o.noactHeight+o.borderWidth}, o.speed, function() {
							//$(this).find("div.topmedia_text").show();

							$(this).removeClass("topmedia_active");
							$(".topmedia_text", $(this)).show();//fadeIn(o.speed);
						}).find("img").animate({width:o.noactWidth, height:o.noactHeight}, o.speed);

						var pos =  thIndex > actIndex ? parseInt(ul.css("left").replace('px', '')) - o.containerWidth : parseInt(ul.css("left").replace('px', '')) + o.containerWidth;
						li.parent().animate({left:pos}, o.speed);
						//$(".topmedia_text", ul).show();
					});

					return false;
				}
			});

			$(this).find("#tmc_left").click(function() {
				$(this).parent().find("ul li.topmedia_active:visible").prevAll("li:visible").eq(0).find(".topmedia_img a").click();
			});
			$(this).find("#tmc_right").click(function() {
				$(this).parent().find("ul li.topmedia_active:visible").nextAll("li:visible").eq(0).find(".topmedia_img a").click();
			});

			function topmediaReset(obj) {
				var curCount = $("ul li:visible", obj).length;
				//$("ul li .topmedia_text", obj).hide();
				obj.find("ul").css({left:(curCount <= Math.floor(o.count / 2) ? o.containerWidth-o.containerShift : -o.containerShift)}).find("li:visible").css({top:o.noactTop}).removeClass("topmedia_active").css({width:o.noactWidth+o.borderWidth, height:o.noactHeight+o.borderWidth}).find("img").css({width:o.noactWidth, height:o.noactHeight});
				obj.find("ul li:visible").eq(curCount <= Math.floor(o.count / 2) ? 0 : 1).css({top:o.actTop}).addClass("topmedia_active").css({width:o.actWidth+o.borderWidth, height:o.actHeight+o.borderWidth}).find(".topmedia_text").show().parent().find("img").css({width:o.actWidth, height:o.actHeight});
			}

		});
	};

	$.fn.mediaRotate = function(o) {
		var o = $.extend({
			hovers: false
		}, o);

		return this.each(function() {
			var th = $(this);
			var count = $("div.centered2", th).length;

			$(this).find(".f-left a, .f-right a").click(function() {
				var iF = $("div.centered2", th).index($("div.centered2:visible:first", th));
				var iL = $("div.centered2", th).index($("div.centered2:visible:last", th));
				var rl = $(this).parent().hasClass("f-left");
				var show = rl ? iF : iL;

				if (o.hovers && th.find("div.centered2").eq(rl ? iL : iF).hasClass("active")) {
					th.find("div.centered2").eq(rl ? iL-1 : iF+1).mouseover();
				}
				th.find("div.centered2").eq(rl ? iL : iF).hide();

				if (show == (rl ? 0 : count-1)) {
					rl ? th.find("div.centered2").eq(show).before(th.find("div.centered2").eq(rl ? count-1 : 0)) : th.find("div.centered2").eq(show).after(th.find("div.centered2").eq(rl ? count-1 : 0));;
					th.find("div.centered2").eq(show).show();
				} else {
					th.find("div.centered2").eq(rl ? show-1 : show+1).show();
				}

				return false;
			});
		});
	};

	$.fn.mediaAdds = function(o) {
		var o = $.extend({
		}, o);

		return this.each(function() {
			var th = $(this);
			$("div.centered2", th).mouseover(function() {
				$("div.centered2", th).removeClass("active");
				$(".subj", th).html($(this).addClass("active").find("span.invisible").html());
			});
		});
	};

	$(function() {
		// main page
		$("#mainMenu").frmenu();
		$("#topmedia").topmedia();
		$("#mvideo, #igrafica").mediaRotate();
		$("#panorama").mediaRotate({hovers: true}).mediaAdds();

		//mm
		$("#other_diapo, #other_video, #other_infogfx").mediaAdds();
	});
})(jQuery);

function vote(obj) {
	var url = jq(obj).attr("action").replace('.html', '.json');
	var params = jq(obj).serializeArray();
	jq.post(url, params, function(data) {
		jq(obj).parent().replaceWith(data);
	}, "html");

	return false;
}

