$(document).ready(function(){
	$('.twitlabel, .close-box').click(function() {
		if( $(this).parent().css('right') == '0px' ) {
			box = $(this).parent();
	
			box.css("z-index", 500)
				.animate({'right': '-472px'})
				.animate({'top': boxTopFixed });
	
				$('.twitbox:not(.active)').fadeIn();
				$(this).parent().toggleClass("active");

				$('.twitlabel').each(function(){
					$(this).parent().css("z-index", 1000 + $(this).index() );
				});
		} else {
			$(this).parent().toggleClass("active");
			$('.twitbox:not(.active)').fadeOut();

			boxTopFixed = parseInt( $(this).parent().css("top") );

			$(this).parent().css("z-index", 1000)
							.animate({'top': 151})
							.animate({'right': '0px'});

		}
	});

	$('.arrow, #additional-arrow').click(function this_function(){
		active_spec++;

		$.get( links[active_spec % links.length], function(data) {
			data = data.replace('text-align: right;', '');
			data = data.replace('<br />', '');
			data = data.replace('<br>', '');
			data = data.replace('<p></p>', '');
			$('#spec-content').html(data);
//				$('.spd_link a').attr('href', links[active_spec % links.length].replace('/spec-anon/', '/spec/'));
			$('.spd_link a').attr('href', '/shares.php');

			$("#additional-arrow").click( this_function );
		});
	});

	active_act = 0;
	$('#act_arrow').click(function(){
		spec_id = $("#act_content .spec_id").html();
		ajax_spec_id = spec_id;

		get_new_spec();
	});




function get_new_spec() {
	$.get( links[ active_act % links.length ], function(data) {
		data = data.replace('Предложение действительно ', '');
		data = data.replace('Узнать больше о предложении', 'Подробнее');
		data = data.replace('background', 'error');

		ajax_spec_id = $(".spec_id", data).html();
		if (ajax_spec_id != spec_id) {	
			$('#act_content').html(data);
//			$('#act_content .spd_link a').attr('href', links[active_act % links.length].replace('/spec-anon/', '/spec/'));
			$('#act_content .spd_link a').attr('href', '/shares.php');
		} else
			get_new_spec();
	});
	active_act++;
}







	$(".c_right").css("min-height", $(".l_menu").height()+140 );
	$(".content").css("padding-bottom", $(".toread").height()+40 );
	$(".toread").css("width", $(".c_right").width() - 10 );

	if ( $(".breadcrumb").height() < 50)
		$(".breadcrumb").css("margin-top", "-41px");
	else $(".breadcrumb").css("margin-top", "-48px");

	$(window).resize(function(){
		$(".content").css("padding-bottom", $(".toread").height()+40 );
		$(".toread").css("width", $(".c_right").width() - 10 );

		if ( $(".breadcrumb").height() < 50)
			$(".breadcrumb").css("margin-top", "-41px");
		else $(".breadcrumb").css("margin-top", "-48px");
	});




	$("#content-menu > li > a").mousemove(function(e){
	      mouseX = e.pageX;
	      mouseY = e.pageY;
	});

	$("#content-menu > li> a").hoverIntent(function(){
		active = $(this).parent().index();

		if ( $("#l_menu > li:eq("+active+") ul").length ) {
		        var left = mouseX + 7 - $(this).offset().left + $(this).position().left;
		        var top = mouseY - 2 - $(this).offset().top + $(this).position().top;

			if ( $(this).offset().top - $(window).scrollTop() > $(this).next("div").height() )
				top -= $(this).next("div").height();

			$(this).next("div").css("left", left)
						.css("top", top)
						.css("z-index", "1000")
						.fadeIn();
		}

	}, function(){

	});

	$("#content-menu > li").hover(function(){
	}, function(){
		$(this).find("div").fadeOut();
	});

});
