// variables
var mouseover = false;
var scrolldirection;
var scrollratio;
var theheartbeat, scrolling;
var menuOffsetTop = menuOffsetLeft = menuMaxHeight = 0;
var currentX = currentY = 0;
var whichIt = null;
var MSeventSrc = null;

// setting event handlers
document.onmousedown = grabIt;
document.onmousemove = moveIt;
document.onmouseup = dropIt;
document.onmousewheel = mouseScroll;

// initialising image objects for scrolling arrows
arrow_up_inactive = new Image();
arrow_up_inactive.src = 'imgs/scroller/arrow_up.gif'
arrow_up_active = new Image();
arrow_up_active.src = 'imgs/scroller/arrow_up_active.gif';
arrow_down_inactive = new Image();
arrow_down_inactive.src = 'imgs/scroller/arrow_down.gif';
arrow_down_active = new Image();
arrow_down_active.src = 'imgs/scroller/arrow_down_active.gif';

function switchit(obj, cmd){
	if (cmd == 'in'){
		mouseover = true;
		if (obj.getAttribute('id') == "scrollerScrollup"){
			scrolldirection = -1;
		}
		else{
			scrolldirection = 1;
		}
	}
	else{
		mouseover = false;
	}
}

function scroll(){
	if (((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - document.getElementById('scrollerScrollbox').offsetHeight))){
		document.getElementById('scrollerScrollbox').style.top = (parseInt(document.getElementById('scrollerScrollbox').offsetTop) + scrolldirection)+'px';
	}
}

function heartBeat(){
	var newPos = parseInt(-1 * scrollratio * (document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop - menuOffsetTop));
	if (!isNaN(newPos)){
		document.getElementById('scrollerData').style.top = newPos+'px';
	}
}

function grabIt(e){
	if (window.event) { // dla IE
		if (event.button == 1){
			if (mouseover) {
				scrolling = window.setInterval("scroll();",1);
			} else {
				whichIt = event.srcElement;
				while (whichIt.id.indexOf("scrollerScrollbox") == -1){
					whichIt = whichIt.parentElement;
					if (whichIt == null){
						return true;
					}
				}
				currentX = (event.clientX + document.body.scrollLeft);
				currentY = (event.clientY + document.body.scrollTop);
			}
			theheartbeat = window.setInterval("heartBeat()",1);
		}
	} else { // dla nie-IE :)
		if (mouseover) {
			scrolling = window.setInterval("scroll();",1);
		} else {
			if (e.target.id == 'scrollerHandler') {
				whichIt = e.target;
			} else {
				whichIt = null;
			}
			currentX = (e.clientX + window.scrollX);
			currentY = (e.clientY + window.scrollY);
		}
		theheartbeat = window.setInterval("heartBeat()",1);
	}
}

function moveIt(e){
	if (whichIt == null){
		return false;
	}
	if (window.event){ // dla IE
		newX = (event.clientX + document.body.scrollLeft);
		newY = (event.clientY + document.body.scrollTop);
		distanceX = (newX - currentX);
		distanceY = (newY - currentY);
		currentX = newX;
		currentY = newY;
		if (((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - document.getElementById('scrollerScrollbox').offsetHeight))){
			document.getElementById('scrollerScrollbox').style.top = (parseInt(document.getElementById('scrollerScrollbox').offsetTop) + distanceY)+'px';
		}
		event.returnValue = false;
	} else { // dla nie-IE
		newX = (e.clientX + window.scrollX);
		newY = (e.clientY + window.scrollY);
		distanceX = (newX - currentX);
		distanceY = (newY - currentY);
		currentX = newX;
		currentY = newY;
		if (((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - document.getElementById('scrollerScrollbox').offsetHeight))){
			document.getElementById('scrollerScrollbox').style.top = (parseInt(document.getElementById('scrollerScrollbox').offsetTop) + distanceY)+'px';
		}
	}
	return false;
}

/*
	Funkcja obsugująca scrollowanie kółkiem myszki w IE
*/

function mouseScroll() {
	var MSeventSrc = event.srcElement;
	while (MSeventSrc.id.indexOf("scrollerContent") == -1 && MSeventSrc.id.indexOf("scrollerFloater") == -1){
		MSeventSrc = MSeventSrc.parentElement;
		if (MSeventSrc == null){
			return true;
		}
	}
	if (window.event.wheelDelta >= 120) {
		distanceY = -10;
	} else if (window.event.wheelDelta < 120) {
		distanceY = 10;
	}
	if (((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((document.getElementById('scrollerScrollbox').offsetTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - document.getElementById('scrollerScrollbox').offsetHeight))){
		document.getElementById('scrollerScrollbox').style.top = parseInt(document.getElementById('scrollerScrollbox').style.top) + distanceY;
	}
	heartBeat();
}

function dropIt(){
	clearInterval(scrolling);
	clearInterval(theheartbeat);
	whichIt = null;
}

function scroller_init(){
	menuOffsetTop = document.getElementById('scrollerFloater').offsetTop;
	menuMaxHeight = document.getElementById('scrollerFloater').offsetHeight;
	getscrollinc();
}

function getscrollinc(){
	var contentinc;
	var scrollerinc;
	var contentHeight = parseInt(document.getElementById('scrollerData').offsetHeight);
	var scrollerHeight = document.getElementById('scrollerContent').offsetHeight;
	if (scrollerHeight < contentHeight){
		contentinc = parseInt(document.getElementById('scrollerContent').offsetHeight) / contentHeight;
		scrollerinc = parseInt(document.getElementById('scrollerFloater').offsetHeight) * contentinc;
		scrollratio = contentHeight / document.getElementById('scrollerFloater').offsetHeight;
		document.getElementById('scrollerFloater').style.visibility = 'visible';
	} else {
		// Nie ma potrzeby scrollowania -- ukrywamy scrollery
		document.getElementById('scrollerFloater').style.visibility = 'hidden';
		document.getElementById('scrollerScrollup').style.visibility = 'hidden';
		document.getElementById('scrollerScrolldown').style.visibility = 'hidden';
		document.getElementById('scrollerHandler').style.visibility = 'hidden';
		scrollerinc = document.getElementById('scrollerFloater').offsetHeight;
		contentinc = 1;
	}
	document.getElementById('scrollerScrollbox').style.height = parseInt(scrollerinc)+'px';
	document.getElementById('scrollerHandler').style.height = parseInt(scrollerinc - 2)+'px';
}

/*
	Funkcja podmieniająca obrazki na strzałkach scrollujących
*/

function setImgSrc( oObject,oNewSrc ) {
	if (oObject && oNewSrc) {
		oObject.src = oNewSrc.src;
	}
}

/*
	Funkcja przewija content scrollera na sam± górę
*/

function goUp() {
	document.getElementById('scrollerScrollbox').style.top = '0px';
	document.getElementById('scrollerData').style.top = '0px';
}

/*
	Funkcja emuluje działanie kotwic w obrębie scrollera
*/

function goToAnchor() {
	/*
		Odczytanie i evalowanie zmiennych z GETa.
		UWAGA! Wszystkie zmienne są evalowane do STRINGów
	*/
	varsArr = window.location.search.substr(1).split('&');
	if (varsArr.length >= 1 && varsArr[0] != ''){
		for (var i=0;i<varsArr.length;i++) {
			eqIndex = varsArr[i].indexOf("=");
			eval('var '+varsArr[i].substring(0,eqIndex)+' = "'+varsArr[i].substr(eqIndex+1)+'";');
		}
	}
	/*
		Skok do kotwicy następuje, jeśli jest w GETcie ustawiona zmienna 'aID' i jeśli w 
		dokumencie istnieje element o ID == aID
	*/
	if (typeof(aID) != 'undefined' && document.getElementById(aID)) {
		var contentH = document.getElementById('scrollerData').offsetHeight; // wysokość całego scrollowanego contentu
		var viewPortH = document.getElementById('scrollerContent').offsetHeight; // wysokość widocznego obszaru
		var anchorTop = document.getElementById(aID).offsetTop; // pozycja kotwicy od góry warstwy-rodzica
		if (anchorTop < (contentH - viewPortH)) { // kiedy kotwica nie znajduje się w obszarze ostatniego ekranu
			document.getElementById('scrollerData').style.top = - anchorTop+'px'
		} else { // kiedy kotwica jest na ostatnim ekranie
			document.getElementById('scrollerData').style.top = - (contentH - viewPortH) + 'px';
		}
		/*
			Ustawienie scrollhandlera w odpowiednim miejscu
		*/
		document.getElementById('scrollerScrollbox').style.top = (document.getElementById('scrollerData').offsetTop / (-1 * scrollratio))+'px';
	};
}
