var scrollArea;
var content;
var scrollBar;
var slider;
var debugInfo;
var timer;
var timer2;
var speedCoef = 0;
var contentWidth;
var contentFullWidth;
var mousedown = false;
var scrollSpeed = 0;
var scrollStart = null;
var slideStart;
var slideshowShift = 0;
var slideshowIncrement = 3;

//pocet pixlov od okraja, ktore budu reagovat na mysku posunutim
var scrollWidth = 120;

//pocet pixlov, o kolko sa posuva za casovu jednotku, ked sa s myskou priblizim az k uplnemu kraju galerie
var maxShift = 3;

//casova jednotka v ms
var timeUnit = 10;

function initScrolling(width){
	content = document.getElementById("scrollContent");
	scrollArea = document.getElementById("scrollArea");
	debugInfo = document.getElementById("debug");
	scrollBar = document.getElementById("scrollbar");
	slider = document.getElementById("slider");
	if(!content || !scrollArea || !scrollBar || !slider)
			return;
	contentFullWidth = content.offsetWidth;
	scrollArea.style.width = width+"px";		
	contentWidth = parseInt(scrollArea.style.width);
	if(contentFullWidth > contentWidth)
		scrollBar.style.display = ""
	content.style.marginLeft = "0px";
	slider.style.marginLeft = "0px";
	//content.onmouseover = scroll;
	//content.onmouseout = stopScrolling;
	scrollBar.onmousedown = mouseDown;
	slider.onmousemove = centerSlider;
	scrollBar.onmouseup = mouseUp;
	document.onmousemove = centerSlider;
	document.onmouseup = mouseUp;
	sliderWidth = Math.max(20,Math.min(300,contentWidth*300/contentFullWidth))
	slider.style.width = sliderWidth+"px";
	if(window.position !== undefined)
		scrollGalleryToPosition(position);	//premenna position je z view_gal_photo.tpl
}

function scroll(){
	if((speedCoef == 0)
	  || (speedCoef > 0 && parseInt(content.style.marginLeft) >= 0)
	  || (speedCoef < 0 && parseInt(content.style.marginLeft) < contentWidth-contentFullWidth)
	  )
		{}
	else{	
		shift = Math.round(speedCoef*maxShift);	
		content.style.marginLeft = (parseInt(content.style.marginLeft)+shift)+"px";
	}
	timer = setTimeout("scroll()",timeUnit);	
}

function scrollTo(percentage){
	content.style.marginLeft = (-Math.round((contentFullWidth-contentWidth)*percentage))+"px";
}

function scrollToPosition(pos){
	var actual = parseInt(content.style.marginLeft);
	if(scrollStart == null)
		scrollStart = actual;
	if(pos > 0)
		pos = 0;
	if(pos <= contentWidth - contentFullWidth)
		pos = contentWidth - contentFullWidth;
		
	if((scrollStart == pos) || ((scrollStart > pos) && (actual <= pos)) || ((scrollStart < pos) && (actual >= pos))){
		if(pos==0 || pos == contentWidth - contentFullWidth){
			//content.style.marginLeft = pos + "px";
			//setSliderPositionAccordingContent();
		}
		stopScrolling();
		return;
	}
	if(scrollSpeed == 0){
		scrollSpeed = (scrollStart > pos)?-2:2;
		done = 0;
	}
	else{
		done = 1-(Math.abs(actual-pos)/Math.abs(scrollStart-pos));
		scrollSpeed *= (done >= 0.5)?0.58:2;
		if(Math.abs(scrollSpeed) < 2)
			scrollSpeed *= 2;
	}
	//debug("actual:"+(actual+scrollSpeed)+" position:"+pos+" done:"+done);
	var shift = actual+scrollSpeed;
	if(((scrollStart > pos) && (actual+scrollSpeed <= pos)) || ((scrollStart < pos) && (actual+scrollSpeed >= pos)))
		shift = pos
	content.style.marginLeft = shift+"px";
	setSliderPositionAccordingContent();
	timer = setTimeout("scrollToPosition("+pos+")",100);	
}

function stopScrolling(){
	clearTimeout(timer);
	scrollStart = null;
	scrollSpeed = 0;	
}

function debug(text){
	debugInfo.innerHTML = text;
}

function setSpeedCoef(evt){ //funkcia zistuje ako daleko od okraju je kurzor a podla toho dynamicky zvacsuje alebo zmensuje rychlost posuvania
 	var position = getMouseX(evt)-findPosX(scrollArea);
 	if(position < scrollWidth)
 		speedCoef = (scrollWidth - position) / scrollWidth;
 	if(position > (contentWidth - scrollWidth))
 		speedCoef = (contentWidth - position - scrollWidth) / scrollWidth;	 	
}

function centerSlider(e){
	evt = e || window.event; 
	if( evt.preventDefault ) { evt.preventDefault(); }
	evt.returnValue = false;
	if(mousedown){
		var position = getMouseX(evt)-findPosX(scrollBar);
		var sliderWidth = getSliderWidth();
		var margin = (position - (sliderWidth/2));
		if(margin < 0)
			margin = 0;
		if(margin > contentWidth - sliderWidth)
			margin = contentWidth - sliderWidth;	
		slider.style.marginLeft = margin+"px";
		clearTimeout(timer2);
		slideContent();
	}
}

function mouseDown(evt){
	mousedown = true;
	centerSlider(evt);
}

function mouseUp(){
	mousedown = false;
}

function getSliderPosition(){
	return parseInt(slider.style.marginLeft);
}

function getSliderWidth(){
	return parseInt(slider.style.width);
}

function slideContent(){
	var percentage = getSliderPosition()/(contentWidth-getSliderWidth());
	scrollTo(percentage);
}

//posunie slider podla toho, ako je posunuty obsah 
function setSliderPositionAccordingContent(){
	var percentage = Math.abs(parseInt(content.style.marginLeft))/(contentFullWidth - contentWidth);
	var newpos = (contentWidth-getSliderWidth())*percentage;
	slider.style.marginLeft = newpos+"px";
}

function scrollGalleryToPosition(position){
	var tbls = content.getElementsByTagName("table");
	var row = tbls[0].rows[0];
	var offset = 0;
	for(i=0; i < position; i++){
		offset += row.cells[i].offsetWidth;
	}
	imgs = row.cells[position].getElementsByTagName("img");
	offset += imgs[0].offsetWidth/2;
	stopScrolling();
	scrollToPosition((contentWidth/2)-offset);
}

//funkcia, ktora ma na starosti pomale posuvanie obrazkov
function thumbSlideshow(){
	if(!slider)
		return;
	slideshowShift += slideshowIncrement;
	if(slideshowShift >= 0){
		slideshowIncrement *= -1;
		slideshowShift = -1;
	}
	if(slideshowShift <= contentWidth-contentFullWidth){
		slideshowIncrement *= -1;
		slideshowShift = contentWidth-contentFullWidth+1;
	}	
	content.style.marginLeft = slideshowShift+"px";
	setSliderPositionAccordingContent();
	timer2 = setTimeout("thumbSlideshow()",100);
}