window.onclick = setFocusedElement;
window.onfocus = checkDateElement;
this.typeOf = function(t) {
	return String(t.constructor).split(" ")[1].split("()").join("");
};

var diacriticaVisible = false;
var helpVisible = false;
var currentFormVisible = false;

	function setFocusedElement(_event) {
		if(_event.srcElement) {
			fElement = _event.srcElement;
		} else {
			fElement = _event.target;
		}
		if(fElement.type == "text") {
			deleteUnknown(fElement);
			focusedElement = fElement;
			showDiacriticaWindow();
		}
	}
	
	function checkDateElement(_event) {
		if(_event.srcElement) {
			fElement = _event.srcElement;
		} else {
			fElement = _event.target;
		}
		//alert(fElement);
		deleteUnknown(fElement);
	}
	
	function deleteUnknown(_elmnt) {
		if(_elmnt.type == "text") {
			if(_elmnt.name.match(/Date/ig)) {
				if(!_elmnt.value.match(/^\d/)) {
					_elmnt.value = "";
				}
			}			
		}
	}

	function toggleVisibility(_objName, _caller) {
		_obj = document.getElementById(_objName);
		_img = _caller.firstChild;
		oldSource = _img.src;
		if(!currentFormVisible){
			newSource = oldSource.replace(/\.png/, "_active.png");
			_img.src = newSource;
			currentFormVisible = true;
		} else {
			newSource = oldSource.replace(/_active\.png/, ".png");
			_img.src = newSource;
			if(diacriticaVisible) {
				hideDiacriticaWindow();
			}
			currentFormVisible = false;
		}
		Effect.toggle(_objName, 'slide', {duration: 0.3});
	}
	
	function toggleVisibility(_objName) {
		if(!currentFormVisible) {
			currentFormVisible = true;
		} else {
			currentFormVisible = false;
		}
		Effect.toggle(_objName, 'slide', {duration: 0.3});
	}
	
	
	function toggleWindow(_heading, _text) {
		helpWindow = document.getElementById("helpText");
		helpContent = document.getElementById("helpContent");
		helpHeading = document.getElementById("helpHeadingText");
		helpContentText = document.createTextNode(_text);
		helpHeading.firstChild.data = _heading;
		var textChanged = false;
		if(helpContent.hasChildNodes()) {
			oldHelpContent = helpContent.firstChild.data;
			if(oldHelpContent != _text) {
				helpContent.replaceChild(helpContentText, helpContent.firstChild);
				if(helpVisible){
					Effect.Appear('helpContent', {duration: 0.2});
					textChanged = true;
				}
			}
		} else {
			helpContent.appendChild(helpContentText);
		}
		if(!textChanged) {
			Effect.toggle('helpText', 'appear', {duration: 0.3});
			helpVisible = !helpVisible;
		}
	}
	
	function showDiacriticaWindow() {
		if(!diacriticaVisible) {
			Effect.toggle('diacriticaInput', 'appear', {duration: 0.3});
		}
		diacriticaVisible = true;
	}
	
	function hideDiacriticaWindow() {
		if(diacriticaVisible) {
			Effect.toggle('diacriticaInput', 'appear', {duration: 0.3});
		}
		diacriticaVisible = false;
	}
	
	function addDiacriticaChar(_charCode) {
		var cursorPos = focusedElement.selectionStart;
		var before = focusedElement.value.substring(0, cursorPos);
		var after = focusedElement.value.substring(cursorPos);
		focusedElement.value = before + String.fromCharCode(_charCode) + after;
		focusedElement.focus();
	}
	
	
	function confirmDelete(_delFormName, _text) {
		deleteTopic = confirm(_text);
		if(deleteTopic) {
			document.getElementById(_delFormName).submit();
		}
	}
	
	function comparePasswords(_form, _text) {
		password = _form.elements["password"].value;
		rewrittenPassword = _form.elements["rewritePassword"].value;
		if(password != rewrittenPassword){
			alert(_text);
			return false;
		}
		return true;
	}
	
	function popup_screencast(_doc) {
		win = window.open("../inc/screencast.php?movie="+_doc, "_blank", "innerHeight=730, innerWidth=870, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no, top=100, left=100, screenX=0, screenY=0");
		win.focus();
	}
	
	function showOnMap(address) {
	    Effect.Appear('mapsContainer', {to: 0.5});
	    Effect.Appear('mapsWindow');
		window.setTimeout('showAddressPlace("'+address+'")', 800);
	}
	
	function showPersonMap() {
		Effect.Appear('mapsPersonContainer', {to: 0.5});
		Effect.Appear('mapsPersonWindow');
	}
	
	function showPersonOnMap(bplace, bpcaption, dplace, dpcaption) {
		window.setTimeout('showAddressPerson("'+bplace+'", "'+bpcaption+'")', 800);
		window.setTimeout('addAddressPerson("'+dplace+'", "'+dpcaption+'")', 1600);
	}
	
	function closeMap() {
		Effect.Fade("mapsWindow");
		Effect.Fade("mapsContainer");
	}

	function closePersonMap() {
		Effect.Fade("mapsPersonWindow");
		Effect.Fade("mapsPersonContainer");
	}
	
	//Tree functions
	
	function expandTree(dateName, clickedObject) {
		document.body.style.cursor = "wait";
		var spanName = "trSubtree" + dateName;
		var subtree = document.getElementById(spanName);
        clickedObject.src = "../../gfx/signets/foldTree.png";
        clickedObject.setAttribute("onclick", 'foldTree("'+dateName+'", this)');
		if(subtree && subtree.hasChildNodes()) {
			document.body.style.cursor = "auto";
			new Effect.Appear(spanName);
		} else {
			createSubtree(dateName, clickedObject);
		}	
	}
	
	function createSubtree(dateName, clickedObject) {
			spanName = "subtree"+dateName;
			cObject = clickedObject;
			var url = '../../pages/inc/subtree.php?name='+dateName;
			new Ajax.Request(url, {onSuccess : processDataRequest});
	}

	function processDataRequest(req) {
        //schreibe die antwort in den div container mit der id content
        var table = cObject.parentNode.parentNode.parentNode.parentNode;
        var resp = req.responseText;
        var rows = resp.split("###");
        //alert(resp);

        var spanNode = document.createElement("div");
        spanNode.setAttribute("id", spanName);
        
        
        var currentTrNode = cObject.parentNode.parentNode.parentNode;
        table.insertBefore(spanNode, currentTrNode.nextSibling);
        
        for(var i=0;i<rows.length;i++) {
        	var entries = rows[i].split("|");
        	var children = entries[0];
        	var text = entries[1];
        	var id = entries[2];
            var newTrNode = document.createElement("tr");
            var newTdNode = document.createElement("td");
            var newANode = document.createElement("a");
            var newDiv = document.createElement("div");
            var aText = document.createTextNode(text);
            
            newTrNode.setAttribute("id", text);
            
            newANode.appendChild(aText);
            newANode.setAttribute("class", "dateLink");
            newANode.setAttribute("href", "../individual/date.php?id="+id);
            
            newDiv.setAttribute("class", "expandTree");
            /*if(children == 1) {
            	var newImgNode = document.createElement("img");
            	newImgNode.setAttribute("style", "cursor: pointer");
            	newImgNode.setAttribute("src", "../../gfx/signets/add.png");
            	newImgNode.setAttribute("onclick", 'expandTree("'+text+'", this)');
            	newDiv.appendChild(newImgNode);
            }*/
            
            newTrNode.appendChild(newTdNode);
            newTdNode.appendChild(newDiv);
            newTdNode.appendChild(newDiv);
            newTdNode.appendChild(newANode);
            
            
            spanNode.appendChild(newTrNode);
        }
		document.body.style.cursor = "auto";
		new Effect.Appear(spanName);
	}


	function foldTree(name, clickedObject) {
		document.body.style.cursor = "wait";
		clickedObject.src = "../../gfx/signets/expandTree.png";
		clickedObject.setAttribute("onclick", 'expandTree("'+name+'", this)');
		var spanName = "subtree" + name;
		new Effect.Fade(spanName, {duration : 0.5});
		document.body.style.cursor = "auto";
	}