/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/
var kontaktpopup;
function Kontaktpopup(){
	var openerID='kontaktauswahl';
	var containerID='kontaktwrapper';
	var linkID='kontaktlinkWrapper';
	var xhr=new Hijax();
	var formXhr=null;
	var placeholder;
	var windowHeight=0;
	var windowWidth=0;
	var current='';
	this.close=function(){
		addClass(document.getElementById(containerID),'hidden');
		addClass(document.getElementById('closelayer'),'hidden');
		window.onresize=null;
		if(document.getElementById(openerID)){
			var inputTag=document.getElementById(openerID).getElementsByTagName('input');
			for(var i=0; i<inputTag.length; i++){
				inputTag[i].checked=false;
			}
			inputTag=null;
		}
		if(location.href.indexOf('#')>0){
			location.href=location.href.substr(0,location.href.indexOf('#'))+'#close';
		}
		return true;
	}
	this.load=function(element){
		element.blur();
		if(element.href.indexOf('?')>-1){
			var url=element.href+'&xhr=1';
		}else{
			var url=element.href+'?xhr=1';
		}
		xhr.setUrl(url);
		xhr.setCallback(function(request){
			if(request.responseText!=''){
				//purge(placeholder);
				//while(placeholder.hasChildNodes()){
				//	placeholder.removeChild(placeholder.lastChild);
				//}
				//getWindowHeight();
				placeholder.innerHTML=request.responseText;
				//var response=document._importNode(request.responseXML.getElementsByTagName('body')[0],true);
				if(placeholder.innerHTML!=''){
					if(!document.getElementById(containerID)){
					//	if(windowHeight>0){
							var divTag=document.createElement('div');
							divTag.style.height=windowHeight+'px';
							divTag.id='closelayer';
							divTag.onclick=function(){
								return !kontaktpopup.close();
							}
							document.getElementsByTagName('body')[0].appendChild(divTag);
					//	}
						var divTag=document.createElement('div');
						divTag.id=containerID;
						document.getElementsByTagName('body')[0].appendChild(divTag);
					}
					document.getElementById(containerID).innerHTML=placeholder.innerHTML;
					while(placeholder.hasChildNodes()){
						placeholder.removeChild(placeholder.lastChild);
					}
					//document.getElementById('xhrPlaceholder').innerHTML='';
					//window.onresize=kontaktpopup.setBoxHeight;
					if(document.getElementById(openerID+'Xhr')){
						prepareOpener(document.getElementById(openerID+'Xhr'));
					}else if(document.getElementById(containerID).getElementsByTagName('form').length>0){
						checkForm.prepare();
						prepareForm(document.getElementById(containerID));
					}
					removeClass(document.getElementById(containerID),'hidden');
					removeClass(document.getElementById('closelayer'),'hidden');
					setBoxSize();
					if(document.getElementById('kontaktFormMarker') && document.getElementById(document.getElementById('kontaktFormMarker').lastChild.nodeValue)){
						current=document.getElementById('kontaktFormMarker').lastChild.nodeValue;
						document.getElementById(document.getElementById('kontaktFormMarker').lastChild.nodeValue).checked=true;
						if(location.href.indexOf('#')>0){
							location.href=location.href.substr(0,location.href.indexOf('#'))+'#f_'+current;
						}else{
							location.href=location.href+'#f_'+current;
						}	
					}
					document.getElementById(containerID).scrollIntoView(true);
				}
			}
			request=null;
		});
		xhr.captureData();
		return true;
	}
	var prepareForm=function(element){
		var form=element.getElementsByTagName('form')[0];
		if(form.action.indexOf('?')>-1){
			var url=form.action+'&xhr=1';
		}else{
			var url=form.href+'?xhr=1';
		}
		form.action=url;
		formXhr=new Hijax();
		formXhr.setContainer(form);
		formXhr.setCallback(function(request){
			if(request.responseText!=''){
				placeholder.innerHTML=request.responseText;
				document.getElementById(containerID).innerHTML=document.getElementById('xhrPlaceholder').innerHTML;
				if(document.getElementById(containerID).getElementsByTagName('form').length>0){
					prepareForm(document.getElementById(containerID));
				}
				while(placeholder.hasChildNodes()){
					placeholder.removeChild(placeholder.lastChild);
				}
				document.getElementById(containerID).scrollIntoView(true);
			}
		});
		formXhr.captureData();
	}
	this.setBoxSize=function(){
		setBoxSize(true);
	}
	var setBoxSize=function(resetFlag){
		var windowHeightBackup=windowHeight
		getWindowHeight();
		if(windowHeight>0 && (windowHeightBackup!=windowHeight || !resetFlag)){
			document.getElementById('closelayer').style.height=windowHeight+'px';
		}
		var windowWidthBackup=windowWidth
		getWindowWidth();
		if(windowWidth>0 && (windowWidthBackup!=windowWidth || !resetFlag)){
			document.getElementById(containerID).style.left=((windowWidth-document.getElementById(containerID).offsetWidth)/2)+'px';
		}
	}
	var prepareContent=function(element){
		var aTags=element.getElementsByTagName('a');
		for(var i=0; i<aTags.length; i++){
			if(!aTags[i].getAttribute('target') && aTags[i].href.indexOf('.php')>0 && (aTags[i].href.indexOf('nID=')>0 || aTags[i].href.indexOf('back=1')>0)){
				aTags[i].onclick=function(){
					return !kontaktpopup.load(this);
				}
			}
		}
		return true;
	}
	var getWindowHeight=function(){
		if(typeof(window.innerHeight)=='number'){
			windowHeight = window.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}else if( document.body && document.body.clientHeight){
			windowHeight = document.body.clientHeight;
		}
		windowHeight=Math.max(document.getElementById('page').offsetHeight,windowHeight);
		windowHeight=Math.max(document.getElementById(containerID).offsetHeight+getOffsetY(document.getElementById(containerID))+10,windowHeight);
	}
	var getWindowWidth=function(){
		if(typeof(window.innerWidth)=='number'){
			windowWidth = window.innerWidth;
		}else if(document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}else if( document.body && document.body.clientWidth){
			windowWidth = document.body.clientWidth;
		}
		windowWidth=Math.max(document.getElementById('page').offsetWidth,windowWidth);
	}
	var getOffsetY=function(element){
		var offsetY=element.offsetTop;
		while(element.offsetParent){
			element=element.offsetParent
			offsetY+=element.offsetTop;
		}
		return offsetY;
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (document.getElementById(openerID)){
			prepareOpener(document.getElementById(openerID));
			createPlaceholder();
			if(location.href.indexOf('#')>0){
				current=location.href.substr(location.href.indexOf('#')+3);
				if(!document.getElementById(current)){
					current=false;
				}else{
					kontaktpopup.load(document.getElementById(current).parentNode.getElementsByTagName('a')[0]);
				}
			}
			return true;
		}else if(document.getElementById(linkID)){
			document.getElementById(linkID).getElementsByTagName('a')[0].onclick=function(){
				return !kontaktpopup.load(this);
			}
			createPlaceholder();
			return true;
		}else{
			return false;
		}
	}
	var prepareOpener=function(element){
		var aTags=element.getElementsByTagName('a');
		if(aTags.length>0){
			for(var i=0; i<aTags.length; i++){
				aTags[i].onclick=function(){
					return !kontaktpopup.load(this);
				}
				aTags[i].parentNode.parentNode.getElementsByTagName('input')[0].onclick=function(){
					return !kontaktpopup.load(this.parentNode.getElementsByTagName('a')[0]);
				}
			}
		}
	}
	var createPlaceholder=function(){
		placeholder=document.createElement('div');
		placeholder.id='xhrPlaceholder';
		placeholder.style.display='none';
		addClass(placeholder,'hidden');
		document.getElementsByTagName('body')[0].appendChild(placeholder);
		if(document.getElementById(containerID)){
			getWindowHeight();
			if(windowHeight>0){
				var divTag=document.createElement('div');
				divTag.style.height=windowHeight+'px';
				divTag.id='closelayer';
				divTag.onclick=function(){
					return !kontaktpopup.close();
				}
				document.getElementById(containerID).style.backgroundImage='none';
				document.getElementById(containerID).appendChild(divTag);
			}
			prepareContent(document.getElementById(containerID));
			setBoxHeight();
		}
	}
}
addLoadEvent(function(){
	kontaktpopup=new Kontaktpopup();
	kontaktpopup.prepare();
});
