var contentBlock = new Array();
var scrollBlock = new Array();
var buttonTop = new Array();
var buttonTopNone = new Array();
var buttonBottom = new Array();
var buttonBottomNone = new Array();
var classContentScrollOn = new Array();
var classContentScrollOff = new Array();
var classScrollOn = new Array();
var classScrollOff = new Array();
var maxHeightOrWidth = new Array();
var scrollButtons = new Array();
var flag1 = new Array();
var flag2 = new Array();
var scrollHW = new Array();
var dpX = 579;
var dpY = 554;

var len, theTime, time, title, titleHeight;
var countScroll;
var currentScroll = -1;

function initScroll(){
	
	contentBlock[0] = document.getElementById('idContBlock');
	scrollBlock[0] = document.getElementById('idScrollBlock');
	buttonTop[0] = document.getElementById('idButtonTop');
	buttonTopNone[0] = document.getElementById('idButtonTopH');
	buttonBottom[0] = document.getElementById('idButtonBot');
	buttonBottomNone[0] = document.getElementById('idButtonBotH');
	classContentScrollOn[0] = 'contBlock2';
	classContentScrollOff[0] = 'contBlock';
	classScrollOn[0] = 'scrollBlock';
	classScrollOff[0] = 'scrollBlockNone';
	maxHeightOrWidth[0] = 560;
	scrollHW[0] = 'height';
	
	contentBlock[1] = document.getElementById('idGalleryPreview');
	scrollBlock[1] = document.getElementById('idGalleryScroll');
	buttonTop[1] = document.getElementById('buttonGalleryLeft');
	buttonTopNone[1] = document.getElementById('buttonGalleryLeftNone');
	buttonBottom[1] = document.getElementById('buttonGalleryRight');
	buttonBottomNone[1] = document.getElementById('buttonGalleryRightNone');
	classContentScrollOn[1] = 'galleryPreview';
	classContentScrollOff[1] = 'galleryPreview';
	classScrollOn[1] = 'scroll';
	classScrollOff[1] = '';
	maxHeightOrWidth[1] = 524;
	scrollHW[1] = 'width';
	
	var galleryScroll;
	if(galleryScroll = document.getElementById('idGalleryScroll')){
		dpX = 530;
		dpY = 505;
		maxHeightOrWidth[0] = 530;
		var list = galleryScroll.getElementsByTagName('a');
		var cutPic, leftPics = 0, fl = true;
		for(var i=0; i<list.length; i++){
			if(list[i].className=='active'){
				curPic = list[i].scrollWidth;
				fl = false;
			}
			if(fl) leftPics+=list[i].scrollWidth;
		}
		if(!fl) scrollBlock[1].scrollLeft=leftPics-(maxHeightOrWidth[1]-curPic)/2;
	}
	
	title = document.getElementById('idTitle');
	if(title.clientHeight>28){
		titleHeight=title.clientHeight-28;
		maxHeightOrWidth[0]=maxHeightOrWidth[0]-titleHeight;
		scrollBlock[0].style.height=dpX-titleHeight;
	}
	
	for(i=0; i<countScroll; i++) scrollButtons[i] = false;
	
	for(i=0; i<countScroll; i++) turnScroll(i);
}

function turnScroll(id){
	if((scrollBlock[id].scrollHeight>maxHeightOrWidth[id])||(scrollBlock[id].scrollWidth>maxHeightOrWidth[id])){
		scrollBlock[id].className=classScrollOn[id];
		if((title.clientHeight>28)&&(id==0)){
			scrollBlock[id].style.height=(dpY-titleHeight)+"px";
		}
		buttonTop[id].style.display='block';
		buttonBottom[id].style.display='block';
		buttonTopNone[id].style.display='none';
		buttonBottomNone[id].style.display='none';
		contentBlock[id].className=classContentScrollOn[id];
		if(scrollHW[id] == 'height'){
			if(scrollBlock[id].scrollTop<=0){
				buttonTop[id].style.display='none';
				buttonTopNone[id].style.display='block';
			}
			if(scrollBlock[id].scrollHeight-scrollBlock[id].scrollTop<=scrollBlock[id].clientHeight){
				buttonBottom[id].style.display='none';
				buttonBottomNone[id].style.display='block';
			}
		}else{
			if(scrollBlock[id].scrollLeft<=0){
				buttonTop[id].style.display='none';
				buttonTopNone[id].style.display='block';
			}
			if(scrollBlock[id].scrollWidth-scrollBlock[id].scrollLeft<=scrollBlock[id].clientWidth){
				buttonBottom[id].style.display='none';
				buttonBottomNone[id].style.display='block';
			}
		}
		scrollButtons[id] = true;
	}else{
		scrollBlock[id].className=classScrollOff[id];
		buttonTop[id].style.display='none';
		buttonBottom[id].style.display='none';
		buttonTopNone[id].style.display='none';
		buttonBottomNone[id].style.display='none';
		contentBlock[id].className=classContentScrollOff[id];
	}
}

function scrollStop()
{
	if(time){
		clearTimeout(time);
		currentScroll = -1;
	}
}


function scrollStart(){
	if(scrollHW[currentScroll] == 'height'){
		if(scrollBlock[currentScroll].scrollTop<=0){
			buttonTop[currentScroll].style.display='none';
			buttonTopNone[currentScroll].style.display='block';
			flag1[currentScroll]=true;
		}
		if((flag1[currentScroll]) && (scrollBlock[currentScroll].scrollTop>0)){
			buttonTop[currentScroll].style.display='block';
			buttonTopNone[currentScroll].style.display='none';
			flag1[currentScroll]=false;
		}
		if(scrollBlock[currentScroll].scrollHeight-scrollBlock[currentScroll].scrollTop<=scrollBlock[currentScroll].clientHeight){
			buttonBottom[currentScroll].style.display='none';
			buttonBottomNone[currentScroll].style.display='block';
			flag2[currentScroll]=true;
		}
		if((flag2[currentScroll]) && (scrollBlock[currentScroll].scrollHeight-scrollBlock[currentScroll].scrollTop>scrollBlock[currentScroll].clientHeight)){
			buttonBottom[currentScroll].style.display='block';
			buttonBottomNone[currentScroll].style.display='none';
			flag2[currentScroll]=false;
		}
		scrollBlock[currentScroll].scrollTop+=len;
	}else{
		if(scrollBlock[currentScroll].scrollLeft<=0){
			buttonTop[currentScroll].style.display='none';
			buttonTopNone[currentScroll].style.display='block';
			flag1[currentScroll]=true;
		}
		if((flag1[currentScroll]) && (scrollBlock[currentScroll].scrollLeft>0)){
			buttonTop[currentScroll].style.display='block';
			buttonTopNone[currentScroll].style.display='none';
			flag1[currentScroll]=false;
		}
		if(scrollBlock[currentScroll].scrollWidth-scrollBlock[currentScroll].scrollLeft<=scrollBlock[currentScroll].clientWidth){
			buttonBottom[currentScroll].style.display='none';
			buttonBottomNone[currentScroll].style.display='block';
			flag2[currentScroll]=true;
		}
		if((flag2[currentScroll]) && (scrollBlock[currentScroll].scrollWidth-scrollBlock[currentScroll].scrollLeft>scrollBlock[currentScroll].clientWidth)){
			buttonBottom[currentScroll].style.display='block';
			buttonBottomNone[currentScroll].style.display='none';
			flag2[currentScroll]=false;
		}
		scrollBlock[currentScroll].scrollLeft+=len;
	}
	time = setTimeout('scrollStart()',theTime);
}

function scrollGo(id, xlen, xtheTime){
	if(currentScroll == -1) currentScroll = id;
	if(scrollButtons[currentScroll]){
		len=xlen;
		theTime=xtheTime;
		scrollStart();
	}
}
