/* Gallery (slide-on-click, auto-slide-left) */
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: false,
		slideElement: 1,
		next: 'a.link-next, a.btn-next, a.next',
		prev: 'a.link-prev, a.btn-prev, a.prev'
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _sliderEl = _options.slideElement;
		var _wrap = _hold.find('div.slider ul');
		var _el = _hold.find('div.slider ul > li');
		var _bigImg = _hold.find('div.gallery-img ul > li');
		var _next = _hold.find(_options.next);
		var _prev = _hold.find(_options.prev);
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		if (_timer) var _t;
		var _active= 0;
		var _active2 = _el.index(_el.filter('.active:eq(0)'));
		if(_active2 < 0) _active2 = 0;
		var _last = _active2;
		
		_bigImg.css({opacity:0}).eq(_active2).css({opacity:1}).css({opacity:'auto'});
		_bigImg.removeClass('active').eq(_active2).addClass('active');
		_el.removeClass('active').eq(_active2).addClass('active');
		
		function scrollEl(){
			_wrap.animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function fadeEl(){
			_bigImg.eq(_last).animate({
				opacity:0
			}, {queue:false, duration: _speed});
			_bigImg.eq(_active2).animate({
				opacity:1
			}, {queue:false, duration: _speed, complete: function(){
				jQuery(this).css('opacity','auto');
			}});
			_bigImg.removeClass('active').eq(_active2).addClass('active');
			_el.removeClass('active').eq(_active2).addClass('active');
			_last = _active2;
		}
		function runTimer(){
			_t = setInterval(function(){
				if (_active == (_count - _wrapHolderW + 1)) _active = -_sliderEl;
				for (var i = 0; i < _sliderEl; i++){
					_active++;
					if (_active > (_count - _wrapHolderW + 1)) {
						_active--;
						break;
					}
				};
				scrollEl();
			}, _timer);
		}
		if (_timer) runTimer();
		_el.click(function(){
			_active2 = _el.index(jQuery(this));
			fadeEl();
			return false;
		});
		_next.click(function(){
			if(_t) clearTimeout(_t);
			if (_active == (_count - _wrapHolderW + 1)) _active = -_sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) {
					_active--;
					break;
				}
			};
			scrollEl();
			if (_timer) runTimer();
			return false;
		});
		_prev.click(function(){
			if(_t) clearTimeout(_t);
			if (_active == 0) _active = _count - _wrapHolderW + 1 + _sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active--;
				if (_active < 0) {
					_active++;
					break;
				}
			};
			scrollEl();
			if (_timer) runTimer();
			return false;
		});
	});
}
function initOpen(){
	var link = jQuery('a.open-block');
	var block = jQuery('div.login-level');
	var h = block.outerHeight(true);
	var close = jQuery('a.btn-close');
	
	block.css({marginTop:-h});
	
	link.click(function(){
		if (block.hasClass('opened')){
			block.animate({marginTop:-h}, 500).removeClass('opened');
		}
		else{
			block.animate({marginTop:0}, 500).addClass('opened');
		}
		return false;
	});
	close.click(function(){
		block.animate({marginTop:-h}, 500).removeClass('opened');
		return false;
	});
}
function initTabs(){
	jQuery('ul.tabset').each(function(){
		var btn_h = jQuery(this);
		var _btn = jQuery(this).find('a.tab');
		var _btn2 = jQuery(this).find('li.item');
		//var _a = _btn.index(_btn.filter('.active:eq(0)'));
		var _a = _btn2.index(_btn2.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			this._box = jQuery(this).attr('rel');
			if(this._box){
				this._box = jQuery(this._box);
				if(_i == _a) this._box.show();
				else this._box.hide();
			}
			this.onmouseover = function(){
				changeTab(_i);
				return false;
			}
		});
		function changeTab(_ind){
			if(_ind != _a){
				if(_btn.get(_a)._box) _btn.get(_a)._box.hide();
				if(_btn.get(_ind)._box) _btn.get(_ind)._box.show();
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_a = _ind;
			}
		}
	});
}
jQuery(document).ready(function(){
	// Little hack for IE < 8
	jQuery('.letters-list li:nth-child(3)').addClass('third');
	jQuery('.letters-list li:nth-child(3)').before('<div style="clear:both;"></div>');
	
	initTabs();	
	//jQuery('div.gallery-slide').gallSlide({ duration: 700 });
	initOpen();
	//jQuery('a.lightbox1').lightBox();
	//jQuery('a.lightbox2').lightBox();
	//initMenu();
});

