// BEHAVIOR LAYER
var resetBtn = document.getElementsBySelector('.reset');
var optionList2;
var optionList3;
resetBtn[0].onclick = function() {
	var sel1 = document.getElementById("district");
	var sel2 = document.getElementById("county");
	var sel3 = document.getElementById("commune");
	refreshDynamicSelectOptions(sel1, sel1, sel2, optionList2, sel3, optionList3);
	
}
// create print page link on the fly if key items are supported
var hasPrintFeature = (window.print) ? true : false;
var supportsW3CDOM = (document.createElement && document.createTextNode && document.insertBefore && document.appendChild);
var functionsNode = document.getElementById('nav-functions');
if (functionsNode == null) {
    functionsNode = document.getElementsBySelector('.popup .section #functions')[0];
    if (functionsNode == null) {
        functionsNode = document.createElement("ul");
        functionsNode.setAttribute("id", "functions");
    }
}
var a, li, span, ul;
var TEXT_DIVIDER = ' | ';
var PRINT_PAGE_ID = 'page-print';

if (hasPrintFeature && supportsW3CDOM && functionsNode && typeof TEXT_PRINT_PAGE == 'string') {
    li = document.createElement('li');
    li.setAttribute('id', PRINT_PAGE_ID);
    a = document.createElement('a');
    a.setAttribute('title', TEXT_PRINT_PAGE_TITLE);
    a.setAttribute('href', '#');
    a.onclick = function() {
        window.print();
        return false;
    }
    a.appendChild(document.createTextNode(TEXT_PRINT_PAGE));
    li.appendChild(a);
    if (functionsNode.tagName.toLowerCase() == "ul") {
        if (document.getElementsBySelector('.popup .section #functions li').length == 0) {
            li.className = "last " + li.className;
        }
        functionsNode.insertBefore(li, functionsNode.firstChild);
        a.className = "print";
        var headline = document.getElementsBySelector('.popup .section h2')[0];
        if (headline != null) {
            headline.parentNode.insertBefore(functionsNode, headline.nextSibling);
        }
    } else if (functionsNode.getElementsByTagName('ul').length == 1) {
        span = document.createElement('span');
        span.appendChild(document.createTextNode(TEXT_DIVIDER));
        li.appendChild(span);
        functionsNode.getElementsByTagName('ul')[0].insertBefore(li, functionsNode.getElementsByTagName('ul')[0].firstChild);
    } else {
        var ul = document.createElement('ul');
        ul.appendChild(li);
        functionsNode.appendChild(ul);
    }
}

function dynamicSelect(id1, id2, id3) {
	// Browser and feature tests to see if there is enough W3C DOM support
    
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options
		var clonedOptions2 = clone.getElementsByTagName("option");
		optionList2 = clonedOptions2;
		var temp = [];
        for (var i=0; i<clonedOptions2.length; i++) {
            temp.push(clonedOptions2[i]);
        }
        clonedOptions2 = temp;
        
		var sel3 = null;
		var clonedOptions3 = null;
		if (id3) {
            temp = [];
			sel3 = document.getElementById(id3);
			clone = sel3.cloneNode(true);
			clonedOptions3 = clone.getElementsByTagName("option");
			optionList3 = clonedOptions3;
            for (var i=0; i<clonedOptions3.length; i++) {
                temp.push(clonedOptions3[i]);
            }
            clonedOptions3 = temp;
		}
		// Onload init: call a generic function to display the related options in the dynamic select box
		//refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel1, sel2, clonedOptions2, sel3, clonedOptions3);
		};
		sel2.onchange = function() {
			refreshDynamicSelectOptions(sel2, sel1, sel2, clonedOptions2, sel3, clonedOptions3);
		};
	}
}

function refreshDynamicSelectOptions(changeSrc, sel1, sel2, clonedOptions2, sel3, clonedOptions3) {
	// Delete all options of the dynamic select box
	var resetAction = changeSrc.selectedIndex == 0;
	if (changeSrc == sel1) {
		while (sel2.options.length) {
			sel2.remove(sel2.options.length - 1);
		}
	}

	// Create regular expression objects for "select" and the value of the selected option of the main 
    // select box as class names
	if (resetAction) {
		if (sel1.selectedIndex == 0) {
			var pattern1 = /(.*)/;
			var pattern2 = null;
		} else {
			var pattern1 = /( |^)(select)( |$)/;
			var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")");
		}
	} else {
		var pattern1 = /( |^)(select)( |$)/;
		var pattern2 = new RegExp("( |^)(" + changeSrc.options[changeSrc.selectedIndex].value + ")");
	}
	if (changeSrc == sel1) {
		fillInOptions(sel2, clonedOptions2, pattern1, pattern2);
	}
	if (sel3) {
		while (sel3.options.length) {
			sel3.remove(sel3.options.length - 1);
		}
		fillInOptions(sel3, clonedOptions3, pattern1, pattern2);
	}
}

function fillInOptions(select, options, pattern1, pattern2) {
	for (var i = 0; i < options.length; i++) {
		//Hinzugefügt am 27.09.2011 von martin.sonnenholzer@aperto.de, um eine allgemeine Suche zu ermöglichen
		if(options[i].getAttribute("value") == '' && !options[i].className.match(pattern1)) {
			select.appendChild(options[i].cloneNode(true));
		}
		// If the classname of a cloned option either equals "select" or equals the 
        // value of the selected option of the main select box
		if (options[i].className.match(pattern1) || options[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			select.appendChild(options[i].cloneNode(true));
		}
	}
}

// attach events to search form input
var searchInputEl = document.getElementById('search-input');
if (searchInputEl) {
    // searchInputEl.setAttribute('autocomplete', 'off'); // prevents exception occuring with mozilla's autocomplete feature
    // this exception though does not break/impact the script but maybe we want to avoid an error at the console...
    var listener = function(evt) {
        toggleValue(evt);
    }
    addEventHandler(searchInputEl, 'focus', listener);
    addEventHandler(searchInputEl, 'blur', listener);
}

var ie = 0;
var agent = navigator.userAgent.toLowerCase();
var isMac = (agent.indexOf('mac')) ? true : false;
if (navigator.appName.indexOf("Explorer") > -1) {
	if (navigator.appVersion.indexOf("MSIE 5") > -1) { ie = 5 }
	else { ie = navigator.appVersion.substring(0,1) }
}
var openedPopups = new Array();
var defaults = new Array();
defaults['url'] = ""; defaults['name'] = "popup"; defaults['width'] = 600; defaults['height'] = 400;
function openPopup (url,name,w,h,switches,x,y) {
	if (!url) { url = defaults['url'] }
	if (!name) { name = defaults['name'] }
	if (!w) { w = defaults['width'] }
	else { w = parseInt(w) }
    if (ie && isMac){ w -= 10 }
    if (ie == 5) { w += 20 }
	if (!h) { h = defaults['height'] }
	else { h = parseInt(h) }
    if (ie && ie < 5) { h -= 20; }
	if (switches && switches.indexOf("f") > -1 && window.screen) {
        /*w = "outerWidth=" + window.screen.availWidth;
          h = ",outerHeight=" + window.screen.availHeight; */
		w = window.screen.availWidth;
		h = window.screen.availHeight;
	}

	var xpos; 	var ypos;
	if (x > -1 && y > -1) {
        xpos = x;
		ypos = y;
	} else {
		if (switches && window.screen) {
            if (switches.indexOf("c") > -1) {
                xpos = parseInt((window.screen.availWidth - w)/2);
				ypos = parseInt((window.screen.availHeight - h)/2);
			}
			if (switches.indexOf("f") > -1) {
                xpos = 0;
				ypos = 0;
			}
		}
	}
	var params = popupParams(w,h,switches,xpos,ypos);
	var newwin;
	if (openedPopups[name] && openedPopups[name].close && !openedPopups[name].closed) {
        openedPopups[name].close();
        openedPopups[name] = "";
    }
    newwin = window.open(url, name, params);
    if (switches && switches.indexOf('b') > -1 && window.moveTo && window.resizeTo && window.focus) {
        newwin.blur();
        newwin.opener.focus();
        newwin.resizeTo(w,h);
        newwin.moveTo(xpos,ypos);
	}
	if (window.focus) { newwin.focus() }
	openedPopups[name] = newwin;
    return false;
}
function popupParams (w,h,switches,xpos,ypos) {
	var width = "width=" + w;
	var height = ",height=" + h;
	var parent = ",dependent=no";
    var dirbar = ",directories=no";
	var fullscreen = ",fullscreen=0";
    var hotkeys = ",hotkeys=yes";
    var locbar = ",location=no";
	var menubar = ",menubar=no";
	var resizable = ",resizable=no";
	var scrollbars = ",scrollbars=no";
	var statusbar = ",status=no";
	var toolbar = ",toolbar=no";
	if (switches) {
		if (switches.indexOf("p") > -1 ) { parent = ",dependent=yes"	}
		if (switches.indexOf("d") > -1 ) { dirbar = ",directories=yes"	}
		if (switches.indexOf("h") > -1 ) { hotkeys = ",hotkeys=no"	}
		if (switches.indexOf("l") > -1 ) { locbar = ",location=yes"	}
		if (switches.indexOf("m") > -1 ) { menubar = ",menubar=yes"	}
		if (switches.indexOf("r") > -1 ) { resizable = ",resizable=yes"	}
		if (switches.indexOf("s") > -1 ) { scrollbars = ",scrollbars=yes" }
		if (switches.indexOf("u") > -1 ) { statusbar = ",status=yes"	}
		if (switches.indexOf("t") > -1 ) { toolbar = ",toolbar=yes"	}
		if (switches.indexOf("k") > -1 || switches.indexOf("b") > -1 ) { fullscreen = ",fullscreen=1" }
	}
	var pos = "";
	if (xpos > -1 && ypos > -1) { pos = ",left=" + xpos + ",top=" + ypos }
		return width + height + parent + dirbar + hotkeys + locbar + menubar + resizable + scrollbars + statusbar + toolbar + pos + fullscreen;
}
function openBigPicture(link) {
    openPopup (link.href, "BigPicture", 480, 735, "prsu"); // 470, 700
}
function openBigPictureGallery(link) {
    openPopup (link.href, "BigPicture", 480, 782, "prsu"); // 470, 700
}
function openBigPictureXXL(link) {
    window.open(link.href);
}
function openStatusInfo(link) {
    openPopup (link.href, "openStatusInfo", 480, 450, "prsu");
}
function openPODDownload(href) {
    openPopup (href, "openPODDownload", 480, 450, "prsu");
}
function targetOpener(link)
{
	window.opener.focus();
	window.opener.location.href=link.href;
	window.close();
}

