// JavaScript Core
// Copyright by johannes.wedenigg@linea7.com © 2007

//*************************************
// lineacore Namespace 
// alle hier definierten funktionen sind über lineacore.funktionsname ansprechbar
//*************************************


function L7Namespace() {
	
	/*	Globale Eigenschaften in lineacore       */
	
	
	
	/* 	Test Funktion zum Testen der korrekten Namespace Adressierung
		Diese Funktion öffnet einfach eine Messagebox und gibt den Test "nachricht" aus    */
		
	this.gibLaut = function(nachricht) {
		window.alert(nachricht);
	}
	
	/* Funktion zum einfachen austauschen von Bildern über "src" */
	
	this.changeImgSrc = function(element,newsrc) {
		element = $(element);
		element.src = newsrc;
	}
	
	/* fading von Elementen erreichen */
	this.toggleBlindElement = function(element) {
		blind_target = $(element);
		
		if (blind_target.style.display) {
			new Effect.BlindDown(blind_target, {duration:0.25});
		} else {
			new Effect.BlindUp(blind_target, {duration:0.25});
		}
	}
	
	this.toggleCompanyList = function() {
			var companyList = $("company-box-content");
			var listToggleLink = $("toggle-company-list-link");
			
			if (companyList.style.display) {
				new Effect.BlindDown(companyList, {duration:0.25});
				
				listToggleLink.innerHTML = "Liste ausblenden";
				
			} else {
				new Effect.BlindUp(companyList, {duration:0.25});
				
				listToggleLink.innerHTML = "Liste einblenden";
			}
			
	}
	
	
	this.selectCompany = function(companyName, companyId) {
			var myCompanyName = companyName;
			var myCompanyId = companyId;
			var companySelectedLabel = $("company-selected-label");
			var companySelectedHeader = $("company-selected");
			var noCompanySelectedHeader = $("no-company-selected");
			var companyList = $("company-box-content");
			var listToggleLink = $("toggle-company-list-link");
			
			new Effect.BlindUp(companyList, {duration:0.25});
			
			listToggleLink.innerHTML = "Liste einblenden";
			
			companySelectedLabel.innerHTML = myCompanyName;
			
			companySelectedHeader.style.display = "block";
			noCompanySelectedHeader.style.display = "none";
	}
	
	this.resetCompanyFilter = function() {
			var companySelectedLabel = $("company-selected-label");
			var companySelectedHeader = $("company-selected");
			var noCompanySelectedHeader = $("no-company-selected");
			var companyList = $("company-box-content");
			var listToggleLink = $("toggle-company-list-link");
			
			new Effect.BlindDown(companyList, {duration:0.25});

			listToggleLink.innerHTML = "Liste ausblenden";
			
			
			companySelectedHeader.style.display = "none";
			noCompanySelectedHeader.style.display = "block";
	}
	
	
	/* Startseite - Intro-Fulltext ein und ausblenden */
	
	
	this.showFulltextBox = function() {
		box = $("intro-fulltext-box");
		
		if (box.style.display == "none") { 
			new Effect.BlindDown(box, {duration:0.50});
		}
		
	}
	
	
	this.hideFulltextBox = function() {
		box = $("intro-fulltext-box");
		
		new Effect.BlindUp(box, {duration:0.50});
	}

	
	
	
	
	
	/* Suchseiten - Suchbox ein-/ausblenden */
	
	this.showSearchBox = function() {
		box = $("search-container");
		show_button = $("bt-show-search");
		hide_button = $("bt-hide-search");
		
		new Effect.BlindDown(box, {duration:0.50});
		show_button.style.display = "none";
		hide_button.style.display = "block";
	}
	
	
	this.hideSearchBox = function() {
		box = $("search-container");
		show_button = $("bt-show-search");
		hide_button = $("bt-hide-search");
		
		new Effect.BlindUp(box, {duration:0.50});
		show_button.style.display = "block";
		hide_button.style.display = "none";
	}

		
		
	
	
	/* Ergebnis wechseln zwischen Freie Objekte und alle Objekte */
	
	
	this.showAllObjects = function() {
		button_alle = $("bt-alle-objekte");
		button_freie = $("bt-freie-objekte");
		
		results_alle = $("result-box-alle");
		results_freie = $("result-box-freie");
		
		if (results_alle.style.display = "none") {
			button_alle.className = "image-replacement alle-objekte-aktiv";
			button_freie.className = "image-replacement freie-objekte-inaktiv";
			results_alle.style.display = "block";
			results_freie.style.display = "none";
		}
	}
	
	this.showFreeObjects = function() {
		button_alle = $("bt-alle-objekte");
		button_freie = $("bt-freie-objekte");
		
		results_alle = $("result-box-alle");
		results_freie = $("result-box-freie");
		
		if (results_freie.style.display = "none") {
			button_alle.className = "image-replacement alle-objekte-inaktiv";
			button_freie.className = "image-replacement freie-objekte-aktiv";
			results_alle.style.display = "none";
			results_freie.style.display = "block";
		}
	}
	
	
	
	
	/* Für berater */
	
	
	this.finishQuestion = function(question) {
		var myQuestion = question;
		
		var recentElement = $('berater-item' + myQuestion);
		var nextElement = $('berater-item' + (myQuestion + 1));
		
		
		recentElement.removeClassName('active');
		recentElement.addClassName('inactive');
		
		nextElement.style.display = "block";
	}
		
		
	
	
	
	
	
	
	
	
	this.changeBoxStatus = function(element) {
		op_target = $(element);
		
		if(op_target) {
			if (op_target.style.display != "none") {
				new Effect.BlindUp(op_target, {duration:0.25});
			}
		}
	}
	
	
	this.blindUp = function(element) {
		blind_target = $(element);
		
		if (blind_target.style.display != "none") {
			new Effect.BlindUp(blind_target, {duration:0.25});
		}
	}
	
	this.blindDown = function(element) {
		blind_target = $(element);
		
		if (blind_target.style.display != "block") {
			new Effect.BlindDown(blind_target, {duration:0.25});
		}
	}
	
	
	
	
	
	this.showTab = function(tab) {
		var panel = $(tab);
		panel.style.display = "block";
	}

	this.hideTab = function(tab) {
		var panel = $(tab);
		panel.style.display = "none";
	}
	
	
	
	
	// Erzeugen eines Request-Objekts //
	this.req = "";
	
	
	
	// loadXMLDoc baut die Verbindung zu einem Skript auf //
	
	this.XHRCall = function(url) {
		lineacore.req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
				lineacore.req = new XMLHttpRequest();
			} catch(e) {
				lineacore.req = false;
			}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
			try {
				lineacore.req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					lineacore.req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					lineacore.req = false;
				}
			}
		}
		if(lineacore.req) {
			lineacore.req.onreadystatechange = lineacore.processReqChange;
			lineacore.req.open("GET", url, true);
			lineacore.req.send("");
		}
	}	
	
	this.showAlert = function() {
		var alert_background = $('alert-background');
		var alert_container = $('alert-container');
		
		alert_background.style.display = "block";
		alert_container.style.display = "block";
	}
	
	this.closeAlert = function() {
		var alert_background = $('alert-background');
		var alert_container = $('alert-container');
		
		alert_background.style.display = "none";
		alert_container.style.display = "none";
	}
	
	// processReqChange verarbeitet die Rückmeldung und übergibt die Daten //
	
	this.processReqChange = function() {
		// only if req shows "loaded"
		
		var alertDate = $('alert-date');
		var alertText = $('alert-text');
		var weiterclick = $('weiter-click');
		var closeclick = $('close-click');
		var myXHRFeedback;
		
		if (lineacore.req.readyState == 4) {
				// only if "OK"
				
				myXHRFeddback = lineacore.req.responseText;
				
				if(myXHRFeedback == "yes") {
					alertDate.innerHTML = "irgendein Datum";
					alertText.innerHTML = "dieses Objekt ist frei";
					closeclick.style.display = "none";
					weiterclick.style.display = "block";
					
					lineacore.showAlert();
					
				} else {
					alertDate.innerHTML = "irgendein Datum";
					alertText.innerHTML = "dieses Objekt ist NICHT frei";
					weiterclick.style.display = "none";
					closeclick.style.display = "block";
					
					lineacore.showAlert();
					
				}
				
			if (lineacore.req.status == 200) {
				// ...processing statements go here...
			} else {
				alert("There was a problem retrieving the XML data:\n" +
					lineacore.req.statusText);
			}
		}
	}

	this.checkFreiGebucht = function(objectID,vonDate,bisDate) {
		var myObjectID = objectID;
		var myVonDate = vonDate;
		var myBisDate = bisDate;
		
		lineacore.XHRCall('frei_gebucht-XHR.php?id=' + myObjectID + '&von=' + myVonDate + '&bis='+ myBisDate);
	}

	// direktes Veränderen von InnerHTML
	
	this.updateInnerHTML = function(elementID,neuerInhalt) {
		$(elementID).innerHTML = neuerInhalt;
	}
	
	// direktes Erweitern eines Attributs, neuer Inhalt wird angehängt
	
	this.appendToAttribute = function(elementID,attributName,neuerInhalt) {
		$(elementID).attributName = $(elementID).attributName + neuerInhalt;
	}
	
	// direktes Einblenden eines Elements (div)
	this.showElement = function(elementID) {
		$(elementID).style.display = "block";
	}
	
	//direktes Ausblenden eines Elements (div)
	this.hideElement = function(elementID) {
		$(elementID).style.display = "none";
	}


    this.onFocusValue = function(id, value)
    {
	var e = $(id);
	if(e.value==value) e.value = "";
    }
    
    this.onBlurValue = function(id, value)
    {
	var e = $(id);
	if(e.value=="") e.value = value;
    }
    
	
			
}  // Ende des Namespace Objekts ----> Diese Klammer NICHT ENTFERNEN!!!!


//*************************************
// Ende des lineacore Namespace
// Initialisieren des Namespace Objekts

var lineacore = new L7Namespace;

// Jetzt können alle Methoden und Eigenschaften von algCoreNamespace über lineacore.blblbla angesprochen werden

// Es folgen weitere Funktionen die noch in den lineacore Namespace überführt werden müssen
//*************************************







//*************************************
// Skripte zum Verändern der Layer-Größen, Einblenden/Ausblenden der Station etc.
//*************************************



function resizeContainer(div_id, div_height)
{
 var div = document.getElementById(div_id);

 if(div.offsetHeight < div_height) {
 
	  var h = div.offsetHeight + 15;
	  
	  if (h < div_height) {	  
		  div.style.height = h+'px';
		  window.setTimeout("resizeContainer('" + div_id +"'," + div_height + ");", 5);
		} else {
	  	  div.style.height = div_height+'px';
	  }
	  
 } else if(div.offsetHeight > div_height) {
 
	  var h = div.offsetHeight - 15;
	  
	  if (h > div_height) {	  
		  div.style.height = h+'px';
		  window.setTimeout("resizeContainer('" + div_id +"'," + div_height + ");", 5);
		} else {
	  	  div.style.height = div_height+'px';
		}
 }
}






//*************************************
// Skripte für Popup Overlay
//*************************************

function hide_popup() {
	
	var popup_box = document.getElementById("popup_box");
	var popup_background = document.getElementById("popup_background");
	
	popup_box.style.display = "none";
	popup_background.style.display = "none";
	
	popup_box.style.top = "0px";
	popup_box.style.left = "0px";
	
	popup_background.style.top = "0px";
	popup_background.style.left = "0px";
	
	popup_box.style.width = "0px";
	popup_box.style.height = "0px";
		
	popup_background.style.width = "0px";
	popup_background.style.height = "0px";
	
	window.onscroll = "";
	window.onresize = "";
}



function show_popup(url,width,height) {
	
	var popup_box = document.getElementById("popup_box");
	var popup_background = document.getElementById("popup_background");
	
	popup_box.style.width = width+"px";
	popup_box.style.height = height+"px";
	
	var screen_size = new getPageSize();
	
	popup_background.style.width = screen_size.width+"px";
	popup_background.style.height = screen_size.height+"px";
	
	popup_background.style.opacity = ".2";
	popup_background.style.filter = 'alpha(opacity=' + '20' + ')';
	
	popup_box.style.display = "block";
	popup_background.style.display = "block";
	
	maintPos();
	
	docClickLoader.loadInto(url, 'popup_content');
	
	window.onscroll = maintPos;
	window.onresize = maintPos;
}

posToCenter = function(elem) {
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;	
	elem.style.left = x+'px';
	elem.style.top = y+'px';	
}

getScrollPos = function() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

getPageSize = function() {
	var docElem = document.documentElement
	this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
	this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

getElementSize = function(elem) {
	this.width = elem.offsetWidth ||  elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}	

maintPos = function() {
	var popup_box = document.getElementById("popup_box");
	posToCenter(popup_box);
	
	var popup_background = document.getElementById('popup_background');
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;

	if(ua.indexOf("MSIE ") != -1) {popup_background.style.width = pagesize.width+'px';} 
	else {popup_background.style.width = pagesize.width+'px';}

	if(ua.indexOf("Opera/9") != -1) {popup_background.style.height = document.body.scrollHeight+'px';}
	else {popup_background.style.height = pagesize.height+scrollPos.scrollY+'px';}

	
}

			






