/*--- gallery like 2 ---*/
function initGall(){
	var slide_speed = 500; //in ms
	var fade_speed = 500; //in ms
	$('div.gallery').each(function(){
		var _hold = $(this);
		var img_hold = _hold.find('div.gal');
		var btn_prev = _hold.find('div.gal-control a.prev-arrow');
		var btn_next = _hold.find('div.gal-control a.next-arrow');
		var list_hold = _hold.find('div.holder div > ul');
		var _btn = list_hold.find('a');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		
		var hold_w = list_hold.parent().width();
		var list_w = list_hold.children().length*list_hold.children().outerWidth(true);
		var _step = hold_w;
		var _m = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.get(_a)._img = $('<img src="'+_btn.eq(_a).attr('href')+'" alt="'+_btn.eq(_a).attr('title')+'"/>');
		_btn.get(_a)._img.addClass('active');
		img_hold.append(_btn.get(_a)._img);
		
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		btn_prev.click(function(){
			moveList(false);
			return false;
		});
		btn_next.click(function(){
			moveList(true);
			return false;
		});
		function moveList(_f){
			if(_f){
				if(list_w - _m > hold_w) _m += _step;
				else _m = 0;
			}
			else{
				if(_m > 0) _m -= _step;
				else _m = Math.ceil((list_w-hold_w)/_step)*_step;
			}
			list_hold.animate({left:-_m},{queue:false, duration: slide_speed});
		}
		function changeEl(_ind){
			if(_ind != _a){
				if(!_btn.get(_ind)._img){
					_btn.get(_ind)._img = $('<img src="'+_btn.eq(_ind).attr('href')+'" alt="'+_btn.eq(_ind).attr('title')+'"/>');
					_btn.get(_ind)._img.css('opacity', 0);
					img_hold.append(_btn.get(_ind)._img);
				}
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_btn.get(_a)._img.removeClass('active').animate({opacity: 0},{queue: false, duration: fade_speed});
				_btn.get(_ind)._img.addClass('active').animate({opacity: 1},{queue: false, duration: fade_speed});
				_a = _ind;
			}
		}
	});
}

$(document).ready(function(){
	initGall();
});
