(function($){
	$(function(){
		gall1 = $('div#content').gallery({
			duration: 300,
			listOfSlides: 'ul.gallery-slides>li',
			switcher: '.gallery ul>li a',
			effect: 'fade',
			event: 'hover'
		});
	});
})(jQuery);

(function($) {
	function Gallery(el, options) {
		this._hold = $(el);
		this.initOptions(options);
		this._timer = options.autoRotation;
		this._t;
		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,
				event: false,
				IE: false,
				swicherReplace: 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 (_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 switcherReplace = _hold.find(_this.options.swicherReplace);
			
			// 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"
			if (switcherReplace) switcherReplace.html((_active+1)+'/'+(_count+1));
			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;
				if (switcherReplace) switcherReplace.html((_active+1)+'/'+(_count+1));
			}
			
			// 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();
			}
			if(_this._timer){
				_this._hold.bind('runTimer', function(){
					if(_this._t) clearTimeout(_this._t);
					_this._t = setInterval(function(){
						toPrepare();
					}, _this._timer);
				});
			}
			_next.click(function(){
				if(_this._t) clearTimeout(_this._t);
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
				toPrepare();
				if (_this._timer) _this._hold.trigger('runTimer');
				return false;
			});
			_prev.click(function(){
				if(_this._t) clearTimeout(_this._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 (_this._timer) _this._hold.trigger('runTimer');
				return false;
			});
			if (_this.options.switcher) _switcher.click(function(){
				if(!_this.options.event){
					_active = _switcher.index($(this));
					if(_this._t) clearTimeout(_this._t);
					if (!_this.options.effect) scrollEl();
					else fadeElement();
					if (_this._timer) _this._hold.trigger('runTimer');
					return false;
				}
			}).mouseenter(function(){
				if(_this.options.event){
					_active = _switcher.index($(this));
					if(_this._t) clearTimeout(_this._t);
					if (!_this.options.effect) scrollEl();
					else fadeElement();
					if (_this._timer) _this._hold.trigger('runTimer');
				}
			});
			if (_this._timer) _this._hold.trigger('runTimer');
		},
		stop: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
		},
		play: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
			if (_this._timer) _this._hold.trigger('runTimer');
		}
	}
}(jQuery));

// click nav
function initClickNav(){
	$('#nav li:has("ul")').find('>a').each(function(){
		var this_link = $(this);
		var this_parent = $(this).parent();
		var this_popup = $(this).siblings('ul');
		var active_class = 'active';
		
		var all_link = $(this).parent().parent().find('>li>a');
		var all_parents = $(this).parent().parent().find('>li');
		var all_popups = $(this).parent().parent().find('>li>ul');
		
		if(this_parent.hasClass(active_class)){
			this_popup.show();
		}
		
		this_link.click(function(){
			all_popups.hide();
			this_popup.show();
			all_parents.removeClass(active_class);
			this_parent.addClass(active_class);
			return false;
		});
	});
}

/*--- popup function ---*/
function initPopup(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	_fader.hide();
	var _popup = -1;
	
	$('a.with-popup').each(function(){
		var _el = this;
		if(_el.hash && _el.hash.length > 1){
			_el._popup = $(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_el._popup.hide();
						_popup = _el._popup;
						showPopup();
					}
					return false;
				}
				_el._popup.find('.close').click(function(){
					if($.browser.msie){
						_popup.hide();
						_fader.fadeOut(200);
						_popup = -1;
						if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
					}
					else{
						_popup.fadeOut(400, function(){
							_fader.fadeOut(200);
							 _popup = -1;
						});
					}
					gallery1.play();
					gallery2.play();
					gallery3.play();
					return false;
				});
			}
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
				if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
			gallery1.play();
			gallery2.play();
			gallery3.play();
		}
		return false;
	});
	$(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27 && _popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
				if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
			gallery1.play();
			gallery2.play();
			gallery3.play();
		}
	});
	function showPopup(){
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({top: 119});
		if($.browser.msie && $.browser.version < 7) if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'hidden');
		_fader.fadeTo(400, 0.5, function(){
			if(_popup != -1){
				if($.browser.msie) _popup.show();
				else _popup.fadeIn(200, function(){
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				});
				_popup.css({
					top: 119
				});
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				gallery1.stop();
				gallery2.stop();
				gallery3.stop();
			}
		});
	}
	function initH(){
		var _h = $('#wrapper').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}

function initHoverIE6(){
	if($.browser.msie && $.browser.version <7){
		var selectors = '#nav li';
		var hover_class = 'hover';
		$(selectors).mouseenter(function(){
			$(this).addClass(hover_class);
		}).mouseleave(function(){
			$(this).removeClass(hover_class);
		});
	}
}
