// vars
var person2field = Array();
var allfields = Array();

// current status
var currentperson = false;
var currentpersonid = 0;

// click status
var clicked = false;
var clickedid = 0;
var clickedcolor = "#000000";
var clickedsub = false;
var clickedsubid = 0;


// personpic status
var personpicOpened = false;
// map status
var mapOpened = false;


// fade
function setOpacity(obj, value) {
	obj.style.opacity = value/10;
	obj.style.MozOpacity = value/10;	
	//obj.style.setProperty("-moz-opacity",value/10);
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}
function setOpacityId(objid, value) {
	obj = document.getElementById(objid);
	setOpacity(obj, value);
}


// open map
function mapOpen() {
	wMap = window.open('http://quikmaps.com/ext/35933?w=414&mh=391&t=1&ln=0&sn=1&zb=0&zs=0&d=1&it=0&icd=0&lat=47.422773977265095&lng=9.372475147247314&zl=16&mt=0', "Anfahrt", "width=416,height=416,location=no,menubar=no,resizable=yes,status=no,toolbar=no,scrollbars=no");
  	wMap.focus();
}

// personname
function personnameOver(personid) {
	showpersonpic(personid);
	setpersoncolor('person'+personid);
	if (currentperson==true && personid!=currentpersonid) {
		unsetpersoncolor('person'+currentpersonid);
	}
	if (person2field.length>0) {
		hideotherfields(person2field[personid]);
		hideothersubfields(person2field[personid])
	}
}
function personnameOut(personid) {
	showallpersonpics();
	if (currentperson==true && personid!=currentpersonid) {
		setpersoncolor('person'+currentpersonid);
	}
	showallfields();
}

// personpic
function personpicOpen(filename) {
	
	if (personpicOpened==false) {
		$('#loadondemand').createAppend(
			
			'img', { src: 'img/upload/articles/'+filename, width: '416', height: '416', border: '0px', onclick: 'personpicOpen("");' }
		);
		$('#loadondemand').show();
		personpicOpened = true;
		
	} else {
		$('#loadondemand').hide();
		$('#loadondemand' + ' img').remove();
		personpicOpened = false;
	}
}
function personpicOver(personid) {
	personnameOver(personid);
	
	/*if (currentperson==true && personid!=currentpersonid) {
		setOpacityId('personimg'+currentpersonid, 3); 
		unsetpersoncolor('person'+currentpersonid);
	}
	if (currentperson==false || personid!=currentpersonid) {
		hideallpersonpics();
		setOpacityId('personimg'+personid, 10); 
		setpersoncolor('person'+personid);
	}*/
}

function personpicOut(personid) {
	personnameOut(personid);
	
	/*if (currentperson==true && personid!=currentpersonid) {
		setOpacityId('personimg'+currentpersonid, 10); 
		setpersoncolor('person'+currentpersonid);
	}
	if (currentperson==false || personid!=currentpersonid) {
		showallpersonpics();
		unsetpersoncolor('person'+personid);
	}*/
}


// subfield onmouseout
function outSubfield(fieldid, parentid) {
	showallfields();
	showallpersonpics();
	unsetmenufieldcolor();
	
	if (clickedsub==true) { 
		showhidepersons(clickedsubid);
		document.getElementById("subfield"+clickedsubid).style.color = clickedcolor;
		document.getElementById("menufield"+clickedsubid).style.color = clickedcolor;
	}
	
	/*if (clickedsub==true && clickedsubid!=fieldid) { 
		showhidepersons(clickedsubid);
		document.getElementById("subfield"+clickedsubid).style.color = clickedcolor;
	} else if (clickedsub==false) {
		showhidepersons(fieldid); 
	}*/
}
// subfield onmouseover
function overSubfield(fieldid, parentid) {
	if (clickedsub==true && clickedsubid!=fieldid) { 
		document.getElementById("subfield"+clickedsubid).style.color = "";
		document.getElementById("menufield"+clickedsubid).style.color = "";
	}
	
	var parentids = new Array();
	parentids.push(parentid);
	hideotherfields(parentids);
	var fieldids = new Array();
	fieldids.push(fieldid);
	setmenufieldcolor(fieldids);
	hideothersubfields(fieldids);
	
	showhidepersons(fieldid); 
}
// subfield onclick
function toggleSubfield(fieldid) {
	if (clickedsub==true) {
		document.getElementById("subfield"+clickedsubid).style.color = "";
	}
	clickedsub = true;
	clickedsubid = fieldid;
	document.getElementById("subfield"+fieldid).style.color = clickedcolor;
	
	if (clicked==true) {
		document.getElementById("workingfield"+clickedid).style.color = "";
		document.getElementById("menufield"+clickedid).style.color = "";
		clicked = false;
		clickedid = 0;
	}
}



// workingfield onmouseout
function outField(fieldid) {
	//unset44color(); 
	showallfields();
	showallpersonpics();
}
// workingfield onmouseover
function overField(fieldid) {
	if (clicked==true && clickedid!=fieldid) { 
		unsetfieldcolor(clickedid);
	}
	
	var fieldids = new Array();
	fieldids.push(fieldid);
	hideotherfields(fieldids);
	
	showhidepersons(fieldid); 
	//set44color(fieldcolor);
}
// workingfield onclick
function toggleField(fieldid) {
	clicked = true;
	clickedid = fieldid;
	document.getElementById("workingfield"+fieldid).style.color = clickedcolor;
	
	if (clickedsub==true) {
		document.getElementById("subfield"+clickedsubid).style.color = "";
		document.getElementById("menufield"+clickedsubid).style.color = "";
		clickedsub = false;
		clickedsubid = 0;
	}
}



//Create Array of All HTML Tags
var allDivTags = new Array();
var allATags = new Array();
	
// showallfields
function showallfields() {
	//Loop through all tags using a for loop
	for (i=0; i<allDivTags.length; i++) {
		//Get all tags with the specified class name.
		if (allDivTags[i].className=="fieldbox" && allDivTags[i].style.opacity!=1) {
			// Do things
			//allDivTags[i].style.visibility='visible';
			setOpacity(allDivTags[i], 10);
		}
	}
	for (i=0; i<allATags.length; i++) {
		//Get all tags with the specified class name.
		if (allATags[i].className=="subfield" && allATags[i].style.opacity!=1) {
			// Do things
			//allATags[i].style.visibility='visible';
			setOpacity(allATags[i], 10);
		}
	}
	
	// menu fields
	unsetmenufieldcolor();
}

// hideotherfields
function hideotherfields(fieldids) {
	if (allDivTags.length==0) {
		allDivTags=document.getElementsByTagName("div");
	}
	// hide
	for (i=0; i<allDivTags.length; i++) {
		//Get all tags with the specified class name.
		if (allDivTags[i].className=="fieldbox") {
			//allDivTags[i].style.visibility='hidden';
			setOpacity(allDivTags[i], 4);
		}
	}
	// show
	for (i=0; i<fieldids.length; i++) {
		e = document.getElementById("fieldbox"+fieldids[i]);
		if (e!=null) {
			//e.style.visibility='visible';
			setOpacity(e, 10);
		}
	}
	
	// menu fields
	setmenufieldcolor(fieldids);
}

// hideothersubfields
function hideothersubfields(fieldids) {
	if (allATags.length==0) {
		allATags=document.getElementsByTagName("a");
	}
	// hide
	for (i=0; i<allATags.length; i++) {
		//Get all tags with the specified class name.
		if (allATags[i].className=="subfield") {
			//allATags[i].style.visibility='hidden';
			setOpacity(allATags[i], 4);
		}
	}
	// show
	for (i=0; i<fieldids.length; i++) {
		e = document.getElementById("subfield"+fieldids[i]);
		if (e!=null) {
			//e.style.visibility='visible';
			setOpacity(e, 10);
		}
	}
	
	// menu fields
	setmenufieldcolor(fieldids);
}


// show text
function showtextonfield(fieldid) {
	document.getElementById("text"+fieldid).style.display = 'inline';
	document.getElementById("textDefault").style.display = 'none';
}
// hide text
function hidetextonfield(fieldid) {
	document.getElementById("text"+fieldid).style.display = 'none';
	document.getElementById("textDefault").style.display = 'inline';
}

/* showhidepersons on workingfields */
function showhidepersons(fieldid) {
	
	// show some
	var persons = field2persons[fieldid];
	for (i=0; i < persons.length; i++) {
		if (persons[i]!='') {
			//document.getElementById("personpic"+persons[i]).style.display = 'inline';
			setOpacityId("personimg"+persons[i], 10);
			setpersoncolor("person"+persons[i]);
		}
	}
	
	// hide others
	var persons = fieldnot2persons[fieldid];
	for (i=0; i < persons.length; i++) {
		if (persons[i]!='') {
			//document.getElementById("personpic"+persons[i]).style.display = 'none';
			setOpacityId("personimg"+persons[i], 3);
			unsetpersoncolor("person"+persons[i]);
		}
	}
}

function showpersonpic(personpic) {
	for (i=0; i < allpersons.length; i++) {
		if(allpersons[i]==personpic) {
			//document.getElementById("personpic"+allpersons[i]).style.display = 'inline';
			setOpacityId("personimg"+allpersons[i], 10);
		} else {
			//document.getElementById("personpic"+allpersons[i]).style.display = 'none';
			setOpacityId("personimg"+allpersons[i], 3);
		}
	}
}
function hideallpersonpics() {
	for (i=0; i < allpersons.length; i++) {
		//document.getElementById("personpic"+allpersons[i]).style.display = 'inline';
		setOpacityId("personimg"+allpersons[i], 3);
		unsetpersoncolor("person"+allpersons[i]);
	}
}
function showallpersonpics() {
	for (i=0; i < allpersons.length; i++) {
		//document.getElementById("personpic"+allpersons[i]).style.display = 'inline';
		setOpacityId("personimg"+allpersons[i], 10);
		unsetpersoncolor("person"+allpersons[i]);
	}
	
	if (clicked==true) { 
		setfieldcolor(); 
		if (clickedsub==true) {
			showhidepersons(clickedsubid);
		} else {
			showhidepersons(clickedid); 
		}
		//set44color(clickedcolor); 
	}
	if (currentperson==true) {
		showpersonpic(currentpersonid);
		setpersoncolor("person"+currentpersonid);
	}
}




/* 44 color */
function set44color(newcolor) {
	document.getElementById('fortyfour').style.color = newcolor;
}
function unset44color() {
	document.getElementById('fortyfour').style.color = '#000000';
}
function set44class() {
	document.getElementById('fortyfour').className = "current";
}
function unset44class() {
	document.getElementById('fortyfour').className = "notcurrent";
}


/* person color */
function setpersoncolor(id) {
	e = document.getElementById(id);
	e.className = "current";
}
function unsetpersoncolor(id) {
	e = document.getElementById(id);
	e.className = "notcurrent";
}
/* field color */
function setfieldcolor() {
	document.getElementById("workingfield"+clickedid).style.color = clickedcolor;
}
function unsetfieldcolor(fieldid) {
	document.getElementById("workingfield"+fieldid).style.color = "";
}
/* menu field color */
function setmenufieldcolor(fieldids) {
	for (i=0; i<fieldids.length; i++) {
		e = document.getElementById("menufield"+fieldids[i]);
		if (e!=null) {
			e.className = "current";
		}
	}
}
function unsetmenufieldcolor() {
	for (i=0; i<allfields.length; i++) {
		if (allfields[i]!=clickedid) {
			e = document.getElementById("menufield"+allfields[i]);
			if (e!=null) {
				e.className = "notcurrent";
			}
		}
	}
}

