$(function(){
	/*initGallery({
		holder:'#header',
		list:'ul.gallery',
		autoRotation:5000,
		prev:false,
		next:false
	}); */
	initGallery({
		holder:'.g2-hold',
		list:'.g2',
		autoRotation:false,
		autoHeight:true,
		prev:'a.prev',
		next:'a.next'
	});
	if(typeof $('.slide-line').slider != 'undefined') initSlider();
});

function initSlider(){
	var hold = $('.slider-box');
	var list = hold.find('.slider-holder > ul > li');
	var btn = hold.find('.btn-slide');
	var defaultValue = 7;
	var prevValue = 0;
	list.filter(':even').css({
		'position':'relative',
		'top':35
	});
	var span = $('<span>');
	var gallList = hold.parent().find('ul.g3 > li').css('opacity', 0);
	var slider = hold.find('.slide-line').slider({
		value:defaultValue,
		min: 0,
		max: list.length-1,
		step: 1,
		create: function(event, ui) {
			btn.css({left:hold.find('a.ui-slider-handle').position().left-((btn.width()-hold.find('a.ui-slider-handle').width())/2) + 14})
			list.eq(defaultValue).css({color:'#B6B6B6'});
			initGallSlider(defaultValue, prevValue, false);
		},
		slide: function(event, ui) {
			btn.css({left:(ui.value*35+ui.value/1.5) - (btn.width()-$(ui.handle).width())/2 + 14});
			list.css({color:'#363636'});
			list.eq(ui.value).css({color:'#B6B6B6'});
			prevValue = gallList.index(gallList.filter('.active'));
			initGallSlider(ui.value, prevValue, true);
		},
		change: function(event, ui) {
			btn.css({left:hold.find('a.ui-slider-handle').position().left-((btn.width()-hold.find('a.ui-slider-handle').width())/2) + 14});
			list.css({color:'#363636'});
			list.eq(ui.value).css({color:'#B6B6B6'});
			prevValue = gallList.index(gallList.filter('.active'));
			initGallSlider(ui.value, prevValue, true);
		}
	});
	slider.find('a.ui-slider-handle').append(span);
	btn.mouseenter(function(){
		span.show();
	});
	span.mouseleave(function(){
		span.hide();
	});
	btn.click(function(){return false});

	list.click(function(){
		var index = list.index($(this));
		slider.slider("option", "value", index)
		return false;
	});
	
	btn.find('.left').click(function(){
		var val = slider.slider("option", "value")
		val--;
		if(val <= 0) val = 0;
		slider.slider("option", "value", val)
		return false;
	});
	btn.find('.right').click(function(){
		var val = slider.slider("option", "value")
		val++;
		if(val >= slider.slider("option", "max")) val = slider.slider("option", "max");
		slider.slider("option", "value", val)
		return false;
	});
	
	function initGallSlider(active, rev, f){
		gallList.removeClass('active');
		gallList.eq(active).addClass('active');
		if(f){
			gallList.eq(rev).stop().animate({opacity:0}, {queue:false, duration:700});
			gallList.eq(active).stop().animate({opacity:1}, {queue:false, duration:700});
		}
		else{
			gallList.eq(rev).css({opacity:0});
			gallList.eq(active).css({opacity:1});
		}
	}
}

function initGallery(option){
	var hold = jQuery(option.holder);
	var duration = option.autoRotation;
	var switcher = option.switcher || false;
	var autoHeight = option.autoHeight || 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.autoHeight){
			list.css({height:list.children().eq(a).outerHeight(true)});
		}
		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));
				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();
				}});
				if(option.autoHeight){
					list.animate({height:list.children().eq(a).outerHeight(true)}, {queue:false, duration:700});
				}
			}
			return false;
		}
	});
};
