$(document).ready(function(){
	SlideGall();
});
/*--- main slide gallery function ---*/
function SlideGall(){
	var gall_cont = $('#carusel');
	if(gall_cont.length){
		var btn_prev = gall_cont.children('.link-prev');
		var btn_next = gall_cont.children('.link-next');
		var list_h = gall_cont.find('.gallery-cont > .gallery');
		var _step = list_h.parent().width();
		var list_w = 0;
		var _list = list_h.children();
		_list.each(function(){
			list_w += $(this).outerWidth(true);
		});
		btn_prev.addClass('disabled');
		if(list_w > _step) btn_next.removeClass('disabled');
		else btn_next.addClass('disabled');
		
		btn_prev.click(function(){
			if(!$(this).hasClass('disabled')) moveList(false);
			return false;
		});
		btn_next.click(function(){
			if(!$(this).hasClass('disabled')) moveList(true);
			return false;
		});
		_list.each(function(){
			var _btn = $(this).find('.btn-more');
			var _text = $(this).find('.more-info > .autor-info');
			var _gall = $(this).find('.more-info > .more-pictures');
			_btn.click(function(){
				openPopup(_text.clone(), _gall.clone());
				return false;
			});
		});
		initPopup();
	}
	var _m = 0;
	function moveList(_f){
		if(_f){
			btn_prev.removeClass('disabled');
			if(_m + _step >= list_w - _step) btn_next.addClass('disabled');
			_m += _step;
		}
		else{
			btn_next.removeClass('disabled');
			if(_m - _step <= 0) btn_prev.addClass('disabled');
			_m -= _step;
		}
		list_h.animate({left: -_m}, {queue:false, duration:800});
	}
}
var openPopup;
function initPopup(){
	var page_gall = $('#carusel');
	var _loader = $('<div class="loader">loading...</div>');
	var _popup = $('<div id="popup"></div>');
	_popup.append(
		'<div class="popup-cont"> \
			<div class="right-col"></div> \
			<div class="left-col"> \
				<div class="big-img"></div> \
				<div class="popup-gallery"> \
					<a href="#" class="popup-prev">Previous</a> \
					<div class="small-gallery"> \
						<a href="#" class="small-prev-link">previous</a> \
						<div></div> \
						<a href="#" class="small-next-link">next</a> \
					</div> \
					<a href="#" class="popup-next">Next</a> \
				</div> \
			</div> \
			<a href="#" class="popup-close">close</a> \
		</div>'
	);
	$('body').append(_popup);
	var btn_close = _popup.find('.popup-close');
	var text_hold = _popup.find('.right-col');
	var big_img = _popup.find('.big-img');
	var btn_prev = _popup.find('.popup-prev');
	var btn_next = _popup.find('.popup-next');
	var gall_hold = _popup.find('.small-gallery > div');
	var gall_btn_prev = _popup.find('.small-gallery > .small-prev-link');
	var gall_btn_next = _popup.find('.small-gallery > .small-next-link');
	var _a, _step, list_h, _list, list_w, _m;
	var _f1 = true;
	openPopup = function(_text, gall_list){
		text_hold.html(_text.html());
		gall_hold.html(gall_list.html());
		_a = 0;
		list_w = 0;
		_f1 = true;
		_m = 0;
		list_h = gall_hold.children('ul');
		_list = list_h.children();
		page_gall.fadeTo(400, 0, function(){
			$(this).css('visibility','hidden');
		});
		_popup.fadeIn(400);
		_step = gall_hold.width();
		_list.each(function(){
			list_w += $(this).outerWidth();
		});
		if(list_w > _step){
			gall_btn_prev.show();
			gall_btn_next.show();
			list_h.append(_list.clone()).append(_list.clone()).css('left', -list_w);
			_m = list_w;
		}
		else{
			gall_btn_prev.hide();
			gall_btn_next.hide();
			list_h.css('left', (_step - list_w)/2);
		}
		_list = list_h.find('a');
		_list.each(function(_i){
			this._img = '<img src="'+$(this).attr('href')+'" alt="'+$(this).attr('rel')+'"/>';
			this._f = false;
			if(_i == _a){
				this._f = $(this._img);
				big_img.html(this._f);
				this._f.addClass('active');
			}
			$(this).click(function(){
				changeEl(_i);
				return false;
			});
		});
	}
	btn_next.click(function(){
		changeEl(_a + 1);
		return false;
	});
	btn_prev.click(function(){
		changeEl(_a - 1);
		return false;
	});
	btn_close.click(function(){
		page_gall.css('visibility','visible').fadeTo(400, 1);
		_popup.fadeOut(400, function(){
			text_hold.empty();
			big_img.empty();
			gall_hold.empty();
		});
		return false;
	});
	gall_btn_prev.click(function(){
		moveList(false);
		return false;
	});
	gall_btn_next.click(function(){
		moveList(true);
		return false;
	});
	function changeEl(_ind){
		if(_ind == _list.length) _ind = 0;
		if(_ind == -1) _ind = _list.length - 1;
		if(_ind != _a){
			_list.get(_a)._f.removeClass('active').animate({opacity: 0}, {queue:false, duration:600});
			if(_list.get(_ind)._f){
				_list.get(_ind)._f.addClass('active').animate({opacity: 1}, {queue:false, duration:600});
			}
			else{
				big_img.append(_loader);
				_loader.stop().css({opacity: 0, display:'block'}).animate({opacity: 1}, 200);
				_list.get(_ind)._f = $(_list.get(_ind)._img).css('opacity', 0).addClass('active').load(function(){
					_loader.stop().animate({opacity: 0}, 200, function(){
						$(this).hide();
					});
					_list.get(_ind)._f.animate({opacity: 1}, {queue:false, duration:600});
				});
				big_img.append(_list.get(_ind)._f);
			}
			_a = _ind;
		}
	}
	function moveList(_f){
		if(_f1){
			_f1 = false;
			if(_f){
				if(_m + _step > list_w*3-_step){
					_m -= list_w;
					list_h.css('left', -_m);
				}
				_m += _step;
			}
			else{
				if(_m -_step < 0){
					_m += list_w;
					list_h.css('left', -_m);
				}
				_m -= _step;
			}
			list_h.animate({left: -_m}, 700, function(){
				_f1 = true;
			});
		}
	}
}