/*
 * Simple carusel
 * @requires jQuery v1.3 or later
 *
 * Copyright 2009 - 2010
 *
 * Webway IT Labs | www.webway.ru
 *
 */
 /*
 
 parametrs
 
 out - container,
 gallered - moveble conteiner,
 gallery - single rotational element,
 nextClass - next botton inside current block,
 prvClass - prev botton inside current block,
 moveTo // left || right - default gallery position (defoult - right);
 	if checked left, gallered gets class 'left_galled' to has permition to fix wrong position without javascript
 modeRounded // false || true - cercular gallery ( for only one direction moveTo),
 speed - rotation speed
 */
 $.fn.ImageGallery = function(options) {
	  var settings = {
				out: 'gall-wr',
				gallered: '.carus-sing',
				gallery: 'li',
				prvClass: '.i_prev',
				nextClass: '.i_next',
				moveTo:'right',
				modeRounded:false,
				scrollby:1,
				speed:300
		};
	   settings = $.extend(settings, options);
 
 	var main = $(this);
	if( main.size() == 0 ) {

		return;
	}

	var g = $(settings.gallered, this),
	list = $(settings.gallery, g),

	ctrl_prev = $(settings.prvClass, this),
	ctrl_next = $(settings.nextClass, this),
	t = null;

	var bAnimationLock = false;

	var nRealW = 0;
	list.each(function(i) {
		nRealW += $(this).get(0).offsetWidth;
	});
	g.width(nRealW);

	var oList = settings.moveTo == 'left' ? list.get(list.size() - 1) : list.get(0);

	if(settings.modeRounded){
		nRealW += oList.offsetWidth;
		g.width(nRealW);
		
		gUpdate();
	}	

	if(settings.moveTo == 'left'){
		
		g.addClass('left_galled').css({"left" : - nRealW + oList.offsetWidth + "px"})
		if( list.size() >= settings.scrollby ) {
			ctrl_prev.show();
		}
	}else{
		if( list.size() >= settings.scrollby ) {
			ctrl_next.show();
		}
	}
	
	var oGal = g.get(0)

	function move( prev ) {

		prev ? movePrev() : moveNext();
	
	};

	function movePrev() {
		if ( bAnimationLock ) {
			return;
		}

		bAnimationLock = true;
		
		g.animate( {"left" : "+=" + oList.offsetWidth*settings.scrollby + "px"}, settings.speed, function(){
			
			if(settings.modeRounded){
				if(settings.moveTo == 'left' &&  this.offsetLeft == 0){

					g.css({"left" : - nRealW + oList.offsetWidth + "px"})
				}
			}else{
				if( -this.offsetLeft + oList.offsetWidth == oList.offsetWidth  ) {
					ctrl_prev.hide();
				}
				if( this.offsetLeft + nRealW > oList.offsetWidth ) {
	
					ctrl_next.show();
				}			
			}
			
			bAnimationLock = false;
		});
	};


	function moveNext() {
		if ( bAnimationLock ) {
			return;
		}

		bAnimationLock = true;

		g.animate( {"left" : "-=" + oList.offsetWidth*settings.scrollby + "px"}, 300, function(){

			
			if(settings.modeRounded){
		
				if(settings.moveTo == 'right' &&  this.offsetLeft + nRealW <= oList.offsetWidth){

					g.css({"left" : 0})
				}
			}else{

				if( -this.offsetLeft + oList.offsetWidth != oList.offsetWidth  ) {
					ctrl_prev.show();
				}
	
				if( this.offsetLeft + nRealW <= oList.offsetWidth ) {
					ctrl_next.hide();
				}

			}
			bAnimationLock = false;
		});
	};

	function gUpdate( ) {
		if(settings.moveTo == 'left'){
			g.prepend($(oList).clone().removeClass('last'))
		}else{
			g.append($(oList).clone())
		}	
	};

	ctrl_prev.click(function(){
		move( 1 );
	});

	ctrl_next.click(function(){
		move();
	});

}
