var AjaxTop = null;
var AjaxInformation = null;

var AjaxLoad = false;
var LastUrl = window.location.href;

function ShowPic(file,w,h){
    window.open('../../image.php?image='+file,'Bild','left=0,top=0,height='+(h+50)+',width='+(w+25)+',scrollbars=yes,resizable=yes');
}

function Show(id){
    window.open('../../image.php?id='+id,'Bild','left=0,top=0,scrollbars=yes,resizable=yes');
}

function _onContentReceive(){
    if(!ContentElement) ContentElement = document.getElementById("BodyContentDiv");
    if(Ajax.State()==4){
        ContentElement.innerHTML = Ajax.Content();
		var content = Ajax.Content();

		/*
		for(var i=0;i<document.images.length;i++){
            if(document.images[i].src.substring(0,CONTENT_DIR.length)==CONTENT_DIR){
            	document.images[i].src = SITE_LOCATION+"/"+CurrentMesse+"/"+CurrentDir+"/"+document.images[i].src.substring(CONTENT_DIR.length+1+SITE_MESSE.length);
            }
        }
        
        // ALT
        for(var i=0;i<document.images.length;i++){
            if(document.images[i].src.substring(0,n)==window.location.href.substring(0,n)){
            	document.images[i].src = document.images[i].src.replace("/"+ThisDir+"/","/"+CurrentDir+"/");
            }
        }
        */

        /*for(var i=0;i<document.links.length;i++){
            if(document.links[i].href.substring(0,n)==window.location.href.substring(0,n)){
				if(GetDir(document.links[i].href)!="uploads"){
				    alert(document.links[i].getAttribute("href"));
                	var pos = document.links[i].href.lastIndexOf("/",document.links[i].href.lastIndexOf("/"))+1;
                	document.links[i].href = SITE_LOCATION+"/"+CurrentMesse+"/"+CurrentDir+"/"+document.links[i].href.substring(pos);
                }
            }
        }*/
        AjaxLoad = 0;
    }else{
        if(AjaxLoad==1){
            ContentElement.innerHTML = Loading;
            AjaxLoad = 2;
        }
    }
}

function GetDir(url){
    var n = url.lastIndexOf("/");
    return url.substring(url.lastIndexOf("/",n-1)+1,n);
}

function LoadContent(url,elmid){
    if(!ContentElement) ContentElement = document.getElementById("BodyContentDiv");
    if(ContentElement){
         if(AjaxLoad==0) ContentElement.innerHTML = Loading;
    }
    Ajax = FCreateAjax();
    Ajax.http.onreadystatechange = _onContentReceive;
    Ajax.Request(url,"POST",true,"only_content=1");
    AjaxLoad = 1;
    LastUrl = url;
    CurrentDir = GetDir(url);
    if(LastUrl) SetCookie("curpage",LastUrl);
    if(elmid!=undefined){
        var obj = document.getElementById(elmid);
        var i = true;
		if(obj.OnClickFunction){
			 i = obj.OnClickFunction(obj);
		}
	    if(i==true && obj.SubMenu){
	        obj.SubMenu.EffectInstance.Open();
	    }
    }
}

function Clear(){
    SetCookie("curpage","");
}

function SetAjaxTop(){
    if(AjaxTop.State()==4){
		var Main = document.getElementById("BodyMain");
		var Top = document.getElementById("BodyHeaderTr");
		if(Top){
		    var content = AjaxTop.Content();
		    try{
				Top.innerHTML = content;
			}catch(e){
				var n = content.indexOf("\n");
				var line = content.substring(0,n);
				var elm = GetFirstChild(Top,"TD");
				if(!elm) elm = document.createElement("TD");
				line = line.substring(line.indexOf("<td ")+4);
				line = line.substring(0,line.indexOf(">"));
				elm.colSpan = "3";
				var attrs = line.split(" ");
				for(var i=0;i<attrs.length;i++){
				    var pos = attrs[i].indexOf("=");
				    if(pos!=-1){
				        elm.setAttribute(attrs[i].substring(0,pos),attrs[i].substring(pos+2,attrs[i].length-1));
				    }
				}
				
				content = content.substring(n);
				n = content.lastIndexOf("</td>");
				content = content.substring(0,n);
				elm.innerHTML = content
				Top.appendChild(elm);
			}
		}
	}
}

function SetAjaxInformation(){
    if(AjaxInformation.State()==4){
		document.getElementById("BodyInformation").innerHTML = AjaxInformation.Content();
	}
}

function GotoMesse(url,elmid)
{
	var snip = url.substring(SITE_LOCATION.length+1);
	CurrentMesse = snip.substring(0,snip.indexOf("/"));
    LoadContent(url,elmid);

    AjaxTop = FCreateAjax();
    AjaxTop.http.onreadystatechange = SetAjaxTop;
    AjaxTop.Request(url,"POST",true,"only_top=1");
	
	
    AjaxInformation = FCreateAjax();
    AjaxInformation.http.onreadystatechange = SetAjaxInformation;
    AjaxInformation.Request(url,"POST",true,"only_info=1");
    
	// by trame medien
	var ref_lauf = document.getElementById("ref_lauf");
	var ref_lauf_immo = document.getElementById("ref_lauf_immo");
	
	if(elmid == 'LinkNews_immomesse')
	{
		ref_lauf.style.display='none';
		ref_lauf_immo.style.display='block';
		
		document.getElementById("immo_m").start();
	}
	else{
		ref_lauf.style.display='block';
		ref_lauf_immo.style.display='none';
		
		document.getElementById("m").start();
	}
}

function Goto(url)
{
	Clear();	
	window.location.href = url;
}