jQuery(document).ready(function(){
	jQuery('div.gallery').gallSlide({
		duration: 1500,
		autoSlide: 9000
//duration: 400 - time between slide transition
//autoSlide: 300 - autoslide speed

	});
	if(jQuery('div.scroller-holder li').length > 3){
		jQuery('div.scroller-holder').addClass('gallery-scroll');
		jQuery('div.scroller-holder').crawlLine({crawElement:'div.scroller-frame' ,textElement:'ul.partner-list'});
	}
	initSlide();
});

jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.slider');
		var _el = _hold.find('ul.slider > li');
		var _next = _hold.find('a.next');
		var _prev = _hold.find('a.prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();
		_hold.hover(function(){
			if(_t) clearTimeout(_t);
		}, function(){
			runTimer();
		});
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
	});
}

jQuery.fn.crawlLine = function(_options){
	// defaults options
	var _options = jQuery.extend({
		speed:2,
		crawElement:'div',
		textElement:'p',
		hoverClass:'viewText'
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this);
		var _el = jQuery(_options.crawElement, _THIS).css('position','relative');
		var _text = jQuery(_options.textElement, _THIS);
		var _clone = _text.css('whiteSpace','nowrap').clone();
		var _elWidth = 0;
		var _k = 1;
		
		// set parametrs *******************************************************
		var _textWidth = 0;
		_text.each(function(){
			_textWidth += jQuery(this).outerHeight(true);
		});
		var _duration = _textWidth*50 / _options.speed;
		_el.append(_clone);
		_el.css('width',_textWidth*2);
		
	    var animate = function() {
			_el.animate({top:-_textWidth}, {queue:false, duration:_duration*_k, easing:'linear', complete:function(){
				_el.css('top','0');
				_k=1;
				animate();
			}})
	    }
	    animate();
		
/*
	    _THIS.hover(function() {
			_el.stop();
			_THIS.addClass(_options.hoverClass);
	    }, function(){
			_THIS.removeClass(_options.hoverClass);
			_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
			animate();
	    })
*/
		_THIS.bind('wheel',function(event,delta){
			var _marginScroll;
			if (delta<0) {
				_marginScroll = parseInt(_el.css('top')) - 20;
				_el.animate({top:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('top')))/_textWidth;
				}});
			} else {
				_marginScroll = parseInt(_el.css('top')) + 20;
				if (_marginScroll > 0) _marginScroll = 0;
				_el.animate({top:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('top')))/_textWidth;
				}});
			}
			return false;
		});
	});
}
function initSlide(){
	var _duration = 400; //in ms
	jQuery('ul.slide-nav > li').each(function(){
		var _hold = jQuery(this);
		var _box = _hold.find('div.slide');
		var _btn = _hold.find('a.opener');
		var _h = _box.height();
		if(_hold.hasClass('opened')) _box.show();
		else _box.hide();
		
		_btn.click(function(){
			if(_hold.hasClass('opened')){
				_hold.removeClass('opened');
				_box.stop().animate({height:0}, _duration, function(){
					jQuery(this).css({display:'none', height:'auto'});
				});
			}
			else{
				_hold.addClass('opened');
				if(_box.is(':hidden')){
					_box.show();
					_h = _box.height();
					_box.height(0);
				}
				_box.stop().animate({height: _h}, _duration, function(){ 
					jQuery(this).height('auto');
				})
			}
			return false;
		});
		
	});
}

