/* part of fxisCMS framework */

$(document).ready(function(){

  //function extension
  $.fn.repairWidth = function() {
      var width = 0;
      $('li',this).each(function(){
        width = width+$(this).outerWidth();
      });
      $(this).width(width+10);
  }
  
  //background effect
  function repositionBackground(){
    $('#backgroundimage').height('auto');
    $('#backgroundimage').css('bottom','0px');
    $('#backgroundimage').width('100%');
    var winHeight = $(window).height();
    if(winHeight >= $('#backgroundimage').height()){
      $('#backgroundimage').css('height',winHeight+'px');
    } else {
      $('#backgroundimage').height('auto');
    }
  }
  $(window).bind("resize", repositionBackground);
  $('.wrapper').hoverIntent(function(){
    adjustOverlay();
    $('.overlay').css('z-index',-1);
    $('.overlay').fadeTo('fast',0.8);
  },function(){
    $('.overlay').fadeOut('fast');
  });
    
  
  //error box overlay
  function adjustOverlay(){
    $('.overlay').height($(window).height());
    $('.overlay').width($(window).width());
    $('.overlay').click(function(){
      closeOverlay();
    });
    
    $('.messagebox #close').click(function(e){
      e.preventDefault();
      closeOverlay();
    });        
  }
  function closeOverlay(){
    $('.overlay').fadeOut('fast');
    $('.messagebox').fadeOut('fast');  
  }
  function adjustMessagebox(){
    var width = $('.messagebox').outerWidth();
    var height = $('.messagebox').outerHeight();
    $('.messagebox').css("top", ( $(window).height() - height ) / 2+$(window).scrollTop() + "px");
    $('.messagebox').css("left", ( $(window).width() - width ) / 2+$(window).scrollLeft() + "px");    
  }
  $(window).bind("resize", adjustOverlay);
  $(window).bind("resize", adjustMessagebox);
  $(window).bind("scroll", adjustMessagebox);
  $(window).bind("scroll", adjustOverlay);

  //fix png
  $('body').supersleight({shim: '/gfx/x.gif'});

  //guestbook
  if($('#guestbook').length > 0){
    //if there is an error
    if($('#guestbookForm .error').length > 0){
      createGuestbookForm();    
    }
    $('#guestbookSubmitMessage').click(function(){
      createGuestbookForm();
    });
    
    //get the submitbutton click
    $('#guestbookForm form').submit(function(e){
      var name = $('#fxiscmsName').val();
      var email = $('#fxiscmsEmail').val();
      var subject = $('#fxiscmsSubject').val();
      var captcha = $('#fxiscmsCaptcha').val();
      var emailCheck = /^(?:$)|(?:[a-zA-Z0-9\-_.]+@[a-zA-Z0-9\-_.]+\.[a-zA-Z]{1,5}$)/.exec(email);
      var nameCheck = /^[0-9a-zA-Z.,-_&@ ]{3,255}$/.exec(name);
      var subjectCheck = /^(?:$)|(?:(?:.+){3,255}$)/.exec(subject);
      var captchaCheck = /^[A-Z1-9]{6}$/.exec(captcha);
      
      //remove errors
      $('#guestbookForm .errorSmall').each(function(){
        $(this).remove();
      });
      var returnCheck = true;
      if(!emailCheck){
        $('<div class="errorSmall">**Geen geldig emailadres opgegeven</div>').hide().appendTo('#guestbookForm .row-fxiscmsemail .right').fadeIn('fast');
        returnCheck = false;
      }
      if(!nameCheck){
        $('<div class="errorSmall">**Geen geldige naam opgegeven</div>').hide().appendTo('#guestbookForm .row-fxiscmsname .right').fadeIn('fast');
        returnCheck = false;
      }
      if(!subjectCheck){
        $('<div class="errorSmall">**Geen geldig onderwerp opgegeven</div>').hide().appendTo('#guestbookForm .row-fxiscmssubject .right').fadeIn('fast');
        returnCheck = false;
      }
      if(!captchaCheck){
        $('<div class="errorSmall">**Geen geldige Captcha code opgegeven</div>').hide().appendTo('#guestbookForm .row-fxiscmscaptcha .right').fadeIn('fast');
        returnCheck = false;
      }             
      
      //return
      if(returnCheck){
        return true;
      } else {
        return false;
      }
    });    
  }
  
  function createGuestbookForm(){
    if($('.overlay').length > 0) $('.overlay').remove();
    $('#guestbookForm').hide().prependTo('body').fadeIn('slow');
    $('<div class="overlay" id="guestbookFormOverlay"></div>').hide().prependTo('body');      
    $('.overlay').fadeIn('fast');
    $('.messagebox').fadeIn('fast');
    adjustOverlay();
    adjustMessagebox();  
  }  
  
  //pagination
  if($('#pagination').length > 0){
    $('#pagination span').each(function(){
      $(this).click(function(){
        var id = $(this).attr('id');
        var idReg = /^pagination-([0-9]+)$/.exec(id);
        if(id[0]){
          //construct url
          var params = $.url.attr('query');
          var path = $.url.attr('path');
          if(!/\/$/.exec(path)) path = path+'/';
          if(params){
            params = params.replace(/page[0-9]+/i,'page'+idReg[1]);
          } else {
            params = 'page'+idReg[1];
          }
          if(!/page/.exec(params)){
            params = params+'&page'+idReg[1];
          }        
          $(location).attr('href',path+'?'+params);
        }
      });
    });
  }  
  
  //contact form
  if($('#contactForm').length > 0){
    //get the submitbutton click
    $('#contactForm form').submit(function(e){
      var name = $('#fxiscmsName  ').val();
      var email = $('#fxiscmsEmail').val();
      var emailCheck = /^[a-zA-Z0-9\-_.]+@[a-zA-Z0-9\-_.]+\.[a-zA-Z]{1,5}$/.exec(email);
      var nameCheck = /^[0-9a-zA-Z.,-_&@ ]{3,255}$/.exec(name);
      
      //remove errors
      $('#contactForm .errorSmall').each(function(){
        $(this).remove();
      });

      var returnCheck = true;
      if(!emailCheck){
        $('<div class="errorSmall">**Geen geldig emailadres opgegeven</div>').hide().appendTo('#contactForm .row-fxiscmsemail .right').fadeIn('fast');
        returnCheck = false;
      }
      if(!nameCheck){
        $('<div class="errorSmall">**Geen geldige naam opgegeven</div>').hide().appendTo('#contactForm .row-fxiscmsname .right').fadeIn('fast');
        returnCheck = false;
      }      
      
      //return
      if(returnCheck){
        return true;
      } else {
        return false;
      }
    });
  }
  
  //captcha Refresh
  if($('#captchaRefresh').length > 0 && $('#captchaImg').length > 0){
    $('#captchaRefresh').click(function(){
      var timestamp = new Date().getTime();
      $('#captchaImg').attr('src','/captcha.php?'+timestamp);
    });
  }
  
  //lightbox
  if ($('.attachments').length > 0){
    $('.attachments .lightbox').attr('rel','bijlage');
    $('.attachments .lightbox').colorbox({speed:500, maxWidth:'80%', maxHeight:'90%'});
  }
  
  //contact form
  if ($('#contactForm #berichtFieldFull').length > 0){
    //wysiwyg   
  };         
});
