/*
	Karimov Alexey (gleam.alex@gmail.com)
	2010/10/08
	Вертикальная листалка картинок для баннеров и галер
*/

// Корневой див и класс указанный в IMG... остальные картинки можно юзать для кнопочек и хуеточек
var CVerticalGallery = function ( root, className ) {
	this.root = root;
	this.className = className;
	this.height = root ? root.style.height : 0;
	this.delta = 0;
	this.deltaLast = 0;
	this.step = 0;
	this.timer = 0;
	this.timerTick = 0;
	this.timerTickCnt = 0;
	this.lineHeight = 0;
	this.pause = 0;

	this.oy = new Array ( );
	this.im = new Array ( );
	
	this.id = -1;

	this.initialize = function ( ) {
		if ( !this.root ) return;

		var e = this.root;
		var	l = e.getElementsByTagName ( 'img' );
		var	j = 0;
		var y = 0;
		var p = this;
		
		for ( j = 0; j < l.length; j ++ ) {
			var img = l [ j ];
			
			if ( img.className != this.className ) continue;

			img.style.position = 'absolute';
			img.style.cursor = 'pointer';
			img.style.zIndex = 1;
			img.style.display = 'block';

			y -= img.height;
			this.lineHeight += img.height;
			
			this.oy [ j ] = y;
			this.im [ j ] = img;

			img.onmouseover = function ( ) { p.pause = 1; };
			img.onmouseout = function ( ) { p.pause = 0; };
		}

		//this.render ( );
		this.goToId ( this.im.length >> 1 );
		this.timerMove ( );
		
	}
	
	this.goToId = function ( t ) {
		this.timerTickCnt = 0;
		
		if ( Math.abs ( this.delta ) > 10 ) return;

		if ( t < 0 ) t = this.im.length - 1;
		this.id = t % this.im.length;

		this.move ( );
	},
	
	this.goNext = function ( ) {
		this.prepare ( 1 );
		this.goToId ( this.id + 1 );
	}
	
	this.goPrev = function ( ) {
		this.prepare ( -1 );
		this.goToId ( this.id - 1 );
	}
	
	this.render = function ( ) {
		var j = 0;
		
		for ( j = 0; j < this.im.length; j ++ ) {
			this.im [ j ].style.top = this.oy [ j ] + 'px';
		}
	},
	
	this.timerMove = function ( ) {
		if ( this.timerTick ) clearTimeout ( this.timerTick );
		
		var p = this;

		if ( !this.pause ) {
			if ( this.timerTickCnt > 10 ) {
				if ( this.deltaLast > 0 ) {
					this.goNext ( );
				} else if ( this.deltaLast < 0 ) {
					this.goPrev ( );
				}
				
				this.timerTickCnt = 0;
			}
		}
		
		this.timerTickCnt ++;
		
		this.timerTick = setTimeout ( function ( ) { p.timerMove ( ); }, 500 );
	},
	
	this.prepare = function ( d ) {
			// Если смещение вверх
			if ( d < 0 ) {
				// Пока есть самая верхняя картинка, с условием, что она за пределами нашей области - перемещаем ее вниз
				for ( j = 0; j >= 0; ) {
					var min_y = 0;
					var max_y = 0;
	
					for ( k = 0, j = -1; k < this.im.length; k ++ ) if ( this.oy [ k ] < min_y && this.oy [ k ] + this.im [ k ].height <= 0 ) { j = k; min_y = this.oy [ k ]; }
					for ( k = this.im.length - 1; k >= 0; k -- ) if ( this.oy [ k ] + this.im [ k ].height > max_y ) max_y = this.oy [ k ] + this.im [ k ].height;

					this.oy [ j ] = max_y;
				}
			}

			// Если смещение вниз
			if ( d > 0 ) {
				// Пока есть самая нижняя картинка, с условием, что она за пределами нашей области - перемещаем ее вверх
				for ( j = 0; j >= 0; ) {
					var min_y = 0;
					var max_y = 0;
	
					for ( k = 0, j = -1; k < this.im.length; k ++ ) if ( this.oy [ k ] > max_y && this.oy [ k ] >= 338 ) { j = k; max_y = this.oy [ k ] + this.im [ k ].height; }
					for ( k = this.im.length - 1; k >= 0; k -- ) if ( this.oy [ k ] - this.im [ k ].height < min_y ) min_y = this.oy [ k ] - this.im [ k ].height;

					this.oy [ j ] = min_y;
				}
			}

	},
	
	this.move = function ( ) {
		var next = 1;
		
		if ( this.timer ) clearTimeout ( this.timer );
		
		if ( this.id < 0 ) return;

		var y = this.oy [ this.id ];
		var i = this.im [ this.id ];
		var h = i.height;
		
		var ny = ( 338 >> 1 ) - ( h >> 1 );

		this.delta = ( ( ny - y ) / 10 );
		
		if ( this.delta < 0 && this.delta > -1 ) this.delta = -1;
		if ( this.delta > 0 && this.delta < 1 ) this.delta = 1;
		if ( this.delta < 0 && this.delta < -50 ) this.delta = -50;
		if ( this.delta > 0 && this.delta > 50 ) this.delta = 50;
		
		if ( ( y + this.delta > ny && this.delta > 0 ) || ( y + this.delta < ny && this.delta < 0 ) ) {
			this.delta = ny - y;
			next = 0;
		}

		if ( !this.delta ) {
			this.delta = ny - y;
			next = 0;
		}

		if ( this.delta ) this.deltaLast = this.delta;

		var p = this;
		var j = 0;
		var k = 0;

		// Если есть смещение - пробегаемся по массиву и выставляем новые значения позиции
		if ( this.delta ) {
			for ( j = 0; j < this.im.length; j ++ ) {
				this.oy [ j ] = Math.round ( this.delta + this.oy [ j ] );
			}
			
			this.prepare ( this.delta );

			// Выставляем картинки в ХТМЛ по позициям
			this.render ( );
		}
		
		
		if ( next ) {
			this.timer = setTimeout ( function ( ) { p.move ( ); }, 30 );
		}
	}

	this.initialize ( );
}

