jQuery(function($){$(document).ready(function(){
	initSlideGall('#gallery');
});


/*--- init Slide Gallery function ---*/
function initSlideGall(divID){
	var _hold = $(divID);
	if(_hold.length > 0){
		var _a = -1;
 		var _c = -1;
		var _f = false;
		var _t;
		var t_p;
		var s_posX;

		var list_h = _hold.find('.present > ul', divID);
		var _list = list_h.children();
		var _btn = $(divID+' .slider-list li');
		_btn.children().each(function(){
			$(this).css({'-khtml-user-select': 'none', 'user-select': 'none', '-moz-user-select': 'none'});
			this.onselectstart="return false";
			$(this).hover(function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
		});
		var _slider = $('<div class="glass"></div>').hide();
		$(divID+' .slider').append(_slider);
		_btn.each(function(_i){
			if($(this).hasClass('active')) _a = _i;
			$(this).children().click(function(){
				_c = -1;
				changeEl(_i);
				_c = 1;
				return false;
			}).mouseenter(function(){
				if(!_f){
					if(_btn.eq(_i).outerWidth() < 120){
						_w = 120;
						_l = _btn.eq(_i).position().left - (_w - _btn.eq(_i).outerWidth())/2;
					}
					else{
						_w = _btn.eq(_i).outerWidth();
						_l = _btn.eq(_i).position().left;
					}
					_slider.animate({width: _w - 103, left: _l}, {queue:false, duration: 300,  easing: 'easeOutBounce'});
				}
			}).mouseleave(function(){
				// Function added by shoeb to keep the scrolling even when the numbers are clicked. Change it to 1 if you dont want the scrolling.
				_c = -1;
			});
		});
		if(_a == -1) _a = 0;
		$('.intro li').hide();
		$('.intro li').eq(_a).show();
		if(_btn.eq(_a).outerWidth() < 120){
			_w = 120;
			_l = _btn.eq(_a).position().left - (_w - _btn.eq(_a).outerWidth())/2;
		}
		else{
			_w = _btn.eq(_a).outerWidth();
			_l = _btn.eq(_a).position().left;
		}
		_slider.css({
			left: _l,
			width: _w - 103
		}).show();
		_list.eq(_a).show();
		/*--- events ---*/
		_hold.hover(function(){
			if(_t) clearTimeout(_t);
		}, function(){
			if(_t) clearTimeout(_t);
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, 8000);
		});
		
		_hold.find('.slider').mousedown(function(e){
			if((e.pageX > _slider.offset().left) && (e.pageX < _slider.offset().left + _slider.outerWidth())){
				t_p = e.pageX - _slider.offset().left;
				_f = true;
			}
		}).mouseup(function(){
			if(_f){
				var _k = 1000;
				var t_i = 0;
				_btn.each(function(_i){
					var _w1 = Math.abs(($(this).position().left + $(this).outerWidth()/2) - (_slider.position().left + _slider.outerWidth()/2));
					if(_w1 < _k){
						t_i = _i;
						_k = _w1;
					}
				});
				if(t_i == _a){
					if(_btn.eq(_a).outerWidth() < 120){
						_w = 120;
						_l = _btn.eq(_a).position().left - (_w - _btn.eq(_a).outerWidth())/2;
					}
					else{
						_w = _btn.eq(_a).outerWidth();
						_l = _btn.eq(_a).position().left;
					}
					_slider.animate({width: _w - 103, left: _l}, {queue:false, duration: 300, easing: 'easeOutBounce'});
				}
				else{
					_c = -1;
					changeEl(t_i);
					_c = 1;
				}
			}
			_f = false;
		}).mousemove(function(e){
			var t_x = e.pageX - $(this).offset().left - t_p;
			if(_f){
				if((e.pageX - t_p >= $(this).offset().left) && (t_x <= $(this).width() - _slider.outerWidth())){
					s_posX = t_x;
					_slider.css('left', t_x);
				}
			}
		})
		_hold.find('.slider-list').mouseleave(function(){
			if(_btn.eq(_a).outerWidth() < 120){
				_w = 120;
				_l = _btn.eq(_a).position().left - (_w - _btn.eq(_a).outerWidth())/2;
			}
			else{
				_w = _btn.eq(_a).outerWidth();
				_l = _btn.eq(_a).position().left;
			}
			_slider.animate({width: _w - 103, left: _l}, {queue:false, duration: 500, easing: 'easeOutBounce'});
		});
		_t = setTimeout(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
		}, 8000);

		/*--- control function ---*/
		function changeEl(_ind){
			if(_c > 0) return false;
			if(_t) clearTimeout(_t);
			
			if(_a != _ind){
				if($('.intro-m').hasClass('opened')) {
					$('.button-up-down a').eq(0).click();
					setTimeout(function(){$('.intro li').hide();$('.intro li').eq(_ind).show();}, 720);
				} else {
					$('.intro li').hide();
					$('.intro li').eq(_ind).show();
				}
				$('.intro .intro-m').css({marginTop: -$('.intro .intro-m').outerHeight()});
				_list.width(_list.parent().width());
				_list.eq(_ind).stop().css({left: list_h.width(), display: 'inline'}).animate({left: 0}, 750);
				if(_ind==0) var slowc = 100;
				else var slowc = 1000;
				_list.eq(_a).stop().animate({left: - list_h.width()}, 650, function(){
					$(this).css({
						display: 'none',
						left: 0
					});
					_list.width('100%');
				});
				if(_btn.eq(_ind).outerWidth() < 120){
					_w = 120;
					_l = _btn.eq(_ind).position().left - (_w - _btn.eq(_ind).outerWidth())/2;
				}
				else{
					_w = _btn.eq(_ind).outerWidth();
					_l = _btn.eq(_ind).position().left;
				}
				_slider.animate({width: _w - 103, left: _l}, {queue:false, duration: 1000, easing: 'easeOutBounce'});
				_btn.eq(_ind).addClass('active');
				_btn.eq(_a).removeClass('active');
				_a = _ind;

			}
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, 8000);
		}

	}
}
})
