/***********************************************
* 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*/

var isOpera=self.opera
var isIe=document.all&&document.getElementById&&!isOpera

var imagesinline = 5;
var smallImgWidth = 105;

var bigImageW = 650;
var bigImageH = 488;

var picMotionWidth = smallImgWidth + 14;
var smallImgHeight = Math.floor(smallImgWidth/1.33);



var m = new Array(0,100);
var t = new Array();
var curImg = "";

var outImg ="";
var outBigImg ="";
var countImg;
var imgArray = new Array();
var flag = false;


if(document.getElementById('nov_motioncontainer')){
document.getElementById('nov_motioncontainer').style.overflow="hidden";
document.getElementById('nov_div_l').style.display="block";
document.getElementById('nov_div_r').style.display="block";
}



function createArray() {
	if(!flag) {		
	    
	    document.getElementById("add").innerHTML="";
/*		
		document.getElementById("add").innerHTML = 
		"<DIV id=innerWideDiv style=\"PADDING-RIGHT: 20px; DISPLAY: none; PADDING-LEFT: 20px; Z-INDEX: 500; PADDING-BOTTOM: 20px; PADDING-TOP: 20px; POSITION: absolute; BACKGROUND-COLOR: white\">" +
		"<DIV style=\"RIGHT: 0px; CURSOR: pointer; POSITION: absolute; TOP: 0px\">"+
		"<IMG id=closeImg height=14 alt=Закрыть src=\"http://alkord.ua/images/galery/close.gif\" width=70>"+
		"</DIV><IMG id=wideImg style=\"FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=100); opacity: 1\" alt=\"Кликните для закрытия\" src=\"http://alkord.ua/img_0/1px.gif\"></DIV>"+
		"<DIV id=loadProcessBlock>"+
			"<TABLE class=\"h100 w100 loadProcessTable\" cellSpacing=0 cellPadding=0 border=0>"+
			"<TBODY>"+
			"<TR>"+
			"<TD class=processMainTd><IMG height=32 src=\"http://alkord.ua/images/galery/load.gif\" width=32 align=absMiddle></TD></TR></TBODY></TABLE></DIV>";
		
*/		
		
		document.getElementById('nov_motioncontainer').style.width=5*picMotionWidth;
    	//alert("R");
		var galleryContainer = document.getElementById("galleryContainer");
		//if(galleryContainer) {
		var containerChilds = galleryContainer.childNodes;
		var n=0;
	        
	    for(i=0; i<containerChilds.length; i++){
				    
	        elem = containerChilds[i];
	        if(elem.tagName && elem.tagName == "IMG"){
	                
	    		imgArray[n] = new Image();
	    		loadImage = new Image();

        		bigImgSrc = elem.src.replace("small","big");
        		imgArray[n].src = bigImgSrc;
    		
    			    		
	    		loadImage.src = elem.src;
	    		//alert("Плюс одна картинка!");
	    		outImg +=
	    			"<div class=\"previewPic\">"+
	    				"<div>"+
	    					"<img class=\"smallPic\" width=\""+ smallImgWidth +"\" height=\""+smallImgHeight+"\" border=\"0\" style=\"cursor:pointer; cursor: hand;\" onMouseDown=\"changePic("+n+")\" src="+loadImage.src+">"+
	    					//"<img class=\"smallPic\" width=\""+ smallImgWidth +"\" height=\""+smallImgHeight+"\" border=\"0\" style=\"cursor:pointer; cursor: hand;\" src="+loadImage.src+">"+
	    				"</div>"+
	    			"</div>";
	    
	            n++;
	        }	    
	    }
	    
	    countImg = n;
	
		document.getElementById('nov_motiongallery').style.width=countImg*picMotionWidth+"px";
    	document.getElementById("imgSmall").innerHTML = outImg;
    	
    	document.getElementById('imgBig').innerHTML = 
    		"<div>"+
    			"<table align=\"center\">"+
    				"<tr>"+
    					"<td align=\"center\">"+
    						"<img class=\"bigPic\" name=\"bigImage\" style=\"opacity: 1;\" src='" + imgArray[0].src + "' width=\""+bigImageW+"\" height=\""+bigImageH+"\" border=\"0\">"+
    					"</td>"+
    				"</tr>"+
    			"</table>"+
    		"</div>";	
    		
    		
		flag = true;
	
		if(n <= imagesinline) {
			document.getElementById("rightarr").src="img_0/button_right_hid.gif";
			document.getElementById('nov_motioncontainer').style.width = n * picMotionWidth;
			document.getElementById("galDiv").style.width = 677 - 119*(imagesinline	- n) - ((n>1) ? n : 2);
		} else {
			document.getElementById("galDiv").style.width = 677;	
		}
	}
	
	
}	

function carousel(){

    iedom=document.all||document.getElementById;
	movestate=null;lefttime=null;righttime=null;
	speed = false;
    ready = true;
    delta = 0;
    
    
    
    
    
    
    
    
/*function, which run the class*/
     this.construct = function(){                     
		this.maxspeed = 10;
        var maxspeed=this.maxspeed;
        var startpos=this.startpos;
        var curPos = startpos;
	    var pref=this.pref;
        var crossmain=_(pref+"motioncontainer");
        var cross_scroll=_(pref+"motiongallery");

		
     if ( iedom ){
        var menuwidth=crossmain.offsetWidth;										 // menuwidth - Длина верхнего дива

        div_l=_(pref+"div_l");
        div_r=_(pref+"div_r"); 
            
        var actualwidth=_(pref+"trueContainer").offsetWidth;             // actualwidth- Длина дива с картинками

        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";

        cross_scroll.style.left= -picMotionWidth*startpos+'px'; //(menuwidth-actualwidth)/startpos+'px';	// Вычисление начального смещения путем деления длин двух дивов




div_l_move = function(){ 
	if (speed == false) {
	    offset = 0;
		cross = cross_scroll; 
  //5 images      
        if(curPos-imagesinline < 0){
    	    menu_w = curPos*picMotionWidth; 
    	    curPos = 0;
    	    document.getElementById("leftarr").src="img_0/button_left_hid.gif";
    	}else{
    		menu_w = menuwidth; 
    		curPos -= imagesinline;
    	}
/*      1 image 	
	    menu_w = picMotionWidth*3; 
	    curPos --;    	
*/	    
		speed = true;
		motionengine( "left", maxspeed); 
	}
};

                
div_r_move = function(){ 
	if (speed == false) {
        offset = 0;
		cross = cross_scroll; 
		
//  5 images
    	if(curPos+imagesinline*2 > countImg ){
    	    menu_w = (countImg - curPos - imagesinline)*picMotionWidth; 
    	    curPos += (countImg - curPos - imagesinline);
    	    
    	}else{
    		menu_w = menuwidth; 
    		curPos +=imagesinline;
    	}
    	
    	
/*    	1 image

	    menu_w = picMotionWidth*3; 
	    curPos ++;
   */ 	
		speed = true;
		
		motionengine( "right", maxspeed); 
	}
};				
 		
 		function do_r_move(){
 		    
            div_r_move();

 		}
 		
 		function do_l_move(){
 		    div_l_move();
 		}

	    div_r.onclick = function(){
	        if(curPos < countImg-imagesinline && ready){
	            ready = false;
	            do_r_move();
	        }
	    };
		div_l.onclick = function(){
		    if(curPos > 0 && ready){
		        ready = false;
		        do_l_move();
		    }
		};
		
    	 checkImg = function(){
    	 
                if(curPos == 0){
                    document.getElementById("leftarr").src="img_0/button_left_hid.gif";
                }else{
                    document.getElementById("leftarr").src="img_0/button_left.gif";
                } 		 
                
                if(curPos == countImg-imagesinline){
                    document.getElementById("rightarr").src="img_0/button_right_hid.gif";
                }else{
                    document.getElementById("rightarr").src="img_0/button_right.gif";
                }
    	 } 		
    }
}
     
	
/*function which run moveleft and moveright functions*/
    motionengine = function ( direct, maxspeed ){								// Джвижущая функция
                                           
         if (direct =="right"){
          scrollspeed=maxspeed;
          clearTimeout(righttime);
         if (movestate!="left") moveleft();	  /*moveleft() - вправо*/		// Все добро двигаем влево, кажется что движение идет вправо
         }
         else if (direct=="left"){
          scrollspeed=maxspeed;
          clearTimeout(lefttime);
         if (movestate!="right") moveright();   /*moveright() - влево*/
         }
         else
          scrollspeed=0;
      }

/*moveleft function*/

moveleft = function(){

		if ( offset < menu_w) {
		    cross.style.left = parseInt(cross.style.left) - delta+"px";
        }

	    delta  =  Math.floor((menu_w - offset)/4);
	    offset += delta

        if(menu_w - offset < 4){
            cross.style.left = parseInt(cross.style.left) - (menu_w - offset) +"px";
            stopmotion();
            speed = false; 
            ready = true;
            checkImg();

        }else{ 
            righttime=setTimeout("moveleft()",25);
        }
 
}

/*moveright function*/

moveright = function(){
                
		if ( offset < menu_w) {
		    cross.style.left = parseInt(cross.style.left) + delta+"px";
        }

	    delta  =  Math.floor((menu_w - offset)/4);
	    offset += delta
        
        
        
        if(menu_w - offset < 4){
            cross.style.left = parseInt(cross.style.left) + (menu_w - offset) +"px";
            stopmotion();
            speed = false;
            ready = true;
            checkImg();
        } else{ 
            righttime=setTimeout("moveright()",25);
        }
}


/*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); }

}

	function changePic(e) {
	//if (e.tagName!="IMG") return true;
		/*
		m[0]=0;
		
		if (window.document.bigImage.src == e.src) return true;
		
		var imgName = e.src.substr(e.src.indexOf('/img'));
		imgName = imgName.substr(1);
		
		if (imgName == curImg) return true
		else	curImg = imgName;
		
		if (imgName.length == 13) {
			imgName = imgName.substr(3,1);
		} else {
			imgName = imgName.substr(3,2);
		}*/
		
		var bigImgName = imgArray[e];
//		window.document.bigImage.style.opacity = m[0]/100;		
		window.document.bigImage.style.opacity = 0;
		m[0] = 0;
		
		if(isIe) {				
			window.document.bigImage.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=0.5)";
			window.document.bigImage.filters[0].Apply();
		}
		
		window.document.bigImage.src= bigImgName.src;
		
		if(isIe) {
			window.document.bigImage.filters[0].Play();
		} else {
			next(window.document.bigImage);
		}
	}
	
	function next(el)
	{
		m[0]+=10;
		
		el.style.opacity = m[0]/100;
		//el.style.filter="alpha(opacity="+m[0]+")";
		
		t[0] = setTimeout("next(window.document.bigImage)",10);
		if (m[0]>99) {
			clearTimeout(t[0]);
			
		}
	}
	

