// JavaScript Document

//ajax
function Ajax(){
	//	Eigenschaften deklarieren und intialisieren	
	this.url="";
	this.params="";
	this.method="GET";
	this.onSuccess=null;
	//this.loadProgress="";
	this.onError=function(msg){alert(msg)}
	}
	
Ajax.prototype.doRequest=function(){
	// Zugriff auf Klasse für readyStateHandler ermöglichen
	var _this=this;
	
	// Überprüfen der Angaben
	if(!this.url){
		this.onError("Es wurde keine URL angegeben!! Der Request wurde abgebrochen.");
		return false;
		};
	if(!this.method){
		this.method="GET";
		}
		else{
			this.method=this.method.toUpperCase();
			}
	
	// XMLHttpRequest-Objekt erstellen
	var xmlHttpRequest=getXMLHttpRequest();
	if(!xmlHttpRequest){
		this.onError("Es konnte kein XMLHttpRequest-Objekt erstellt werden.");
		return false;
		}
		
	
	// Einblenden des Progresszeichens
	/*if (_this.loadProgress != "" && _this.loadProgress != null){
		var showProgress = document.getElementById(_this.loadProgress).style.visibility = "visible";
	}*/
	// Fallunterscheidung nach Übertragungsmethode
	switch(this.method){
		
		case "GET":		xmlHttpRequest.open(this.method, this.url+"?"+this.params, true);
						xmlHttpRequest.onreadystatechange=readyStateHandler;
						xmlHttpRequest.send(null);
						break;
		case "POST":	xmlHttpRequest.open(this.method, this.url, true);
						xmlHttpRequest=onreadystatechange=readyStateHandler;
						xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						xmlHttpRequest.send(this.params);
						break;
		}
	
	
	// Private Methode zur Verarbeitung der erhaltenen Daten
	function readyStateHandler(){
		if(xmlHttpRequest.readyState < 4){
			//_this.onError("readyState war fehlerhaft bei Annahme des Response: readyState < 4 -> "+xmlHttpRequest.readyState);
			return false;
			}
		if(xmlHttpRequest.status == 200 || xmlHttpRequest.status == 304){
			if(_this.onSuccess){
				// Ausblenden des Progresszeichens
				/*if (_this.loadProgress != "" && _this.loadProgress != null){
					var showProgress = document.getElementById(_this.loadProgress).style.visibility = "hidden";
				}*/
					
				_this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
					
					
					
				}
			}
			else{
				if(_this.onError){
					_this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] Es trat ein Fehler bei der Datenübertragung auf!");
					}
				}
		}
	}
	
// Gibt browserunabhängig ein XMLHttpRequest-Objekt zurück
function getXMLHttpRequest(){
	if(window.XMLHttpRequest){
		//XMLHttpRequest für Firefox, Opera, Safari & Co
		return new XMLHttpRequest();
		}
		else{
			if(window.ActiveXObject){
				try{
					// XMLHTTP für InternetExplorer (NEU)
					return new ActiveXObject("Msxml2.XMLHTTP");
					}
				catch(e){
					// XMLHTTP für InternetExplorer (ALT)
					try{
						return new ActiveXObject("Microsoft.XMLHTTP");
						}
					catch(e){
						return null;
						}
					}
				}
				return null;
			}
	}
	

function getPos(){
	var pos;
	if (window.innerHeight)
	{
		  pos = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		  pos = document.body.scrollTop;
	}	
	return pos;
}


//img-request for popup
function imgRequest(fileid){
	var myAjax = new Ajax();
	myAjax.url="index.php";
	myAjax.params="article_id=20&fileid="+fileid+"&browserwidth="+window.getWidth()+"&browserheight="+window.getHeight()+"";
	myAjax.method="GET";
	myAjax.onSuccess = popInsert;
	myAjax.onError=function(msg){alert(msg)}
	myAjax.doRequest();
}
//popup-functions
function popInsert(txt, xml){
	var opspan = document.createElement("span");
	opspan.id = "opspan";
	opspan.style.width = ""+(window.getWidth())+"px";
	opspan.style.height = ""+window.getScrollHeight()+"px";
	opspan.style.backgroundColor = "#fff";
	opspan.style.position = "absolute";
	opspan.style.top = "0";
	opspan.style.left = "0";
	
	var infopop = document.createElement("div");
	infopop.id = "infopop";
	infopop.style.backgroundColor="#fff";
	infopop.style.border = "2px solid #fff";
	infopop.style.position = "absolute";
	infopop.style.zIndex = "15";
	infopop.style.top = ""+(25+window.getScrollTop())+"px";
	infopop.style.left = "auto";
	infopop.style.right = "auto";
	
	infopop.innerHTML = ""+txt+"";
	document.getElementById("wrap").appendChild(opspan);
	document.getElementById("wrap").appendChild(infopop);
	$('infopop').addEvent('click', function(){
		removePop();
	});
	$('infopop').addEvent('mouseover',function(){
			this.style.cursor = "pointer";							 
									 
		});
	
}

function removePop(){
	var plane = $('opspan');
	var pu = $('infopop');
	pu.parentNode.removeChild(pu);
	plane.parentNode.removeChild(plane);
}


//popup events auf bilder
window.addEvent('domready', function(){
	$$('img.pop').each(function(el){
		el.style.display = "block";
		el.addEvent('click',function(el){
			var fileid = this.className.substr(4);
			imgRequest(fileid);							 
									 
		});
		el.addEvent('mouseover',function(el){
			this.style.cursor = "pointer";							 
									 
		});
					   
	});								 
										 
});