$(function() {

	$( "#logindialog" ).dialog({
		autoOpen: false,
		show: 'fade',
		hide: 'fade',
		width: 300
	});
	
	$( "#subscribedialog" ).dialog({
		autoOpen: false,
		show: 'fade',
		hide: 'fade',
		width: 230,
		height:100
	});

	$('#loginlink').click(function(){
		$('#logindialog').dialog('open');
		return false;
	});
	
	$('#subscribelink').click(function(){
		$('#subscribedialog').dialog('open');
		return false;
	});
	
	$("a#show_all_banners").toggle(
		function(){
			$("div.links_container").css("display","block");
			$("div.slides_container").css("display","none");
			$("ul.pagination").css("opacity","0");
			$(this).text("Скрыть");
		},
		function(){
			$("div.links_container").css("display","none");
			$("div.slides_container").css("display","block");
			$("ul.pagination").css("opacity","1");
			$(this).text("Все микросайты");
	});
});

$(function() {

	$( "#polldialog" ).dialog({
		autoOpen: false,
		width: 450
	});

	$('#polllink').click(function(){
		$('#polldialog').dialog('open');
		return false;
	});
});




$(function(){
	
	$("#topmenu-in .pop").each(function() {
		$(this).removeClass("hidden-pop");
	})
	$('#topmenu-in>li').each(function() {
		if ($(this).find("div.iframe-banner-wrapper").height() == 0 ) {
			if ( $(this).find('ul li').length < 10 ) {
				$(this).children("div.pop").addClass("no-banner");
			}
			$(this).find("div.iframe-banner-wrapper").remove();
		}
		var pop = $(this).children("div.pop");
		if (pop.find("a.description").length > 0) {
			pop.css('width', '347px');
		}
		var menu_width = $('#topmenu').width();
		var pop_width  = pop.width();
		var li_width   = $(this).width();
		var li_pos     = $(this).position();
		var pop_pos    = Math.ceil(li_pos.left + li_width/2 - pop_width/2);

		if (pop_pos < 0) {
			pop.css({'left':'-8px', 'right': 'auto'});

		} else if (pop_width + li_pos.left > menu_width) {
			pop.css({'right':'-5px', 'left':'auto'});
		} else {

			pop.css({'left': pop_pos, 'right':'auto'});
		}

		if ($(this).find("iframe").size()) {
			var bannerframe = $(this).find("iframe");
			var ih = bannerframe.contents().find('body').height();
			
			if (ih > $(this).find(".in").height()) {
				bannerframe.attr("height", ih + "px");
			} else {
				bannerframe.attr("height", $(this).find(".in").height() + "px");
			}
		}
		
		pop.hide().css("visibility", "visible");
	})
	
	$('#topmenu-in>li').hover(function() {
		$("#topmenu-in div.pop").hide();
		var pop = $(this).children("div.pop");
		pop.show();
		pop.find("div.ctc").css("width", pop.width());
		pop.find("div.cbc").css("width", pop.width());
	})

	$('#topmenu').hover(
		function () {},
		function() {
			$("div.pop").hide();
		}
	)
});
