/*
 * jQuery slideNews v1.0.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
jQuery.fn.slideNews = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		btPause: 'a.btn-pause',
		tabsNews: 'ul.tabs a',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		duration: 2000
	},_options)

	return this.each(function(){
		var _this = $(this);
		var _lenghtSlide = $(_options.scrollEl, $(_options.holderList, _this)).length;
		var _gWidth = $(_options.holderList, _this).innerWidth();
		var _liWidth = $(_options.scrollEl, $(_options.holderList, _this)).outerWidth();
		var _liSum = $(_options.scrollEl, $(_options.holderList, _this)).length * _liWidth;
		var _rightArrow = $(_options.btNext,  _this);
		var _leftArrow = $(_options.btPrev,  _this);
		var _tabs = $(_options.tabsNews,  _this);
		var _pause = $(_options.btPause,  _this);
		var _scrollEl = $(_options.scrollElParent, $(_options.holderList, _this));
		var _margin = 0;
		var _duration = _options.duration;
		var f = 0;
		var _step = _gWidth;
		var _slideTimer = false;
		
		_this.hover(function(){
			if (_slideTimer) clearTimeout(_slideTimer);
		}, function(){
			if (_options.autoSlide) _slideTimer = setTimeout(function(){_this.autoSlide()}, _options.autoSlide);
		});
		_this.bind('mousemove', function(){if (_slideTimer) clearTimeout(_slideTimer);});
		
		_this.nextSlide = function() {
			if (_slideTimer) clearTimeout(_slideTimer);
			if (_liSum - _gWidth  <= _margin + _step) {
				if (f == 0) {_margin = _liSum - _gWidth; f = 1;} 
				else {_margin = 0;f = 0;}
			} else _margin = _margin + _step;
			$(_scrollEl).animate({marginLeft: -_margin+"px"}, {queue:false, duration: _duration});
			$(_tabs).removeClass("active");
			$(_tabs).eq(_lenghtSlide-(_liSum-_margin)/_liWidth).addClass("active");
			if (_options.autoSlide) _slideTimer = setTimeout(function(){_this.autoSlide()}, _options.autoSlide);
			return false;
		}
		if (_options.btNext) {
			$(_rightArrow).click(_this.nextSlide);
		}
		if (_options.btPrev) {
			$(_leftArrow).click(function(){
				if (_slideTimer) clearTimeout(_slideTimer);
				_margin = _margin - _step;
				if (_margin < 0) _margin = _liSum - _liWidth;
				$(_scrollEl).animate({marginLeft: -_margin + "px"}, {queue:false, duration: _duration});
				$(_tabs).removeClass("active");
				$(_tabs).eq(_lenghtSlide-(_liSum-_margin)/_liWidth).addClass("active");
				
				if (_options.autoSlide) _slideTimer = setTimeout(function(){_this.autoSlide()}, _options.autoSlide);
				return false;
			});
		}
		if (_options.btPause) {
			var _autoSlideDuration = false;
			_pause.click(function(){
				if ($(this).is('.play')) {
					$(this).removeClass('play');
					_options.autoSlide = _autoSlideDuration;
					_slideTimer = setTimeout(function(){
						_this.autoSlide()
						alert(_options.autoSlide);
					}, _options.autoSlide);
				} else {
					$(this).addClass('play');
					_autoSlideDuration = _options.autoSlide;
					_options.autoSlide = false;
				}
				return false;
			});
		}
		if (_options.tabsNews) {
			$(_tabs).click(function(){
				if (_slideTimer) clearTimeout(_slideTimer);
				$(_tabs).removeClass("active");
				var _gn = $(_tabs).index($(this));
				_margin = _step*_gn;
				$(_scrollEl).animate({marginLeft: -_margin + "px"}, {queue:false, duration: _duration});
				$(this).addClass("active");
				
				if (_options.autoSlide) _slideTimer = setTimeout(function(){_this.autoSlide()}, _options.autoSlide);
				return false;
			});
		}
		if (_options.autoSlide) {
			_this.autoSlide = function() {
				_this.nextSlide();
			}
			_slideTimer = setTimeout(function(){_this.autoSlide()}, _options.autoSlide);
		}
	});
}

$(document).ready(function(){
	$('#gallery').slideNews({
		btNext:'a.btn-next',
		btPrev:'a.btn-prev',
		btPause: 'a.btn-pause',
		tabsNews:'div.nav a',
		holderList: 'div.gallery',
		scrollElParent: 'ul',
		scrollEl: 'li',
		autoSlide: 3000
	});
	var _control = $('div.control');
	var _div = $('div', _control);
	var _ul = $('ul', _control);
	var _link = $('span a', _control);
	_link.data('open', false);
	_link.click(function(){
		if (!_link.data('open')) {
			_link.data('open', true);
			_ul.animate({marginLeft:0}, {queue:false, duration:400});
			_div.animate({width:220}, {queue:false, duration:400});
		} else {
			_link.data('open', false);
			_ul.animate({marginLeft:-220}, {queue:false, duration:400});
			_div.animate({width:0}, {queue:false, duration:400});
		}
	});	
});
