(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:0,
			actWidth:120,
			actHeight:83,
			noactTop:22,
			noactWidth:56,
			noactHeight:39
		}, o);

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

			$(this).find(".topmedia_rubric a").click(function() {
				var items = $(this).parent().parent().parent().find("ul li");
				if($(this).hasClass("active")) {
					$(this).removeClass("active");
					items.show();
				} else {
					$(this).parent().parent().find("a").removeClass("active");
					$(this).addClass("active");

					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(o.speed, 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', '')) - 66});
						}
						if (dir == 'forvard' && thIndex == curCount-1) {
							ul.append($("li:visible", ul).eq(0)).css({left:parseInt(ul.css("left").replace('px', '')) + 66});
						}

						li.animate({top:o.actTop}, 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}, o.speed, function() {
							$(this).removeClass("topmedia_active");
						}).find("img").animate({width:o.noactWidth, height:o.noactHeight}, o.speed);

						var pos =  thIndex > actIndex ? parseInt(ul.css("left").replace('px', '')) - 66 : parseInt(ul.css("left").replace('px', '')) + 66;
						ul.animate({left:pos}, o.speed);
					});

					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) ? 66 : 0)}).find("li:visible").css({top:o.noactTop}).removeClass("topmedia_active").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").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, #mvideo2").mediaRotate();
		$("#panorama").mediaRotate({hovers: true}).mediaAdds();

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

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

	return false;
}

$j(function(){
	$j('dl.tabs dt').click(function(){
		$j(this)
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
	});
});