var DoMove=false;
var isOverDetail=false;
var isOverPage=false;
var moveElement="";
var xOffset=0;
var yOffset=0;
var didMove=false;
var detailVisible=false;
var currentDetail="";
var allowOpen=true;
var curX=20;
var curY=20;
var loadDone=false;
var boxMoved=false;
/*
var dbFadeIn= new animBox("detailBox",0,0,0,0,20);
var dbFadeOut = new animBox("detailBox",0,0,0,0,20);
dbFadeIn.addFade(0,100);
dbFadeIn.ds="block";
dbFadeOut.addFade(100,0);
dbFadeOut.de="none";
*/

function StartMove(e) {
	if (!DoMove) {
		if (!e) e=window.event;
		
		var od=document.getElementById("detailBox");

		//detailVisible=(od.style.display=="none"?false:true);
		//alert(detailVisible);
		//var on=document.getElementById("operaWarning");
		if (detailVisible && isOverDetail) {
			od.style.zIndex=1000;
			//on.style.zIndex=500;
			od.style.cursor="move";
			
			xOffset=e.clientX-(parseInt(od.style.left)?parseInt(od.style.left):10);
			yOffset=e.clientY-(parseInt(od.style.top)?parseInt(od.style.top):10);
			debug(e.clientX+", "+xOffset+" | "+e.clientY+", "+yOffset+" | "+od.style.left+", "+od.style.top);
		
			moveElement="detailBox";
			DoMove=true;
		} 
		/*else if(detailVisible && isOverPage) 
			document.body.onmouseup= function () { if(isOverPage) closeDetail(); allowOpen=false; document.body.onmouseup=null; return false; };
		allowOpen=true;*/
		
		if (DoMove) {
			document.body.onmousemove=Move;
			document.body.onmouseup=StopMove;
			document.body.ondrag = function () { return false; };
		    document.body.onselectstart = function () { return false; };

			/*document.body.onselectstart = function()
			{
				event.returnValue = false;
				return false;
			};
			document.getElementById("blurElem").focus();
			document.getElementById("blurElem").blur();*/

		}
	} else if (DoMove) StopMove();
	
	return !DoMove;

}

function StopMove(e) {
	if(!e) e=window.event;
	
	if (moveElement.length) {
		var pos=getScrollPos();
		var o=document.getElementById(moveElement);
		curX=parseInt(o.style.left);
		curY=parseInt(o.style.top)-pos;
		o.style.cursor="default";
		DoMove=false;
		document.body.onmousemove=null;
		document.body.onmouseup=null;
		document.body.onselectstart=null;
		document.body.ondrag=null;

		/*if (moveElement=="news") {
			saveNewsPos();
		} else {
			saveDetailPos();
		}*/
		
		moveElement="";
	}
	//isOver=false;
}

function Move(e) {
	if (!e) e=window.event;
	var o=document.getElementById(moveElement);

	o.style.left=(e.clientX-xOffset)+"px";
	o.style.top=(e.clientY-yOffset)+"px";
	didMove=true;
	boxMoved=true;
	
	return false;
}

function showDetail(detailName,title,dotX,dotY) {
	//if (!e) e=window.event;
	if (allowOpen && currentDetail!=detailName) {
	
		var pos=getScrollPos();
		var o=document.getElementById("detailBox");
		var d=document.getElementById("detailContent");
		var t=document.getElementById("detailTemp");
		var mw=250;
		var mh=200;
		var tHTM='<p>'+title+'<\/p>';
		
		if (!title) tHTM='';
		loadDone=false;
		t.style.display="block";
//		if (is('opera')) {
//			d.innerHTML='<img src="'+detailName+'" alt="" onload="centerImageBox();" id="detailBoxImage">'+tHTM+'<p><a href="#" onclick="return closeDetail();">Schließen</a></p>';
//		} else {
			if (detailVisible) {
				mw=(d.offsetWidth-51)/2;
				mh=(d.offsetHeight-51)/2;
			}
			d.innerHTML='<img src="/Images/loading.gif" width="51" height="51" class="detailLoading" id="detailBoxImage" style="padding: '+mh+'px '+mw+'px;">';
			if (detailName=="Type") {
				//alert("Type: "+title);
				if (dotX) {
					tHTM='<p>'+dotX+'</p>';
				} else {
					tHTM='<p>Apartment Typ '+title.toUpperCase()+'</p>';
					if (title=="2P") tHTM='<p>Apartment Typ 2 Penthouse</p>';
					if (title=="2aP") tHTM='<p>Apartment Typ 2A Penthouse</p>';
				}
				t.innerHTML='<img src="/SaleImages/Type'+title+'M.jpg" onLoad="detailLoadDone();" id="detailBoxImage" alt="">'+tHTM+'<p><a href="#" onclick="return closeDetail();">Schließen</a></p>';
			} else if(detailName=="Overview") {
				dotX=Math.round(dotX*0.75);
				dotY=Math.round(dotY*0.75);
				tHTM='<p>Grundri&szlig;, '+title+'. Etage</p>';
				t.innerHTML='<div style="position:relative"><div style="position:absolute; left: 0'+dotX+'px; top: 0'+dotY+'px;">'+
			 				'<img src="/SaleImages/MarkerM.gif" width="15" height="15"></div>'+
							'<img src="/SaleImages/OverviewM.jpg" onLoad="detailLoadDone();" id="detailBoxImage" alt="">'+tHTM+
							'<p><a href="#" onclick="return closeDetail();">Schließen</a></p></div>';
				//dummy
			} else {
				t.innerHTML='<img src="'+detailName+'" onLoad="detailLoadDone();" id="detailBoxImage" alt="">'+tHTM+'<p><a href="#" onclick="return closeDetail();">Schließen</a></p>';
			}
//		}
		if (!detailVisible) {
			if (!boxMoved) {
				curX=getWindowWidth()/2-180;
				curY=getWindowHeight()/2-175;
			} 
			o.style.left=curX+"px";
			o.style.top=curY+pos+"px";
			o.style.display="block";
			centerImageBox();
		}
		o.style.display="block";
//		startAnim(dbFadeIn);
		detailVisible=true;
		currentDetail=detailName;
	} 
	allowOpen=true;

	return false;
}

function detailLoadDone(o) {
//	if (o.offsetWidth>0) {
		var dc=document.getElementById("detailContent");
		var dt=document.getElementById("detailTemp");
		
		dt.style.display="block";
		while (dt.offsetWidth==0) { /* dummy */ }
		
		centerImageBox(true);
		centerImageBox(true);  // and again to make sure firefox and Safari reposition the box BEFORE resizing due to new content... 
		dc.innerHTML=dt.innerHTML.replace(/onLoad="?detailLoadDone\(.*?\);"?/img, '');
		dt.style.display="none";
		dt.innerHTML="";
		loadDone=true;
//	}
}

function centerImageBox(useTemp) {
	var pos=getScrollPos();
	var bo=document.getElementById("detailBox");
	if (!boxMoved) {
		if (useTemp) {  // reposition box based on size of temp div
			var to=document.getElementById("detailTemp");
			curX=getWindowWidth()/2-(to.offsetWidth+60)/2;
			curY=getWindowHeight()/2-(to.offsetHeight+60)/2;
			
		} else {	// reposition based on actual size
			curX=getWindowWidth()/2-bo.offsetWidth/2;
			curY=getWindowHeight()/2-bo.offsetHeight/2;
		}
		//alert(getWindowWidth()+" - "+io.offsetWidth+" - "+bo.offsetWidth+"\n"+getWindowHeight()+" - "+io.offsetHeight+" - "+bo.offsetHeight+"\n"+x+" - "+y);
	} else {
		if (curX+bo.offsetWidth-15>getWindowWidth()) curX=getWindowWidth()-(bo.offsetWidth-15);
		if (curY+bo.offsetHeight-15>getWindowHeight()) curY=getWindowHeight()-(bo.offsetHeight-15);
	}
	if (curX<-15) curX=-15;
	if (curY<-15) curY=-15;
	bo.style.left=curX+"px";
	bo.style.top=(curY+pos)+"px";
}

function getWindowWidth() {
	if(document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if(document.body.clientWidth)
		return document.body.clientWidth;
	else if (window.innerWidth) 
		return window.innerWidth;
	else return 0;
}

function getWindowHeight() {
	if(document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if(document.body.clientHeight)
		return document.body.clientHeight;
	else if (window.innerHeight) 
		return window.innerHeight;
	else return 0;
}

function closeDetail(e) {
	if (!e) e=window.event;

	detailVisible=false;
	var pos=getScrollPos();
	var o=document.getElementById("detailBox");
	o.style.display="none";
//	startAnim(dbFadeOut);
	curX=parseInt(o.style.left);
	curY=parseInt(o.style.top)-pos;
	currentDetail="";

	return false;
}

function getScrollPos() {
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	return y;
}

function setOffset(e) {
	if (!e) e=window.event;
	
	var pos=getScrollPos();
	var o=document.getElementById("debug");	
	var nb=10-pos;
	
	if (nb!=parseInt(o.style.bottom))
		o.style.bottom=10-pos+"px";

	debug(pos);

}
	

document.onmousedown=StartMove;
window.onscroll = document.documentElement.onscroll = setOffset;

function formatDetail() {
	var newWidth=document.getElementById("detailContent").offsetWidth;
	var newHeight=document.getElementById("detailContent").offsetHeight;
	document.getElementById("detail").style.width=newWidth+60;
	document.getElementById("detail").style.height=newHeight+70;
	document.getElementById("detailSpacer1").width=newWidth+60;
	document.getElementById("detailSpacer1").height=newHeight+70;
	document.getElementById("detailSpacer2").width=newWidth+20;
	document.getElementById("detailSpacer2").height=newHeight+30;
	if (is("gecko")) document.getElementById("detailContent").style.top=20;
	if (is("opera")) {
		document.getElementById("detailClose").style.left=newWidth+15;
	} else {
		document.getElementById("detailClose").style.left=newWidth+25;
	}
	document.getElementById("detail").style.visibility="visible";
}

function showDetailOld(detailName) {
	debugPrint(detailName);
	loadFragmentInToElement("d_"+detailName, 'detailContent');
	document.getElementById("detail").style.visibility="visible";
	var x=readCookie("detailPosX");
	var y=readCookie("detailPosY");
	if(x && y) {
		var de=document.getElementById("detail");
		de.style.left=x;
		de.style.top=y;
	}
}


function hideDetail() {
	isOverDetail=false;
	StopMove();
	document.getElementById("detail").style.visibility="hidden";
}

function saveDetailPos() {
	var ne=document.getElementById("detailBox");
	if (ne.style.visibility!="hidden") {
		writeCookie("detailPosX",ne.style.left,90);
		writeCookie("detailPosY",ne.style.top,90);
	}
}


function changeTrans(elem,p) {
	var e=document.getElementById(elem);
	e.style.filter="alpha(opacity="+p+")";
	e.style.mozopacity=p/100;
	e.style.khtmlopacity=p/100;
	e.style.opacity=p/100;
}

function debug(t) {
	document.getElementById("debug").innerHTML=t;
}

function is(browserName) {
	if (navigator.userAgent.toLowerCase().indexOf(browserName.toLowerCase())>-1) {
		return true;
	} else {
		return false;
	}
}
