/*

This script was created by Ian Lloyd (http://lloydi.com/). I'd appreciate it 
if you could keep this comment in the script if you choose to use it (albeit 
in an amended form.

The original blog entry relating to this can be found here: http://tinyurl.com/p9tqb.

The addEvent function comes courtesy of Scott Andrew: http://tinyurl.com/qcmrd

*/
function addEvent(elm, evType, fn, useCapture)
{
    if(elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    } else {
        elm['on' + evType] = fn;
    }
}

function addPrintLinks()
{
var el = document.getElementsByTagName("div");
for (i=0;i<el.length;i++)
  {
  if (el[i].className=="print_section")
    {
    var newLink = document.createElement("a");
    var newLink2 = document.createElement("a");
    var newLinkText = document.createTextNode("print this section only");
    var newLinkText2 = document.createTextNode("print the whole page");

    var newLinkPara = document.createElement("p");
    newLinkPara.setAttribute("class","printbutton");
    newLinkPara.setAttribute("style", "top:50px");
    
    //set up the 'print this section' link
    newLink.setAttribute("href","#");
    var btId = "printbut_" + el[i].id;
    newLink.setAttribute("id",btId);
    newLink.appendChild(newLinkText);
    newLink.setAttribute("href","#");
    newLinkPara.appendChild(newLink);
    
    //set up the print all link
    newLink2.setAttribute("href","#");
    var bt2Id = "printall_" + el[i].id;
    newLink2.setAttribute("id",bt2Id);
    newLink2.appendChild(newLinkText2);
    newLink2.setAttribute("href","#");
    newLinkPara.appendChild(newLink2);

    //add the behaviours for the new links
    newLink.onclick = togglePrintDisplay;
    newLink.onkeypress = togglePrintDisplay;
    newLink2.onclick = printAll;
    newLink2.onkeypress = printAll;

    //insert the para and the two links into the DOM
    el[i].appendChild(newLinkPara);
    
    }
  }
}

function togglePrintDisplay(e)
{
    if (window.event) {
        window.event.returnValue = false;
        window.event.cancelBubble = true;
    } else if (e) {
        e.stopPropagation();
        e.preventDefault();
    }

    window.print();
}

function printAll(e)
{
var el = document.getElementsByTagName("div");
for (i=0;i<el.length;i++)
  {
  if (el[i].className.indexOf("print_section")!=-1)
    {
    el[i].setAttribute("className","print_section print");
    el[i].setAttribute("class","print_section print");
    }
  }
if (window.event) 
  {
  window.event.returnValue = false;
  window.event.cancelBubble = true;
  } 
else if (e) 
  {
  e.stopPropagation();
  e.preventDefault();
  }
    window.print();
}

// global flag
var isIE = false;

// global request and XML document objects
var req;
var siteBaseUrl = '';

function loadSlideshow() {
    var url = siteBaseUrl + "slideshow/" + curr_slideshow + "/content.xml";
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = handleSlideshow;
        req.open("GET", url, true);
        req.send(null);
        isIE = window.ActiveXObject;
        // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = handleSlideshow;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function loadSlideshows(week, jaar) {
    var week = parseInt(week);
    week = week < 10 ? "0" + week : week;
    var url = siteBaseUrl + "slideshow/overzicht/" + jaar + "/" + week + "/content.xml";
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = handleSlideshows;
        req.open("GET", url, true);
        req.send(null);
        isIE = window.ActiveXObject;
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = handleSlideshows;
            req.open("GET", url, true);
            req.send();
        }
    }
}

var fastSpeed = .75; // aantal seconden
var normalSpeed = 2; // aantal seconden
var slowSpeed = 3; // aantal seconden
var timeout = normalSpeed; // aantal seconden
var image_nrs;
var image_titles;
var image_copyrights;
var image_desc;
var cur_image;

function handleSlideshows() {
  // only if req shows "loaded"
  if (req.readyState == 4) {
    cur_image = 0;
    var titles = req.responseXML ? req.responseXML.getElementsByTagName("title") : [];
    var ids = req.responseXML ? req.responseXML.getElementsByTagName("id") : [];
    var target = document.getElementById("slideshow_title");
    target.innerHTML = "";
    for (i = 0;i < titles.length;i++) {
      if (titles[i].firstChild != null) {
        var newoption = document.createElement("option");
        var newtxt = document.createTextNode(titles[i].firstChild.nodeValue);
        newoption.setAttribute("value", ids[i].firstChild.nodeValue);
        if (ids[i].firstChild.nodeValue == curr_slideshow || i == 0) {
          newoption.setAttribute("selected", "true");
        }
        newoption.appendChild(newtxt);
        target.appendChild(newoption);
      }
    }
    if (curr_slideshow == -1) {
      var target = document.getElementById("photos");
      target.innerHTML = "";
      setTitle("");
      setDate("");
      image_nrs = Array();
      setAantalFoto();
      setImageDesc("");
      setImageTitle("");
      setImageCopyright("");
      if (ids.length > 0) {
        curr_slideshow = ids[0].firstChild.nodeValue;
        loadSlideshow();
      }
    }
  }
}

var timerId;

function setAantalFoto() {
    var target = document.getElementById("aantalFoto");
    if (image_nrs.length == 0) {
        target.innerHTML = "<b>?</b> " + I18N.OF + " <b>?</b>";
    } else {
        target.innerHTML = "<b>" +(cur_image + 1) + "</b> " + I18N.OF + " <b>" + image_nrs.length + "</b>";
    }
}

function setImageDesc(desc) {
    if (desc == null) {
        desc = "";
    }
    target = document.getElementById("image_desc");
    target.firstChild.nodeValue = desc;
    target = document.getElementById("image_desc2");
    target.firstChild.nodeValue = desc;
}

function setImageTitle(desc) {
    if (desc == null) {
        desc = "";
    }
    target = document.getElementById("image_title");
    target.firstChild.nodeValue = desc;
    target = document.getElementById("slideshow_titleprint");
    target.firstChild.nodeValue = "'" + desc + "'";
}

function setImageCopyright(desc) {
    if (desc == null) {
        desc = "";
    }
    target = document.getElementById("fotoGraaf");
    target.innerHTML = "&nbsp;&nbsp;" + desc;
    target = document.getElementById("fotoGraaf2");
    target.innerHTML = "&nbsp;&nbsp;" + desc;
}

function setTitle(title) {
    var target = document.getElementById("slideshow_bigtitle");
    target.innerHTML = title;
}

function handleSlideshow() {
  // only if req shows "loaded"
  if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200) {
      cur_image = 0;
      var images = req.responseXML.getElementsByTagName("image");
      var target = document.getElementById("photos");
      image_nrs = new Array(images.length);
      image_titles = new Array(images.length);
      image_copyrights = new Array(images.length);
      image_desc = new Array(images.length);
      target.innerHTML = "";
      for (i = 0;i < images.length;i++) {
        if (isIE) {
          image_nrs[i] = images[i].childNodes[0].firstChild.nodeValue;

          image_titles[i] = (images[i].childNodes[1].firstChild != null) ? images[i].childNodes[1].firstChild.nodeValue : "";

          if (((images[i].childNodes[4] != null) && (images[i].childNodes[4].firstChild != null)) &&
            ((images[i].childNodes[5] != null) && (images[i].childNodes[5].firstChild != null))) {
              image_copyrights[i] = images[i].childNodes[4].firstChild.nodeValue + '<br>' + images[i].childNodes[5].firstChild.nodeValue;
          } else if(images[i].childNodes[2].firstChild != null)
          {
            image_copyrights[i] = images[i].childNodes[2].firstChild.nodeValue;
          }else
          {
            image_copyrights[i] = "";
          }
          
          image_desc[i] = (images[i].childNodes[3].firstChild != null) ?
          images[i].childNodes[3].firstChild.nodeValue : "";
        } else {
          image_nrs[i] = images[i].childNodes[1].firstChild.nodeValue;
          if (images[i].childNodes[3].firstChild != null) {
            image_titles[i] = images[i].childNodes[3].firstChild.nodeValue;
          } else image_titles[i] = "";

          if (images[i].childNodes[9] && images[i].childNodes[9].firstChild != null && images[i].childNodes[11] && images[i].childNodes[11].firstChild != null)
          {
            image_copyrights[i] = images[i].childNodes[9].firstChild.nodeValue + '<br>' + images[i].childNodes[11].firstChild.nodeValue;
          }else if (images[i].childNodes[5] && images[i].childNodes[5].firstChild != null) {
            image_copyrights[i] = images[i].childNodes[5].firstChild.nodeValue;
          } else image_copyrights[i] = "";
          
     
          if (images[i].childNodes[7] && images[i].childNodes[7].firstChild != null) {
            image_desc[i] = images[i].childNodes[7].firstChild.nodeValue;
          } else image_desc[i] = "";
        }
        var newimg = document.createElement("img");
        newimg.setAttribute("width", "345px");
        newimg.setAttribute("height", "243px");
        newimg.setAttribute("src", "" + image_nrs[i] + "");
        newimg.setAttribute("style", "position:absolute;left:0px;top:0px");
        newimg.style.position = "absolute";
        newimg.style.top = "0px";
        newimg.style.visibility = (i > 0) ? "hidden" : "visible";
        newimg.setAttribute("id", image_nrs[i]);
        target.appendChild(newimg);
      }
      var timestamp = req.responseXML.getElementsByTagName("timestamp");
      setDate(timestamp[0].firstChild.nodeValue);
      target = document.getElementById("week");
      target.innerHTML = "";
      for (i = 1;i < 54;i++) {
        var newoption = document.createElement("option");
        var newtxt = document.createTextNode(i);
        if (i == curr_week) {
          newoption.setAttribute("selected", "true");
        }
        newoption.appendChild(newtxt);
        target.appendChild(newoption);
      }
      target = document.getElementById("jaar");
      target.innerHTML = "";
      for (i = 2007;i < 2011;i++) {
        var newoption = document.createElement("option");
        var newtxt = document.createTextNode(i);
        if (i == curr_year) {
            newoption.setAttribute("selected", "true");
        }
        newoption.appendChild(newtxt);
        target.appendChild(newoption);
      }
      gotoImage(0, false);
      var titles = req.responseXML.getElementsByTagName("title");
      setTitle(titles[0].firstChild.nodeValue);
      loadSlideshows(curr_week, curr_year);
    } else {
      alert("Onbekende slideshow!");
    }
  }
}


Date.prototype.getWeek = function() {
  /*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */
  var dowOffset = 1; 
  var newYear = new Date(this.getFullYear(),0,1);
  var day = newYear.getDay() - dowOffset; //the day of week the year begins on
  day = (day >= 0 ? day : day + 7);
  var daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset() - newYear.getTimezoneOffset()) * 60000) / 86400000) + 1;
  var weeknum;

  //if the year starts before the middle of a week
  if(day < 4) {
    weeknum = Math.floor((daynum+day-1)/7) + 1;
    
    if (weeknum > 52) {
      var nYear = new Date(this.getFullYear() + 1,0,1);
      var nday = nYear.getDay() - dowOffset;
      nday = nday >= 0 ? nday : nday + 7;
      
      // if the next year starts before the middle of the week, it is week #1 of that year
      weeknum = nday < 4 ? 1 : 53;
    }
  }
  else {
    weeknum = Math.floor((daynum+day-1)/7);
  }
  
  return weeknum;
}

var maandNaam = I18N.MONTHS;

function setDate(date) {
    var target = document.getElementById("dateTime");
    if (date == "") {
        target.innerHTML = "";
    } else {
    var d = new Date(date);
    var t_hour = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
    var t_min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_year = d.getFullYear();
    var curr_day = d.getDate();
    if (I18N.LANGUAGE == 'hu')
    {
      month = (curr_month+1) < 10 ? '0'+(curr_month+1) : (curr_month+1);
      target.innerHTML = curr_year + " " + month + " " + curr_date + " / " + t_hour + ":" + t_min;
    }
    else
    {
      target.innerHTML = t_hour + ":" + t_min + " / " + curr_date + " " + maandNaam[curr_month] + " " + curr_year;
    }
    
    var target = document.getElementById("dateTime2");
    target.innerHTML = curr_date + " " + maandNaam[curr_month] + " " + curr_year;
    curr_week = d.getWeek();
    curr_year = d.getWeek() == 1 && d.getMonth() == 11 ? curr_year + 1 : curr_year;
  }
}

function playSlideshow() {
  if (timerId) clearInterval(timerId);
  timerId = setTimeout("gotoImage(-1, true)", timeout * 1000);
  var knop = document.getElementById("play_knop");
  knop.src = "/images/slideshow/bt_play.gif";
}

function stopSlideshow() {
    clearTimeout(timerId);
    var knop = document.getElementById("play_knop");
    knop.src = "/images/slideshow/bt_play2.gif";
}

function gotoImage(photo_nr, cont) {
    var cur_elem = document.getElementById(image_nrs[cur_image]);
    cur_elem.style.visibility = "hidden";
    if (cont) { cur_image++; } else cur_image = photo_nr;
    if (cur_image >= image_nrs.length) { cur_image = 0; }
    else if (cur_image < 0) { cur_image = image_nrs.length - 1; }
    var new_elem = document.getElementById(image_nrs[cur_image]);
    new_elem.style.visibility = "visible";
    setAantalFoto();
    setImageDesc(image_desc[cur_image]);
    setImageTitle(image_titles[cur_image]);
    setImageCopyright(image_copyrights[cur_image]);
    if (cont) {
        timerId = setTimeout("gotoImage(-1, true)", timeout * 1000);
    }
}

function changeWeek(week)
{
    curr_week = week.options[week.selectedIndex].text;
    curr_slideshow = -1;
    loadSlideshows(curr_week, curr_year);
}
function changeJaar(jaar)
{
    curr_year = jaar.options[jaar.selectedIndex].text;
    curr_slideshow = -1;
    loadSlideshows(curr_week, curr_year);
}

function changeSpeed(speed) {
    if (speed == 0) {
        timeout = normalSpeed;
    } else if (speed == 1) {
        timeout = fastSpeed;
    } else {
        timeout = slowSpeed;
    }
}

