google.load("prototype", "1.6");
google.load("scriptaculous", "1.8.1");
google.load("maps", "2");
window.onerror = function() { return true; }

function ajaxLoad(url, id, clbk) {
	var xhr = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
	xhr.open('GET', url, true);
	xhr.onreadystatechange = function() {
		if(xhr.readyState === 4) {
			var el = document.getElementById(id);
			if(el != null) el.innerHTML = xhr.responseText;
			if(clbk) setTimeout(clbk,100);
		}
	};
	xhr.send(null);
}

var currURL = '';
function loadParam() {
	setTimeout(loadParam,500);
	var url = window.location.href.toLowerCase();
	if(url == currURL) return;

	currURL = url;
	var p = url.indexOf('#');
	var com = url.substring(p+1);
	if(com.length > 0) {
		if(com == 'about') loadPages(com, 1);
		else if(com == 'cv') loadPages(com, 2);
		else if(com == 'thesis') loadPages(com, 3);
		else if(com == 'projects') loadPages(com, 4);
		else if(com == 'poems') loadPages(com, 5, togglePoems);
		else if(com == 'map') loadPages(com, 6, mapLoad);
		else if(com == 'videomap') loadPages(com, 7, mapVideoLoad);
		else clearPage();
	}
}

function openContent(link) {
	try	{	ajaxLoad("content/"+link, "ContentDIV");	}	catch(err){}
}

var gCurrentImg = 0;
var aPredragImg = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var gPredragImg;
var gCurrentImgID = -1;

var gContentDIV;
var gSubContentDIV;
var gPredragImgDIV;

function setUp() {
	gPredragImgDIV = document.getElementById("PredragImgDIV");
	gContentDIV = document.getElementById("ContentDIV");
	gSubContentDIV = document.getElementById("SubContentDIV");

	gContentDIV.innerHTML = "";
	gSubContentDIV.innerHTML = "";
	Effect.Fade("ContentDIV", {duration: 0.01});
	Effect.Fade("SubContentDIV", {duration: 0.01});

	fisherYates(aPredragImg);
	setImage(0);
	setTimeout(loadParam,50);
}

function clearPage() {
	resetLinks();
	setImage(0);
	Effect.Fade("ContentDIV", {duration: 0.25});
	Effect.Fade("SubContentDIV", {duration: 0.25});
	Effect.Appear("BrowserInfoDIV", {duration: 0.25});

	gContentDIV.innerHTML = "";
	gSubContentDIV.innerHTML = "";
}

function loadPages(link, imgID, clbk) {
	resetLinks();
	setImage(imgID);
	ajaxLoad("content/"+link+"/sub.html", "SubContentDIV");
	ajaxLoad("content/"+link+"/index.html", "ContentDIV", clbk);

	Effect.Fade("BrowserInfoDIV", {duration: 0.25});
	Effect.Appear("ContentDIV", {duration: 0.25});
	Effect.Appear("SubContentDIV", {duration: 0.25});
	if(document.getElementById(link+"Link"))	document.getElementById(link+"Link").className = "NoLink";
}

function resetLinks() {
	if(document.getElementById("aboutLink"))	document.getElementById("aboutLink").className = "";
	if(document.getElementById("cvLink"))		document.getElementById("cvLink").className = "";
	if(document.getElementById("thesisLink"))	document.getElementById("thesisLink").className = "";
	if(document.getElementById("projectsLink"))	document.getElementById("projectsLink").className = "";
	if(document.getElementById("poemsLink"))	document.getElementById("poemsLink").className = "";
	if(document.getElementById("mapLink"))		document.getElementById("mapLink").className = "";
	if(document.getElementById("videomapLink"))	document.getElementById("videomapLink").className = "";
}
function setImage(imgID) {
	if(imgID != gCurrentImgID) {
		gCurrentImgID = imgID;
		gPredragImgDIV.innerHTML = '';
		var img = makeImage('img/smallPredrag' + aPredragImg[imgID] + '.png');
		gPredragImgDIV.appendChild(img);
		setTimeout("loadBigImage("+imgID+");", 250);
	}
}

function loadBigImage(imgID) {
	var img = document.getElementById("PredragImg");
	img.src = 'img/predrag' + aPredragImg[imgID] + '.jpg';
}

function fisherYates(myArray) {
	var i = myArray.length;
	if(i == 0) { return false; }
	while(--i) {
		var j = Math.floor(Math.random() * (i + 1));
		var tempi = myArray[i];
		var tempj = myArray[j];
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
	return true;
}

function makeImage(path) {
	var img = document.createElement('img');
	img.setAttribute('src', path);
	img.setAttribute('id', 'PredragImg');
	img.setAttribute('align', 'left');
	img.setAttribute('width', '270');
	img.setAttribute('height', '270');
	return img;
}
