var motion = new Object();
motion.load = function( mDiv, hDiv) {
	this.exName = mDiv;
	this.motionDiv = document.getElementById( mDiv);
	this.holdDiv = document.getElementById( hDiv);
	this.img_array = this.motionDiv.getElementsByTagName( "img");
	this.step_delta = this.img_array[0].width + 20;
	this.begin_position = 0;
	this.end_position = 0;
	this.speed = 0;
	this.acceleration = 0;
	this.condition = 0;
	this.mDivWidth = this.img_array.length * this.step_delta;
	this.currentImg = 0;
}

motion.load.prototype = {
	changeMulti: function( direct) {
		clearTimeout( this.toVar);
       	this.end_position += direct * this.step_delta;			// конечная позиция
       	this.insertImg( direct);
		this.move( direct); 
	},
	move: function( direct) {
       	var thisObj	= this;	
		this.speed = direct * Math.round( Math.abs( this.end_position - this.motionDiv.offsetLeft) / 7);
        if ( Math.abs( this.speed) < 5) {
			if ( this.exName == "motionDiv") {
				current_id = "nadpis_" + this.img_array[this.currentImg].id;
				document.getElementById( current_id).style.visibility = "visible";
			}
		}
		if ( Math.abs( this.speed) < 2) {
			this.speed = direct * 1;
		}
		if ( direct > 0) {
			this.condition = (this.motionDiv.offsetLeft < this.end_position);
		} else {
			this.condition = (this.motionDiv.offsetLeft > this.end_position);
		}
		if ( this.condition) {
			this.stepMove();
			this.toVar = window.setTimeout( function(){ thisObj.move( direct)}, 30);
		} else {
			clearTimeout( this.toVar);
         	this.toVar = null;
			this.speed = 0;
		}
	},
	stepMove: function() {
	    this.motionDiv.style.left = ( this.motionDiv.offsetLeft + this.speed) + "px";	
	},
	insertImg: function( direct) {
		if ( this.end_position <= - ( this.mDivWidth)) {
			this.img_array = this.motionDiv.getElementsByTagName( "img");
			this.motionDiv.style.left = (this.motionDiv.offsetLeft + this.step_delta) + "px";
			this.end_position += this.step_delta;
			this.begin_position += this.step_delta;
			this.motionDiv.appendChild( document.getElementById( this.img_array[0].id));
			this.currentImg = this.img_array.length - 1;
		} else if ( this.end_position > 0) {
			this.img_array = this.motionDiv.getElementsByTagName( "img");
			this.motionDiv.style.left = ( this.motionDiv.offsetLeft - this.step_delta) + "px";
			this.end_position -= this.step_delta;
			this.begin_position -= this.step_delta;
			this.last_img = this.img_array.length - 1;
			this.motionDiv.insertBefore( document.getElementById( this.img_array[this.last_img].id), this.motionDiv.firstChild);
			this.currentImg = 0;
		} else {
			if ( direct > 0) {
				this.currentImg --;
			} else {
				this.currentImg ++;
			}
		}
	}
}

