/*
 * Wietse Wind, iPublications
*/

base = '/ipub/';

$(document).ready(function(){
  $("#menuswitch").show();
  
  loader = new Image();
  loader.src = base + 'images/loader.gif';
  
  MenuBeweegt  = 0;
  
  koffieLoaded  = 0;
  koffie        = new Image();
  koffie.src    = base + 'images/koffie.png';
  koffieTimeOut = null;
  $(koffie).load(function(){
    koffieLoaded = 1;
  });

  function blanktarget(){
    $("a").each(function(a,b){
      if(b.getAttribute('rel')){
        if(b.getAttribute('rel') == 'external'){
          href = b.getAttribute('href');
          if(!href.match('void')){
            $(b).unbind('click');
            $(b).click(function(){
              window.open(href);
            });
            $(b).attr("href", 'javascript:void(0)');
          }
        }
        if(b.getAttribute('rel') == 'inpage'){
          var href = b.href;
          if(!href.match('#')){
            if(href.match('p/')){
              href = substr(href, strpos(href, 'p/')+2);
              if(href.match('p/')){
                href = substr(href, strpos(href, 'p/')+2);
              }
              $(this).click(function(){
                getContents(base + '/' + href,'container-content');
              });
            }
            $(this).removeAttr('href');
          }
        }
      }
    });
  }
 
  blanktarget();
  
  function resizeTheContent(){
    if(loadingPage < 1){
      $("#container-content").animate({marginLeft: ($("#body-do-not-scroll").width()-999)/2 + 'px'}, 400, function(){
      });
    }
  }
  
  loadingPage     = 0;
  ajaxPageLoader  = null;
  ajaxPageTimeOut = null;  
  
  var resizeTimer = null;
  $(window).bind('resize', function() {
      if (resizeTimer) clearTimeout(resizeTimer);
      resizeTimer = setTimeout(resizeTheContent, 175);
  });
  
  $("#apenstaartje").html("@");
  $(".emailaddr").css({'cursor' : 'pointer'}).removeAttr('href').click(function(){
    getContents(base + '/contact.html','container-content');
  });
  
  function toArray(strg){
    strg = strg.replace(/left|top/g,'0px');
    strg = strg.replace(/right|bottom/g,'100%');
    strg = strg.replace(/(\d+)(\s|\)|$)/g,"$1px$2");
    var res = strg.match(/(\d+)(px|\%|em|pt)\s(\d+)(px|\%|em|pt)/);
    return [parseFloat(res[1]),res[2],parseFloat(res[3]),res[4]];
  }

  function strpos( haystack, needle, offset){
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
  }
  
  function substr( f_string, f_start, f_length ) {
    f_string += '';
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
  }

  (function($){$.extend($.fx.step,{backgroundPositionPlus: function(fx) {if (fx.state == 0 && typeof fx.end == 'string') {fx.start = [0,-35];var end = toArray(fx.end);fx.end = [end[0],end[2]];fx.unit = [end[1],end[3]];}var nowPosX = [];nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];}});})(jQuery);
  (function($){$.extend($.fx.step,{backgroundPositionMin: function(fx) {if (fx.state == 0 && typeof fx.end == 'string') {var start = $.curCSS(fx.elem,'backgroundPosition');start = toArray(start);fx.start = [start[0],start[2]];var end = toArray(fx.end); fx.end = [-end[0],-end[2]];fx.unit = [end[1],end[3]];}var nowPosX = [];nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];}});})(jQuery);

  $("#container-content").css({'margin-left' : ($("#body-do-not-scroll").width()-999)/2 + 'px'});
  $('body').css({backgroundPosition: '0px 165px'});
  $("#icons-menu").css({'padding-top' : '25px'});
  menuDisp = 1;

  function rand( min, max ) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  function getContents(pagina,doeldiv){
    if(loadingPage > 0){
      ajaxPageLoader.abort();
      $("#" + doeldiv).stop();
      if($("#loaderimage")){
        clearTimeout(ajaxPageTimeOut);
        clearTimeout(koffieTimeOut);
        $("#loaderimage").stop().animate({left: '-100%'}, 300, function(){
          $(this).remove();  
        });
        // $("#" + doeldiv).html('');
      }
    }
    loadingPage = 1;
    loaded = 0;
    $("#" + doeldiv).animate({marginLeft: '-999px'}, 600, function(){
      $(this).animate({marginLeft: $("#body-do-not-scroll").width() + 'px'}, 1, function(){
        $(this).hide();
        var mktime = new Date;
        ajaxPageLoader = $.get(pagina + '?moment=' + rand(1,999999999), {}, function(data){
          loaded = 1;
          $("#loaderimage").animate({left: '-100%'}, 500, function(){
            $(this).remove();  
          });
          $("#" + doeldiv).show().html($(data)).animate({marginLeft: ($("#body-do-not-scroll").width()-999)/2 + 'px'}, 500, function(){
            loadingPage = 0;
            blanktarget();
            // $(this).css({'margin-left' : 'auto', 'margin-right' : 'auto'});
          });
        }, 'html');
       });
    });
    ajaxPageTimeOut = setTimeout(function(){
      if(loaded < 1){
        $("#loaderimage").remove();  
        $("#icons-menu").after('<div id="loaderimage" style="position: absolute; left: ' + $("body").width() + 'px; margin-top: 85px; text-align: center; width: 100%;"><img border="0" src="' + base + 'images/loader.gif" /><div id="koffie"></div></div>');
        $("#loaderimage").animate({left: '0px'}, 600, function(){
          if(koffieLoaded > 0){
            koffieTimeOut = setTimeout(function(){
              if($("#koffie")){
                $("#koffie").html('<img src="' + base + 'images/koffie.png" border="0" style="margin-top: 20px;" /><br />En we laden gewoon rustig door...').hide().fadeIn(3000);
              }
            }, 6000);
          }
        });
      }
    }, 850);
  }  
  
  ///////
  
  // $.get("/ipub/homepage.html" + '?moment=' + rand(1,999999999), {}, function(data){$("#container-content").html($(data));}, 'html');

  $("#main-menu li").children().each(function(a,b){
    if(a != 4){
      var href = b.getAttribute('href');
      if(href.match('p/')){
        href = substr(href, strpos(href, 'p/')+2);
        if(href.match('p/')){
          href = substr(href, strpos(href, 'p/')+2);
        }
        $(this).click(function(){
          getContents(base + '/' + href,'container-content');
        });
      }
      $(this).removeAttr('href');
    }
  });

  $("#sub-menu li").children().each(function(a,b){
    var href = b.href;
    if(!href.match('#')){
      if(href.match('p/')){
        href = substr(href, strpos(href, 'p/')+2);
        if(href.match('p/')){
          href = substr(href, strpos(href, 'p/')+2);
        }
        $(this).click(function(){
          getContents(base + '/' + href,'container-content');
        });
      }
      $(this).removeAttr('href');
    }
  });

  ///////

  $("#menuswitch").removeAttr("href").click(function(){
    if(MenuBeweegt < 1){
      MenuBeweegt = 1;
      if(menuDisp > 0){
        $("#icons-menu").animate({height: '0px', paddingTop : '0'}, 500, function(){
          $(this).hide();
        });
        $('body').animate({backgroundPositionMin: '(0px 35px)'}, 500, function(){
          $('body').css({'background-position': '0px -35px'});
          MenuBeweegt = 0;
        });
        $(this).css({'background-position': '0 -28px'});
        menuDisp = 0;
      }else{
        $("#icons-menu").animate({height: '175px', paddingTop : '25px'}, 500, function(){
          $(this).show();
        });
        $('body').animate({backgroundPositionPlus: '(0px 165px)'}, 500, function(){
          MenuBeweegt = 0;  
        });
        $(this).css({'background-position': '0 0'});
        menuDisp = 1;      
      }
    }
  });
});