/***********************************************
* 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 && !isOpera;

if(isIE || isOpera) {	
	window.attachEvent("onload", initG);
} else {	
	window.addEventListener("load", initG, true);
}*/

var isOpera=self.opera
var isIe=document.all&&document.getElementById&&!isOpera

var imagesinline = 5;
var smallImgWidth = 105;

var picMotionWidth = smallImgWidth+14;
var smallImgHeight = Math.floor(smallImgWidth/1.33);


var m = new Array(0,100);
var t = new Array();

var outImg ="";
var countImg;
var imgArray = new Array();
//var flag = false;
var ordNumber = 0;
var gRecArray = new Array();
var idd;

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";
}
//document.getElementById('galDiv').style.width = 655;
//document.getElementById('manual').style.display = "none";

function showPopup(pBigImageSrc) {
	window.open(pBigImageSrc, '', 'width=810, height=610, location=0, address=0, status=0, toolbar=0');
	void 0;
}

function addGalBlock() {
	var d = new Date();
	var uniqId = d.getMilliseconds();
	
	if(document.getElementById('galDiv'+uniqId)) {
		uniqId = Math.random() * 899 + 100;	
	}
	
	/*while(uniqId < 100 || document.getElementById('galDiv'+uniqId)) {
		uniqId = d.getMilliseconds();
	}*/	
	
	var galBlockHtml = '</pre><TABLE id=mainTbl align=center><TBODY><TR><TD align=middle>'+
		'<DIV class="galleryDiv fleft" id=galDiv'+uniqId+'>'+
		'<DIV onclick="div_l('+uniqId+');" class=div_l id=nov_div_l'+uniqId+' style="DISPLAY: block">'+
		'<IMG id=leftarr'+uniqId+' height=34 src="/img_0/button_left_hid.gif" width=35 border=0></DIV>'+
		'<DIV onclick="div_r('+uniqId+');" class=div_r id=nov_div_r'+uniqId+' style="DISPLAY: block">'+
		'<IMG id=rightarr'+uniqId+' height=34 src="/img_0/button_right.gif" width=35 border=0></DIV>'+
		'<DIV class=motioncontainer id=nov_motioncontainer'+uniqId+' style="OVERFLOW: hidden">'+
		'<DIV id=nov_motiongallery'+uniqId+' style="POSITION: absolute; TOP: 0pt">'+
		'<DIV id=nov_trueContainer'+uniqId+' style="POSITION: absolute">'+
		'<DIV id=imgSmall'+uniqId+'></DIV>'+
		'</DIV></DIV></DIV></DIV></TD></TR></TBODY></TABLE>'+
		'<p><DIV id=galleryContainer'+uniqId+' style="BORDER-RIGHT: #191919 1px dashed; PADDING-RIGHT: 15px; BORDER-TOP: #191919 1px dashed; DISPLAY: none; PADDING-LEFT: 15px; PADDING-BOTTOM: 15px; BORDER-LEFT: #191919 1px dashed; PADDING-TOP: 15px; BORDER-BOTTOM: #191919 1px dashed">&nbsp;</DIV></p>'+
		'<div id="imgBig'+uniqId+'"></div>'+
		'<script language="javascript">createArray("'+uniqId+'");</script>';	
	document.write(galBlockHtml);
	document.close();
}


function clr(obj) {
	var childs = obj.childNodes;
	for(i=0; i<childs.length; i++) {
		if(childs[i] && childs[i].tagName && childs[i].innerHTML.indexOf('addGalBlock') !=0)
			childs[i].innerHTML = childs[i].innerHTML.replace('addGalBlock()', '');
		else
			clr(childs[i]);		
	}
}


function createArray(pUniqId) {	
	
	if (!parent.document.getElementById('templateframe')) {		
		
		document.getElementById('nov_motioncontainer'+pUniqId).style.width = 5 * picMotionWidth;
    	
		var galleryContainer = document.getElementById("galleryContainer"+pUniqId);
		var containerChilds = galleryContainer.childNodes;
		var n=0;
	    imgArray['g'+pUniqId] = [];
	    
	    for(i=0; i<containerChilds.length; i++){
				    
	        elem = containerChilds[i];
	        if(elem.tagName && elem.tagName == "IMG"){
	                
	    		
	    		imgArray['g'+pUniqId][n] = new Image();
	    		loadImage = new Image();
	    		
	    		loadImage.src = elem.src;
	    		
	    		
	    		/*extIndex = loadImage.src.lastIndexOf(".");
			    ext = loadImage.src.substr(extIndex);*/
			    bigImageSrc = loadImage.src.replace("small","big");
			    //bigImageSrc = loadImage.src.substr(0, extIndex) + "_big" + ext;
			    
			    imgArray['g'+pUniqId][n].src = bigImageSrc;
			    
	    		outImg +=
	    			"<div class=\"previewPic\">"+
	    				"<div>"+
	    					"<img class=\"smallPic\" width=\""+ smallImgWidth +"\" height=\""+smallImgHeight+"\" border=\"0\" style=\"cursor:pointer; cursor: hand;\" onMouseDown=\"changePic("+n+", "+pUniqId+")\" src="+loadImage.src+">"+
	    				"</div>"+
	    			"</div>";
	    
	            n++;
	        }	    
	    }
	    
	    countImg = n;
	
		document.getElementById('nov_motiongallery'+pUniqId).style.width = countImg * picMotionWidth+"px";
		document.getElementById("imgSmall"+pUniqId).innerHTML = outImg;
		outImg = '';
		
		//alert(imgArray['g'+pUniqId][0].src);
		
		document.getElementById('imgBig'+pUniqId).innerHTML = 
    		'<div>'+
    			'<table align="center">'+
    				'<tr>'+
    					'<td align="center">'+
    						'<img class="bigPic" id="bigImage'+pUniqId+'" style="opacity: 1;" src="' + imgArray['g'+pUniqId][0].src + '" border="0">'+
    					'</td>'+
    				'</tr>'+
    			'</table>'+
    		'</div>';
	
		if(n <= imagesinline) {
			document.getElementById("rightarr"+pUniqId).src="img_0/button_right_hid.gif";
			document.getElementById(pref+'motioncontainer'+pUniqId).style.width = n * picMotionWidth;
			document.getElementById("galDiv"+pUniqId).style.width = 677 - 120*(imagesinline	- n);
		} else {
			document.getElementById("galDiv"+pUniqId).style.width = 675;	
		}
		
		//++ordNumber;
		gRecArray[pUniqId] = new galleryRecord(pUniqId, countImg);
		document.getElementById(pref+"motiongallery"+pUniqId).style.left = -picMotionWidth*gRecArray[pUniqId].startpos+'px';
	}	
}	

 

function galleryRecord(uId, n) {
	this.startpos = 0;
	this.curPos = 0;
	this.n = n;
	this.crossmain = document.getElementById(pref+"motioncontainer"+uId);
	//alert("this.crossmain.style: " + this.crossmain);
	this.cross_scroll = document.getElementById(pref+"motiongallery"+uId);
	//this.menuwidth = this.crossmain.offsetWidth;
	this.menuwidth = 595;
	this.actualwidth = document.getElementById(pref+"trueContainer"+uId).offsetWidth;
	this.first = document.getElementById(pref+"motiongallery"+uId);
	this.last = this.first.cloneNode(true);
    this.insertPoint = document.getElementById(pref+"motioncontainer"+uId);
    this.ready = true;
}


    var maxspeed = 10;
   //var startpos=0;
    //var curPos = startpos;
    var pref = "nov_";
    var iedom=document.all||document.getElementById;
	var movestate=null;
	var lefttime=null;
	var righttime=null;
	var speed = false;
    //var ready = true;
    var delta = 0;		
  //  var div_l;
  //  var div_r;
	var actualwidth;
	var first;
	var last;
	var insertPoint;
	var crossmain;
	var cross_scroll;
	var menuwidth;
	var cross;
	//cross_scroll.style.left= -picMotionWidth*gRecArray[idd].startpos+'px'; //(menuwidth-actualwidth)/startpos+'px';	// Вычисление начального смещения путем деления длин двух дивов	
function initportvars(pUId){	
    crossmain 		= gRecArray[pUId].crossmain;
    cross_scroll 	= gRecArray[pUId].cross_scroll;
    menuwidth 		= gRecArray[pUId].menuwidth;										 // menuwidth - Длина верхнего дива
   // div_l=_(pref+"div_l"+iddd);
   // div_r=_(pref+"div_r"+iddd); 
    //actualwidth 	= gRecArray[pUId].actualwidth;             // actualwidth- Длина дива с картинками

    first 			= gRecArray[pUId].first;              		   // Блочок для создания копии полоски картинок
    //last 			= first.cloneNode(true);
    //last.id = pref+'motiongallery_move'+pUId;
    //insertPoint = gRecArray[pUId].insertPoint;
    //insertPoint.appendChild(last);
    //last.style.left = actualwidth+"px";
   
}



function div_l_move(uId){
	
	if (speed == false) {
	    offset = 0;
		cross = cross_scroll; 
  //5 images      
        if(gRecArray[uId].curPos-imagesinline < 0){        	
    	    menu_w = gRecArray[uId].curPos*picMotionWidth; 
    	    gRecArray[uId].curPos = 0;
    	    //document.getElementById("leftarr"+uId).src="img_0/button_left_hid.gif";
    	}else{
    		menu_w = gRecArray[uId].menuwidth; 
    		gRecArray[uId].curPos -= imagesinline;
    	}
/*      1 image 	
	    menu_w = picMotionWidth*3; 
	    curPos --;    	
*/	    
		speed = true;
		motionengine( "left", maxspeed, uId); 
	} 
/*	if (speed == false) {
	    offset = 0;
		cross = cross_scroll; 

	    menu_w = picMotionWidth*1; 
	   gRecArray[uId].curPos--;    	
	    
		speed = true;
		motionengine("left", maxspeed, uId); 
	}*/
}

                
function div_r_move(uId){ 
	if (speed == false) {
        offset = 0;
		cross = cross_scroll; 
		
//  5 images
    	if(gRecArray[uId].curPos+imagesinline*2 > gRecArray[uId].n ){
    	    menu_w = (gRecArray[uId].n - gRecArray[uId].curPos - imagesinline)*picMotionWidth; 
    	   
    	    gRecArray[uId].curPos += (gRecArray[uId].n - gRecArray[uId].curPos - imagesinline);
    	    
    	}else{
    		menu_w = gRecArray[uId].menuwidth; 
    		gRecArray[uId].curPos +=imagesinline;
    	}
    	 //alert("gRecArray[uId].menuwidth: " + gRecArray[uId].menuwidth);
    	
/*    	1 image

	    menu_w = picMotionWidth*3; 
	    curPos ++;
   */ 	
		speed = true;
		//alert("div_r_move");
		motionengine( "right", maxspeed, uId); 
	}
/*	if (speed == false) {
        offset = 0;
		cross = cross_scroll; 
	
	    menu_w = picMotionWidth*1; 
	    gRecArray[uId].curPos++;
    	
		speed = true;
		
		motionengine("right", maxspeed, uId); 
	}*/
}				
 		
function div_r(uId){
	//iddd=uId;
	initportvars(uId);
	
    if((gRecArray[uId].curPos < (gRecArray[uId].n - imagesinline)) && (gRecArray[uId].ready)){
    	gRecArray[uId].ready = false;
        div_r_move(uId);
    }
}

function div_l(uId){
	//iddd=uId;
	initportvars(uId);
	
    if(gRecArray[uId].curPos > 0 && gRecArray[uId].ready){
        gRecArray[uId].ready = false;
        div_l_move(uId);
    }
}
		
 function checkImg(uId){
 
        if(gRecArray[uId].curPos == 0){
            document.getElementById("leftarr"+uId).src="img_0/button_left_hid.gif";
        }else{
            document.getElementById("leftarr"+uId).src="img_0/button_left.gif";
        }
        
        if(gRecArray[uId].curPos == gRecArray[uId].n-imagesinline){
            document.getElementById("rightarr"+uId).src="img_0/button_right_hid.gif";
        }else{
            document.getElementById("rightarr"+uId).src="img_0/button_right.gif";
        }
 } 		
     
	
/*function which run moveleft and moveright functions*/
function motionengine(direct, maxspeed, uId){								// Джвижущая функция
         //alert("motionengine: "+direct)                       
         if (direct =="right"){
          scrollspeed=maxspeed;
          clearTimeout(righttime);
          moveleft(uId);	  /*moveleft() - вправо*/		// Все добро двигаем влево, кажется что движение идет вправо
         }
         else if (direct=="left"){
          scrollspeed=maxspeed;
          clearTimeout(lefttime);
          moveright(uId);   /*moveright() - влево*/
         }
         else
          scrollspeed=0;
      }

/*moveleft function*/

function moveleft(uId){
		idd = (uId)?uId:idd;
		
		//alert("menu_w: "+menu_w+", offset: " + offset);
		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; 
            gRecArray[idd].ready = true;
            checkImg(idd);

        }else{
        	
            righttime=setTimeout("moveleft()",25);
        }
 
}

/*moveright function*/

function moveright(uId){
		idd = (uId)?uId:idd;    
		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;
            gRecArray[idd].ready = true;
            checkImg(idd);
        } else{        	
            lefttime=setTimeout("moveright()",25);
        }
}


/*End of speed of carousel*/
function stopmotion( e ){
          clearTimeout(lefttime);
          clearTimeout(righttime);
          movestate="";
      }

function _(e) { return document.getElementById ? document.getElementById(e) : eval("document.all."+v); }

function changePic(e, pUniqId) {
		
		var bigImgName = imgArray['g'+pUniqId][e];
		//alert(pUniqId);
		var bigImg = document.getElementById( "bigImage"+pUniqId );
		bigImg.style.opacity = 0;
		m[0] = 0;
		
		if(isIe) {				
			bigImg.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=0.5)";
			bigImg.filters[0].Apply();
		}
		
		bigImg.src= bigImgName.src;
		
		if(isIe) {
			bigImg.filters[0].Play();
		} else {
			next(pUniqId);
		}
	}
	
	function next(pUniqId)
	{
		var el = document.getElementById( "bigImage"+pUniqId );
		m[0]+=10;
		
		el.style.opacity = m[0]/100;
		//el.style.filter="alpha(opacity="+m[0]+")";
		
		t[0] = setTimeout("next("+pUniqId+")",10);
		if (m[0]>99) {
			clearTimeout(t[0]);
			
		}
	}