$(function(){
	initPopup();
});

function initPopup(){
	initGallery({
		holder: '.lightbox',
		list:'ul.slideset',
		autoRotation:false,
		effect:'fade',
		prev:'a.link-prev',
		next:'a.link-next'
	});
	jQuery('body').myPopup();
}

function initGallery(option){
	var hold = jQuery(option.holder);
	var duration = option.autoRotation;
	var switcher = option.switcher || false;
	var event = option.event || 'click';
	hold.each(function(){
		var _this = jQuery(this);
		var list = _this.find(option.list),
			count = list.children().length,
			w = list.parent().width(),
			_t,
			a = 0,
			r = a;
			
		if(option.prev && option.next){
			var prev = _this.find(option.prev).attr('rel', 'prev').click(animateSlide);
			var next = _this.find(option.next).attr('rel', 'next').click(animateSlide);
		}
		if(option.switcher){
			switcher = _this.find(option.switcher);
			switcher.eq(r).removeClass('active');
			switcher.eq(a).addClass('active');
			switcher.bind(event, function(){
				var ind = switcher.index($(this));
				animateSlide(ind);
				return false;
			});
		}
		if(option.autoRotation) runTimer();
		if(option.effect == 'fade') {
			list.children().css('opacity', 0);
			list.children().eq(a).css('opacity', 1).addClass('active');
		}
		if(option.stopOnHover && _t){
			list.mouseenter(function(){
				clearTimeout(_t);
			}).mouseleave(runTimer);
		}
		function runTimer(){
			_t = setTimeout(function(){
				animateSlide('next');
			}, duration);
		}
		
		function animateSlide(e){
			r = a;
			if(typeof e == 'string' && e == 'next') a++;
			else if(typeof e == 'number') a=e;
			else{
				if(e.target.rel == 'next') a++;
				else if(e.target.rel == 'prev') a--;
			}
			if(_t) clearTimeout(_t);
			if(a == count) a=0;
			else if(a == -1) a=count-1;
			list.children().eq(r).removeClass('active');
			list.children().eq(a).addClass('active');
			if(option.switcher){
				switcher.eq(r).removeClass('active');
				switcher.eq(a).addClass('active');
			}
			if(option.effect == 'fade'){
				list.children().eq(r).animate({opacity:0}, {queue:false, duration:700});
				list.children().eq(a).animate({opacity:1}, {queue:false, duration:700, complete:function(){
					if(option.autoRotation) runTimer();
				}});
			}
			else{
				list.animate({marginLeft:-w*a}, {queue:false, duration:700, complete:function(){
					if(option.autoRotation) runTimer();
				}});
			}
			return false;
		}
	});
};

jQuery.fn.myPopup = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: '.close, a.btn-close',
		divFader: 'fader',
		wrapper: '#wrapper'
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _IE = jQuery.browser.msie;
		var links = _hold.find(_options.linkOpenName);
		var _fader = jQuery('<div class="'+_options.divFader+'"></div>');
		var _select = jQuery(_options.wrapper).find('select');
		var popup;
		jQuery('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});
		
		function init(_obj){
			popup = jQuery(_obj);
			var btnClose = popup.find(_options.linkCloseName);
			var submitBtn = popup.find('.link-submit');
			
			if (_IE) _select.css({visibility: 'hidden'});
			var w = jQuery('body').width();
			var _w = jQuery(_options.wrapper).width();
			if (_w > w) w =_w;
			var h = jQuery(window).height();
			var _offset = jQuery(window).scrollTop();
			
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			var te = jQuery(_options.wrapper).height();
			if (jQuery(window).height() > te) te = jQuery(window).height();
			
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: te
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			jQuery(window).resize(function(){
				w = jQuery('body').width();
				_w = jQuery(_options.wrapper).width();
				if (_w > w) w =_w;
				popup.animate({
					left: w/2 - popup.outerWidth(true)/2
				}, {queue:false, duration: 300});
				_fader.css({
					width: w
				});
			});
			function closedPopup(opt1){
				popup.fadeOut(300, function(){
					popup.css({left: '-9999px'}).show();
					if (_IE) _select.css({visibility: 'visible'});
					submitBtn.unbind('click');
					jQuery(window).unbind('resize');
					if (opt1) _fader.hide();
					else {
						if (submitBtn.attr('href')) init(submitBtn.attr('href'));
						else init(submitBtn.attr('title'));
					}
				});
			}
			btnClose.click(function(){
				closedPopup(true);
				return false;
			});
			submitBtn.click(function(){
				closedPopup();
				return false;
			})
			_fader.click(function(){
				closedPopup(true);
				return false;
			});
		}
		links.click(function(){
			if (jQuery(this).attr('rel')) init(jQuery(this).attr('rel'));
			else init(jQuery(this).attr('title'));
			return false;
		});
	});
}
