(function($) {
	function Gallery(el, options) {
		this._hold = $(el);
		this.initOptions(options);
		this.initialize();
	}
	
	$.fn.gallery = function(options) {
		return new Gallery(this.get(0), options);
	};
	
	Gallery.prototype = {
		initOptions: function(_obj){
			this.options = {
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				IE: false,
				pause: false,
				delayStart: false
			};
			for(key in _obj) this.options[key] = _obj[key];
		},
		initialize: function(){
			var _this = this;
			var _hold = _this._hold;
			var _speed = _this.options.duration;
			var _timer = _this.options.autoRotation;
			var _el = _hold.find(_this.options.listOfSlides);
			if (_this.options.effect) var _list = _el;
			else var _list = _el.parent();
			var _switcher = _hold.find(_this.options.switcher);
			var _next = _hold.find(_this.options.nextBtn);
			var _prev = _hold.find(_this.options.prevBtn);
			var _count = _el.index(_el.filter(':last'));
			var _w = _el.outerWidth(true);
			var _h = _el.outerHeight(true);
			if (_timer) var _t;
			if (_this.options.switcher) var _active = _switcher.index(_switcher.filter('.active:eq(0)'));
			else var _active = _el.index(_el.filter('.active:eq(0)'));
			if (_active < 0) _active = 0;
			var _last = _active;
			var pause = _hold.find(_this.options.pause);
			var link123 = _hold.find('a.details');
			
			// Installation directions
			if (!_this.options.direction) {
				var _wrapHolderW = Math.ceil(_list.parent().width() / _w);
				if (((_wrapHolderW - 1) * _w + _w / 2) > _list.parent().width()) _wrapHolderW--;
			}
			else{
				var _wrapHolderW = Math.ceil(_list.parent().height()/_h);
				if (((_wrapHolderW-1)*_h + _h/2) > _list.parent().height()) _wrapHolderW--;
			}
			
			// Setting "fade" or "slide" effect
			if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
			else var rew = _count;
			if (!_this.options.effect) {
				if (!_this.options.direction) _list.css({marginLeft: -(_w * _active)})
				else _list.css({marginTop: -(_h * _active)})
			}
			else {
				_list.css({
					opacity: 0
				}).removeClass('active').eq(_active).addClass('active').css({
					opacity: 1
				}).css('opacity', 'auto');
				_switcher.removeClass('active').eq(_active).addClass('active');
			}
			
			// Disable or enable buttons "prev next"
			if (_this.options.disableBtn) {
				if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
				_prev.addClass(_this.options.disableBtn);
			}
			
			// Function to "fade"
			function fadeElement(){
				if ($.browser.msie && _this.options.IE){
					_list.eq(_last).css({opacity:0});
					_list.removeClass('active').eq(_active).addClass('active').css({opacity:'auto'});
				}
				else{
					_list.eq(_last).animate({opacity:0}, {queue:false, duration: _speed});
					_list.removeClass('active').eq(_active).addClass('active').animate({
						opacity:1
					}, {queue:false, duration: _speed, complete: function(){
						$(this).css('opacity','auto');
					}});
				}
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
				_last = _active;
			}
			
			// Function for "slide"
			function scrollEl(){
				if (!_this.options.direction) _list.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
				else _list.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
			}
			function toPrepare(){
				if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
				for (var i = 0; i < _this.options.slideElement; i++){
					_active++;
					if (_active > rew) {
						_active--;
						if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
					}
				};
				if (_active == rew) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
				if (!_this.options.effect) scrollEl();
				else fadeElement();
			}
			function runTimer(){
				_t = setInterval(function(){
					_next.trigger('click');
				}, _timer);
			}
			_next.click(function(){
				if(_t) clearTimeout(_t);
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
				toPrepare();
				if (_timer && !_list.hasClass('stop')) runTimer();
				return false;
			});
			_prev.click(function(){
				if(_t) clearTimeout(_t);
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
				if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
				for (var i = 0; i < _this.options.slideElement; i++){
					_active--;
					if (_active < 0) {
						_active++;
						if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
					}
				};
				if (_active == 0) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
				if (!_this.options.effect) scrollEl();
				else fadeElement();
				if (_timer && !_list.hasClass('stop')) runTimer();
				return false;
			});
			if (_this.options.switcher) _switcher.click(function(){
				if (link123.hasClass('opened')) link123.eq(_active).trigger('click');
				_active = _switcher.index($(this));
				if(_t) clearTimeout(_t);
				if (!_this.options.effect) scrollEl();
				else fadeElement();
				if (_timer) runTimer();
				return false;
			});
			// if (_timer) runTimer();
			if (_this.options.delayStart){
				setTimeout(function(){
					if (_timer) runTimer();
				}, _this.options.delayStart)
			} else {
				if (_timer) runTimer();
			}
			if (pause) pause.click(function(){
				if (!_list.hasClass('stop')){
					if (_t) clearTimeout(_t);
					_list.addClass('stop');
					pause.addClass('play');
				}
				else{
					if (_t) clearTimeout(_t);
					if (_timer) runTimer();
					_list.removeClass('stop');
					pause.removeClass('play');
				}
				return false;
			});
		}
	}
}(jQuery));

function initSlide(){
	$('ul.txt-hold > li').each(function(){
		var hold = $(this);
		var link = hold.find('a.details');
		var block = hold.find('div.section');
		var box = hold.find('div.txt-holder');
		var h = box.outerHeight(true);
		var hh = block.outerHeight(true) + h;
		var hhl = block.outerHeight(true);
		var btn = $('a.link-next, a.link-prev');
		var pause = $('a.pause');
		
		// box.css({height:0});
		hold.parent().css({height:hhl});
		link.click(function(){
			if (!box.hasClass('opened')) {
				pause.trigger('click');
				hold.parent().animate({height:hh}, 300);
				/*
				box.animate({
						height: h
					}, 300).addClass('opened');
				*/
				box.addClass('opened');
				link.addClass('opened');
			}
			else {
				pause.trigger('click');
				/*
				box.animate({
					height: 0
				}, 300).removeClass('opened');
				*/
				box.removeClass('opened');
				hold.parent().animate({height:hhl}, 300);
				link.removeClass('opened');
			}
			return false;
		});
		btn.click(function(){
			if (box.hasClass('opened')) pause.trigger('click');
			/*
			box.animate({
				height: 0
			}, 300).removeClass('opened');
			*/
			box.removeClass('opened');
			hold.parent().animate({height:hhl}, 300);
			link.removeClass('opened');
			return false;
		});
	});
}
function initTabs(){
	$('div.tabset > ul').each(function(){
		var btn_h = $(this);
		var _btn = $(this).find('a.tab');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			this._box = this.href.substr(this.href.indexOf("#") + 1);
			if(this._box){
				this._box = $('#'+this._box);
				if(_i == _a) this._box.show();
				else this._box.hide();
			}
			this.onclick = 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;
			}
		}
	});
}
$(document).ready(function(){
	initSlide();
	$('div.gallery-holder').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'ul.big-img > li',
		switcher: 'ul.switcher > li',
		effect: 'fade',
		pause:'a.pause',
		IE:true,
		delayStart: 5000
	});
	$('div.gallery-holder').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'ul.txt-hold > li',
		switcher: 'ul.switcher > li',
		effect: 'fade',
		pause:'a.pause',
		IE:true,
		delayStart: 5000
	});
	initTabs();
});

function show_more(id) {
	if ($('#more_' + id).hasClass('close')) {
		$('#more_' + id).removeClass('close');
		$('#more_' + id + ' a').html('More');
		$('#extended_' + id).slideUp();
	} else {
		$('#more_' + id).addClass('close');
		$('#more_' + id + ' a').html('Close');
		$('#extended_' + id).slideDown();
	}
}

var customselectnav = false;

function selectnav() {
	if (customselectnav==false) {
		customselectnav=true;
		return;
	} else {
		location=document.catmenu.eventdate.options[document.catmenu.eventdate.selectedIndex].value;
	}
}

