/**
 * Studenten voor Morgen
 * global scripts
 *
 * Copyright 2011-04, Tuxion
 * www.tuxion.nl/aangenaam
 */
jQuery(document).ready(function($){

  //Remove input field value on click
  $.fn.search = function() {
    return this.focus(function() {
      if( this.value == this.defaultValue ) {
        this.value = "";
      }
    }).blur(function() {
      if( !this.value.length ) {
        this.value = this.defaultValue;
      }
    });
  };
  $("#subscribe_this_email_address").search();

  //Define vars
  var slides = {}, _slides = new Array(), url_hash, menu_width;
  slides.all = $('.slide-wrapper');
  slides.all.data('status', 'close');
  slides.all.each(function(){$(this).data('hash',$(this).attr('id'));_slides[$(this).attr('id')] = $(this);});
  
  //Set menu width function
  slides.set_menu_width = function(instant)
  {
    var sidebar_width = $('#sidebar_right').width(); //Get width of sidebar 200px
    var content_width = $('.slide-wrapper.active').width(); //Get width of active content 640px
    $('.slide-wrapper.active .menu-wrapper').css('width', 0).hide(); //Get width of active content 640px

    var menu_count = $('.slide-wrapper').size()-1;
    menu_width = Math.floor(($(window).width() - (sidebar_width + content_width)) / menu_count);
    if(instant)
      $('.slide-wrapper').css('width', menu_width+'px');
    else
      $('.slide-wrapper').animate({'width': menu_width+'px'}, 200);
  }

  //Slides open function
  slides.open = function(slide)
  {
    var BWR = false;

    if(BWR)
    {
      slide.addClass('active');
      slide.data('status', 'open');

      $('.menu-wrapper').each(function(){
        if($(this).is(':hidden'))
          $(this).show().animate({'width':menu_width+'px'}, 1000, function(){$(this).css('width', 'auto');});
      });
      slide.find('.menu-wrapper').animate({'width':'0px'}, 1000,function(){$(this).hide()});
      slide.find('.inner').hide().fadeIn();
    }
    else
    {
      slide.addClass('active');
      slide.data('status', 'open');

      $('.menu-wrapper').each(function(){
        if($(this).is(':hidden'))
          $(this).show().animate({'width':menu_width+'px'}, 200, function(){$(this).css('width', 'auto');});
      });
      slide.find('.menu-wrapper').animate({'width':'0px'}, 200,function(){$(this).hide()});
      slide.find('.inner').hide().fadeIn();
    }
  }
  
  //Slides close function
  slides.close = function(slide)
  {
    slide.stop(true);
    slide.removeClass('active');
    slide.data('status', 'close');
  }

  //Slides autoload function
  slides.autoload = function(){
    url_hash = String(window.location).split("#")[1];
    if(url_hash == undefined) window.location = '#'+slides.all.data('hash');
    slides.current = (url_hash ? _slides[url_hash] : false);
    setTimeout(function(){
      if(slides.current){
        if(slides.current.data('status') == 'close'){
          slides.open(slides.current);
        }
      }
      else
      {
        slides.open(slides.all.data('hash'));
        alert(slides.all.data('hash'));
      }
    }, 10);
  }
  slides.autoload();
  $(window).hashchange(function(){
    if(slides.current && slides.current.data('status') == 'open'){
      slides.close(slides.current);
    }
    slides.autoload();
  });

  //define width of slides
  setTimeout(function(){slides.set_menu_width(false);}, 100);
  //on resize: calculate width of slides again
  $(window).resize(slides.set_menu_width);

  slides.all.bind({
    'click': function(e){
      if($(this).data('status') == 'close'){
        window.location = '#'+$(this).attr('id');
      }
    }
  });
 
  //slide up all message blocks
  $('.mod-wrapper:not(.box) .wrap').hide();
  $('.mod-wrapper:not(.box) h2.msg_title').toggleClass('collapsed');
  $('.mod-wrapper.archive .module').hide();
  $('.mod-wrapper.archive h3').toggleClass('collapsed');

  //set h3 onclick handler
  $('h3').click(function() {
    var active_h = $(this);
    $(this).toggleClass("collapsed");
    $(this).parent().find('.module, .mod-container-wrapper').slideToggle(200);
    if(!$(this).hasClass('collapsed')){
      setTimeout(function(){
        $(active_h).closest('.content-wrapper').scrollTo($(active_h), 600, {offset: {top:-10}});
      }, 250);
    }
    return false;
  });
  $('.mod-wrapper h2').click(function() {
    var wrap = $(this).parent().find('.wrap');
    var active_h = $(this);
    if($(wrap).is(':hidden')){
      //alert('test'+$(this).text());
      setTimeout(function(){
        $(active_h).closest('.content-wrapper').scrollTo($(active_h), 600, {offset: {top:-10}});
      }, 250);
    }
    $(this).toggleClass("collapsed");
    $(wrap).slideToggle(200);
    return false;
  });

  //# Read more links
  
  //Expand read-more-content if someone clicks on read-more-link
  $('.read-more-link').click(function(){
    var read_more_content = $(this).parent().parent().find('.read-more-content');

    if(read_more_content.is(':hidden')){
      var msg_title = $(this).parent().parent().find('h4');
      $(this).closest('.content-wrapper').scrollTo(msg_title, 600, {offset: {top:-10}});
    }
    read_more_content.slideToggle();

    var txt = $(this).text() == 'Lees meer' ? 'Sluiten' : 'Lees meer';
    $(this).text(txt);
    return false;

  });
});
