$(function(){
	initGallery({
		holder:'div.tabs',
		list:'div.g1',
		switcher:'ul.tabset > li',
		autoRotation:5000,
		effect:'fade'
	});
	jQuery('body').myPopup();
});

jQuery.fn.myPopup = function(_options){
	// defaults options
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: 'a.close, a.btn-close',
		divFader: 'fader',
		wrapper: 'body'
	},_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, link){
			var ext = /\.html$/;
			var name = /[^inc\/]\w*[^\.\/]/;
			if(ext.test(_obj)){
				var id = '#' + _obj.match(name);
				if(jQuery(id).length == 0){
					jQuery.ajax({
						url:_obj,
						dataType:'html',
						success:function(msg){
							var box = jQuery(msg);
							jQuery('body').append(box);
							var lightBox = '#'+box.attr('id');
							initLightBox(lightBox, link);
						},
						error:function(){
							alert('Ajax Error !');
						}
					});
				}
				else initLightBox(id, link)
			}
			else initLightBox(_obj, link)
			function initLightBox(lightBox, link){
				popup = jQuery(lightBox);
				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).outerHeight(true);
				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'),submitBtn );
							else init(submitBtn.attr('title'), submitBtn);
						}
					});
				}
				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('href')) init(jQuery(this).attr('href'), jQuery(this));
			else init(jQuery(this).attr('title'), jQuery(this));
			return false;
		});
	});
}


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;
		var wait = false;
		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(switcher);
			switcher.eq(r).removeClass('active');
			switcher.eq(a).addClass('active');
			switcher.bind(event, function(){
				var ind = switcher.index($(this));
				wait = true;
				animateSlide(ind);
				return false;
			});
		}
		if(option.autoRotation) runTimer();
		if(option.effect == 'fade') {
			list.children().css('opacity', 0);
			list.children().removeClass('active').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(!wait) if(option.autoRotation) runTimer();
				}});
			}
			else{
				list.animate({marginLeft:-w*a}, {queue:false, duration:700, complete:function(){
					if(option.autoRotation) runTimer();
				}});
			}
			return false;
		}
	});
};
