/**
 * newsSlider
 * @jQuery function
 */
 var this_news = 0;
 var count_block = 0;
var NSlider = {

	easeFunc: "expoinout",
	easeTime: 500,
	delay: 800,
	timer: null,
	timerClose: null,
	timerHover: null,
	cur: -1,
	curPanel: 0,
	
	init: function(el)
	{
		this.el = el;
		this.slider = $(el);
		this.length = this.slider.find('li').length;
		this.Count_news = this.slider.find('li').size();
		this.slider_panel = $(el + '_panel');
		this.slider_opened = $(el + '_opened');
		this.wrap = this.slider.find('.wrap');
		this.panelWidth = this.slider.find('.panel').width();
		this.panelCount = this.slider.find('.panel').size();
		this.sliderWidth = this.panelWidth * this.panelCount;
		
		this._initEvents();
	},
	
	_initEvents: function()
	{
		$('body').mousemove(function(e)
		{
			if($(e.target).parents(NSlider.el).length == 0)
			{
				if(NSlider.timerClose == null)
					NSlider.timerClose = setTimeout('NSlider.hide()', NSlider.delay);
			}
			else
			{
				clearTimeout(NSlider.timerClose);
				NSlider.timerClose = null;
			}
		});
		
		
		this.slider.find('li').each(function(i, el) {
			$(el).find('a').mouseover(function(){
				NSlider.timer = setTimeout('NSlider.view('+ i +')', NSlider.delay);
				this_news = i;
				NSlider._check_nav();
			}).mouseout(function(e){
				clearTimeout(NSlider.timer);
				NSlider.timer = null;
			});
		});
		
		if (this.sliderWidth >= 0)
		{
			this.wrap.width(this.sliderWidth);
			
			if(this.panelCount > 1)
			{
				this.slider_panel.append("<a href=\"#back\" class=\"back\" title=\"Назад\"></a>");
				this.slider_panel.append("<a href=\"#forward\" class=\"forward\" title=\"Вперед\"></a>");
			}
			
			function _timerHoverClear()
			{
				clearTimeout(NSlider.timerHover);
				NSlider.timerHover = null;
			}
			
			this.slider.find("a.back").click(function()
			{
				var thiswrap = $(this).parents(NSlider.el).find(".wrap");
				var cnt = parseInt(thiswrap.css("left"));
				cnt = Math.floor(cnt/NSlider.panelWidth)*NSlider.panelWidth;
				//if (cnt != 0)
				//{
					NSlider.curPanel--;
					if(NSlider.curPanel <= 0) NSlider.curPanel = 0;
					//this_news = this_news - 1; 
					//alert (this_news);
					NSlider.view(NSlider.curPanel + this_news - 1);
					clearTimeout(NSlider.timerClose);
					NSlider.timerClose = null;
					cnt = cnt + NSlider.panelWidth;
					//alert (this_news);
					count_block = Math.ceil(this_news/3);
					//if (NSlider.Count_news % this_news==2)
					if (count_block*3 == this_news)
					{
						thiswrap.animate({left:cnt}, NSlider.easeTime, NSlider.easeFunc);
						//count_block = 3;
					}
					//count_block = count_block - 1;
					this_news = this_news - 1;
					NSlider._check_nav();
				//}
				_timerHoverClear();
				return false;
			
			}).hover(function()
			{
				//var self = $(this);
				//NSlider.timerHover = setTimeout(function(){return self.trigger('click')}, 3000);
			}, function() {
				_timerHoverClear();
			});
			
			this.slider.find("a.forward").click(function()
			{
				var thiswrap = $(this).parents(NSlider.el).find(".wrap");
				var cnt = parseInt(thiswrap.css("left"));
				
				cnt = Math.ceil(cnt/NSlider.panelWidth)*NSlider.panelWidth;
				
				//if (cnt != -(NSlider.sliderWidth-NSlider.panelWidth))
				//{
					//if(NSlider.curPanel >= NSlider.panelCount-1) return false;
					NSlider.view(NSlider.curPanel + this_news + 1);
					/*if (NSlider.Count_news == this_news)
					{
						NSlider.curPanel++;
					}*/
					clearTimeout(NSlider.timerClose);
					NSlider.timerClose = null;
					cnt = cnt - NSlider.panelWidth;
					//alert (NSlider.Count_news % this_news + ' - ' + NSlider.Count_news + ' - ' + this_news);
					
					//alert (Math.ceil(NSlider.Count_news/3));
					count_block = Math.ceil(this_news/3);
					//if (NSlider.Count_news % this_news==1)
					if (count_block*3-1 == this_news)
					{
						thiswrap.animate({left:cnt}, NSlider.easeTime, NSlider.easeFunc);
						//count_block = count_block + 1;
					}
					this_news = this_news + 1;
					//alert (count_block);
					NSlider._check_nav();
					//alert (NSlider.Count_news + " - " + this_news);
				//}
				_timerHoverClear();
				return false;
				
			}).hover(function()
			{
				//var self = $(this);
				//NSlider.timerHover = setTimeout(function(){return self.trigger('click')}, 3000);
			}, function() {
				_timerHoverClear();
			});
			
			this._check_nav();
		}
	},
	
	_check_nav: function()
	{
		var b = this.slider.find('.back');
		var f = this.slider.find('.forward');
		var tmp = this.slider.find('li').size();
		
		////!this.curPanel ? b.hide() : b.show();
		//this.curPanel >= this.panelCount-1 ? f.hide() : f.show();
		if (this_news != 0)
		{
			b.show();
			f.show();
		} else {
			b.hide();
		}
		if (this_news == tmp - 1)
		{
			b.show();
			f.hide();
			//alert (this_news + ' - ' + tmp);
		}
	},
	
	view: function(i)
	{
		if(this.cur == i) return;
		
		this.cur = i;
		var self = this.slider.find('li:eq('+ i +')').find('a');
		var title = self.attr('title');
		var text = self.attr('post');
		var href = self.attr('href');
		if(text == undefined) text = '';
		//this_news = this_news+i;
		//count_block = count_block+i;
		//this.hide();
		this.slider_opened.html('<h3 class="news-title">'+ title +'</h3>' + '<div class="news-text"><a href="'+ href +'">' + text + '</a></div>');
		this.slider_panel.slideDown(150);
	},
	
	hide: function()
	{
		this.cur = -1;
		this.slider_panel.hide('fast');
	}
}

// DOM onload
$(function() {
	NSlider.init('#news');
});
