var req = null;

function getXMLHttpRequest()
{             
    //erstellen des requests
    var req = null;
    
	try
	{
        return new XMLHttpRequest();
    }
    catch (ms)
	{
        try
		{ 
            return new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (nonms) 
		{
            try
			{
                return new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (failed)
			{
                return null;
            }
        }  
    } 
}
 
function Layer()
{
	var thisObject = this;
	var backgroundcolor = "#FFFFFF";
	var backgroundopacity = 0.6;
	var width = 100;
	var height = 100;
	var contentdiv;
	var randomid = Math.floor(Math.random()*100);
	var opening = false;	
	var redline = true;
	var index = 2;  
	var fading = false;
	var noajax = (arguments.length > 0 && arguments[0] == "noajax") ? true : false;
	
	this.setFading = function(_fading)
	{
		fading=_fading;
	}
	this.setBackgroundColor = function(color)
	{
		backgroundcolor = color;
	} 
	 
	this.setBackgroundOpacity = function(opacity)
	{
		backgroundopacity = opacity;
	}
	
	this.setRedline = function(red_line)
	{
		redline = red_line;
	}
	
	this.setIndexLevel = function(l_index)
	{
		index = l_index * 2;
	}
		
	this.showLayer = function()
	{
		var layerurl = /*VIPURL*/"/Global/Applications/Suche.jsp";
		var req = getXMLHttpRequest();
		
		req.open("POST", layerurl, true);

	    req.onreadystatechange = function(){            
		    switch(req.readyState) 
			{  
		        case 4:
		            if(req.status!=200) 
					{
		                alert("Fehler:"+req.status); 
		            }
					else 
					{
						var div2 = document.createElement("div");
						var div = document.createElement("div");
						div.innerHTML = req.responseText.substring(req.responseText.indexOf("<layeropening>")+14, req.responseText.indexOf("</layeropening>"));
						div2.appendChild(getHeader("Standortsuche"));
						div2.appendChild(div);
						thisObject.setIndexLevel(2);
						thisObject.showContentAsLayer(div, true, "Standortsuche", "bgc_neutral_verlauf_3");
						//setKontaktContent(gui.layer(div2, 'bgc_neutral', null));
						 
		            }
		            break;
		                    
		        default:
		            return false; 
		            break;     
		    }
		};
	  
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		req.send(null);
	}
	
	this.showFilialfinder = function()
	{
		var layerurl = /*VIPURL*/"/Global/Applications/Filialfinder_Suche.jsp";
		var req = getXMLHttpRequest();
		
		req.open("POST", layerurl, true);

	    req.onreadystatechange = function(){            
		    switch(req.readyState) 
			{  
		        case 4:
		            if(req.status!=200) 
					{
		                alert("Fehler:"+req.status); 
		            }
					else 
					{
						var div2 = document.createElement("div");
						var div = document.createElement("div");
						div.innerHTML = req.responseText.substring(req.responseText.indexOf("<layeropening>")+14, req.responseText.indexOf("</layeropening>"));
						div2.appendChild(getHeader("Unsere Feiertagsöffnungszeiten"));
						div2.appendChild(div);
						thisObject.setIndexLevel(2);
						thisObject.showContentAsLayer(div, true, "Unsere Feiertagsöffnungszeiten", "bgc_white");
						//setKontaktContent(gui.layer(div2, 'bgc_neutral', null));
						 
		            }
		            break;
		                    
		        default:
		            return false; 
		            break;     
		    }
		};
	  
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		req.send(null);
	}	
	
	this.showLayerUrl = function(url, title, width, height)
	{
		var iframe = document.createElement("iframe");
		iframe.id = "iframe_layer";
		iframe.frameBorder = 0;
		iframe.style.border = "none";
		iframe.style.width = width + "px";
		iframe.style.height = height + "px";
		iframe.src = url;
		
		thisObject.showContentAsLayer(iframe, true, title, "bgc_neutral_verlauf_3");
	}
	 
	
	this.showKontakt = function(params)
	{ 
		var layerurl = params;
		var req = getXMLHttpRequest(); 
		
		req.open("POST", layerurl, true);

	    req.onreadystatechange = function(){             
		    switch(req.readyState) 
			{  
		        case 4:
		            if(req.status!=200) 
					{ 
		                alert("Fehler:"+req.status); 
		            } 
					else 
					{ 
						var div2 = document.createElement("div");
						var div = document.createElement("div");
						div.innerHTML = req.responseText.substring(req.responseText.indexOf("<layerkontakt>")+14, req.responseText.indexOf("</layerkontakt>"));
						div2.appendChild(getHeader(i18n.getText("inter_joboffers_request")));
						div2.appendChild(div);
						//setKontaktContent(gui.layer(div2, 'bgc_neutral', null));
						thisObject.showContentAsLayer(div, true, i18n.getText("inter_joboffers_request"), "bgc_neutral_verlauf_3");
		            }
		            break;
		                    
		        default:
		            return false; 
		            break;     
		    }
		};
	  
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		
		if(params.indexOf("http:") > -1)
			req.send(null);
		else
			req.send(params);
	    
	    setBackgroundLayer();
	    
	    try
	    {
		   	var pagename = " ";
		    
		    if (layerurl.indexOf("?") > -1 )
		    {
			    
		    	var subject = layerurl.substring(layerurl.indexOf("subject=")+8).toLowerCase();
						    
			    
			    if(subject.length>0)
			    {
			    	subject = subject.substring(0,2);	    
			    
			    	if(subject.indexOf("&")!=-1)
			    		subject = subject.substring(0,1);
			    	
			    	if(subject==4)
		    			pagename = i18n.getText("inter_estate_offer");
			    	else if(subject==5)
		    			pagename = i18n.getText("inter_question_leasing");
		    		else if(subject==6)
		    			pagename = i18n.getText("inter_question_estate_offer");
			    	else if (subject==11)
						pagename = i18n.getText("inter_question_concept_plus");
					else if (subject==12)
						pagename = i18n.getText("inter_question_ernaehrungsberatung");
			    
			    }
			    ET_Event.eventStart(pagename,'Kundenservice/Kontakt/'+pagename,'layeraufruf','');
			    
				
			}
	    }
	    catch(e){}
	    
	}	
	
	this.showContentAsLayer = function(content, background, title, contentclass, contentid)
	{
		
		if(title && title.length > 0)
		{
			var div = document.createElement("div");
			//div.style.background = backgroundcolor;
			div.appendChild(getHeader(title));
			div.appendChild(content);
			
			if(redline)
			{
				var linered = document.createElement("div");
				linered.id = "line_red";
				linered.name = "line_red";
				linered.innerHTML = "&nbsp;";
				div.appendChild(linered);
			}
			
			contentdiv = gui.layer(div, contentclass, contentid);
		}
		else
			contentdiv = gui.layer(content, contentclass, contentid);
		
		contentdiv.style.visibility = "hidden";
		contentdiv.style.display = "inline";
		contentdiv.style.position = "absolute";
		contentdiv.style.top = 0;
		contentdiv.style.zIndex = -2;
		
		document.getElementsByTagName("body")[0].appendChild(contentdiv);
		
		setTimeout(function(){internalshow(background)}, 1000);
		
	}
	
	var internalshow = function(background)
	{
		setWidthHeight(contentdiv.offsetWidth, contentdiv.offsetHeight);
		
		if(background)
			setBackgroundLayer();
		
		setContentLayer(contentdiv.offsetWidth, contentdiv.offsetHeight, false, true);

		
		if(document.getElementsByTagName("body")[0]!=null && document.getElementsByTagName("body")[0]!=""){
			if(contentdiv!=null && contentdiv!="")
				document.getElementsByTagName("body")[0].removeChild(contentdiv);
		}
		
		contentdiv.style.position = "";
		contentdiv.style.visibility = "";	
		contentdiv.style.zIndex = 0;
				
		if(document.getElementById('LayerContent'+randomid))
			document.getElementById('LayerContent'+randomid).appendChild(contentdiv);
		
		if(fading)
			fadin(2);
		
	}
	
	this.setImage = function(image)
	{
		setWidthHeight(image.width, image.height);
		setContentLayer(image.width, image.height, true, false);
		setCloseDiv();
		
				
		if(document.getElementById('LayerContent'+randomid))
			document.getElementById('LayerContent'+randomid).appendChild(image);
		
	}
	
	this.close = function()
	{
		var content = document.getElementById("LayerContent"+randomid);	
		var background = document.getElementById("LayerBackground"+randomid);
		var iframe = document.getElementById("LayerIFrame"+randomid);
		
		var iframes = content.getElementsByTagName("iframe");
		for(var i=0; i < iframes.length; i++)
		{
			iframes[i].src = "";
		}
		
		if(content)
			document.getElementsByTagName("body")[0].removeChild(content);
		
		if(background)
			document.getElementsByTagName("body")[0].removeChild(background);
		
		if(iframe)
			document.getElementsByTagName("body")[0].removeChild(iframe);
	
	
	
	}
	
	var setWidthHeight = function(breite, hoehe)
	{
		var contenthoehe = document.body.offsetHeight;
		var contentbreite = document.body.offsetWidth;

		if (document.all) {
			height = document.documentElement.clientHeight;
			width = document.body.clientWidth;
		} else {
			height = window.innerHeight;
			width = window.innerWidth;
		}

		width = (width / 2) - (breite / 2);
		height = (height / 2) - (hoehe / 2);

		if (width < 0) {
			width = 0;
		}

		if (height < 0) {
			height = 20;
		}
	}
	
	var setBackgroundLayer = function()
	{
		if(!document.getElementById("LayerBackground"+randomid))
		{
			var background = document.createElement("iframe");
			background.name = "LayerBackground"+randomid;
			background.id = "LayerBackground"+randomid;
			background.style.position = "absolute";
			background.style.top = "0px";
			background.style.left = "0px";
			background.style.border = "none";
			background.frameBorder = "0";
			
			background.style.zIndex = index;
			
			background.style.backgroundColor = backgroundcolor;
			background.style.opacity=backgroundopacity;
			
			if(document.all)
				background.style.filter = 'alpha(opacity=' + (backgroundopacity*100) + ')';
			
			background.style.height = document.getElementsByTagName("html")[0].scrollHeight + "px";
			background.style.width = document.getElementsByTagName("html")[0].scrollWidth + "px";
			
			document.getElementsByTagName("body")[0].appendChild(background);
		}
	}
	
			
	
	var setContentLayer = function(owidth, oheight, closediv, shadow)
	{
		var closedivpixel = 0;
		var top = 0;
		if(closediv)
			closedivpixel = 20;
		
		var layercontent = document.createElement("div");
		
				
		layercontent.name = "LayerContent"+randomid;
		layercontent.id = "LayerContent"+randomid;
		layercontent.style.position = "absolute";
		
		if(document.all)
		{
			layercontent.style.top = (height - 10 + document.documentElement.scrollTop) + "px";
			top = (height - 10 + document.documentElement.scrollTop);
		}
		else
			layercontent.style.top = (height - 10 + window.pageYOffset) + "px";
		
		
		layercontent.style.left = width + "px";
		layercontent.style.zIndex = index + 1;
		
		if(fading)
		{
			if(ie)
				layercontent.style.filter="Alpha(opacity="+oc+", finishopacity=0, style=0)";
			
			layercontent.style.opacity=oc/100;
		}
		
		document.getElementsByTagName("body")[0].appendChild(layercontent);
	}
	
	var getHeader = function(title)
	{
		var div = document.createElement("div");
		div.name = "layerheader";
		div.id = "layerheader";
		
		var div2 = document.createElement("div");
		div2.className = "content1";
		div2.style.cssFloat = "left";
		div2.style.styleFloat = "left";
		div2.innerHTML = title;
		
		div.appendChild(div2);
		div2 = document.createElement("div");
		div2.name = "closeTxt";
		div2.id = "closeTxt";
		
		var ahref = document.createElement("a");
		ahref.id="layerclosebutton";
		ahref.name="layerclosebutton";
		ahref.href = "";
		ahref.className = "close ahrefwhite";
		ahref.onclick = function(){thisObject.close(); return false;};
		var txtClose = (this.noajax) ? "Schließen" : i18n.getText("inter_close");
		ahref.appendChild(document.createTextNode(txtClose));
		
		div2.appendChild(ahref);
		div.appendChild(div2);
		
		return div;
	}
	
	var setKontaktContent = function(contentdiv)
	{
		
		contentdiv.style.visibility = "hidden";
		contentdiv.style.display = "inline";
		contentdiv.style.position = "absolute";
		contentdiv.style.top = 0;
		if(opening==true)
			contentdiv.style.zIndex = 0;
		else
			contentdiv.style.zIndex = -2;
		
		document.getElementsByTagName("body")[0].appendChild(contentdiv);
		
		setWidthHeight(contentdiv.offsetWidth, contentdiv.offsetHeight);
		setContentLayer(contentdiv.offsetWidth, contentdiv.offsetHeight, false, true);
		
		
		document.getElementsByTagName("body")[0].removeChild(contentdiv);
		contentdiv.style.position = "";
		contentdiv.style.visibility = "";	
		if(opening==true)
			contentdiv.style.zIndex = 2;
		else
			contentdiv.style.zIndex = 0;
		
		setBackgroundLayer();
						
		if(document.getElementById('LayerContent'+randomid))
			document.getElementById('LayerContent'+randomid).appendChild(contentdiv);
	
	}
	
	var setDrawingLayerContent = function(text)
	{		
		setWidthHeight(322, 256);
		setContentLayer(322, 256, false, true);
		
				
		if(document.getElementById('LayerContent'+randomid))
			document.getElementById('LayerContent'+randomid).innerHTML=text.substring(text.indexOf("<body>")+6, text.indexOf("</body>"));

	}
	
	var fadin = function(oc)
	{
		var layerdiv = document.getElementById('LayerContent'+randomid);
		if(ie)
			layerdiv.style.filter="Alpha(opacity="+oc+", finishopacity=0, style=0)";
		
		layerdiv.style.opacity=oc/100;
				
		if(oc<100)
		{
			aktiv = window.setTimeout(function(){fadin(oc + 2)},1);
		}
	}
}