(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() {
				
				$(this).parent().parent().parent().find("a").removeClass("active");
				$(this).addClass("active");
				
				var type = $(this).parent().attr("class");
				
						
				$(this).parent().parent().parent().parent().parent().find("ul.media_container li").each(function() {
					if ($(this).hasClass(type)) {
						$(this).show();
					} else {
						$(this).hide();
					}
				});
				
				
topmediaReset(th);

				
				return false;
			
			});
			
			
			var interval = 64;
			
			$(this).find("ul.media_container li .topmedia_img a.img_link").click(function() {
				
				if ($(this).hasClass('img_link')) {
				
				$(this).toggleClass("img_link");
				var li = $(this).parents('li');
				
				var curCount = $("ul.media_container li", th).length;
				
				
				
				if (!li.hasClass("topmedia_active") && curCount > o.count) {
					
					var ul = li.parent();
					
					var li_position = $("li", ul).index(li);
					
					var active_li_position = $("li", ul).index($("li.topmedia_active", ul));
					
					var direction = li_position > active_li_position ? 'forvard' : 'back';
					
					
					
						
					$("li.topmedia_active .topmedia_text", ul).fadeOut(o.speed, 			
							function() {
						
						
								if (direction == 'back' && li_position == 0) {
									ul.prepend($("li", ul).eq(curCount-1)).css({left:parseInt(ul.css("left").replace('px', '')) - interval});
									}
								if (direction == 'forvard' && li_position == curCount-1) {
									ul.append($("li", ul).eq(0)).css({left:parseInt(ul.css("left").replace('px', '')) + interval});					
									}
							
								
								  
								li.animate(
										{top:o.actTop}, 
										o.speed+5, 
										function() {
											$(this).addClass("topmedia_active");
											$(".topmedia_text", $(this)).fadeIn(o.speed);
												}).find("img").animate({width:o.actWidth, height:o.actHeight}, o.speed);
								
								
								
								li.siblings("li.topmedia_active").animate(
										{top:o.noactTop}, 
										o.speed, 
										function() { 
											$(this).removeClass("topmedia_active").find(".topmedia_img a").addClass("img_link"); 
																						
											
											
											
										}).find("img").animate({width:o.noactWidth, height:o.noactHeight}, o.speed);	
								
								
								
					var shift =  li_position > active_li_position ? parseInt(ul.css("left").replace('px', '')) - interval : parseInt(ul.css("left").replace('px', '')) + interval;
								
						
					
								li.parent().animate({left:shift}, o.speed);
							
								

						
					});
					
					
					
					
					
					
					
					return false; }
				
				
				
				}
			
			
			
			return;
			
			/* если не центаральная картинка   и число видимых больше нуля */
			
			});

$(this).parent().find("img.butt_left").click(function(){
	
	var curCount = $("ul.media_container li:visible", th).length;
	
	
	
	if (curCount > o.count) {
	
	var ul = $("ul.media_container");
	
	var a_index =  $("ul.media_container li:visible", th).index($("li.topmedia_active:visible", ul));
	
	
	
	$("ul.media_container li:visible", th).eq(a_index - 1).find(".topmedia_img a").trigger('click');
	
	}
	
});

$(this).parent().find("img.butt_right").click(function(){
	
	var curCount = $("ul.media_container li:visible", th).length;
	
	
	
	if (curCount > o.count) {
	
	var ul = $("ul.media_container");
	
	var a_index =  $("ul.media_container li:visible", th).index($("li.topmedia_active:visible", ul));
	
	
	
	$("ul.media_container li:visible", th).eq(a_index + 1).find(".topmedia_img a").trigger('click');
	
	}
	
});

$(this).parent().find("img.butt_right").click(function(){}); 
 

			function topmediaReset(obj) {
				var curCount = $("ul.media_container li:visible", obj).length;
				$("ul.media_container li .topmedia_text", obj).hide();
				obj.find("ul.media_container").css({left:(curCount <= Math.floor(o.count / 2) ? 70 : 0)}).find("li:visible").css({top:o.noactTop}).removeClass("topmedia_active").find("img").css({width:o.noactWidth, height:o.noactHeight});
				obj.find("ul.media_container 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(".lft a, .rght 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("lft");
				
				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.mediarotate = function(options) {
		var options = $.extend({
			hovers: false
		}, options);
		
		return this.each(function() {
			var container = $(this);
			
			var total_el = $("div.centered2", container).length;
			
			$(this).find(".lft a, .rght a").click(function() {
				
				var visible_element_index = $("div.centered2", container).index($("div.centered2:visible", container));
			
				var left_button = $(this).parent().hasClass("lft");
												
				container.find("div.centered2").eq(visible_element_index).hide();
				
				container.find("div.centered2").eq(left_button ? visible_element_index-1 : visible_element_index+1).mouseover();
				
				
				if (visible_element_index == (left_button ? 0 : total_el-1)) 
				{
					left_button ? container.find('div.centered2').eq(visible_element_index).before(container.find('div.centered2').eq(total_el-1)) : container.find('div.centered2').eq(visible_element_index).after(container.find('div.centered2').eq(0));
					container.find("div.centered2").eq(visible_element_index).addClass('active').show().mouseover();
				
				}
				else
				{
					container.find("div.centered2").eq(left_button ? visible_element_index-1 : visible_element_index+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();
		$("#panorama2").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;
}

function release(obj) {
	
	jq(obj).parents("form.victorina_form").find('input.form_submit').attr('disabled','');

}



