// page init
jQuery(function(){
	initPopups();
	initGallery();
});

// lightbox gallery init
function initGallery() {
	// settings
	var _waitAnimation = true;
	var _autoSlide = false;
	var _easing = 'linear';
	var _activeClass = 'active';
	var _switchTime = 5000;
	var _speed = 600;

	$('div.light-box div.gallery').each(function(){
		// gallery options
		var _holder = $(this);
		var _pagerLinks = _holder.find('div.nav li');
		var _btnPrev = _holder.find('a.next');
		var _btnNext = _holder.find('a.prev');
		var _slidesHolder = _holder.find('div.mask-holder');
		var _slider = _slidesHolder.find('>ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _direction;
		var _timer;

		// slider height
		_holder.css({position:'relative'});
		_slider.css({height:_slides.eq(0).outerHeight(true)});
		_slides.show().css({position:'absolute',top:0,left:_slideWidth});
		_slides.eq(_currentIndex).css({left:0});

		// gallery control
		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});
		_pagerLinks.each(function(_ind){
			$(this).click(function(){
				if(_animating) return;
				if(_ind != _currentIndex) {
					_oldIndex = _currentIndex;
					_currentIndex = _ind;
					_direction = (_oldIndex < _currentIndex);
					switchSlide();
				}
				return false;
			});
		});
		function prevSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slidesCount-1;
			_direction = false;
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else _currentIndex = 0;
			_direction = true;
			switchSlide();
		}

		// externam methods
		_holder.bind('switchto',function(e,d){
			if(d && typeof d.num != 'undefined' && d.num != _currentIndex) {
				_oldIndex = _currentIndex;
				_currentIndex = d.num;
				_slides.eq(_currentIndex).css({left:0});
				_slides.eq(_oldIndex).css({left:-_slideWidth});
			}
		})

		// gallery animation
		function refreshStatus() {
			_pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			_slides.eq(_currentIndex).css({left:(_direction ? _slideWidth : -_slideWidth)}).animate({left:0},{duration:_speed, queue:false,easing:_easing,complete:function(){
				_animating = false;
			}});
			_slides.eq(_oldIndex).animate({left:(_direction ? -_slideWidth : _slideWidth)},{duration:_speed, queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();
		autoSlide();
	});

	// switch corresponind bio in lightbox
	var lbGallery = $('div.light-box div.gallery');
	$('div.team > ul > li').each(function(ind){
		$(this).children().click(function(){
			lbGallery.trigger('switchto',{num:ind});
			return false;
		})
	});
}
// init popups
function initPopups(){
	$('a.open-popup').each(function(){
		$(this).modalPopup();
	})
};

//lightbox plugin
jQuery.fn.modalPopup = function(options){return new modalPopup(jQuery(this).eq(0),options)}
function modalPopup(link, options) {this.init(link,options)}
modalPopup.prototype = {
	init:function(link,options){
		var el = this;
		//options
		el.options = jQuery.extend({
			fadeSpeed:400,
			closer:'a.close',
			scroll:true,
			wrapper:'#wrapper',
			IE:true,
			zIndex:999
		},options);	
		//popup & default css styles
		if (jQuery.browser.msie && el.options.IE) el.popup = jQuery(link.attr('href')).css({visibility:'hidden'})
		else el.popup = jQuery(link.attr('href')).css({opacity:0,visibility:'hidden'});
		if (el.options.zIndex) el.popup.css({zIndex : el.options.zIndex});
		el.closer = jQuery(el.popup.find(el.options.closer));
		el.popup.visible = false;
		modalPopup.prototype.activePopup = false;
		if (!modalPopup.prototype.firstRun) {
			modalPopup.prototype.firstRun = 'done';
			if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects = jQuery('select');
			//create fader
			if (!jQuery('#fader').length) jQuery('body').append('<div id="fader"></div>');
			modalPopup.prototype.fader = jQuery('#fader');
			modalPopup.prototype.fader.css({position:'absolute',top:0,left:0,background:'#000',opacity:0,display:'none'});	
			if (el.options.zIndex) modalPopup.prototype.fader.css({zIndex : el.options.zIndex-1});
			modalPopup.prototype.wrapper = jQuery(el.options.wrapper);
			//fader click event
			modalPopup.prototype.fader.click(function(){
				if (modalPopup.prototype.activePopup == false) el.hideFader()
				else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
				return false;
			});
			//esc event
			jQuery(document).keydown(function (e) {
				if (e.keyCode == 27) {
					if (modalPopup.prototype.activePopup == false) el.hideFader()
					else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
					return false;
				}
			});
		}
		
		el.wrapper = modalPopup.prototype.wrapper;
		
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			el.popupSelects = jQuery('select',el.popup);
			modalPopup.prototype.selects = modalPopup.prototype.selects.not(el.popupSelects);
		}
		
		//open event
		link.click(function(){
			el.show();
			return false;
		});
		//close event
		el.closer.click(function(){
			el.close();
			return false;
		});
		//resize event
		jQuery(window).resize(function(){
			if (el.popup.visible) el.positioning(false);
		});
		if (el.options.scroll) {
			jQuery(window).scroll(function(){
				el.positioning(true);
			});
		}
	},
	
	close:function(){
		var el = this;
		el.hidePopup(function(){el.hideFader()});
	},
	
	show:function(){
		var el = this;
		if (modalPopup.prototype.activePopup == el) {return false;}
		if (modalPopup.prototype.activePopup) {
			modalPopup.prototype.activePopup.hidePopup(function(){
				el.showPopup()
				el.positioning(true);
			});
		} else {
			el.showFader(function(){el.showPopup()});
			el.positioning(true);
		} 
	},
	
	showPopup:function(){
		var el = this;
		el.popup.visible = true;
		modalPopup.prototype.activePopup = el;
		if (jQuery.browser.msie && el.options.IE) el.popup.css({visibility:'visible'})
		else el.popup.stop().css({'visibility':'visible'}).animate({opacity:1},el.options.fadeSpeed)
	},
	hidePopup:function(callback){
		var el = this;
		if (jQuery.browser.msie && el.options.IE) {
			el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
			el.popup.visible = false;
			modalPopup.prototype.activePopup = false;
			if (jQuery.isFunction(callback)) callback();
		} else {
			el.popup.stop().animate({opacity:0},el.options.fadeSpeed,function(){
				el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
				el.popup.visible = false;
				modalPopup.prototype.activePopup = false;
				if (jQuery.isFunction(callback)) callback();
			});
		}
	},
	showFader:function(callback){
		var el = this;
		el.fader.stop().css({display:'block'}).animate({opacity:0.7},el.options.fadeSpeed,function(){
			if (jQuery.isFunction(callback)) callback();
		});
		if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'hidden'});
	},
	hideFader:function(){
		var el = this;
		el.fader.stop().animate({opacity:0},el.options.fadeSpeed,function(){
			el.fader.css({display:'none'});
			if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'visible'});
		});
	},
	positioning:function(openFlag){
		var el = this;
		//x offset
		var windowW = jQuery(window).width();
		var popupW = el.popup.outerWidth();
		var wrapperW = el.wrapper.outerWidth();
		
		if (windowW < wrapperW) {
			el.popup.css({left:wrapperW/2-popupW/2})
			el.fader.css({width:wrapperW});
		} else {
			 el.popup.css({left:windowW/2-popupW/2});
			 el.fader.css({width:windowW})
		}
		//y offset
		var docH;
		var windowH = jQuery(window).height();
		var wrapperH = el.wrapper.outerHeight();
		if (windowH < wrapperH) docH = wrapperH
		else docH = windowH;
		
		var popupH = el.popup.outerHeight();
		if (openFlag) {
			var popupH = el.popup.outerHeight();
			if (popupH < windowH) el.popup.css({top:windowH/2-popupH/2+jQuery(window).scrollTop()});
			else if (jQuery(window).scrollTop()+popupH > docH){
				el.popup.css({top:docH-popupH});
			} else {
				el.popup.css({top:jQuery(window).scrollTop()});
			}
		}
		el.fader.css({height:docH});
	}
}
