var minWidth=5; var maxWidth=10;
var minHeight=5; var maxHeight=10;
var interval=5; var innerTime = null;
var outterTime = null;
var img; var imgSrc; var div; var imgD;
var wndObj; var imgPath='/upload/';
var ajaxName='info';

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

/*** general utils ***/
function getDocumentHeight() {
	return Math.max(document.compatMode!='CSS1Compat'?document.body.scrollHeight:document.documentElement.scrollHeight,getViewportHeight());
}
 
function getViewportHeight() {
 	return ((document.compatMode || isIE) && !isOpera)?(document.compatMode=='CSS1Compat')?document.documentElement.clientHeight:document.body.clientHeight:(document.parentWindow || document.defaultView).innerHeight;
}

function getBodyScrollTop() {
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft() {
   return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getCenterScreen(w,h,scroll){
	if(scroll==null){
		scl = getBodyScrollLeft();
		sct = getBodyScrollTop();
		
		var wLeft=((screen.availWidth/2) + scl) -(parseInt(w)/2);
    	var wTop=((screen.availHeight/2) + sct) -(parseInt(h))+20;
	} else {		
		var wLeft=((screen.availWidth/2)) -(parseInt(w)/2);
    	var wTop=((screen.availHeight/2)) -(parseInt(h)/2)-60;
	}
    
    return [wLeft,wTop];
}

/*** window ***/
function createWindow(width,height,url,id,wndScroll){
	if(id==null) id='';
	if(wndScroll==null) wndScroll='auto';		
		wndObj = document.createElement('div');
		wndObj.id='wndObj'+id;
		wndObj.style.position = 'absolute';
	var center = getCenterScreen(width,height);

		wndObj.style.left = center[0] + "px";
		wndObj.style.top = center[1] + "px";
		wndObj.style.height = height + "px";
		wndObj.style.width = width + "px";
		wndObj.style.zIndex=1000;		
		wndObj.innerHTML = "<span id='topLine"+id+"' style='width: 100px; height: 100px;'>&nbsp;</span><table border='0' width='100%' style='height: "+parseInt(height)+"px;' cellspacing='0' cellpadding='0'><tr class='windowTitleRow"+id+"'><td id='wndTitle"+id+"' onmousedown=\"ddDown(event,document.getElementById('wndObj"+id+"'));\">&nbsp;</td><td id='wndButton"+id+"'><img src='"+imgPath+"/close_button.gif' onclick='closeWindow();'></td></tr><tr><td style='height: "+(height-40)+"px;' colspan='2' id='wndContent"+id+"'><span style='width: 100%; height: "+height+"px;'><iframe id='wndContentContainer"+id+"' src='"+url+"' scrolling='"+wndScroll+"' frameborder='0' style='border: 0pt none; visibility: visible; width: "+width+"px; height: "+height+"px;'></iframe></span></td></tr></table><span id='bottomLine"+id+"'>&nbsp;</span>";
		document.body.appendChild(wndObj);
		
	// Correct topLine and bottomLine width
	var tpl=document.getElementById('topLine'+id);
	var btl=document.getElementById('bottomLine'+id);
		if(tpl!=null && btl!=null){
			if(isIE) tpl.style.width=parseInt(width)+17+'px';
			else tpl.style.width=parseInt(width)-2+'px';
			
			btl.style.width=parseInt(width)-2+'px';
		}
		
	// Fade effect
	createFadeLay(id,null);
}

function closeWindow(){
	document.body.removeChild(wndObj);
	document.body.removeChild(div);
}

/*** dragndrop ***/
var ddPosX=0; ddPosY=0; ddisDown=false; ddsrc=""; ddcurrEl='';
var ddMouseUp; 
var ddMouseDown; 
var ddMouseMove;

function ddUnInit(){
	// add oure jsCommand
	window.onmouseup=ddMouseUp;
	window.onmousedown=ddMouseDown;
	window.onmousemove=ddMouseMove;
}

function ddDown(e,el){
	currEl=el;
	if(currEl.className=="moveable") searchTales(currEl.id);
	ddposX=parseInt(e.clientX)-parseInt(el.style.left);
	ddposY=parseInt(e.clientY)-parseInt(el.style.top);
	ddisDown=true;
}
function ddDrag(e){
	if(!ddisDown) return;
	currEl.style.left=(parseInt(e.clientX)-ddposX)+'px';
	currEl.style.top=(parseInt(e.clientY)-ddposY)+'px';
}

/*** ajax.window ***/


/*** Fade - effects ***/
/*** you can start function withot src
     then create only fade layer ***/
function createFadeLay(id,src){
	if(src!=null){
		// create image layer
		imgD = document.createElement('div');
		imgD.id='bigImg'+id;
		imgD.style.width=minWidth+'px';
		imgD.style.height=minHeight+'px';
		imgD.style.display="block";
		imgD.style.background="#fff";
		imgD.style.left='0';
		imgD.style.top='0';
		imgD.style.border="#f0f0f0 1px solid";
		imgD.style.zIndex="1000";
		imgD.style.position="fixed";
		imgD.style.visibility = "hidden";
		
		// create an image 
		imgD.innerHTML="<img src=\""+src+"\" style=\"visibility: hidden;\" onclick=\"outterFade();\" id=\"imgSrc"+id+"\">";
		
		// append parent
		document.body.appendChild(imgD);
	}

	// create black layout
	div = document.createElement('div');
	div.style.opacity='0.5';
	div.style.filter='alpha(opacity=80, style=0)';
	div.style.position='absolute';
	div.style.top=0;
	div.style.left=0;
	div.style.zIndex='99';
	div.style.width='100%';
	div.style.height=getDocumentHeight()+"px";
	div.style.backgroundColor='#000000';
	document.body.appendChild(div);
}
	
function openFadeImg(id,src){ 
	createFadeLay(id,src);   
	img = document.getElementById('bigImg'+id);
	imgSrc = document.getElementById('imgSrc'+id);
	var center=getCenterScreen(imgSrc.width,imgSrc.height,true);	
     	// Set left, top
	 	img.style.left = center[0]+'px';
    	img.style.top = center[1]+'px';
   		img.style.display = 'block';
   		
   	maxWidth=imgSrc.width; maxHeight=imgSrc.height;   	
	innerFade();
}

function innerFade(){	
	// Clear inner timeout
	if(outterTime!=null) clearTimeout(outterTime);
	img.style.visibility='visible';
		
		// Calculate width	
		if(parseInt(img.style.width+10)<=maxWidth){
			img.style.width=parseInt(img.style.width)+10+'px';
		} else {
			// Calculate different between width+100 and max
			var dif=maxWidth-(parseInt(img.style.width)+10);
			
				if(dif<=0) var rWidth=true;
				else img.style.width=parseInt(img.style.width)+dif+'px';
		}
		
		// Calcualte height
		if(parseInt(img.style.height+10)<=maxHeight){
			img.style.height=parseInt(img.style.height)+10+'px';
		} else {
			// Calculate different between width+100 and max
			var dif=maxHeight-(parseInt(img.style.height)+10);
			
				if(dif<=0) var rHeight=true;
				else img.style.height=parseInt(img.style.height)+dif+'px';
		}
		
		if(rWidth==true && rHeight==true){
			imgSrc.style.visibility='visible';
			img.style.width=maxWidth+'px';
			img.style.height=maxHeight+'px';
			
			return true;
		}
		
		// Start timeout
		if(innerTime!=null) clearTimeout(innerTime);
		innerTime = setTimeout("innerFade()",interval);
}

function outterFade(){
	imgSrc.style.visibility='hidden';
	
	// Clear inner timeout
	if(innerTime!=null) clearTimeout(innerTime);
	
		// Calculate width	
		if((parseInt(img.style.width)-10)>minWidth) img.style.width=parseInt(img.style.width)-10+'px';
		else var rWidth=true;
		
		if((parseInt(img.style.height)-10)>minHeight) img.style.height=parseInt(img.style.height)-10+'px';
		else var rHeight=true;
		
		 
		if(rWidth==true && rHeight==true) {
			img.style.visibility='hidden'; 
			
			// Unlink our elements
			document.body.removeChild(div);
			document.body.removeChild(imgD);
			
			clearTimeout(outterTime);
			return true;
		}		
					
		// Start timeout
		outterTime = setTimeout("outterFade()",interval);		
}

// Sliding ///	
var sliderCountImg=0;
var sliderCurImg=0;
var sliderTimer=false;

var sliderToLeft = null;
var sliderToRight = null;
var sliderCommentToLeft = null;
var sliderCommentToRight = null;

var startSlide=false;
var slideInnerTime=null;
if(typeof(sliderPlus) == 'undefinef') var sliderPlus=0;

// Menat' li kakoj-to eshe div
var sliderChangeOverDiv = true;
var sliderChangeDivName = [ 'sliderParent' ];

// Centrovka po gorizontali
var sliderHorisontalDiv = true;
var sliderHorisontalDivName = [ 'sliderLeft', 'sliderRight' ];
if(typeof(sliderHorPlus) == 'undefinef') var sliderHorPlus=0;
if(typeof(sliderMaxWidth) == 'undefinef') var sliderMaxWidth=700;

function slidePrepare(){
	var slide=document.getElementById('slider');
	var sliderC=document.getElementById('sliderComment');
	var table; var tr; var desc=''; var width=0;

	if(slide!=null){							 				
		// sozdadim tablicu s opisaniem
		for( obj in slidePhoto ){
			if(typeof(slidePhoto[obj]) != 'undefined'){
				// a zaodno proverim maximal'nuyu 6irinu
				if(slidePhoto[obj]['width']>sliderMaxWidth){					
					// s4itaem na skol'ko umen'6it' visotu
					var difH=slidePhoto[obj]['width']-sliderMaxWidth;
						slidePhoto[obj]['width']=sliderMaxWidth;
						slidePhoto[obj]['height']=slidePhoto[obj]['height'] - difH;
				}
				
				var wth = parseInt(slidePhoto[obj]['width']);
				desc=desc+ "<td width='" + wth + "' style='width: " + wth + "px; padding: 2px;'>" + slidePhoto[obj]['alt'] + "</td>";
				width=parseInt(width)+parseInt(wth);				
			}
		}
		
		// if first image width > div width then correcting div width
		if(slidePhoto[sliderCurImg]['width']<slide.style.width){
		 slide.style.width=slidePhoto[sliderCurImg]['width']+'px';
		 sliderC.style.width=slidePhoto[sliderCurImg]['width']+'px';
	    } if(slidePhoto[sliderCurImg]['height']<slide.style.height) slide.style.height=slidePhoto[sliderCurImg]['height']+'px';
		
		if(slidePhoto[sliderCurImg]['width']>slide.style.width){
		 slide.style.width=slidePhoto[sliderCurImg]['width']+'px';
		 sliderC.style.width=slidePhoto[sliderCurImg]['width']+'px';
		} if(slidePhoto[sliderCurImg]['height']>slide.style.height) slide.style.height=slidePhoto[sliderCurImg]['height']+'px';
		
		// Esli nado eshe kakoj to div pomenyat' razmer - sdelaem eto
		if(sliderChangeOverDiv==true){
			for(var i=0 ; i < sliderChangeDivName.length ; i++){ 
				var div = document.getElementById(sliderChangeDivName[i]);

				if(div != null){
					if(typeof(slidePhoto[sliderCurImg]) != 'undefined') div.style.width = parseInt(slidePhoto[sliderCurImg]['width'])  + sliderPlus + "px";
				}
			}
		}
		
		// Esli nado otcentrovat' strelki
		if(sliderHorisontalDiv == true){
			for(var i = 0; i < sliderHorisontalDivName.length; i++ ){
				var div = document.getElementById(sliderHorisontalDivName[i]);
				
				if(div != null){
					if(typeof(slidePhoto[sliderCurImg]) != 'undefined') div.style.top = Math.ceil(parseInt(slidePhoto[sliderCurImg]['height'])/2) + sliderHorPlus + "px";
				}
			}
		}
		
		// Esli tekushee izobrazhenie ne 0, zna4it komenti nado otmotat'
		var slideCommentLeft = 0;
		if(sliderCurImg != 0){
			for(var i=0; i < sliderCurImg; i++){
				slideCommentLeft = parseInt(slideCommentLeft) - parseInt(slidePhoto[i]['width']);
			}
		}
		
		// Upravlyaushyaya tablica
		slide.innerHTML='<ul style="position: relative; left: 0px; width: '+width+'px; z-index: 100;" id="slideTbl"><li id="sliderLi' + (sliderCurImg) + '" style="float: left; vertical-align: top;"><img width="' + slidePhoto[sliderCurImg]['width'] + '" height="' + slidePhoto[sliderCurImg]['height'] + '" src="'+slidePhoto[sliderCurImg]['img']+'"' + (typeof(slidePhoto[sliderCurImg]['bimg']) != "undefined"?" onclick='openFadeImg(\"" + 0 + "\",\"" + slidePhoto[sliderCurImg]['bimg'] + "\");'":"") + '></li>' + (typeof(slidePhoto[(sliderCurImg+1)]) != 'undefined'? '<li id="sliderLi' + (sliderCurImg+1) + '" style="vertical-align: top; float: left;"><img width="' + slidePhoto[(sliderCurImg+1)]['width'] + '" height="' + slidePhoto[(sliderCurImg+1)]['height'] + '" src="'+slidePhoto[(sliderCurImg+1)]['img']+'" ' + (typeof(slidePhoto[(sliderCurImg+1)]['bimg']) != "undefined"?" onclick='openFadeImg(\"" + 1 + "\",\"" + slidePhoto[(sliderCurImg+1)]['bimg'] + "\");'":"") + '></li>':'') + '</ul>';
		sliderC.innerHTML='<table border="0" width="'+width+'" style="position: relative; left: '+slideCommentLeft+'px; z-index: 100;" id="slideCommentTbl" cellpadding="0" cellspacing="0"><tr>'+desc+'</tr></table>';
	} 
}

function slide(direct){
	if(direct==null) direct="right";
	var tbl=document.getElementById('slideTbl');
	var tr=document.getElementById('slideTr');
	var stop=false;
	
	var slide=document.getElementById('slider');
	var slideC=document.getElementById('slideCommentTbl');
	var slideCm=document.getElementById('sliderComment');
	
	// check direction
	if(startSlide==false){
		if(direct=='right'){
			if(typeof(slidePhoto[sliderCurImg+1]) != "undefined"){
				if(sliderCountImg%2){
					var td3=document.createElement('li');
						td3.id='sliderLi' + (parseInt(sliderCurImg)+1);
						td3.style.cssText="float: left; vertical-align: top;";
						td3.innerHTML="<img src='"+slidePhoto[sliderCurImg+1]['img']+"'" + (typeof(slidePhoto[sliderCurImg+1]['bimg']) != 'undefined'?" onclick='openFadeImg(\"" + sliderCurImg+1 + "\",\"" + slidePhoto[sliderCurImg+1]['bimg'] + "\");'":"") + ">";
						tbl.appendChild(td3);

					if(typeof(slidePhoto[sliderCurImg+2])!='undefined'){
						var td4=document.createElement('li');
							td4.id='sliderLi' + (parseInt(sliderCurImg)+2);
							td4.style.cssText="float: left; vertical-align: top;";
							td4.innerHTML="<img src='"+slidePhoto[sliderCurImg+2]['img']+"'" + (typeof(slidePhoto[sliderCurImg+2]['bimg']) != 'undefined'?" onclick='openFadeImg(\"" + sliderCurImg+2 + "\",\"" + slidePhoto[sliderCurImg+2]['bimg'] + "\");'":"") + ">";
							tbl.appendChild(td4);
					}
				}	
				
				// Proverim visotu sleduyushey kartinki
				if(slidePhoto[sliderCurImg+1]['height']<slide.style.height){			
					slide.style.height=slidePhoto[sliderCurImg+1]['height']+'px';
				}
				
				// I izmenim shirinu podpisi srazu
				slideCm.style.width=slidePhoto[sliderCurImg+1]['width']+'px';
				
				// zapuskaem
				sliderTimer=setInterval('slide("right")',10);
			} else stop=true;
				
		}
		
		// i vlevo
		if(direct=='left'){
			if(typeof(slidePhoto[sliderCurImg-1]) != "undefined"){				
				// Proverim visotu predidushego kartinki
				if(slidePhoto[sliderCurImg-1]['height']<slide.style.height){			
					slide.style.height=slidePhoto[sliderCurImg-1]['height']+'px';
				}
				
				// I izmenim shirinu podpisi srazu
				slideCm.style.width=slidePhoto[sliderCurImg-1]['width']+'px';				
							
				// zapuskaem
				sliderTimer=setInterval('slide("left")',10);
			} else stop = true;
		}
	}
		
	if(stop == false){		
		// sliding
		if(direct=='right'){	
			if(startSlide==false){
				sliderToLeft = parseInt(tbl.style.left)-parseInt(slidePhoto[sliderCurImg]['width']);
				sliderCommentToLeft = parseInt(slideC.style.left)-parseInt(slidePhoto[sliderCurImg]['width']);
			}
			
			startSlide=true;		
					
			if(parseInt(tbl.style.left)-5<=sliderToLeft){
				if(sliderTimer!=false) clearInterval(sliderTimer);
				
				tbl.style.left=(sliderToLeft)+'px';
				slideC.style.left=(sliderCommentToLeft)+'px';
				
				startSlide=false;
				
				// Esli nado eshe kakoj to div pomenyat' razmer - sdelaem eto
				if(sliderChangeOverDiv==true){
					for(var i=0 ; i < sliderChangeDivName.length ; i++){ 
						var div = document.getElementById(sliderChangeDivName[i]);
			
						if(div != null){
							if(typeof(slidePhoto[sliderCurImg + 1]) != 'undefined') div.style.width = parseInt(slidePhoto[sliderCurImg + 1]['width'])  + sliderPlus + "px";
						}
					}
				}
				
				// Esli nado otcentrovat' strelki
				if(sliderHorisontalDiv == true){
					for(var i = 0; i < sliderHorisontalDivName.length; i++ ){
						var div = document.getElementById(sliderHorisontalDivName[i]);
						
						if(div != null){
							if(typeof(slidePhoto[sliderCurImg + 1]) != 'undefined') div.style.top = Math.ceil(parseInt(slidePhoto[sliderCurImg + 1]['height'])/2) + sliderHorPlus + "px";
						}
					}
				}
				
				sliderCurImg++;
				sliderCountImg++;
				corectWindow(slidePhoto[sliderCurImg]['width'],slidePhoto[sliderCurImg]['height']);
			} else {
				var tblLeft=(parseInt(tbl.style.left)-5);
				var slideLeft=(parseInt(slideC.style.left)-5);
				
				if(tblLeft>0){
				 tbl.style.left=(tblLeft*-1)+"px";
				 slideC.style.left=(slideLeft*-1)+"px";
				} else {
				 tbl.style.left=tblLeft+'px';
				 slideC.style.left=slideLeft+'px';
				}		
			}
		} else {
			if(startSlide==false){
				// Proverim est' li ya4ejki
				if(document.getElementById('sliderLi' + (sliderCurImg-1)) == null){
					// I pododvinem spisok na width vlevo
					tbl.style.left = parseInt(tbl.style.left) - parseInt(slidePhoto[sliderCurImg - 1]['width']) + "px";
					
					var tecTd = document.getElementById('sliderLi' + sliderCurImg);
					var td3=document.createElement('li');
						td3.id='sliderLi' + (parseInt(sliderCurImg) - 1);
						td3.style.cssText="float: left; vertical-align: top;";
						td3.innerHTML="<img src='"+slidePhoto[sliderCurImg - 1]['img']+"' width='"+slidePhoto[sliderCurImg - 1]['width']+"'" + (typeof(slidePhoto[(sliderCurImg-1)]['bimg']) != 'undefined'?" onclick='openFadeImg(\"" + (sliderCurImg-1) + "\",\"" + slidePhoto[(sliderCurImg-1)]['bimg'] + "\");'":"") + ">";
						tbl.insertBefore(td3,tecTd);
				}
				
				sliderToRight=parseInt(tbl.style.left)+parseInt(slidePhoto[sliderCurImg-1]['width']);
				sliderCommentToRight=parseInt(slideC.style.left)+parseInt(slidePhoto[sliderCurImg-1]['width']);
			}
			
			startSlide=true;
			
			if(parseInt(tbl.style.left)+5>=sliderToRight){
				if(sliderTimer!=false) clearInterval(sliderTimer);
				
				tbl.style.left=(sliderToRight)+'px';
				slideC.style.left=(sliderCommentToRight)+'px';
				
				startSlide=false;
				
				// Esli nado eshe kakoj to div pomenyat' razmer - sdelaem eto
				if(sliderChangeOverDiv==true){
					for(var i=0 ; i < sliderChangeDivName.length ; i++){ 
						var div = document.getElementById(sliderChangeDivName[i]);
			
						if(div != null){
							if(typeof(slidePhoto[sliderCurImg - 1]) != 'undefined') div.style.width = parseInt(slidePhoto[sliderCurImg - 1]['width'])  + sliderPlus + "px";
						}
					}
				}
				
				// Esli nado otcentrovat' strelki
				if(sliderHorisontalDiv == true){
					for(var i = 0; i < sliderHorisontalDivName.length; i++ ){
						var div = document.getElementById(sliderHorisontalDivName[i]);
						
						if(div != null){
							if(typeof(slidePhoto[sliderCurImg - 1]) != 'undefined') div.style.top = Math.ceil(parseInt(slidePhoto[sliderCurImg - 1]['height'])/2) + sliderHorPlus + "px";
						}
					}
				}
				
				sliderCountImg--;
				sliderCurImg--;
				corectWindow(slidePhoto[sliderCurImg]['width'],slidePhoto[sliderCurImg]['height']);
			} else {
				var tblLeft=(parseInt(tbl.style.left)+5);
				var slideLeft=(parseInt(slideC.style.left)+5);
				
				if(tbl.style.left>0){
				 tbl.style.left=(tblLeft)+"px";
				 slideC.style.left=(slideLeft)+"px";
				} else {
				 tbl.style.left=(tblLeft)+"px";
				 slideC.style.left=(slideLeft)+"px";
				}
			}
		}
	}
}

function corectWindow(w,h){
	var slide=document.getElementById('slider');
	var slideC=document.getElementById('sliderComment');
	
	// Calculate width	
	if(parseInt(slide.style.width+10)<=w){
		if(parseInt(slide.style.width)==w) rWidth=true;
		else slide.style.width=parseInt(slide.style.width)+10+'px';
	} else {
		// Calculate different between width+100 and max
		if(w<parseInt(slide.style.width)) var dif = (parseInt(slide.style.width)-w)+10;
		else var dif=w-(parseInt(slide.style.width)+10);

		if(dif<=0 || (dif>=10 && dif<=20)) var rWidth=true;
		else if(dif>20) slide.style.width=parseInt(slide.style.width)-15+'px';
		else slide.style.width=parseInt(slide.style.width)+dif+'px';
	}
	
	// Calcualte height
	if(parseInt(slide.style.height+10)<=h){
		if(parseInt(slide.style.height)==h) rHeight=true;
		else slide.style.height=parseInt(slide.style.height)+10+'px';
	} else {
		slide.style.height=parseInt(slide.style.height)-10+'px';
		// Calculate different between width+100 and max
		var dif=h-(parseInt(slide.style.height)+10);
		
		if(dif<=0 || (dif>=10 && dif<=20)) var rHeight=true;
		else if(dif>20) slide.style.height=parseInt(slide.style.height)-15+'px';
		else slide.style.height=parseInt(slide.style.height)+dif+'px';
	}
	
	if(rWidth==true && rHeight==true){
		slide.style.width=w+'px';
		slideC.style.width=w+'px';
		slide.style.height=h+'px';			
		
		if(slideInnerTime!=null){
		 clearInterval(slideInnerTime);
		 slideInnerTime=null;
		}
		return true;
	}
	
	// Start timeout
	if(slideInnerTime==null) slideInnerTime = setInterval("corectWindow('"+w+"','"+h+"')",10);
}

/**** scroll ****/
var scrollItem = new Array();
var scrollMaxItem = 4;
var scrollWidth = 128;
var scrollHeight = 112;
var scrollBigHeight = 200;
var scroll;
var scrollFirstNode = 0;
var scrollEndNode = 0;
var scrollCurrent = 0;
var scrollTimer = null;
var scrollNoFirstLast = false;
var scrollCenter = false;
var scrollDefaultTitle = '';

function scrollShowBig(scrCr){
	if(scrCr == null) scrCr = scrollCurrent;
	
	// Show a description
	if(typeof(scrollItem[scrCr]['descript']) != 'undefined'){
		var dObj = document.getElementById('scrollDescript');
		
		if(dObj != null){
			if(scrollItem[scrCr]['descript'] != ''){		
				dObj.innerHTML = scrollItem[scrCr]['descript'];
			} else {
				dObj.innerHTML = scrollDefaultTitle;
			}
		}
	}
	
	if(typeof(scrollItem[scrCr]['big']) != 'undefined'){
		var big = document.getElementById('scrollBig');
			if(big != null){
				big.src = scrollItem[scrCr ]['big'];
				big.style.marginTop = '0px';
				
				if(scrollCenter != false){
					if(typeof(scrollItem[scrCr]['big_height']) != 'undefined'){
						if(scrollItem[scrCr]['big_height'] < scrollBigHeight){
							big.style.marginTop= Math.ceil((scrollBigHeight - scrollItem[scrCr]['big_height'])/2) + "px";
						}
					}
				}
			}
	} else if(typeof(scrollItem[scrCr]['href']) != 'undefined'){
		document.location.href = scrollItem[scrCr]['href'];
	}
}

function scrollItemPrepare(){
	scroll = document.getElementById('scroll');
	
	var dObj = document.getElementById('scrollDescript');		
	if(dObj != null) scrollDefaultTitle = dObj.innerHTML;

	if(scrollNoFirstLast == true) var ret = '';
	else var ret = '<li id="scrollLiFirst">&nbsp;</li>';
	
	// add scroll item into parent block
	for(var i=0; i<scrollMaxItem; i++){
		if(typeof(scrollItem[i]) != 'undefined'){
			// Centering the image
			var style='';
			if(scrollCenter == true){
				if(typeof(scrollItem[i]['height']) != 'undefined'){
					if(scrollItem[i]['height'] < scrollHeight) var style = " style='margin-top: " + Math.ceil((scrollHeight - scrollItem[i]['height'])/2) + "px'";
				} 
			}
				
			ret = ret + "<li onclick='scrollShowBig(\"" + i + "\")' id='scrollLi" + scrollItem[i]['id'] +"'><a href='#' onclick='return false;'><img" + style + " src='" + scrollItem[i]['img'] + "' border='0'></a></li>";
		}
	}
	
	scroll.innerHTML = ret;
	scrollFirstNode = scrollItem[0]['id'];
	
	if(typeof(scrollItem[scrollMaxItem - 1]) != 'undefined'){
		scrollEndNode = scrollItem[scrollMaxItem - 1]['id'];
	} else {
		scrollEndNode = scrollItem[(scrollItem.length - 1)]['id'];
	}
}

function scrollStart(direct){
	if(direct == null) dircet = 'up';
	
	if(direct == 'down'){
		// If this is the first element - then show end element
		if(typeof(scrollItem[(scrollCurrent + scrollMaxItem)]) == 'undefined'){
			var stop = true;
			scrollStop();
			
			if(scrollNoFirstLast == false){
				// If no first li then create them
				if((parseInt(scrollCurrent) + parseInt(scrollMaxItem)) == scrollItem.length && document.getElementById('scrollLiLast') == null){
					var li = document.createElement('li');
						li.id = 'scrollLiLast';
						li.innerHTML='&nbsp;';
						scroll.appendChild(li);
					
						// Remove first child
						scroll.removeChild(document.getElementById('scrollLi' + scrollFirstNode));							
						scrollFirstNode = scrollItem[scrollCurrent + 1]['id'];				
				}
			}
		} else stop = false;
		
		if(stop == false){
			var li = document.createElement('li');
				li.id = 'scrollLi' + scrollItem[scrollCurrent + scrollMaxItem]['id'];
				li.rel = scrollCurrent + scrollMaxItem;
				li.onclick=function (){ scrollShowBig(this.rel);	};
				li.style.cursor='hand';		
				
				// Centering the image
				var style='';
				if(scrollCenter == true){
					if(typeof(scrollItem[(scrollCurrent + scrollMaxItem)]['height']) != 'undefined'){
						if(scrollItem[(scrollCurrent + scrollMaxItem)]['height'] < scrollHeight) var style = " style='margin-top: " + Math.ceil((scrollHeight - scrollItem[(scrollCurrent + scrollMaxItem)]['height'])/2) + "px'";
					} 
				}
					
				li.innerHTML = "<a href='#' onclick='return false;'><img" + style + " src='" + scrollItem[scrollCurrent + scrollMaxItem]['img'] + "' border='0'></a>";
				
				// remove last node
				scroll.removeChild(document.getElementById('scrollLi' + scrollFirstNode));
				
				// if this is first or last img then remove first or last li
				if(scrollNoFirstLast == false){
					if(scrollCurrent == 0){
						scroll.removeChild(document.getElementById('scrollLiFirst'));
					}
				}
				
				// Change first node
				scrollFirstNode = scrollItem[(scrollCurrent + 1)]['id'];
				
				// append child
				scroll.appendChild(li);
				
				// Change last node
				scrollEndNode = scrollItem[((scrollCurrent) + scrollMaxItem)]['id'];
				scrollCurrent = scrollCurrent + 1;
		}
	} else {
		// If this is the first element - then show end element
		if(typeof(scrollItem[(scrollCurrent - 1)]) == 'undefined'){
			var stop = true;
			scrollStop(); 
			
			if(scrollNoFirstLast == false){
				// If no first li then create them
				if(scrollCurrent == 0 && document.getElementById('scrollLiFirst') == null){
					var li = document.createElement('li');
						li.id = 'scrollLiFirst';
						scroll.insertBefore(li, document.getElementById('scrollLi' + scrollItem[0]['id']));
				}
			} 
			
			// default values
			scrollCurrent = 0;
			scrollFirstNode = scrollItem[0]['id'];
			scrollEndNode = scrollItem[scrollMaxItem - 1]['id'];
		} else stop = false;
				
		if(stop != true){
			if(scrollNoFirstLast == false){
				// if this is the last li then delete empty li
				if((parseInt(scrollCurrent) + parseInt(scrollMaxItem)) == scrollItem.length){
					scroll.removeChild(document.getElementById('scrollLiLast'));
				
					// Vosstanovim pervij element
					var li = document.createElement('li');
						li.id = 'scrollLi' + scrollItem[scrollCurrent]['id'];
						li.rel = scrollCurrent;
						li.onclick=function (){ scrollShowBig(this.rel);	};
						li.style.cursor='hand';			
						
						// Centering the image
						var style='';
						if(scrollCenter == true){
							if(typeof(scrollItem[scrollCurrent]['height']) != 'undefined'){
								if(scrollItem[scrollCurrent]['height'] < scrollHeight) var style = " style='margin-top: " + Math.ceil((scrollHeight - scrollItem[scrollCurrent]['height'])/2) + "px'";
							} 
						}
				
						li.innerHTML = "<a href='#' onclick='return false;'><img" + style + " src='" + scrollItem[scrollCurrent]['img'] + "' border='0'></a>";
						scroll.insertBefore(li,document.getElementById('scrollLi' + scrollFirstNode));
				}
			}
			
			// Last element is scrollCurrent + scrollMaxItem
			scroll.removeChild(document.getElementById('scrollLi' + scrollEndNode));
			
			// update first node
			scrollFirstNode = scrollItem[scrollCurrent]['id'];
			scrollEndNode = scrollItem[(scrollCurrent - 2) + scrollMaxItem]['id']
			scrollCurrent = scrollCurrent - 1;
	
			// add previos picture
			var li = document.createElement('li');
				li.id = 'scrollLi' + scrollItem[scrollCurrent]['id'];
				li.rel = scrollCurrent;
				li.style.cursor='hand';
				li.onclick=function (){ scrollShowBig(this.rel);	};
				
				// Centering the image
				var style='';
				if(scrollCenter == true){
					if(typeof(scrollItem[scrollCurrent]['height']) != 'undefined'){
						if(scrollItem[scrollCurrent]['height'] < scrollHeight) var style = " style='margin-top: " + Math.ceil(scrollItem[scrollCurrent]['height'] - (scrollHeight/2)) + "px'";
					} 
				}
				
				li.innerHTML = "<a href='#' onclick='return false;'><img" + style + " src='" + scrollItem[scrollCurrent]['img'] + "' border='0'></a>";
			
			scroll.insertBefore(li,document.getElementById('scrollLi' + scrollItem[scrollFirstNode]['id']));
		}
	}
	
	if(scrollTimer != null) clearInterval(scrollTimer);
	scrollTimer = setInterval('scrollStart("' + direct + '")', 500);	
}

function scrollStop(){
	if(scrollTimer != null) clearInterval(scrollTimer);
}