/* $Id: fom.js,v 1.49 2009-10-01 15:09:04 jasper Exp $ */

// CSS selectors
$("html").addClass("js");

// Content correctie en animatie
if (document.location.pathname == "/") $("#main > div").hide();
else ($.scrollTo("#main", 1000));

function handleContent(element) {
  // Image replacement
  $(".altImg", element).prepend("<span/>");

  $("div.rounded, p.buttons a, p.buttons button", element).each(function(){ round($(this)); });

  $(".correctMe:visible", element).each(function(){ fixDivHeight($(this)) });

  // Popup
  $("a.popup", element).click(function(){ return popup(this); });
  $("body.popup a", element).attr("target", "_blank");

  // Links
  $("a", element).each(function(){
    // Extern
    if (this.hostname.replace(/:[0-9]+$/, "") != document.location.hostname && this.protocol.match(/^http/)) {
      $(this).click(function(){
        var popup = window.open(this.href);
        return false;
      });
    }
    // Ajax load?
    else if (this.hash != "") {
      $(this).click(function(e){
        var a = e.currentTarget;

        // Alleen link naar hash?
        if ($(a).attr("href").indexOf("#") == 0) {
          $($(a).attr("href")).slideDown("fast", function(){ $.scrollTo($(a).attr("href"), 1000); });
          return false;
        }

        var loadTarget = $(a.hash);
        if (loadTarget.size() == 0) return true;
        $(a).addClass("loading");

        // MSIE snapt het niet en cached aanroepen
        var loadUrl = a.href.replace(/#.+$/, "");
        loadUrl += (loadUrl.indexOf("?") > 0 ? "&" : "?") +"time="+ (new Date()).getTime();

        loadContent(loadUrl, a.hash.substr(1), true, a);

        return false;
      });
    }
  });
}
handleContent($("body"));


function fixDivHeight(div) {
  var unit = 20;
  div.css({height: "auto"});
  if (div.outerHeight() % unit > 0) {
    div.height(div.height() + unit - div.outerHeight() % unit);
  }
}

function round(element){
  element.append("<div class=ne/><div class=se/><div class=sw/><div class=nw/>");
  if (element.get(0).nodeName == "BUTTON") element.wrapInner("<div class=button/>");
}

function showMainChild(id, scroll) {
  var scrollToNew = function(){
    fixDivHeight($("#"+ id));
    if (scroll != false) $.scrollTo("#"+ id, 1000);
  }
  var showNew = function(){ $("#"+ id).slideDown($("#"+ id).outerHeight() / 2, scrollToNew); }
  if ($("#"+ id +":visible").size() > 0) {
    scrollToNew();
    return;
  }
  var old = $("#main > div:visible");
  if (old.length != 0) old.slideUp(old.outerHeight() / 2, showNew);
  else showNew();
}

function loadContent(loadUrl, targetId, scroll, callingA){
  var loadTarget = $("#"+ targetId);
  if (loadTarget.size() == 0) {
    document.location.href = "/";
    return;
  }

  // Loggen GA
  if (typeof pageTracker != "undefined") {
    var GAlogPath = loadUrl;
    GAlogPath = GAlogPath.replace(/^http:\/\/[^\/]+/, "").replace(/\?time=[0-9]+/, "");
    pageTracker._trackPageview(GAlogPath);
  }

  $.get(
    loadUrl
  , {}
  , function(data, textStatus){
    if (textStatus == "success") {
      loadTarget.html("");
      loadTarget.append($("#main > div", data).html());

      round(loadTarget);
      handleContent(loadTarget);
      loadTarget.css({ height: "auto", display: "none"});
      showMainChild(targetId, scroll);

      if (callingA) $(callingA).removeClass("loading");
    }
    else if (callingA) document.location.href = a.href;
  }
  , "html"
  );
}

function popup(a){
  var popup = window.open(a.href, "popup", "width=600,height=600,scrollbars=yes");
  popup.focus();
  return false;
}

var flashBNer = null;
function setBNer(voornaam){
  flashBNer = voornaam;
  if (typeof checkBNer == "function") checkBNer(voornaam);
}

// Ticker scrollen
Ticker = {
  sleep: 6000
, start: function(){ setTimeout(Ticker.showNext, Ticker.sleep); }
, showNext: function(){
    $("#ticker li:first-child").animate({marginTop: "-"+ $("#ticker").height() +"px"}, "normal", "swing", function(){
      $("#ticker li:first-child").appendTo("#ticker ul").css({marginTop: "0px"});
      setTimeout(Ticker.showNext, Ticker.sleep);
    });
  }
}
Ticker.start();

