var isIE = false;
var req;
var images = new Array;
var selectedIndex = 0;
var mouseX;

//------------------------------------------------------------------------------------------------------------------------------

function gallery() {
	var StartPoint = document.getElementById("thumbnails"); 
	var Slike = StartPoint.getElementsByTagName("img");
	for (var i=0; i<Slike.length; i++) {
		var imgsrc = Slike[i].src;							// izvuci SRC iz html-a
		var caption = Slike[i].title;						// izvuci TITLE iz html-a
		var description = Slike[i].alt;						// izvuci ALT iz html-a
		var Dot = imgsrc.indexOf('.thumb.jpg');				// 
		var Dir = imgsrc.indexOf('/Arte/');					//
		var id = imgsrc.substring(Dir+6,Dot);				// ostane samo broj
		var thumbsrc = imgsrc.substring(0,Dot) + ".thumb.jpg";
		var imgsrc = imgsrc.substring(0,Dot) + ".jpg";
		var image = new Array(id,caption,imgsrc,thumbsrc,description);
		images[i] = image;									// global, posto fali 'var'
	}
	prepareThumbs();
}

function prepareThumbs() {
	var StartPoint = document.getElementById("thumbnails"); 
	var thumbLI = StartPoint.getElementsByTagName('LI');
	for (var i=0; i<thumbLI.length; i++) {
		thumbLI[i].onclick = function () {
			var thumbsrc = this.firstChild.firstChild.getAttribute('src');
			var Dot = thumbsrc.indexOf('.thumb.jpg');
			var Dir = thumbsrc.indexOf('/Arte/');
			var thumbid = thumbsrc.substring(Dir+6,Dot);
			selectedIndex = getReference(thumbid);			// global, posto fali 'var'
			swapImage();
			return false;
		}
	}
}

function getReference(thumbid) {
	for (var i=0; i<images.length; i++) {
		var imgsrc = images[i][2];
		var Dot = imgsrc.indexOf('.jpg');
		var Dir = imgsrc.indexOf('/Arte/');
		var imgid = imgsrc.substring(Dir+6,Dot);
		if(thumbid === imgid) {
			return i;
		}
	}
	return false;
}

function swapImage() {
	var bigimage = document.getElementById('mainimage');
	setOpacity(bigimage,0);
	bigimage.setAttribute('src', images[selectedIndex][2]);
	bigimage.setAttribute('alt', images[selectedIndex][4]);
	bigimage.setAttribute('title', images[selectedIndex][1]);

	var bigimagelink = document.getElementById('mainimagelink');
	bigimagelink.setAttribute('href', images[selectedIndex][2]);

	document.getElementById('galleryimage').className = 'disc';
	bigimage.onload = function () {		//kill the animated disc here
		document.getElementById('galleryimage').className = 'nodisc';
		fadeIn('mainimage',0);
	};
	// captions
	var headers = document.getElementsByTagName('h2');
	headers[0].removeChild(headers[0].firstChild);
	var newheader = document.createTextNode(images[selectedIndex][1])
	headers[0].appendChild(newheader);
	// description
	var DES = document.getElementById("description");
	DES.innerHTML = images[selectedIndex][4];
	// thumbnails
	var thumbs = document.getElementsByTagName('LI');
	for (var i=0; i<thumbs.length; i++) {
		if(thumbs[i].firstChild.className == 'selected') {
			thumbs[i].firstChild.className = '';
		}
	}
	for (var i=0; i<thumbs.length; i++) {
		if(i == selectedIndex) {
			thumbs[i].firstChild.className = 'selected';
		}
	}
}



function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
	return true;
}

//------------------------------------------------------------------------------------------------------------------------------

function slider()  {
	var StartPoint = document.getElementById("thumbnails"); 
	var listitems = StartPoint.getElementsByTagName('LI');
	var panel = document.getElementById('panel2');
	panel.style.width = (listitems.length * (150 + 4 + 24)) +"px"; // ukljucuje sirinu thumbnaila (150) + border (2px) + margin (12px)
	
	var timer = null;
	document.getElementById('panel1').onmouseover = function() {
		trackthemouse();
		timer = setInterval("movePanel()",10);
	}
	document.getElementById('panel1').onmouseout = function() {
		donttrackthemouse();
		clearInterval(timer);
	}

	var links = document.getElementsByTagName('A');
	for (var i=0; i<links.length; i++) {
		if(links[i].getAttribute('id') == "moveLeft") {
			var ref = this;
			links[i].timerleft = null;
			links[i].onmouseover = function() {
				ref.timerleft = setInterval("moveLeft(2)",10);
			}
			links[i].onmouseout = function() {
				clearInterval(ref.timerleft);
			}
		}
		if(links[i].getAttribute('id') == "moveRight") {
			var ref = this;
			links[i].timerright = null;
			links[i].onmouseover = function() {
				ref.timerright = setInterval("moveRight(2)",10);
			}
			links[i].onmouseout = function() {
				clearInterval(ref.timerright);
			}
		}
	}
}

function trackthemouse() {
	document.onmousemove = getMouseXY;
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE);
	function getMouseXY(e) {
		if (IE) {
			tempX = event.clientX + document.body.scrollLeft
		} else {
			tempX = e.pageX
		} 
	if (self.innerWidth) {
		frameWidth = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
	}
	else if (document.body) {
		frameWidth = document.body.clientWidth;
	}
	var windowMiddle = (frameWidth/2) +  180; // ovih 180 je koliko je panel pomeren u desno u odnosu na sam centar;
	if (tempX < 0) {
		tempX = 0;
	}
		mouseX = tempX - windowMiddle 
	}
}

function donttrackthemouse() {
	document.onmousemove = null;
}

function movePanel() {
	var distance = mouseX/30;
	if (distance < 0)	{
		distance = -(distance);
	}
	if (mouseX < 0) {
		moveRight(distance);
	}
	// If the mouse moves to the right, the photo will scroll to the left:
	if (mouseX > 0) {
		moveLeft(distance);
	}
}

function moveLeft(distance) {
	var panel1 = document.getElementById('panel1');
	var panel2 = document.getElementById('panel2');
	var panel1width = parseInt(panel1.offsetWidth);
	var panel2width = parseInt(panel2.offsetWidth);
	var panel2X = panel2.offsetLeft;
	var limit = 0 - panel2width + panel1width;
	if(panel2X <= 0 && panel2X > limit) {
		panel2X  = panel2X - distance;
		panel2.style.left = panel2X +"px";
		if(panel2X > limit && panel2X <= limit + distance) {
			panel2X  = limit;
			panel2.style.left = panel2X +"px";
		}
	}
}

function moveRight(distance) {
	var panel2 = document.getElementById('panel2');
	var panel2X = panel2.offsetLeft;
	if(panel2X >= 0 - distance) {
		panel2X  = panel2X + distance;
		panel2.style.left = 0 +"px";
	}
	if(panel2X < 0 ) {
		panel2X  = panel2X + distance;
		panel2.style.left = panel2X +"px";
	}
}

//------------------------------------------------------------------------------------------------------------------------------

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//addLoadEvent(addSlideshow);
addLoadEvent(gallery);
addLoadEvent(slider);