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

function checkRC() {
	var rcDiv = null;
	rcDiv = document.getElementById("rc");
	if(rcDiv == null) {
		return true;
	} else {
		collapseall();
		expandFirst();
	}
}

function checkMedia() {
	var albumTable = null;
	albumTable = document.getElementById("piclist");
	if(albumTable == null) {
		return true;
	} else {
		initGallery();
	}
}

/*************************************************
 Beginning of GalleryCode
**************************************************/

function initGallery() {
	albumTable = document.getElementById("piclist");
	var links = albumTable.getElementsByTagName("a");
	for (var i=0;i<links.length; i++) {
		links[i].onclick = function() {
			getPic(this.getAttribute("href"));
			return false;
		}
	}
}

function getPic(link) {
	var ImageId = link.substr(link.lastIndexOf("=")+1,link.length);
	var url = "getImage.php?ImageId=" + ImageId;

	request.open("GET",url,true);
	request.onreadystatechange = showPic;
	request.send(null);
}

function showPic() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var imageUrl = request.responseText;
			imageUrl = "/typera"+imageUrl;
			var imageDiv = document.createElement("div");
			imageDiv.setAttribute("id","picture");
			var closeLink = document.createElement("a");
			closeLink.setAttribute("href","");
			var imageSrc = document.createElement("img");
			imageSrc.setAttribute("src",imageUrl);
			closeLink.appendChild(imageSrc);
			closeLink.onclick = function() {
				closePic();
				return false;
			}
			var imageTxt = document.createTextNode("Klicka på bilden för att stänga den.");
			imageDiv.appendChild(imageTxt);
			imageDiv.appendChild(closeLink);
			
			document.getElementById("lc").appendChild(imageDiv);

			albumTable = document.getElementById("piclist");
			var links = albumTable.getElementsByTagName("a");
			for (var i=0;i<links.length; i++) {
				links[i].onclick = function() {
					return false;
				}
			}
		} else {
			alert("Request Status is "+request.status);
		}
	}
}

function closePic() {
	var lc = document.getElementById("lc");
	lc.removeChild(document.getElementById("picture"));
	initGallery();
}

/*************************************************
Collapsable menu by Stefan Falk
**************************************************/

function collapseall() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	var divs = document.getElementsByTagName("div");
	for (var i=0; i<divs.length; i++)
	{
		var currentClass = divs[i].className;
		
		// Assign an ID to each "rcwrapper" and collapse it by setting the height to 0px
		if (currentClass == 'rcwrapper') {
			divs[i].setAttribute("id",i);
			divs[i].style.height = '0px';
			
			// Move the footer and change the CSS so that it's visible during animation
			var numNodes = divs[i].childNodes.length;
			var footer = divs[i].childNodes[numNodes-1];			
			if (footer.nodeType == 3) {								// Fix for Firefox' "bug" with linebreaks being Nodes in the DOM
				var footer = divs[i].childNodes[numNodes-2];
				
				var newFooter = footer.cloneNode(true);
				divs[i].removeChild(footer);
				divs[i].insertBefore(newFooter,divs[i].childNodes[0]);
				
				newFooter.className = "newFooter";
			}
		
			// Create the links for the different topics
			var link = document.createElement("a");
			var linktext = document.createTextNode(divs[i-1].childNodes[0].nodeValue);
			link.setAttribute("href","#");
			link.appendChild(linktext);
			
			// Connect the Onclick function to the links
			link.onclick = function() {
				var elem = this.parentNode.nextSibling;						// Fix for differences between Firefox...
				if (elem.nodeType != "1") {									// .. and ..
					var elem = this.parentNode.nextSibling.nextSibling;		// .. Internet Explorer, again!
				}
				if (elem.style.height == '0px') {
					expandDiv(elem.getAttribute("id"));
				}
				return false;
			}
			divs[i-1].replaceChild(link,divs[i-1].childNodes[0]);
		}
	}
}

function expandDiv(divId) {
	var divName = document.getElementById(divId);
	if (divName.movement) {
		clearTimeout(divName.movement);
	}
	allDivs = getAllDivs();
	for (i=0; i < allDivs.length; i++) {
		if (allDivs[i] == divId) {
			var divHeight = parseInt(divName.style.height);
			var maxHeight = '150';
			if (divHeight == maxHeight) {
				return true;
			}
			if (divHeight < maxHeight) {
				var dist = Math.ceil((maxHeight - divHeight)/10);
				divHeight = divHeight + dist;
			}
			divName.style.height = divHeight + 'px';
			var repeat = "expandDiv('"+divId+"')";
			divName.movement = setTimeout(repeat,1);
		}
		else
		{
			collapseDiv(allDivs[i]);
		}
	}
}

function collapseDiv(divId) {
	var divName = document.getElementById(divId);
	if (divName.movement) {
		clearTimeout(divName.movement);
	}
	var divHeight = parseInt(divName.style.height);
	var minHeight = '0';
	if (divHeight == minHeight) {
		return true;
	}
	if (divHeight > minHeight) {
		var dist = Math.ceil((divHeight - minHeight)/10);
		divHeight = divHeight - dist;
	}
	divName.style.height = divHeight + 'px';
	var repeat = "collapseDiv('"+divId+"')";
	divName.movement = setTimeout(repeat,100);
}

function getAllDivs() {
	if(!document.getElementsByTagName) return false;
	var allDivs = Array();
	var divs = document.getElementsByTagName("div");
	var x=0;
	for (var i=0; i<divs.length; i++)
	{
		var currentClass = divs[i].className;
		if ( currentClass == 'rcwrapper') {
			allDivs[x] = divs[i].getAttribute("id");
			x++;
		}
	}
	return allDivs;
}

function expandFirst() {
	allDivs = getAllDivs();
	expandDiv(allDivs[0]);
}

addLoadEvent(checkRC);
addLoadEvent(checkMedia);


