/***********************************************
* CMotion Image Gallery- Ž Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* This copyright notice must stay intact for legal use
* Modified for autowidth and optional starting positions in
* http://www.dynamicdrive.com/forums/showthread.php?t=11839 by jschuer1 8/5/06
***********************************************/
/* Modified and updated by Maxim Bielushkin*/

function getMotionWidth() {
	return 282+70;
}

var picMotionWidth = getMotionWidth();

function carousel() {
	iedom=document.all||document.getElementById;
	movestate=null;
	lefttime=null;
	righttime=null;
	speed = false;

	/*function, which run the class*/
	this.construct = function() {
	this.maxspeed = 50;
	var maxspeed = this.maxspeed;
	var startpos = this.startpos;
	var pref = this.pref;
	var crossmain = _(pref + 'motioncontainer');
	var cross_scroll = _(pref + 'motiongallery');

	if (iedom) {
		var menuwidth = crossmain.offsetWidth;
		div_l = _(pref + 'div_l');
		div_r = _(pref + 'div_r');

		var actualwidth = _(pref + 'trueContainer').offsetWidth;

		var first = _(pref + 'motiongallery');
		var last = first.cloneNode(true);
		last.id = pref + 'motiongallery_move';
		
		var insertPoint = _(pref + 'motioncontainer');
		insertPoint.appendChild(last);
		last.style.left = actualwidth + 'px';

		var cross_scroll_move = _(pref + 'motiongallery_move');

		if (startpos)
			cross_scroll.style.left = (menuwidth-actualwidth)/startpos + 'px';

		div_l.onclick = function() { 
			if (speed == false) {
				offset = 0;
				cross = cross_scroll; 
				cross_move = cross_scroll_move;
				width = actualwidth;
				menu_w = menuwidth;
				speed = true;
				motionengine('left', maxspeed); 
			}
		};

		div_r.onclick = function() { 
			if (speed == false) {
				offset = 0;
				cross = cross_scroll;
				cross_move = cross_scroll_move;
				width = actualwidth;
				menu_w = menuwidth;
				speed = true;
				motionengine('right', maxspeed);
			}
		};

/*
href_r.onclick = function(){ 
if (speed == false) {
offset = 0;
cross = cross_scroll;
cross_move = cross_scroll_move;
width = actualwidth;
menu_w = menuwidth;
speed = true;
motionengine( "right", maxspeed);
}
};*/

// div_l.onmouseout= function(){ stopmotion(); };
//div_r.onmouseout= function(){ stopmotion(); };
	}
	loadedyes = 1;
}

/*function which run moveleft and moveright functions*/
motionengine = function ( direct, maxspeed ) {
	if (direct =="right") {
		scrollspeed=maxspeed;
		clearTimeout(righttime);
	
		if (movestate!="left") moveleft();
	}
	else if (direct=="left") {
		scrollspeed=maxspeed;
		clearTimeout(lefttime);
		if (movestate!="right") moveright();
	}
	else
		scrollspeed=0;
	}

	/*moveleft function*/

	moveleft = function(){
if(offset + scrollspeed > picMotionWidth){
scrollspeed = scrollspeed - (offset + scrollspeed - (picMotionWidth));
}
var saveOff = offset,left = cross.style.left,move = cross_move.style.left;

if (!isNaN(loadedyes)){
movestate="left";

if (iedom&&parseInt(cross.style.left)>(menu_w-width-menu_w-scrollspeed)){
if ( offset > menu_w-scrollspeed ) {
var dim = (menu_w+5) - offset; 
cross.style.left=parseInt(cross.style.left)-dim+"px";
}
else{ cross.style.left=parseInt(cross.style.left)-scrollspeed+"px";}
}
else{ cross.style.left = parseInt(cross_move.style.left)+width-scrollspeed+"px";}

if (iedom&&parseInt(cross_move.style.left)>(menu_w-width-menu_w-scrollspeed)){

if ( offset > menu_w-scrollspeed ) {
var dim = (menu_w+5) - offset;
cross_move.style.left=parseInt(cross_move.style.left)-dim+"px";

}
else{ cross_move.style.left=parseInt(cross_move.style.left)-scrollspeed+"px";}
}
else{ cross_move.style.left = parseInt(cross.style.left)+width+"px";}


offset = offset + scrollspeed;
}


if (offset >= picMotionWidth) {
stopmotion();
speed = false;

}
else{ lefttime=setTimeout("moveleft()",40);}

}

/*moveright function*/
moveright = function(){
if(offset + scrollspeed > picMotionWidth){
scrollspeed = scrollspeed - (offset + scrollspeed - (picMotionWidth));
}
if (loadedyes){
movestate="right";

if (iedom&&parseInt(cross.style.left)<width+scrollspeed){ 

if ( offset > menu_w-scrollspeed ) {
var dim = (menu_w+5) - offset;
cross.style.left=parseInt(cross.style.left)+dim+"px";
}
else cross.style.left=parseInt(cross.style.left)+scrollspeed+"px";
} else cross.style.left = parseInt(cross_move.style.left)-width+scrollspeed+"px";

if (iedom&&parseInt(cross_move.style.left)<width+scrollspeed){

if ( offset > menu_w-scrollspeed ) {
var dim = (menu_w+5) - offset;
cross_move.style.left=parseInt(cross_move.style.left)+dim+"px";
}
else cross_move.style.left=parseInt(cross_move.style.left)+scrollspeed+"px";
}
else cross_move.style.left = parseInt(cross.style.left)-width+"px"; 

offset = offset + scrollspeed;
}
 
if (offset >= picMotionWidth) {stopmotion();speed = false;}
else righttime=setTimeout("moveright()",40);

}

/*End of speed of carousel*/
stopmotion = function ( e ){
clearTimeout(lefttime);
clearTimeout(righttime);
movestate="";
}

_ = function (e) { return document.getElementById ? document.getElementById(e) : eval("document.all."+v); }

}