// ////// ********** SCRIPT FOR MAP.ASPX ********** ////// //

// /////////// Global functions /////////// //

window.onresize = setMapBar;

function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  value = value.replace(/^\n+/,'');
  value = value.replace(/\n+$/,'');
  return value;
}

function loadXMLDoc(dname) 
{
var xmlDoc;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET",dname,false);
  xmlDoc.send("");
  return xmlDoc.responseXML;
  }
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return xmlDoc;
  }
alert("Error loading document");
return null;
} 

function getWindowHeight() {
    var windowHeight=0;
    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;
		    }
	    }
    }
    return windowHeight;
}

// /////////// Show/Close Box animatie /////////// //
function closeMapVeldInfo(evt) {
    if (document.getElementById("mapVeldInfo")) {
        div = document.getElementById("mapVeldInfo");
        div.style.overflow = "hidden";
        startHeight = div.style.height;
        div = document.getElementById('mapVeldInfo');
	    zz = new Tween(div.style,'height','',parseInt(startHeight),10,1,'px');
        if(!evt) evt = window.event;
		var maxHeight = 0;
		var duration = 0.2;
		var easingFunc = Tween.backEaseIn;
		zz.func = easingFunc;
		zz.continueTo(maxHeight,duration);
		zz.onMotionFinished = function(){div.style.display = 'none'};
    }
}
function showMapVeldInfo(evt) {
    getMapVeldInfo(evt);
    if (document.getElementById("mapVeldInfo")) {
        div = document.getElementById("mapVeldInfo");
        div.style.display = "block";
        div.style.overflow = "hidden";
        startHeight = 0;
        div = document.getElementById('mapVeldInfo');
	    zz = new Tween(div.style,'height','',parseInt(startHeight),10,1,'px');
        if(!evt) evt = window.event;
		var maxHeight = 400;
		var duration = 1.0;
		var easingFunc = Tween.bounceEaseOut;
		zz.func = easingFunc;
		zz.continueTo(maxHeight,duration);
		//zz.onMotionFinished = function(){div.style.overflow = 'auto'};
    }
}


// /////////// Show veldInfo in Box /////////// //

var numTotal;
var numCounter;
var arrImgPath;
var divImg;
var divCounter;

function getMapVeldInfo(id) {
    
    docXML = loadXMLDoc("../_handlers/DataFileRequestHandler.ashx?strFile=map.xml");
    
    // Get active "veld"-node
    var i,rootElement,arrIDs,activeNode
    if(docXML!=null){
		rootElement = docXML.documentElement;
        if(rootElement!=null){
		    for(i=0;i<rootElement.childNodes.length;i++){
		 	    if(rootElement.childNodes[i].nodeType == 1 && 
		 	    rootElement.childNodes[i].getAttribute("id") == id){
				    activeNode = rootElement.childNodes[i];
				    break;
			    }
		    }
        }
	}
	
	// Get content from active "veld"-node, except "visuals"
	var strTitel,strSoort,strVisual,strFase,strTekst,strStart;
	for (var i=0;i<activeNode.childNodes.length;i++) {
	    if (activeNode.childNodes[i].nodeName == "titel" && activeNode.childNodes[i].firstChild) 
	        strTitel = trim(activeNode.childNodes[i].firstChild.nodeValue);
	    if (activeNode.childNodes[i].nodeName == "soort" && activeNode.childNodes[i].firstChild)
	        strSoort = trim(activeNode.childNodes[i].firstChild.nodeValue);
	    if (activeNode.childNodes[i].nodeName == "fase" && activeNode.childNodes[i].firstChild)
	        strFase = trim(activeNode.childNodes[i].firstChild.nodeValue);
	    if (activeNode.childNodes[i].nodeName == "tekst" && activeNode.childNodes[i].firstChild)
	        strTekst = trim(activeNode.childNodes[i].firstChild.nodeValue);
	    if (activeNode.childNodes[i].nodeName == "start" && activeNode.childNodes[i].firstChild)
	        strStart = trim(activeNode.childNodes[i].firstChild.nodeValue);
	}
	// Get visuals (array img srcs)
	var strVisual;
	arrImgPath = new Array();
	var num = 0;
	var tempImgPath = "noPhoto";
	if (activeNode.childNodes) {
	    for (var i=0;i<activeNode.childNodes.length;i++) {
	        if (activeNode.childNodes[i].nodeName == "visuals") {
	            var visualsNode = activeNode.childNodes[i];
                if (visualsNode.childNodes) {
                    for (var j=0;j<visualsNode.childNodes.length;j++) {
                        if (visualsNode.childNodes[j].nodeName == "visual" && visualsNode.childNodes[j].firstChild) {
                            arrImgPath[num] = visualsNode.childNodes[j].firstChild.nodeValue;
                            tempImgPath = arrImgPath[num];
                            num++;
                        }    
                    }
                }
	        }
	    }
	}
	arrImgPath[num-1] = tempImgPath;
	
	if (strSoort=="" || strSoort == null) strSoort = "0";
	if (strSoort == "0") strSoort = "Naamloos object";
	if (strSoort == 1) {
	    strSoort = "Kavels te koop";
    } else if (strSoort == 2) {
        strSoort = "Woningen te koop";
    } else if (strSoort == 3) {
        strSoort = "Ontwerp je woning";
    } else if (strSoort == 4) {
        strSoort = "Woningen te huur";
    } else if (strSoort == 5) {
        strSoort = "Verkocht";
	} else if (strSoort == 6) {
        strSoort = "Openbare Ruimte";
    } else if (strSoort == 7) {
        strSoort = "Toekomstige hoogbouw";
    } else if (strSoort == 8) {
        strSoort = "Overig";
    } else {
        strSoort = "Onbekend objectsoort";
    }
	if (strTitel=="" || strTitel == null) strTitel = "Geen titel";
	if (strFase=="" || strFase == null) strFase = "0";
	if (strTekst=="" || strTekst == null) strTekst = "Er is momenteel geen informatie over "+ strTitel +" beschikbaar.";
	if (strStart=="" || strStart == null) strStart = " ";
	
	// Render HTML text
	if (document.getElementById("veldTextTitle"))
	    document.getElementById("veldTextTitle").innerHTML = strTitel;
	if (document.getElementById("soort"))
	    document.getElementById("soort").innerHTML = strSoort;
	if (document.getElementById("text"))
	    document.getElementById("text").innerHTML = strTekst;
	if (document.getElementById("start"))
	    document.getElementById("start").innerHTML = strStart;
	if (document.getElementById("fase")) {
	    document.getElementById("fase").innerHTML = "";
        for (var i=1;i<8;i++) {
            if (document.getElementById("txtFase_" + i + ""))
                document.getElementById("txtFase_" + i + "").style.display = "none";
        }
        if (!isNaN(strFase)) {
            if (strFase > 0 && strFase < 9) {
                if (document.getElementById("txtFase_" + strFase + ""))
                    document.getElementById("txtFase_" + strFase + "").style.display = "block";
                var strFaseRoman = "";
				if (strFase == 1) strFaseRoman = "I";
				if (strFase == 2) strFaseRoman = "II";
				if (strFase == 3) strFaseRoman = "III";
				if (strFase == 4) strFaseRoman = "IV";
				if (strFase == 5) strFaseRoman = "V";
				if (strFase == 6) strFaseRoman = "VI";
				if (strFase == 7) strFaseRoman = "VII";
				if (strFase == 8) strFaseRoman = "VIII";
				if (document.getElementById("fase")) document.getElementById("fase").innerHTML = "Fase: " + strFaseRoman;
            }
        } else {
            if (document.getElementById("fase")) {
                var divFase = document.getElementById("fase");
                if (strFase.indexOf("www.") > -1) {
                    var a = document.createElement("a")
                    divFase.appendChild(a);
                    divFase.firstChild.setAttribute("href","http://" + strFase);
                    //divFase.firstChild.setAttribute("target","_blank");
                    var aNode = divFase.firstChild;
                    aNode.innerHTML = "Klik hier voor meer informatie";
                } else {
                    divFase.innerHTML = strFase;
                }
            }
	    }
    }
	
	// Vul html Img met eerste uit array
	if (document.getElementById("visual")) divImg = document.getElementById("visual");
	if (arrImgPath[0]) {
        if (divImg)
            divImg.innerHTML = "<div><img alt='' src='" + arrImgPath[0] + "'></div>";
	} else {
        if (divImg)
            divImg.innerHTML = "<div><img alt='' src='/_images/velden/noPhoto.gif'></div>";
    }

	if (arrImgPath[1]) {
	    numTotal = arrImgPath.length;
	} else {
	    numTotal = 1;
	}
	numCounter = 0;
	
	// Toon Img Tools indien > 1 Images
	if (document.getElementById("visualTools")) {
	    if (numTotal > 1) {
	        if (document.getElementById("Counter")) {
	            divCounter = document.getElementById("Counter")
	            document.getElementById("visualTools").style.display = "block";
	            divCounter.innerHTML = "Afbeelding 1 van " + numTotal;
	        }
	    } else {
	        document.getElementById("visualTools").style.display = "none";
	    }
	}
}

// prev/next buttons functionaliteiten
function prevVisual() {
    window.focus();
    numCounter--;
    if (numCounter < 0) numCounter = arrImgPath.length-1;
    divImg.innerHTML = "<img src='" + arrImgPath[numCounter] + "' alt='' />";
    divCounter.innerHTML = "Afbeelding " + (numCounter+1) + " van " + numTotal;
}
function nextVisual() {
    window.focus();
    numCounter++;
    if (numCounter == arrImgPath.length) numCounter = 0;
    divImg.innerHTML = "<img src='" + arrImgPath[numCounter] + "' alt='' />";
    divCounter.innerHTML = "Afbeelding " + (numCounter+1) + " van " + numTotal;
}


// /////////// Show/Hide Dots checkboxes /////////// //

var arrStatusTypes = new Array(8);
arrStatusTypes[0] = "Tekoop";
arrStatusTypes[1] = "WoTekoop";
arrStatusTypes[2] = "Ontwerp";
arrStatusTypes[3] = "Tehuur";
arrStatusTypes[4] = "Verkocht";
arrStatusTypes[5] = "Openbareruimte";
arrStatusTypes[6] = "Hoogbouw";
arrStatusTypes[7] = "Overig";

// zet aan/uit o.b.v. click checkbox
function showHideDots(strType,bChecked) {
    if (!bChecked) {
        var strDisplay = "none";
    } else {
        var strDisplay = "block";
    }
    var strType = strType;
    
    for(var i=0; i<arrStatusTypes.length; i++) {
        if (strType == "cb" + arrStatusTypes[i]) {
            var arrDots = document.getElementsByTagName("div");
            var statusType = arrStatusTypes[i];
            for (var j=0; j<arrDots.length; j++) {
                if (arrDots[j].className == "dot" + statusType) {
                    arrDots[j].style.display = strDisplay;
                }
                if (arrDots[j].className == "headerTitle" + statusType) {
                    arrDots[j].style.display = strDisplay;
                }
                if (arrDots[j].className == "intro" + statusType) {
                    arrDots[j].style.display = strDisplay;
                }
            }
        }
    }
}
// zet aan/uit o.b.v. querystring
function setDots() { 
    var chosenStatus = location.search.substring(1,location.search.length);
    chosenStatus = chosenStatus.substring(chosenStatus.indexOf("=")+1);
    for (var i=0; i<arrStatusTypes.length; i++) {
        if (document.getElementById("cb" + chosenStatus)) {
            var cbChosen = document.getElementById("cb" + chosenStatus);
            cbChosen.checked = true;
        }
        if (chosenStatus != "") {
            if (document.getElementById("cb" + arrStatusTypes[i]).checked == false)
                showHideDots("cb" + arrStatusTypes[i], false);
        } else {
            document.getElementById("cb" + arrStatusTypes[i]).checked = true;
        }
    }
}
// Zet balk met checkboxes aan onderkant van scherm
function setMapBar() {
    var windowHeight = getWindowHeight();
    if (document.getElementById("mapBar")) {
        var mapBar = document.getElementById("mapBar");
        var headerHeight = 121;
        var yPos = windowHeight - mapBar.offsetHeight - headerHeight;
        var minHeight = 760;
        if(windowHeight > 550 && windowHeight < minHeight) {
            if (document.getElementById("header")) {
                mapBar.style.top = yPos;
                document.getElementById("contentMapWrapper").style.height = windowHeight - headerHeight;
            }
        }
    }
}

//////  END SCRIPT FOR MAP.ASPX  //////
