var aSeminarDivs = new Array();
var currentSeminarDiv = 0;
var doFade = true;

$(document).ready(function(){
    $('.referenties .box a img').each(
    function(){
        var top = ( $('.referenties .box').height() - $(this).height() ) / 2;
        $(this).css('margin-top', top);
       $(this).css('visibility', 'visible'); 
    });
    
    $('html').css('overflow-y', 'scroll') ;
    
    if($('.seminar-home').length > 0){
        $('.seminar-home').each(function(){
            aSeminarDivs.push($(this));  
        });
        faderOut();
    }
    
    $('.scroller').hover(function() {doFade = false;},
                         function() {doFade = true;});
        
});

function faderOut(){
    setTimeout(function(){
        if(doFade) {
            goFaderOut();    
        } else {
            faderOut();
        }
    }, 4000);
}

function goFaderOut()
{
    $(aSeminarDivs[currentSeminarDiv]).fadeOut(500,function(){
        currentSeminarDiv++;
        if(currentSeminarDiv >= aSeminarDivs.length) {
            currentSeminarDiv = 0;
        }
        faderIn();    
    });
}

function faderIn(){
    setTimeout(function(){ 
       $(aSeminarDivs[currentSeminarDiv]).fadeIn(500, function() {
                faderOut();
       });
    }, 500);
}

function loadXML(URL, callbackFunction)  {
    this.URL = URL;
    this.callbackFunction = callbackFunction; 
    this.debug = false;
    
    this.aSync = true;
    this.xmlhttp = null;
    
    this.responseRecieved = false; 
    this.responseXml = "";
}

loadXML.prototype.initiateWatch = function()
{
    this.xmlhttp=null;
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
      this.xmlhttp=new XMLHttpRequest();
    }
    // code for IE
        else if (window.ActiveXObject)
      {
      this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (this.xmlhttp!=null)
      {
      this.xmlhttp.onreadystatechange=this.state_Change.bind(this);
      this.xmlhttp.open("GET",this.URL,this.aSync);
      this.xmlhttp.send(null);
      }
    else
    {
      if(this.debug) {
          alert("Your browser does not support XMLHTTP.");
      }
    }
}

loadXML.prototype.state_Change = function()
{
// if xmlhttp shows "loaded"
if (this.xmlhttp.readyState==4)
  {
  // if "OK"
  if (this.xmlhttp.status==200)
    {
        this.responseXml = this.xmlhttp.responseText;
        this.responseRecieved = true; 
        if(this.callbackFunction != null) {
          this.callbackFunction();
        }
    }
  else
    {
        if(this.debug) {
             alert("Problem retrieving XML data:" + this.xmlhttp.statusText);
         }
    }
  }
}

function toArray(pseudoArray) {
     var result = [];
     for (var i = 0; i < pseudoArray.length; i++)
         result.push(pseudoArray[i]);
     return result;
}

Function.prototype.bind = function (object) {
     var method = this;
     var oldArguments = toArray(arguments).slice(1);
     return function () {
         var newArguments = toArray(arguments);
         return method.apply(object, oldArguments.concat(newArguments));
     };
}


var XmlLoader = null;
function showLightBox(pageXML){
    XmlLoader = new loadXML(pageXML, function() { showLightBoxCallback(); });
    XmlLoader.initiateWatch();
}   

function StringtoXML(text){
    if (window.ActiveXObject){
      var doc=new ActiveXObject('Microsoft.XMLDOM');
      doc.async='false';
      doc.loadXML(text);
    } else {
      var parser=new DOMParser();
      var doc=parser.parseFromString(text,'text/xml');
    }
    return doc;
}

   
function showLightBoxCallback()
{
    if(XmlLoader == null) {      
        return;
    }
    var lightBox = $('#lightbox');  
    var responseXml = XmlLoader.responseXml;  
    var xmlObj = StringtoXML(responseXml);    
    var lightBoxHtml = "<div id=\"lightbox\">";
    lightBoxHtml += "<div class=\"lightbox-bg\"></div> ";
    lightBoxHtml += "<div id=\"lightbox-spacer\">&nbsp;</div>";
    lightBoxHtml += "<div class=\"box-content\">";
    lightBoxHtml += "<div class=\"box-top\">";
    lightBoxHtml += "<div class=\"txt\" id=\"lightboxTitle\"><h3>" + xmlObj.getElementsByTagName('titel')[0].childNodes[0].nodeValue + "</h3></div><div class=\"close\"> X </div>";
    lightBoxHtml += "</div>";
    lightBoxHtml += "<div class=\"box-middle\">";
    lightBoxHtml += "<div class=\"box-scroll\">";
    lightBoxHtml += xmlObj.getElementsByTagName('content')[0].childNodes[0].nodeValue;
    lightBoxHtml += "</div>";
    lightBoxHtml += "</div>";
    lightBoxHtml += "<div class=\"box-bottom\">";
    lightBoxHtml += "</div>";
    lightBoxHtml += "</div>";
    lightBoxHtml += "<script type=\"text/javascript\">$('.box-scroll').jScrollPane({scrollbarWidth: 10, scrollbarMargin: 20 , showArrows: true});</script>";

    $('#spacer').before(lightBoxHtml);
    lightBox.show(); 

    $("#lightbox .box-top .close").bind("click", function () {
        $('#lightbox').remove();
    });

    $("#lightbox .lightbox-bg").bind("click", function () {
        $('#lightbox').remove();
    });   
}

var currentUID = -1;

function switchPerson(picture, contentDiv, uid){
    if(currentUID != uid){
        document.getElementById('rightBg').innerHTML = "<img src='" + picture + "' />";
        document.getElementById('bottomTxt').innerHTML = document.getElementById(contentDiv).innerHTML;
        currentUID = uid;
    }
}

function getLightbox(uri){
    
    var lightBoxHtml = "";
    var lightBox = $('#lightboxHolder');
    lightBoxHtml += "<div class=\"lightbox-bg\"></div>";
    lightBoxHtml += "<div id=\"lightboxHolder\">";
    lightBoxHtml += "<div class=\"close\"> X </div>";
    lightBoxHtml += "<iframe name=\"seminar\" scrolling=\"no\" border=\"0\" src=\""+uri+"\"></iframe>";
    lightBoxHtml += "</div>";

    
    $('#spacer').before(lightBoxHtml);
    lightBox.show();
    $("#lightboxHolder").bind("click", function () {
        $('#lightboxHolder').remove();     
        $('.lightbox-bg').remove();     
    });
    
    $(".lightbox-bg").bind("click", function () {
        $('#lightboxHolder').remove();     
        $('.lightbox-bg').remove();     
    });
    
    $(window).resize(function(){
        var htmlHeight = $('html').innerHeight();
        if(parseInt(htmlHeight) <= 550){
            $('.lightbox-bg').css('height','550px');
        } else {
            $('.lightbox-bg').css('height',htmlHeight);    
        }
    });

    $(window).trigger("resize");

    return false;
}


function removeScreen(){
        $('#lightboxHolder').remove();     
        $('.lightbox-bg').remove();    
}