$(document).ready(function(){
	
	//закладки для популярных статей
	$('.b-pop .switcher li a').click(function() {
		if($(this).hasClass('active')) {}
		else{
			bem = $(this).attr('href');
			bem = bem.substr(1,bem.length);
			
			$('.switcher li a').removeClass('active');
			$(this).addClass('active');
			
			$('div.n-block:visible').slideUp(300, function() {$('#'+bem).slideDown()});
		}
		return false;
	});
	// закладки для информеров
	$('.b-informer .switcher li a').click(function() {
		if($(this).hasClass('active')) {}
		else{
			bem = $(this).attr('href');
			bem = bem.substr(1,bem.length);
			
			$('.switcher li a').removeClass('active');
			$(this).addClass('active');
			$('div.n-blockalt:visible').slideUp(300, function() {$('#'+bem).slideDown()});
		}
		return false;
	});

	//Клик на закладки в страничке раздела, вверху на черном фоне
	$('.sw li a').click(function() {
		if($(this).hasClass('here')) {}
		else{
			bem = $(this).attr('href');
			bem = bem.substr(1,bem.length);
			
			$('.sw li a').removeClass('here');
			$(this).addClass('here');
			$('div.wraphot:visible').hide();
			
			$('.'+bem).show()
		}
		return false;
	});
	//Форма комментирования
	var state = true;
	$('.addcomment').click(function() {
		if(state){
			state = false;
			$('.addcommentform').stop().slideToggle(500, function(){state=true;});
		}
		return false;
	});		
	// карусель в разделе мультмедиа
	$(".caro").jCarouselLite({
	    btnNext: ".rightcar",
	    btnPrev: ".leftcar",
			visible: 2
	});
	// карусель для блогов
	$(".blogentries").jCarouselLite({
	    btnNext: ".rightcar1",
	    btnPrev: ".leftcar1",
			visible: 3
	});
	//наведение курсора в разделе мультимедиа
	$('.multi li .bl').hover(function() {
			$(this).parent().addClass('active');
			$('.caro').addClass('carohovered');
		},function(){
			$(this).parent().removeClass('active');
			$('.caro').removeClass('carohovered');
	});

	//увеличить размер шрифта
  $("#largerfont").click(function(){
  	var currentFontSize = $('.post>p').css('font-size');
 		var currentFontSizeNum = parseFloat(currentFontSize, 10);
  	var newFontSize = currentFontSizeNum*1.2;
		$('.post>p,.post>ul').css('font-size', newFontSize);
		return false;
  });
  // уменьшить
  $("#smallerfont").click(function(){
  	var currentFontSize = $('.post>p').css('font-size');
 		var currentFontSizeNum = parseFloat(currentFontSize, 10);
  	var newFontSize = currentFontSizeNum*0.9;
		$('.post>p, .post>ul').css('font-size', newFontSize);
		return false;
  });
	
	//подсказки внутри инпутов, берутся из тайтла, сам плагин "after all"
	$('input[title!=""],textarea[title!=""]').hint();
	
	//статьи по теме, кликалка туда-сюда по кругу
	$('.rel-next').click(function(){
		$('#relatedpostlist li:visible').
		        hide().
		        next('li').
	          css({display:'inline'});
		      if ($('#relatedpostlist li:visible').length < 1) {
		        $('#relatedpostlist li:first').css({display:'inline'});
		      }
		return false;
	});
	$('.rel-prev').click(function(){
		$('#relatedpostlist li:visible').
		        hide().
		        prev('li').
	          css({display:'inline'});
		      if ($('#relatedpostlist li:visible').length < 1) {
		        $('#relatedpostlist li:last').css({display:'inline'});
		      }
		return false;
	});
	
	//popup for blog
	$('.b-popup .js').click(function(){
		$('.b-popup .popup').hide();
		$(this).parent().children('.popup').fadeIn('fast');
	});
	$('.b-popup .h3wrap').click(function(){
		$(this).parent().fadeOut('fast');
	});
	$('.printmeplease').click(function(){$('#screenstyles').attr({href:'css/print.css'});});
	$('.backtonormal').click(function(){$('#screenstyles').attr({href:'css/master.css'});});
	//popup for issues archives
	$('.month:nth-child(3n)').addClass('monthlastinrow');
	$('.mdata tr:first-child').addClass("firstrow");
	$('.mdata td a').wrap('<div class="b-popup"></div>');
	// POPUP WITH DATE ARCHIVE
	$('.mdata td .b-popup').hoverIntent(function(){
		popupc = $(this).children('.popup');
		if(popupc.length>0){//if popup already created
			// have a little whiskey
		}
		else{//creating popup
			thisdate = $(this).children('a').text();
			var d = new Date();
			thisid = d.getTime();
			adate = $('a',this);

			adate.after('<div class="popup" style="display:none">'+
				'<div class="h3wrap"><h4>'+ thisdate+ '</h4></div>'+
				'<div class="popup-top"></div>'+
				'<div class="popup-str"><div class="popup-str-in" id="id'+thisid+'">'+
					'<img src="img/loader.gif" alt="Идет загрузка..." />		'+
				'</div></div>'+
				'<div class="popup-bot"></div>'+
			'</div>');
			popupc =  $(this).children('.popup');
			// THIS SHOULD BE DONE WITH AJAX !!!!!!!!!!!!!!!!!!!!!! Not its just static
			$('#id'+thisid).html('<div class="vnomersmall"><a href="#"><img src="img/smnomer.png" alt="" /></a><h5>№ 49 (802)</h5><ul><li><a href="#">Финансовый кризис: робкий оптимизм</a></li><li><a href="#">Депутаты ставят под угрозу кредит МВФ</a></li><li><a href="#">Рейтинг надежных банков: ТОП-20</a></li><li><a href="#">Фондовые индексы упали из-за проблем </a></li><li><a href="#">автопроизводителей и банков</a></li></ul></div>');
		}
		popupc.fadeIn('fast');
		$(this).addClass('hoveredtd');
	}, function(){
		$('.popup',this).fadeOut('fast');
		$(this).removeClass('hoveredtd');
	});
	
	//hiding popup on "blur" (click elsewhere)
	$(document).click(function(e){
		tar = $(e.target);
		if(!(tar.parents('.b-popup').length > 0) && !(tar.hasClass(".b-popup"))){$('.popup').fadeOut('fast');}
//		if(tar.hasClass("add")){tar.next().toggle();}
	});
	
	Cufon.replace('.page h1 span,.aftepage h2');
	});
	/**
	* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
	* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
	* 
	* @param  f  onMouseOver function || An object with configuration options
	* @param  g  onMouseOut function  || Nothing (use configuration options object)
	* @author    Brian Cherne <brian@cherne.net>
	*/
	(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
	
	
	
	/*after all*/
	jQuery.fn.hint = function () {
	  return this.each(function (){
	    var t = jQuery(this);
	    var title = t.attr('title');
	    if (title) {
	      t.blur(function (){
	        if (t.val() == '') {
	          t.val(title);
	          t.addClass('blur');
	        }
	      });
	      t.focus(function (){
	        if (t.val() == title) {
	          t.val('');
	          t.removeClass('blur');
	        }
	      });
	      t.blur();
	    }
	  });
	}

