/* A day will come when we won't have to do this anymore */
var isIE = $.browser.msie;

/* Wand & sparkles globals */
var nr = 0;
var lastX = 0;
var lastY = 0;
var mainX = 0;
var mainY = 0;

/* AJAX globals */
var xhr = null;
var curHash = '';
var curPage = 'home';
var curLang = 'NL';

/* Testimonials globals */
var testimonialInterval = 0;
var testimonialTimeout = 0;

/* Klanten globals */
var klantenArray = [];
var klantenIndex = 0;
var klantenTimeout = 0;

/* Get content globals */
var fadeOutCounter = 0;
var initCounter = 0;

/* Tarieven globals */
var tarievenObject = {};
var tarievenVanId = 0;
var tarievenNaarId = 0;

/* Video subtitle globals */
var videoTime = 0;
var subsTimeout = 0;

/* Language menu globals */
var languageMenuTimout = 0;

/* Preload images needed for hover etc. */
preloadImages('../images/sparkle.png', '../images/button-kwaliteit.png', '../images/button-specialisatie.png', '../images/button-nieuws.png', '../images/button-tarieven.png', '../images/button-klanten.png', '../images/button-walloffame.png', '../images/button-offerte.png', '../images/button-medewerkers.png');
$(document).ready(function() {
  
  /* Wand & sparkles */
  $('body').mousemove(function(e) {
    if(!isIE) {
      mainX = e.pageX;
      mainY = e.pageY;
    }
    clearInterval(testimonialInterval);
    testimonialInterval = setInterval(getTestimonial, 15000);
  });
  if(!isIE) {
    $('*').click(function() {
      addSparkles(true);
    });
    setInterval(addSparkles, 25);
  }
  
  /* Language menu hover */
  $('#menu .languagechoice > li').hover(function() {
    clearTimeout(languageMenuTimout);
    $('#menu .languagechoice > li').css('background', 'url("../images/languagechoice.png") no-repeat');
    $('.languages', this).show();
  }, function() {
    languageMenuTimout = setTimeout(function() {
      $('#menu .languagechoice > li .languages').hide();
      $('#menu .languagechoice > li').css('background', 'none');
    }, 500);
  });
  
  /* Overwrite main menu */
  overwriteMenu();
  
  /* Mirror buttons hover */
  $('#mirror-map area').hover(function() {
    if(!isIE || curPage != 'home') {
      var h = $(this).attr('href');
      for(var i = 0; i < $('#menu ul li a').length; i++) {
        var bits = $('#menu ul li a').eq(i).attr('href').split('/');
        if(bits[bits.length-1] == h) {
          if(!isIE) { $('#menu ul li:not(.languagechoice li)').eq(i).css({opacity:1}); }
          break;
        }
      }
      $('#button-' + h.replace('.php', '')).show();
    }    
  }, function() {
    if(!isIE || curPage != 'home') {
      if(!isIE) { $('#menu ul li:not(.languagechoice li):not(.active)').css({opacity:0.85}); } 
      $('.mirror-button').each(function() {
        if(!$(this).hasClass('.active')) {
          $(this).hide();
        }
      });
    }
  }).click(function(e) {
    e.preventDefault();
    fadeTestimonial();
    var h = $(this).attr('href');
    if(h.indexOf('.') != -1) {
      window.location.href = h;
    } else {
      loadMainContent(curLang + '/' + h);
    }
  });
  
  /* Check for keyword in hash */
  var hash = window.location.hash.replace('#', '');
  if(hash != curHash) {
    loadMainContent(hash);
  } else {
    doResize();
  }
  doPageSpecificInits();
  setInterval(checkHash, 300);
});

/* Add onResize function */
$(window).resize(doResize);
window.onresize = doResize;

function checkHash() {
  var hash = window.location.hash.replace('#', '');
  if(hash != curHash) {      
    loadMainContent(hash);
  }
}
function doPageSpecificInits() {
  $('#temp-iframe').remove();
  //$('#button-play').unbind('click').fadeOut(300);

  /* Home */
  if($('.introtext').length > 0) {
    $('.introtext').delay(1000).animate({left:'-1700px'}, {duration:14000, easing:'linear'});
  }
  
  /* Offerte */
  if($('.nextbutton').length > 0) {
    $('select').customStyle();
    $('.fileinput input').css({opacity:0});
    $('.fileinput input').change(function() {
      var vbits = $(this).val().split('\\');
      $(this).prev().html(vbits[vbits.length - 1]);
    });
    if(isIE) {
      $('.fileinput span').css({margin:'0 0 0 95px'});
    }
    $('.nextbutton, .prevbutton, .sendbutton').each(function() {
      if($(this).hasClass('extramargintop') && $.browser.webkit) {
        $(this).css({margin:'22px 0 0 0'});
        $('.form-holder textarea').css({height:'212px'});
      }
    });
    $('body').append($('<iframe src="" name="temp-iframe" id="temp-iframe" style="padding:0; margin:0; border:0; width:0; height:0; position:absolute; left:0; top:0;"></iframe>'));
    $('.nextbutton').click(function(e) {
      e.preventDefault();
      $('.form-holder').stop(true, true).animate({left:'-=319px'}, 400);
    });
    $('.prevbutton').click(function(e) {
      e.preventDefault();
      $('.form-holder').stop(true, true).animate({left:'+=319px'}, 400);
    });
    $('.sendbutton').click(function(e) {
      e.preventDefault();
      var ok = true;
      var msg = strLegevelden + '<br />';
      var fields = ['off-bedrijfsnaam', 'off-contactpersoon', 'off-straat', 'off-huisnummer', 'off-postcode', 'off-plaats', 'off-telefoon', 'off-fax', 'off-email', 'off-vakgebied', 'off-aantalpaginas', 'off-aantalwoorden', 'off-brontaal', 'off-doeltaal1', 'off-doeltaal2', 'off-doeltaal3', 'off-bijlage1', 'off-bijlage2', 'off-bijlage3', 'off-opmerkingen'];
      for(var i in fields) {
        var id = '#' + fields[i];
        if($(id).hasClass('required')) {
          $(id).prev().removeClass('emptyfield');
          var val = $(id).val();
          if(val == '') {
            $(id).prev().addClass('emptyfield');
            msg += '<br /> - ' + $(id).prev().html();
            ok = false;
          }
        }
      }
      if(ok) {
        $('#message').remove();
        $('#temp-iframe').load(function() {
          $('body').append($('<div id="message">' + $(this).contents().text() + '</div>'));
          $('#message').css({left:(($(window).width() - 340) / 2) + 'px', top:(($(window).height() - 200) / 2) + 'px'});
          setTimeout(function() {
            $('#message').fadeOut(200, function() {
              $(this).remove();
            });
          }, 3000);
        });
        $('#off-form').submit();
      } else {
        $('#message').remove();
        $('body').append($('<div id="message">' + msg + '</div>'));
        $('#message').css({left:(($(window).width() - 340) / 2) + 'px', top:(($(window).height() - 200) / 2) + 'px'});
        setTimeout(function() {
          $('#message').fadeOut(200, function() {
            $(this).remove();
          });
        }, 3000);
        $('.form-holder').stop(true, true).animate({left:'0px'}, 400);
      }
    });
  }
  
  /* Medewerkers */
  if($('.medewerkerlink').length > 0) {
    $('.medewerkerlink').click(function(e) {
      e.preventDefault();
      $('.hidden').not($(this).next().next()).slideUp(500);
      $(this).next().next().css({color:'#eac4c3'}).slideDown(500);
      var id = $(this).attr('href').split('#')[1];
      if(id == '1') {
        // Film
        if(!isIE) { 
          $('#mirror-vignette').css({background:'url(../images/vignette-film.png) no-repeat'}).fadeIn(400);
        } else {
          $('#mirror-vignette').css({background:'url(../images/vignette-film.png) no-repeat'}).show();
        }
        $('#mirror-content').fadeOut(400, function() {
          $(this).removeAttr('style').html($('<div style="margin:0 0 0 -10px;height:230px;width:230px;z-index:1;"><div id="playerLite"></div></div>'));
          setTimeout(function() {
            var flashvars = {
              vidWidth: "230",
              vidHeight: "230",
              vidPath: "../../video.flv",
              autoPlay: "true",
              autoLoop: "false",
              watermark: "hide",
              seekbar: "hide"
            };
            var params = {
              menu: "false",
              allowfullscreen: "false",
              allowscriptaccess: "always",
              wmode:"transparent"
            };
            var attributes = {
              id: "playerLite",
              name: "playerLite",
              wmode:"transparent"
            };
            
            swfobject.embedSWF("../playerLite/swf/playerLite.swf", "playerLite", flashvars.vidWidth, flashvars.vidHeight, "9.0.0","../playerLite/swf/expressInstall.swf", flashvars, params, attributes);
            subtitles();            
          }, 500);
          $(this).fadeIn(600);
        });
      } else {
        clearTimeout(subsTimeout);
        $('#subtitles').fadeOut(400, function() {
          $(this).html('');
        });
        if(!isIE) { 
          $('#mirror-vignette').fadeOut(400);
        } else {
          $('#mirror-vignette').hide();
        }
        $('#mirror-content').fadeOut(400, function() {
          $(this).css({height:'229px', width:'229px', left:'23px'});
          $(this).html($('<img src="../website_images/medewerkers/foto_' + id + '.png" />"')).fadeIn(600);
        });
      }
    });
  }
  
  /* Nieuws */
  if($('.nieuwslink').length > 0) {
    $('#mirror-content').css({height:'229px', width:'229px', left:'23px', top:'70px'})
    $('.nieuwshide').click(function(e) {
      e.preventDefault();
      $('.nieuws-overlay-holder').animate({top:'-2000px'}, 800);
    });
    $('.nieuwslink').click(function(e) {
      e.preventDefault();
      $('.list li ul').not($(this).next('ul')).slideUp(500);
      $(this).next('ul').slideDown(500);
      
      // Woord van de maand
      if($(this).attr('href') == '#woordvandemaand') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'blogcat', blogcat:1},
          success:function(data) {
            $('.nieuws-overlay h1').html('');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.bloglink').click(function(e) {
              e.preventDefault();
              var id = $(this).attr('href');
              $('.blogtext').not($(id)).slideUp(500);
              $(id).slideDown(500);
            });
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
        
        // Sponsoring
      } else if($(this).attr('href') == '#sponsoring') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'sponsoring', blogcat:1},
          success:function(data) {
            $('.nieuws-overlay h1').html('');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.bloglink').click(function(e) {
              e.preventDefault();
              var id = $(this).attr('href');
              $('.blogtext').not($(id)).slideUp(500);
              $(id).slideDown(500);
            });
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
      
      // Tweets
      } else if($(this).attr('href') == '#tweets') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'tweets'},
          success:function(data) {
            $('.nieuws-overlay h1').html('Tweets');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
        
        // Magische links
      } else if($(this).attr('href') == '#magischelinks') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'blogcat', blogcat:5},
          success:function(data) {
            $('.nieuws-overlay h1').html('');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.bloglink').click(function(e) {
              e.preventDefault();
              var id = $(this).attr('href');
              $('.blogtext').not($(id)).slideUp(500);
              $(id).slideDown(500);
            });
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
        // Nieuwsbrieven
      } else if($(this).attr('href') == '#nieuwsbrieven') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'nieuwsbrieven'},
          success:function(data) {
            $('.nieuws-overlay h1').html('');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
      // Vacatures
      } else if($(this).attr('href') == '#vacatures') {
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay .nieuwscontent').html('');
        $.ajax({
          url:'ajax.php',
          data:{what:'blogcat', blogcat:4},
          success:function(data) {
            $('.nieuws-overlay h1').html('');
            $('.nieuws-overlay .nieuwscontent').html(data);
            $('.bloglink').click(function(e) {
              e.preventDefault();
              var id = $(this).attr('href');
              $('.blogtext').not($(id)).slideUp(500);
              $(id).slideDown(500);
            });
            $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
          }
        });
      }
      
      // Empty mirror content if new page is not partners
      if($(this).attr('href') != '#partners') {
        $('#mirror-content').fadeOut(400, function() {
          $(this).css({height:'229px', width:'229px', left:'23px', top:'70px'}).html($('<img src="../website_images/nieuws/eifeltoren.png" />"')).fadeIn(600);
          $('#nieuws-plaque').fadeIn(400);
        });
      }
    });
    
    /* Partners */
    $('.partnerlink').click(function(e) {
      e.preventDefault();
      $('.hidden').not($(this).next()).slideUp(500);
      $(this).next().css({color:'#d0c9d6'}).slideDown(500);
      var id = $(this).attr('href').split('#')[1];
      $('#mirror-content').fadeOut(400, function() {
        $(this).css({height:'229px', width:'229px', left:'23px', top:'70px'});
        $(this).html($('<img src="../website_images/partners/foto_' + id + '.png" />"')).fadeIn(600);
      });
      $('#nieuws-plaque').fadeOut(400);
    });
    
    /* Pers */
    $('.perslink').parent().click(function(e) {
      if($(this).attr('target') == '') {
        e.preventDefault();
        var id = $(this).attr('href').split('#')[1];
        $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
        $('.nieuws-overlay h1').html($('.perslink', this).html());
        $('.nieuws-overlay .nieuwscontent').html('');
        if($(this).hasClass('hasFoto')) {
          $('.nieuws-overlay .nieuwscontent').append($('<img src="../website_images/pers/foto_' + id + '.jpg" />"'));
        }
        if($(this).hasClass('hasPDF')) {
          $('.nieuws-overlay .nieuwscontent').append($('<br /><a href="../website_files/pdf_' + id + '.pdf" target="_blank">Download PDF</a>"'));
        }
        $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
      }
    });
    
    /* Fotoalbum */
    $('.fotoalbumlink').click(function(e) {
      e.preventDefault();
      var id = $(this).attr('href').split('#')[1];
      $('.nieuws-overlay').css({height:($(window).height()-50) + 'px'});
      $('.nieuws-overlay .nieuwscontent').html('');
      $.ajax({
        url:'ajax.php',
        data:{what:'fotoalbum', fotoalbumid:id},
        success:function(data) {
          $('.nieuws-overlay h1').html('');
          $('.nieuws-overlay .nieuwscontent').html(data);
          var w = $('.thumbs .mask .holder .thumb').length * 82;
          $('.thumbs .left').click(function() {
            if($('.thumbs .mask .holder').css('left').replace('px', '') < -81) {
              $('.thumbs .mask .holder').stop(true, true).animate({left:'+=82px'}, 400);
            }
          });
          $('.thumbs .right').click(function() {
            if(Number($('.thumbs .mask .holder').css('left').replace('px', '')) > 660 - w) {
              $('.thumbs .mask .holder').stop(true, true).animate({left:'-=82px'}, 400);
            }
          });
          $('.thumbs .mask .holder .thumb img').click(function() {
            var newSrc = $(this).attr('src').replace('_thumb', '');
            $('.groot .foto img').fadeOut(200, function() {
              $(this).removeAttr('src');
              $(this).attr('src', newSrc);
            });
          });
          $('.groot .foto img').load(function(){setTimeout(imgLoaded, 200);});
          $('.nieuws-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, {duration:1200, complete:function(){setTimeout(imgLoaded, 800);}});
        }
      });
    });
  } else {
    $('#nieuws-plaque').fadeOut(400);
  }
  
  /* Tarieven */
  if($('.tarieftaal').length > 0) {
    $('.termsshow').click(function(e) {
      e.preventDefault();
      $('.terms-overlay').css({height:($(window).height()-50) + 'px'});
      $('.terms-overlay-holder').animate({top:(0-($('#left-content').offset().top)) + 'px'}, 1200);
    });
    $('.termshide').click(function(e) {
      e.preventDefault();
      $('.terms-overlay-holder').animate({top:'-2000px'}, 800);
    });
    $('.tarieftaal').css({display:'block', opacity:0.5}).hover(function() {
      $(this).not('.active').css({opacity:1});
    }, function() {
      $(this).not('.active').css({opacity:0.5});
    });
    $('.tarieftaal div').click(function() {
      var bits = $(this).attr('id').split('-');
      if(bits[0] == 'van') {
        tarievenVanId = bits[1];
      } else {
        tarievenNaarId = bits[1];
      }
      if(tarievenVanId != 0 && tarievenNaarId != 0) {
        $('.tarieven-ppw').fadeOut(300, function() {
          $(this).html('&euro; ' + tarievenObject['v' + tarievenVanId + 'n' + tarievenNaarId]).fadeIn(400);
        });
      }
      $(this).parent().parent().find('.active').removeClass('active').stop(true, true).fadeTo(300, 0.5);
      $(this).parent().addClass('active').stop(true, true).fadeTo(200, 1);
    });
  }
}
function subtitles() {
  if(curLang != 'NL') {
    if(videoTime == 0) {
      videoTime = Number(new Date());;
    }
    var videoTimeSince = Number(new Date()) - videoTime;
    var last = false;
    if(videoTimeSince > 71000) {
      $('#subtitles').fadeOut(400, function() {
        $(this).html('');
      });
      last = true;
    } else if(videoTimeSince > 63000) {
      $('#subtitles').html('They showered the girl with gold and jewels to thank her for her help, and she lived happily every after.');
    } else if(videoTimeSince > 58700) {
      $('#subtitles').html('At last, they were able to sell them to people from far-away countries.');
    } else if(videoTimeSince > 53000) {
      $('#subtitles').html('The people were very grateful. Never before had the market dealers sold so many of their wares.');
    } else if(videoTimeSince > 46700) {
      $('#subtitles').html('And so the young girl helped these people to find the right meaning of all those strange, exotic letters and words.');
    } else if(videoTimeSince > 42000) {
      $('#subtitles').html('“Please, Miss,” they would cry. “Help me with these letters! I don’t understand them!”');
    } else if(videoTimeSince > 37500) {
      $('#subtitles').html('On her path, she met many people who didn\'t know as many words or letters as she did.');
    } else if(videoTimeSince > 32800) {
      $('#subtitles').html('She studied hard until one day she was old enough to go out into the big, wide world on her own.');
    } else if(videoTimeSince > 27000) {
      $('#subtitles').html('She learned how to turn the letters of her own kingdom into letters of foreign lands and strange cultures.');
    } else if(videoTimeSince > 21800) {
      $('#subtitles').html('In the deep south of the kingdom in which she lived, she found wise people who taught her everything there was to learn about letters.');
    } else if(videoTimeSince > 18000) {
      $('#subtitles').html('One day, she decided to earn her living with letters.');
    } else if(videoTimeSince > 11000) {
      $('#subtitles').html('Her favourite letters were those with magical powers and a moral, telling of good conquering evil.');
    } else if(videoTimeSince > 6200) {
      $('#subtitles').html('Not just any letters, but letters with the power to touch one’s soul.');
    } else if(videoTimeSince > 1500) {
      $('#subtitles').html('Once upon a time, long, long ago, there lived a girl who loved letters.').fadeIn(400);
    }
    if(!last) { subsTimeout = setTimeout(subtitles, 100); }
  }
}
function imgLoaded() {
  if($('.groot .foto img').height() != $('.groot .foto').height()) {
    $('.groot .foto').animate({height:$('.groot .foto img').height()}, {duration:400, complete:function() {
      $('.groot .foto img').fadeIn(400);
    }});
  } else {
    $('.groot .foto img').fadeIn(400);
  }
}
function overwriteMenu() {
  $('#menu ul li a').click(function(e) {
    e.preventDefault();
    fadeTestimonial();
    var h = $(this).attr('href');
    if(h.indexOf('.') != -1) {
      window.location.href = h;
    } else {
      loadMainContent(h);
    }
  });
  $('#menu ul li:not(.languagechoice li):not(.active)').hover(function() {
    if(!isIE) { $(this).stop(true, true).fadeTo(400, 1); }
    if(!isIE || curPage != 'home') {
      var bits = $('a', this).attr('href').split('/');
      $('#button-' + bits[bits.length-1].replace('.php', '')).show();
    }
  }, function() {
    if(!$(this).parent().hasClass('active') && !isIE) {
      $(this).stop(true, true).fadeTo(300, 0.85);
    }
    if(!isIE || curPage != 'home') {
      $('.mirror-button').each(function() {
        if(!$(this).hasClass('.active')) {
          $(this).hide();
        }
      });
    }
  });
  if(!isIE) {
    $('#menu ul li:not(.languagechoice li):not(.active)').css({opacity:0.85}); 
    $('#menu ul li.active').css({opacity:1});
  }
}
function getTestimonial() {
  fadeTestimonial();
  if(curPage != 'klanten' && curPage != 'home') {
    $.ajax({
      url:'ajax.php',
      data:{what:'testimonial'},
      success:function(data) {
        $('#testemonial').html(data).fadeIn(1500);
        testimonialTimeout = setTimeout(fadeTestimonial, 10000);
      }
    });
  }
}
function fadeTestimonial() {
  clearTimeout(testimonialTimeout);
  $('#testemonial').fadeOut(700);
}
function getSafeCoords(counter) {
  var rX = rY = 0;
  var tooClose = false;
  rX = 20 + Math.round(Math.random() * ($(window).width() - 210));
  rY = 40 + Math.round(Math.random() * ($(window).height() - 340));
  if(Math.abs($('#mirror').offset().left - rX) < 300 && Math.abs($('#mirror').offset().top - rY) < 550) {
    tooClose = true;
  }
  if(Math.abs($('#logo').offset().left - rX) < 250 && Math.abs($('#logo').offset().top - rY) < 250) {
    tooClose = true;
  }
  if($('.mirror-dotted').length > 0 && !tooClose) {
    for(var i = 0; i < $('.mirror-dotted').length; i++) {
      if(Math.abs($('.mirror-dotted').eq(i).offset().left - rX) < 200 && Math.abs($('.mirror-dotted').eq(i).offset().top - rY) < 350) {
        tooClose = true;
        break;
      }
    }
  }
  if(tooClose) {
    if(counter < 10) {
      return getSafeCoords(counter + 1);
    } else {
      return false;
    }
  } else {
    return [rX, rY];
  }
}
function placeKlanten() {
  if(curPage == 'klanten' || curPage == 'home') {
    if(klantenIndex >= klantenArray.length) {
      klantenIndex = 0;
    }
    var white = '../website_files/white_' + klantenArray[klantenIndex].intID + '.png';
    var size = ((klantenIndex + 1) % 3 == 0) ? '' : ' medium';
    var k = $('<div class="hoverable mirror-dotted' + size + '" id="klant' + klantenIndex + '" title="' + klantenArray[klantenIndex].alt + '"><img src="' + white + '" class="hoverable" alt="' + klantenArray[klantenIndex].alt + '" /></div>');
    var r = getSafeCoords(0);
    if(r === false) {
      if(!isIE) { 
        klantenTimeout = setTimeout(placeKlanten, 1000);
      } else {
        klantenTimeout = setTimeout(placeKlanten, 2000);
      }
      return;
    }
    var rX = r[0];
    var rY = r[1];
    k.css({display:'none', top:rY + 'px', left:rX + 'px'})
    $('body').append(k);
    if(!isIE) { 
      $('#klant' + klantenIndex).fadeIn(1200, function() {
        var i = $(this);
        setTimeout(function() { $(i).fadeOut(800, function() { $(this).remove(); }); }, 12000);
      });
    } else {
      $('#klant' + klantenIndex).show();
      var i = $('#klant' + klantenIndex);
      setTimeout(function() { $(i).remove(); }, 12000);
    }
    $('#klant' + klantenIndex).hover(function() {
      $(' img', this).attr('src', $(' img', this).attr('src').replace('white_', 'color_'));
    }, function() {
      $(' img', this).attr('src', $(' img', this).attr('src').replace('color_', 'white_'));
    });
    preloadImages('../website_files/color_' + klantenArray[klantenIndex].intID + '.png');
    klantenIndex++;
    if(!isIE) { 
      klantenTimeout = setTimeout(placeKlanten, 1000);
    } else {
      klantenTimeout = setTimeout(placeKlanten, 2000);
    }
  }
}
function loopKlanten(klanten) {
  clearTimeout(klantenTimeout);  
  if(curPage == 'klanten') {
    $('#mirror-mask').hide();
    $('#mirror').animate({opacity:0}, {duration:400, complete:function() {
      $(this).removeAttr('style').hide();
      $('#mirror-mask').show();
    }});
  }
  klantenIndex = 0;
  klantenArray = eval('[' + klanten + ']');
  if($('.mirror-dotted').length == 0) {
    if(klantenArray.length) {
      placeKlanten();
    }
  } else {
    if(!isIE) {
      $('.mirror-dotted').fadeOut(100, function() {
        $(this).remove();
        if($('.mirror-dotted').length == 0) {
          if(klantenArray.length) {
            placeKlanten();
          }
        }
      });
    } else {
      $('.mirror-dotted').remove();
      if(klantenArray.length) {
        placeKlanten();
      }
    }
  }
}
function loadMainContent(hash) {
  /* Check language and page choice */
  var bits = hash.split('/');
  if(bits[0] == 'EN' || bits[0] == 'F' || bits[0] == 'D' || bits[0] == 'NL') {
    var language = bits[0];
    var keyword = (bits[1] != undefined) ? bits[1] : curPage;
  } else {
    var language = curLang;
    var keyword = bits[0];
  }
  
  /* Only if new content is not the same as current content */
  if(hash != curHash) {
    /* Stop any previous ajax call */
    if(xhr != null) {
      xhr.abort();
    }
    
    window.location.hash = language + '/' + keyword;
    $('#menu ul.lang li').removeClass('active');
    
    /* Set current language as active */
    $('#menu ul.lang li a').each(function() {
      var bits = $(this).attr('href').split('/');
      if(bits[0] == language) {
        $(this).parent().addClass('active');
      }
    });
    curHash = hash;
    curPage = keyword;
    $('.mirror-button').removeClass('.active').hide();
    $('#button-' + curPage).addClass('.active').show();
    curLang = language;
    
    /* Cleanup */
    clearTimeout(klantenTimeout);
    if(!isIE) {
      $('#mirror-vignette').fadeOut(200);
      $('.mirror-dotted').fadeOut(100, function() { $(this).remove(); });
    } else {
      $('#mirror-vignette').hide();
      $('.mirror-dotted').remove();
    }
    $('#subtitles').fadeOut(400, function() {
      $(this).html('');
    });
    
    /* Get and place new content */
    xhr = $.ajax({
      url:'ajax.php',
      data:{p:keyword, t:language},
      success:function(data) {
        var bits = data.split('-+||+-');
        fadeOutCounter = 0;
        $('body').animate({backgroundColor:bits[0]}, 400);
        customAnimateOut('#right-content, #mirror-content, #left-content, h1, h2, h3, form, .medewerker, .nieuwslink', function() {
          fadeOutCounter++;
          if(fadeOutCounter == 3) {
            $('#mirror-content').removeAttr('style');
            $('#left-content').html(bits[1]);
            $('#mirror-content').html(bits[2]);
            $('#right-content').html(bits[3]);
            $('#menu ul').eq(0).html(bits[4]);
            overwriteMenu();
            doResize();
            if(curPage == 'home') {
              $('#mirror').show();
            }
            if(curPage == 'klanten' || curPage == 'home') {
              loopKlanten(bits[5]);
            } else {
              $('#mirror').show();
              if(curPage == 'tarieven') {
                tarievenObject = {};
                var pairs = bits[5].split('; ');
                for(var i in pairs) {
                  var tarieven = pairs[i].split(':');
                  tarievenObject['' + tarieven[0]] = tarieven[1];
                }
              } else if(curPage == 'nieuws') {
                $('#mirror-content').css({height:'229px', width:'229px', left:'23px', top:'70px'}).html($('<img src="../website_images/nieuws/eifeltoren.png" />')).fadeIn(600);
                $('#nieuws-plaque').html(bits[5]).fadeIn(400);
              }
              customAnimateIn('#left-content, #mirror, #mirror-content, #right-content');
              doPageSpecificInits();
            }
          }
        });
      }
    });
  }
}
function customAnimateOut(who, callback) {
  if(isIE) {
    $(who).css({opacity:0});
    if(callback != undefined) {
      fadeOutCounter = 2;
      callback();
    }
  } else {
    $(who).stop(true, true).animate({opacity:0}, {duration:400, complete:callback});
  }
}
function customAnimateIn(who, callback) {
  if(isIE) {
    $(who).removeAttr('style');
    if(callback != undefined) {
      callback();
    }
  } else {
    $(who).stop(true, true).animate({opacity:100}, {duration:800, complete:callback});
  }
}
function doResize() {
  var miscH = 60 + 150;
  var minH = $('#content-holder').outerHeight() + miscH;
  $('#content').css({height:$('#content-holder').outerHeight()});
  var winH = $(window).height();
  $('#overflow').css({width:$(window).width(), height:$(window).height(), visibility:'visible'});
  if(winH < minH) {
    $('#holder').css({height:minH});
    var newT = 60;
  } else {
    $('#holder').css({height:'100%'});
    var newT = 60 + ((winH - minH) / 2);
  }
  if($('#content-holder').css('display') == 'none') {
    var d = (isIE) ? 0 : 400;
    $('#content-holder').css({opacity:100, top:newT + 'px'}).fadeIn(d);
  } else { 
    $('#content-holder').stop(true, true).animate({opacity:100, top:newT + 'px'}, 500);
  }
}
function addSparkles(skipCheck) {
  if(skipCheck !== true) { var skipCheck = false; }
  if(skipCheck || (lastX != mainX && lastY != mainY)) {
    lastX = mainX;
    lastY = mainY
    $('body').append($('<img src="images/sparkle.png" id="sparkle_' + nr + '"/>'));
    var margin = Math.round((0.5 - Math.random()) * 15);
    $('#sparkle_' + nr).css({position:'absolute', left:mainX + margin, top:mainY, zIndex:300000});
    $('#sparkle_' + nr).animate({top:'+=100px', opacity:0}, {duration:1000, easing:'linear', complete:function() { $(this).remove(); }});
    nr++;
  }
}
function preloadImages() {
  for(var i in arguments) {
    var oImg = new Image();
    oImg.src = arguments[i];
  }
}
function toggleMultipleFocus() {
  for(var i in arguments) {
    toggleFocus(arguments[i]);
  }
}
function toggleFocus(id) {
  var val = $(id).val();
  $(id).focus(function() {
    if($(this).val() == val) {$(this).val('');}
  }).blur(function() {
    if($(this).val() == '') {$(this).val(val);}
  });
}
