/* --- Configuration Variables --- */

var detailBoxFade = true;
var detailDarkBackground = true;
var detailAlwaysCenter = true;
var detailSearchImages = true;

/* -------------------------------- */

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 resW=0;
var resH=0;
var loadDone=false;
var boxMoved=false;
var imgList=new Array();

var dbFadeIn= new animBox("detailBox",0,0,0,0,15);
var dbFadeOut = new animBox("detailBox",0,0,0,0,15);
dbFadeIn.addFade(0,100);
dbFadeIn.ds="block";
dbFadeOut.addFade(100,0);
dbFadeOut.de="none";


function fillImgList() {
	if (detailSearchImages) {
		var o;
		var r;
	
		for (var i=0; i< document.getElementsByTagName("a").length; i++) { 
		
			o=document.getElementsByTagName("a")[i]
			for (var a=0; a<o.attributes.length; a++) {
				if (o.attributes[a].name=="onclick") {
					if (o.attributes[a].value.match(/showDetail\(.*\)/)) {
						r=o.attributes[a].value.match(/['"].*?['"]/g);
						for (var x=0; x<r.length; x++) {
							r[x]=r[x].substring(1,r[x].length-1);						
						}
						imgList.push(r);
					}
	//				s=o.attributes[a].value.replace("return ","");
				}
			}
		
		}
	}
}

function inImgList(s) {
	
	if (imgList.length>1) {
		for (var i=0; i<imgList.length; i++) {
			if (imgList[i][0]==s) 
				return i;
		}
	}
	return -1;
	
}

function hasPrevImg(s) {
	if (imgList.length<=1) 
		return false;
	
	if (inImgList(s)>0)
		return true;
	else
		return false;
}

function hasNextImg(s) {
	if (imgList.length<=1) 
		return false;
	
	if (inImgList(s)<imgList.length-1)
		return true;
	else
		return false;
}

function nextImg() {
	var i=inImgList(currentDetail)+1;
	
	if (i<imgList.length) 
		showDetail(imgList[i][0],imgList[i][1]);
		
}
function prevImg() {
	var i=inImgList(currentDetail)-1;
	
	if (i>=0)
		showDetail(imgList[i][0],imgList[i][1]);
}



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 pos=0;  // ScrollPos not needed when DetailBox is "position: fixed;". Keep variable though so it's easier to put it back in if ever needed. 
		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 pos=0;  // ScrollPos not needed when DetailBox is "position: fixed;". Keep variable though so it's easier to put it back in if ever needed. 
		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;
		document.getElementById("detailFull").style.display="";
		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="detailBoxImageLoading" 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 (hasPrevImg(detailName))
			document.getElementById('detailPrev').style.display="block";
		else
			document.getElementById('detailPrev').style.display="none";

		if (hasNextImg(detailName))
			document.getElementById('detailNext').style.display="block";
		else
			document.getElementById('detailNext').style.display="none";

		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();
		}
		if (detailDarkBackground) 
			document.getElementById("dbDarkBack").style.display="block";
		if (detailBoxFade && !detailVisible) {
			o.style.filter="alpha(opacity=0)";
			o.style.opacity=0;
			o.style.MozOpacity=0;
			startAnim(dbFadeIn);
		} else {
			o.style.display="block";
		}
		currentDetail=detailName;
		detailVisible=true;
		document.onkeydown=imgKeys;
	} 
	allowOpen=true;

	return false;
}

function imgKeys(e) {
	if (!e) e=window.event;
	
	var o=e;
	if (e.type=="keydown") {
//		if (e.charCode==32) nextImg();
		if (e.keyCode==37) prevImg();
		if (e.keyCode==39) nextImg();
		if (e.keyCode==27) closeDetail();
	}
	

}

function detailLoadDone(o) {
	if (loadDone==false) {
		loadDone=true; // Firefox 3.6 Fix to avoid that this get's called twice and hangs the second time... 
		var dc=document.getElementById("detailContent");
		var dt=document.getElementById("detailTemp");
		
		dt.style.display="block";
		while (dt.offsetWidth==0) { /* dummy */ }

		resW=0;
		resH=0;
		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 pos=0;  // ScrollPos not needed when DetailBox is "position: fixed;". Keep variable though so it's easier to put it back in if ever needed. 
	var wW=getWindowWidth();
	var wH=getWindowHeight();
	var bo=document.getElementById("detailBox");
	var to=document.getElementById("detailTemp");
	if (!boxMoved) {
		if (useTemp) {  // reposition box based on size of temp div
			if ((to.offsetWidth+30)>wW | (to.offsetHeight+30)>wH) {
				ResizeImage();
				curX=Math.round(wW/2-(resW+60)/2);
				curY=Math.round(wH/2-(resH+60)/2);
			} else {
				if (resW | resH) {
					curX=Math.round(wW/2-(resW+60)/2);		// check if image has been resized and use reseize Values if that is the case. 
					curY=Math.round(wH/2-(resH+60)/2);		// Opera 11 and Webkit browsers are too slow updating the detailTemp size after image resize. 
				} else {
					curX=Math.round(wW/2-(to.offsetWidth+60)/2);
					curY=Math.round(wH/2-(to.offsetHeight+60)/2);
				}
			}
		} else {	// reposition based on actual size     ### DOESN'T WORK in Firefox?!?
			curX=wW/2-bo.offsetWidth/2;
			curY=wH/2-bo.offsetHeight/2;
		}
		//alert(getWindowWidth()+" - "+io.offsetWidth+" - "+bo.offsetWidth+"\n"+getWindowHeight()+" - "+io.offsetHeight+" - "+bo.offsetHeight+"\n"+x+" - "+y);
	} else {
		if ((to.offsetWidth+30)>wW | (to.offsetHeight+30)>wH) {
			ResizeImage();
			if (detailAlwaysCenter) {
				curX=Math.round(wW/2-(resW+60)/2);
				curY=Math.round(wH/2-(resH+60)/2);
			}
		}
	}
//	if (curX<-15) curX=-15;
//	if (curY<-15) curY=-15;
	bo.style.left=curX+"px";
	bo.style.top=(curY+pos)+"px";
}

function ResizeImage() {
	var to=document.getElementById("detailTemp");
	var di=document.getElementById("detailBoxImage");

	var wW=getWindowWidth();
	var wH=getWindowHeight();

	var iR=di.offsetWidth/di.offsetHeight;
	var iNH=di.offsetHeight;
	var iNW=di.offsetWidth;
	var vO=0;
	var hO=0;
	

	if (to.offsetHeight+30>wH) {
		vO=to.offsetHeight+30-wH;
		iNH=di.offsetHeight-vO;
		iNW=iNH*iR;
		document.getElementById("detailFull").style.display="block";
	}
	
	if (iNW+30>wW) {
		hO=iNW+30-wW;
		iNW=iNW-hO;
		iNH=iNW/iR;
//		resd=true;
		document.getElementById("detailFull").style.display="block";
	}

	resH=iNH+(to.offsetHeight-di.offsetHeight);
	resW=iNW+(to.offsetWidth-di.offsetWidth);
	di.style.width=Math.round(iNW)+"px";
	di.style.height=Math.round(iNH)+"px";
	document.getElementById("detailFull").style.top=Math.round(iNH)+10+"px";
	
}
		
function fullDetail() {
	
	var di=document.getElementById("detailBoxImage");
	di.style.width="";
	di.style.height="";
	document.getElementById("detailFull").style.display="";
	centerImageBox(false);
	
}
	

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;
	window.onkeydown=null;
	boxMoved=false;
	//var pos=getScrollPos();
	var pos=0;  // ScrollPos not needed when DetailBox is "position: fixed;". Keep variable though so it's easier to put it back in if ever needed. 
	var o=document.getElementById("detailBox");
	if (detailBoxFade) {
		startAnim(dbFadeOut);
	} else {
		o.style.display="none";
	}
	if (detailDarkBackground) 
		document.getElementById("dbDarkBack").style.display="none";
	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;




/* Old functions, no longer used */


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;
	}
}

