/*
Copyright (C) 2008 Rauli Haverinen. <troubadour@raulihaverinen.fi>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* GENERAL HELPER
--------------------------------- */

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

/* // TODO IEHACK
onload=function(){
if (document.getElementsByClassName == undefined) {
	document.getElementsByClassName = function(className)
	{
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = document.getElementsByTagName("*");
		var results = [];

		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}

		return results;
	}
}
*/

/* ORGANIZATION QUERY 
--------------------------------- */

// method from http://satya61229.blogspot.com/2007/04/dynamically-populate-select-list-by.html

function GetXmlHttpObject() { //handler) {
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById(lvl).innerHTML= xmlHttp.responseText;
	}
}

// Will populate data based on input
function htmlData(url, check, qStr) {
	lvl = "lvl"+(eval(check.charAt(3))+1);
	if (url.length==0) {
		document.getElementById(lvl).innerHTML=""; 
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	url=url+"&"+check+"&"+qStr; // url=url+"?"+check+"&"+qStr; 
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true) ;
	xmlHttp.send(null);
}

/* MEDIA PLAYER
--------------------------------- */

function playPop(url) {
	var NewWindow = window.open(url, 'playpop', 'width=710, height=250, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}
function presentationPop(url) {
	var NewWindow = window.open(url, 'presentationpop', 'width=900, height=700, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}
function customPop(url,width,height) {
	var NewWindow = window.open(url, 'attachmentpop', 'width='+width+', height='+height+', menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}

/* SHOW/HIDE ITEMS
--------------------------------- */

function showItem(id) {
	document.getElementById(id).setAttribute("style", "display:block");
}
function showClass(classes) {
	if (!document.getElementsByClassName) return false;
	var select = document.getElementsByClassName(classes);
	for(var i = 0; i<select.length; i++) { // TODO bug: changes every second ???
		select[i].className='alt_'+classes; //style.display="block";
	}
}
function changeClass(id, newClass) {
	var identity=document.getElementById(id);
	identity.className=newClass;
}

function delElement(id) {
	var element = document.getElementById(id);
	var elgone = (element.parentNode).removeChild(element);
}
function markRemove(id) {
	var element = document.getElementById(id);
	element.parentNode.removeChild(element);
//	element.innerHTML = "<span class=\"info\">Submit form to save changes.</span>";
//	element.removeAttribute("id");
	// element.parentNode.removeChild(element);
}
/*
function markRemove(id) {
	var element = document.getElementById(id);
	if(removeAllChilds(id)) {
		
		// new row content
		var firsttd = document.createElement("td").appendChild(document.createTextNode("Submit form to save changes."));
	
		// ability to remove afterwards
		var sectd = document.createElement("td");
	
		// add to page
		element.appendChild(firsttd);
		element.appendChild(sectd);

		element.removeAttribute("id");
	// element.parentNode.removeChild(element);
	}
}*/

function removeAllChilds(id) {
	var removeunder = document.getElementById(id);
	if(removeunder.hasChildNodes()) {
		while(removeunder.childNodes.length > 0) {
			removeunder.removeChild(removeunder.firstChild);
		}
	}
}

function timeNow() {
	var get_time = new Date();
	var v = get_time.getYear()
	var v = v + 1900;
	var kk = get_time.getMonth()
	var kk = kk + 1;
	var kk = kk + "";
	if (kk.length == 1) { kk = "0" + kk; }
	var pv = get_time.getDate()
	var pv = pv + "";
	if (pv.length == 1) { pv = "0" + pv; }
	var h = get_time.getHours()
	var h = h + "";
	if (h.length == 1) { h = "0" + h; }
	var m = get_time.getMinutes()
	var m = m + "";
	if (m.length == 1) { m = "0" + m; }
	var s = get_time.getSeconds()
	var s = s + "";
	if (s.length == 1) { s = "0" + s; }
	var time_now = (v + "-" + kk + "-" + pv + " " + h + ":" + m + ":" + s);
	return time_now;
}

/* STRUCTURE SPECIFIC
--------------------------------- */

function prepareSitemapLink() {
	if (!document.getElementById("sitemaplink")) return false;
	var sitemap_link = document.getElementById("sitemaplink");

	sitemap_link.onclick = function() {
		var sidebar_structure = document.getElementById("sidebar");
		var scructure_links = sidebar_structure.getElementsByTagName("li");
		for ( var i=0; i < scructure_links .length; i++ ) {
			if(scructure_links[i].getAttribute("class") != "structureopen" && scructure_links[i].getAttribute("class") != "pagelink" && scructure_links[i].getAttribute("class") != "pageopen")
				scructure_links[i].setAttribute("class","sitemapopen");
		}
		prepareRemoveSitemapLink();
		return false;
	}
}
function prepareRemoveSitemapLink() {
	if (!document.getElementById("sitemaplink")) return false;
	var sitemap_link = document.getElementById("sitemaplink");
	sitemap_link.onclick = function() {
		var sidebar_structure = document.getElementById("sidebar");
		var scructure_links = sidebar_structure.getElementsByTagName("li");
		for ( var i=0; i < scructure_links .length; i++ ) {
			if(scructure_links[i].getAttribute("class") != "structureopen"&& scructure_links[i].getAttribute("class") != "pagelink" && scructure_links[i].getAttribute("class") != "pageopen")
				scructure_links[i].setAttribute("class","structurehidden");
		}
		prepareSitemapLink();
		return false;
	}
}

addLoadEvent(prepareSitemapLink);
