// open a link in a new window 
function openLink(url) {
  win = window.open(url, '_blank', 'toolbar=no,location=no,directories=no,resizable=yes,scrollbars=yes,left=100,top=100');
}

//load a xmlfile and call format-function giving data and parameter
function ajax(file, format, position) { // file,format function, position(id of pagelem)
  var server = null; // init server-connection
  if (window.XMLHttpRequest) { // no IE
    server = new XMLHttpRequest(); // create XML
  } else if (window.ActiveXObject) { // IE-Style
    server = new ActiveXObject("Microsoft.XMLHTTP"); // ActiveX http
  }
  if (server != null) { // we have a valid connection
    server.open("GET", file, true); // get the file 
    server.onreadystatechange = function() { // wait on ready
      if (server.readyState == 4) { // file is ready
        var xmldata = server.responseXML; // get xml-data object
        format(xmldata, position); // call format and output to position
      }
    }
    server.send(null);
  }
}

// News functions
//setnews can be called by ajax to display the news from news.xml
function setnews(daten, newslist) {
  var allnews = daten.getElementsByTagName('entry');
  var htmlnews = "";
  for (i = 0; i < allnews.length; i++) {
    // get data from xml 
    var news = daten.getElementsByTagName('news').item(i).firstChild.nodeValue;
    var firstDay = daten.getElementsByTagName('firstday').item(i).firstChild.nodeValue;
    var lastDay = daten.getElementsByTagName('lastday').item(i).firstChild.nodeValue;
    // build comparable dates
    var myHeute = heute();
    var compHeute = myHeute.substr(6, 4) + myHeute.substr(3, 2) + myHeute.substr(0, 2);
    var compFirst = firstDay.substr(6, 4) + firstDay.substr(3, 2) + firstDay.substr(0, 2);
    var compLast = lastDay.substr(6, 4) + lastDay.substr(3, 2) + lastDay.substr(0, 2);
    if (compHeute >= compFirst && compHeute <= compLast) {
      htmlnews = htmlnews + "<hr/><small>News vom " + firstDay + ":</small><ul><li>" + news + "</li></ul>";
    }
  }
  newslist.innerHTML = htmlnews;
} //end setnews

//show matching tour entries for this date (needs single date - NOT called from ajax)
function showDesc(xmlDoc, cId, day) {
  //xmlDoc=formatTour(xmlDoc);
  var htmlDesc = "";
  var busy = 0;
  var terminliste = xmlDoc.getElementsByTagName('termin');
  for (i = 0; i < terminliste.length; i++) {
    var datum = xmlDoc.getElementsByTagName('datum').item(i).firstChild.nodeValue;
    if (day == datum.substr(0, 10)) {
      var wo = xmlDoc.getElementsByTagName('wo').item(i).firstChild.nodeValue;
      var hinweis = xmlDoc.getElementsByTagName('hinweis').item(i).firstChild.nodeValue;
      var ausgebucht = xmlDoc.getElementsByTagName('ausgebucht').item(i).firstChild.nodeValue;
      var status = xmlDoc.getElementsByTagName('status').item(i).firstChild.nodeValue;
      htmlDesc = htmlDesc + "<hr/><ul><li><b>" + datum + "</b><br/>" + wo + "<br/>" + hinweis + "<br/>" + status + "</li></ul>";
      busy = 1;
      if (ausgebucht == "j") {
        busy = 2;
      }
    }
  }
  if (busy == 0) {
    htmlDesc = "<hr/><ul><li><b>" + day + "</b><br/>noch ohne Termin</li></ul>";
  }
  myOut = document.getElementById(cId.getItem('desctarget'));
  myOut.innerHTML = htmlDesc;
  return busy;
}

// Tourinfo functions

//load the tourinfo.xml for rendering 
function loadTourinfo() {
  var oTabBox = bb.document.getElementById('tabs');
  var iSelectedIndex = oTabBox.getProperty('selectedIndex');
  if (iSelectedIndex == 2) {
    var aURL = 'tourinfo.xml';
    var oSelectedTab = oTabBox.getProperty('elements')[iSelectedIndex];
    bb.command.load(aURL, 'GET', null, null, oSelectedTab, 'replaceChildren');
  }
}

function openRoute(adresse) {
  url = "http://maps.google.de/maps?f=d&saddr=" + document.forms[0].start.value + "&daddr=" + adresse;
  win = window.open(url, '_blank', 'toolbar=no,location=no,directories=no,resizable=yes,scrollbars=yes,left=100,top=100');
}

// This function picks up the click and opens the corresponding info window
function showMarker(i) {
  GEvent.trigger(gmarker[i], "click");
}

// set marker on google map 
function setMarker(xmlDoc, dummy) {
  var termin = xmlDoc.getElementsByTagName('termin');
  for (i = 0; i < termin.length; i++) {
    var long = xmlDoc.getElementsByTagName('long').item(i).firstChild.nodeValue;
    var lat = xmlDoc.getElementsByTagName('lat').item(i).firstChild.nodeValue;
    var html = xmlDoc.getElementsByTagName('maphtml').item(i).firstChild.nodeValue;
    gmarker[i] = createMarker(long, lat, html);
  }
}

// A function to create the marker and set up the event window
function createMarker(long, lat, html) {
  var point = new GLatLng(long, lat);
  var marker = new GMarker(point);
  html = html.replace("<![CDATA[", "");
  html = html.replace("]]>", "");
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  map.addOverlay(marker);
  return marker;
}

//Animation Function 

var animationRef;
function slide(oDirect, iPxPerSec) {
  var oAnimationInfo = {
    attributeName : "left"
  }
  var oContainer = document.getElementById("container");
  var deltaWidth = oContainer.offsetWidth - oContainer.parentNode.offsetWidth;
  var start = parseInt(oContainer.style.left);
  var distance;
  if (oDirect == "left") {
    distance = deltaWidth - Math.abs(start);
    stop = -deltaWidth;
  } else {
    distance = Math.abs(start);
    stop = 0;
  }
  if (distance != 0) {
    oAnimationInfo.dur = (Math.round(100 * distance / iPxPerSec) / 100) + "s";
    oAnimationInfo.values = start + "px;" + stop + "px";
    var callback = function() {
      animationRef = null;
    }
    animationRef = bb.smil.animate(oContainer, oAnimationInfo, callback);
  } else {
    animationRef = null;
  }
}

function heute() {
  var Heute = new Date();
  var DieserMonatstag = Heute.getDate();
  var DieserMonatNummmer = Heute.getMonth() + 1;
  var DiesesJahr4stellig = Heute.getFullYear();

  // zweistellige Darstellung
  var DieserMonatstag2 = ((DieserMonatstag < 10) ? "0" : "") + DieserMonatstag;
  var DieserMonatNummmer2 = ((DieserMonatNummmer < 10) ? "0" : "") + DieserMonatNummmer;

  return DieserMonatstag2 + "." + DieserMonatNummmer2 + "." + DiesesJahr4stellig;
}



//Sample
//format function for simple list list xml-file <link><text></text><url></url></link><link...
//unused sample for "other" method to walk down the tree
function linklist(daten, liste) {
var ergebnisse = daten.getElementsByTagName("link"); // get xml elem named link
for ( var i = 0; i < ergebnisse.length; i++) { // for all link elems found    
  var name, url; // init name and url                           
  var alink = ergebnisse[i]; // get a link element  
  for ( var j = 0; j < alink.childNodes.length; j++) { // for all childs of this link elem
    with (alink.childNodes[j]) { // do     
      if (nodeName == "text") { // if nodename is text 
        name = firstChild.nodeValue; // we have the name elem            
      } else if (nodeName == "url") { // if nodename is url 
        url = firstChild.nodeValue; // we found the url elem 
      }
    }
  }

  var li = document.createElement("li");
  var a = document.createElement("a");
  a.setAttribute("href", url);
  var txt = document.createTextNode(name);
  a.appendChild(txt);
  li.appendChild(a);
  liste.appendChild(li);
} // end for i                                             
} // end linklist 


