var history_xml;
function history(file)
{
//	file = "startseite.xml";
	history_xml = AJAX("GET",file,null, false, null).responseXML;
	var n = history_xml.getElementsByTagName("year").length;
	//alert(n);
	var i;
	var select = document.getElementById("select_year");
	var img, a, parent;
	for (i = 0; i < n; i++){
		var option = document.createElement("option");
		option.appendChild(document.createTextNode(history_xml.getElementsByTagName("year")[i].getAttribute("id")));
		select.appendChild(option);
	}
	for (i = 0; i < history_xml.getElementsByTagName("img").length; i++) {
		img = history_xml.getElementsByTagName("img")[i];
		
		//a = history_xml.createElement("a");
		//parent = img.parentNode;
		//alert(parent.nodeName);
		img.parentNode.insertBefore(history_xml.createElement("a"), img);
		img.previousSibling.setAttribute("href","images/Bilder_Geschichte_neu/"+img.getAttribute("src"));
		img.previousSibling.setAttribute("rel","lightbox[xxx]");
		img.previousSibling.appendChild(img);
		history_xml.getElementsByTagName("img")[i].setAttribute("src",
			"images/Bilder_Geschichte_neu/"+img.getAttribute("src"));
		history_xml.getElementsByTagName("img")[i].setAttribute("height", "120");
	}
	history_year(0);
}

function history_year(index) {
	var div = document.getElementById("history_content");
	var node = history_xml.getElementsByTagName("year")[index];
	while(div.firstChild != null)
		div.removeChild(div.firstChild);
	div.appendChild(document.createElement("h1"));
	div.firstChild.appendChild(document.createTextNode(node.getAttribute("id")
		+ ": "+node.getAttribute("heading")
													   ));
		//	div.appendChild(node.nodeValue);
	copy_tree(div,node)
}


function AJAX(method, url, data, async, func_answer) {
//	alert(method+"\n"+url+"\n"+async);
    var _http = null;
	       //alert("Method:"+method + "\nurl:" + url + "\nasync"+async);
	if (window.XMLHttpRequest) {
        _http = new XMLHttpRequest();
	//	alert("Normal");
    }
    else if (window.ActiveXObject) {
		//alert("WINDOOOF");
        _http = new ActiveXObject("MSXML.XMLHTTP");
    }
    else
        alert("Kein AJAX möglich!");
	
    if (_http != null) {
        _http.open(method, url, async);
        _http.send(data);
        _http.onreadystatechange = func_answer;
	//	alert(_http.getAllResponseHeaders());
	//	alert(document);
    }
	//     if (!async)
	//        alert(_http.responseText);
	//      if (async && document.getElementById("wb_debug") != undefined)
	//         document.getElementById("wb_debug").setAttribute("style","background: red");
    if (async)
		http = _http;
//	else
//		alert(url+" "+_http.status+"\n"+_http.responseText);
    return _http;
}


function wb_treeview_toggle(node) {
	if (node.nextSibling.getAttribute("class") == "wb_tree_visible") {
		node.nextSibling.setAttribute("class", "wb_tree_hidden");
		node.parentNode.setAttribute("class", "wb_tree_closed");
		node.firstChild.setAttribute("src","images/plus.gif");
	}
	else {
		node.nextSibling.setAttribute("class", "wb_tree_visible");
		node.parentNode.setAttribute("class", "wb_tree_opened");
		node.firstChild.setAttribute("src","images/minus.gif");
	}
}

var wb_old_tree_node = null;
function wb_treeview_build(node, json, fnOnClick) {
	wb_treeview_build_r(node, json, fnOnClick);
	node.firstChild.setAttribute("class", "wb_tree_visible");
	node.setAttribute("class", "wb_tree_closed");
}

function wb_treeview_build_r(node, json, fnOnClick) {
	var i;
    var div = document.createElement("div");
    div.setAttribute("class", "wb_tree_hidden");
    var ul = document.createElement("ul");
    ul.setAttribute("class","wb_tree");
    for (i = 0; i < json.items.length; i++) {
		var li = document.createElement("li");
		var txt = document.createElement("div");
		txt.appendChild(document.createElement("img"));
		txt.appendChild(document.createTextNode(json.items[i].text));
		if (json.items[i].items != undefined) {
			txt.setAttribute("onclick", "wb_treeview_toggle(this)");
			txt.firstChild.setAttribute("src","images/plus.gif");
			li.appendChild(txt);
			wb_treeview_build_r(li,json.items[i],fnOnClick);
			li.setAttribute("class","wb_tree_closed");
		}
		else {
			li.setAttribute("onclick", 'if (wb_old_tree_node != null) wb_old_tree_node.setAttribute("class","wb_tree");this.setAttribute("class","wb_tree_selected"); wb_old_tree_node = this;'+fnOnClick+"('"+json.items[i].id+"')");
			txt.firstChild.setAttribute("src","images/square.gif");
			li.appendChild(txt);
			li.setAttribute("class","wb_tree");
		}
        ul.appendChild(li);
    }
    div.appendChild(ul);
    node.appendChild(div);
}


function init() {
	var menut = AJAX("GET","menu.json",null, false, null).responseText;
	wb_treeview_build(document.getElementById("myTreeView"), JSON.parse(menut), "load");
	load("startseite.xml")
}
function copy_tree(dest, source) {
	var node = source.firstChild;
	while (node != null) {
		//alert(node.nodeName+"\n"+node.nodeType+"\n"+node.nodeValue);
		switch(node.nodeType) {
			case 1: dest.appendChild(document.createElement(node.nodeName));
				for (var i = 0; i < node.attributes.length; i++)
					dest.lastChild.setAttribute(node.attributes[i].nodeName,node.attributes[i].nodeValue);
				copy_tree(dest.lastChild,node);
				break;
			case 3: var newnode =document.createTextNode(node.nodeValue);  
				dest.appendChild(newnode);		
				break;
		}
		node = node.nextSibling;
	}
}

function load(id) {
	var i;
	var content_node = document.getElementById("content"); 
	while (content_node.firstChild != null)
		content_node.removeChild(content_node.firstChild);


	var xml = AJAX("GET", id, null, false, null);
/*	alert(id+"\n"+xml.status);
	alert(content_node);
	alert(xml.responseXML.getElementsByTagName("body").length);
	alert(xml.responseText);
*/
	if (!xml.status || xml.status == 200) {
		var body = xml.responseXML.getElementsByTagName("body")[0];
		copy_tree(content_node, body);
		/*		var n = body.getElementsByTagName("*").length;
		
		alert(content_node+"\n"+body.firstChild);
		alert(document.createTextNode(id + " nicht gefunden!"));
		i = 0;
		alert(body.getElementsByTagName("*").length);
		while (++i < 4 && body.firstChild != null) {
			alert(body.firstChild.nodeName);
			clone =  document.importNode(body.getElementsByTagName("*")[i], true);
			alert(clone.getElementsByTagName("*").length+" Kinder");
			content_node.appendChild(clone);
		}
		 */
		if (body.getAttribute("onload") != "")
			eval( body.getAttribute("onload"));
	}
	else
		content_node.appendChild(document.createTextNode(id + " nicht gefunden!"));
	//document.getElementById("content").appendChild(body);
 }


var map;
var directionsDisplay;
var directionsService;

function anfahrt_werkstatt() {
    var latlng = new google.maps.LatLng(48.814349834467225, 9.286304878234867);
    var myOptions = {
	zoom: 15,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var m_werkstatt =  new google.maps.Marker({
											  position: new google.maps.LatLng(48.8146500, 9.2936310), 
											  map: map, 
											  title:""
											  });
	var m_rathaus =  new google.maps.Marker({
											position: new google.maps.LatLng(48.8085600, 9.2761500), 
											map: map, 
											title:""
											});
	var i_werkstatt = new google.maps.InfoWindow({
												 content: "<b>Fliegergruppe Fellbach e.V.</b><br/>Erbach 8<br/>70734 Fellbach"
												 
												 });
	i_werkstatt.open(map, m_werkstatt);
	var i_rathaus = new google.maps.InfoWindow({
											   content: "<b>Fellbacher Rathaus / Marktplatz</b>"
											   
											   });
	i_rathaus.open(map, m_rathaus);
}


function anfahrt_ahof () {
    var latlng = new google.maps.LatLng(48.814349834467225, 9.286304878234867);
    var myOptions = {
	zoom: 15,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	
	var start = "Marktplatz 1, 70734 Fellbach, Deutschland";
	
	var end =  "Segelfluggelände Bartholomä, Wolf-Hirth-Str. 10, 73566 Bartholomae";
	
	var request = {
    origin:start, 
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(map);
	
	directionsService = new google.maps.DirectionsService();
	
	directionsService.route(request, function(result, status) {
							if (status == google.maps.DirectionsStatus.OK) {
							directionsDisplay.setDirections(result);
							
							}
							});
}	


var diaschau_tr;
var diaschau_selected;
var diaschau, diaschau_dir;
function diaschau_init(dir,file) {
	var img, http, imgnode,i;
	document.getElementById("div_scrollarea").appendChild(document.createElement("table"));
	document.getElementById("div_scrollarea").lastChild.setAttribute("border","1");
	document.getElementById("div_scrollarea").lastChild.setAttribute("width","1000px");
	
	document.getElementById("div_scrollarea").firstChild.appendChild(document.createElement("tr"));
	diaschau_tr = document.getElementById("div_scrollarea").firstChild.firstChild;
	//	document.getElementById("div_scrollarea").firstChild.appendChild(document.createElement("tr"));
	//	tr = document.getElementById("div_scrollarea").firstChild.firstChild;
	
	diaschau = JSON.parse(AJAX("GET",dir+"/"+file,null,false,null).responseText);
	for (var i = 0; i < diaschau.pictures.length; i++) {
		diaschau_tr.appendChild(document.createElement("td"));
		diaschau_tr.lastChild.appendChild(document.createElement("div"));
		diaschau_tr.lastChild.firstChild.setAttribute("class","diaschau_normal");
		imgnode = document.createElement("img");
		imgnode.setAttribute("src", dir+"/"+diaschau.pictures[i].klein);
		imgnode.setAttribute("onclick", "diaschau_load("+i+")");
		diaschau_tr.lastChild.firstChild.appendChild(imgnode);
	}
	document.getElementById("pfeil_l").setAttribute("src",dir+"/"+"pfeil_l.gif");
	document.getElementById("pfeil_r").setAttribute("src",dir+"/"+"pfeil_r.gif");
	
	diaschau_selected = -1;
	diaschau_dir = dir;

	diaschau_load(0);
//	setTimeout("diaschau_auto()",2000);
}


function diaschau_auto() {
	if (diaschau_selected < diaschau.pictures.length - 1) {
		diaschau_vor();
		setTimeout("diaschau_auto()",2000);
	}
}

var timer = null;
function diaschau_scroll(x) {
	//	alert(timer);
	if (timer != null)
		clearTimeout(timer);
	var x_akt = diaschau_tr.parentNode.parentNode.scrollLeft;
	if (x_akt > x) {
		diaschau_tr.parentNode.parentNode.scrollLeft--;
	}
	else if (x_akt < x) {
		diaschau_tr.parentNode.parentNode.scrollLeft++;
	}
	if (diaschau_tr.parentNode.parentNode.scrollLeft != x_akt) {
		timer = setTimeout("diaschau_scroll("+x+")",2);
	}
	
}
function diaschau_load(nr) {
	if (diaschau_selected > -1)
		diaschau_tr.getElementsByTagName("td")[diaschau_selected].firstChild.setAttribute("class","diaschau_normal");
	diaschau_tr.getElementsByTagName("td")[nr].firstChild.setAttribute("class","diaschau_highlighted");
	diaschau_selected = nr;
	diaschau_scroll(158*nr-200);
	
	document.getElementById("div_foto_0").firstChild.setAttribute("src",diaschau_dir+"/"+diaschau.pictures[nr].gross);
	if (diaschau.pictures[nr+1] != null)
		document.getElementById("div_foto_2").firstChild.setAttribute("src",diaschau_dir+"/"+diaschau.pictures[nr+1].gross);
	if (diaschau.pictures[nr-1] != null)
		document.getElementById("div_foto_1").firstChild.setAttribute("src",diaschau_dir+"/"+diaschau.pictures[nr-1].gross);
}

function diaschau_zurueck() {
	if (diaschau_selected > 0)
		diaschau_load(diaschau_selected-1);
}

function diaschau_vor() {
	if (diaschau_selected < diaschau.pictures.length - 1)
		diaschau_load(diaschau_selected+1);
}


