var viewFilter = {
  init: function(){
    var viewSelector = $('div.filters select');
    viewSelector.bind('change', function(){
      var link = $(this).attr('value');
      $('#filter').submit();
    });
    var viewSelector = $('div.filters .radio');
    viewSelector.bind('click', function(){
      var link = $(this).find('input').attr('value');
      $('#filter').submit();
    });
  }
}

var fontControls = {
  init: function(){
    var blocks = $('.primary-column, .secondary-column, .main-column, .tertiary-column');

    $('#serviceblock .font1').click(function() {
      blocks.css( {
        'fontSize' : '100%',
        'lineHeight': '100%'
      });
      setCoockie("fontsize", 100);
    });
    $('#serviceblock .font2').click(function() {
      blocks.css( {
        'fontSize' : '105%',
        'lineHeight': '105%'
      });
      setCoockie("fontsize", 105);
    });
    $('#serviceblock .font3').click(function() {
      blocks.css( {
        'fontSize' : '110%',
        'lineHeight': '110%'
      });
      setCoockie("fontsize", 110);
    });
    function setCoockie(c_name, value) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate() + 7);
      document.cookie = c_name + "=" + escape(value)
          + ((7 == null) ? "" : ";expires=" + exdate.toGMTString());
    }

    function getCookie(c_name) {
      if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
          c_start = c_start + c_name.length + 1;
          c_end = document.cookie.indexOf(";", c_start);
          if (c_end == -1)
            c_end = document.cookie.length;
          return unescape(document.cookie.substring(c_start, c_end));
        }
      }
      return "";
    }

    fontsizer = getCookie("fontsize") == "" ? 100 : getCookie("fontsize");
    blocks.css( {
      'fontSize' : fontsizer + "%"
    });
    
  }
}

var hover = {
  init: function(){
    
    $('.hover li').each(function(){
      var item = $(this);

      if(item.find('a[class*=location]').length > 0){
        item.attr('clickable', true);
        
        item.bind('mouseover', function(){
          item.addClass('hover');
        }).bind('mouseout', function(){
          item.removeClass('hover');
        }).bind('click', function(){
          if(item.attr('clickable') == 'true'){
            var link = item.find('a[class*=location]').attr('href');
            var target = item.find('a[class*=location]').attr('target');
            if(target == '_blank'){
              window.open(item.find('a[class*=location]').attr('href'));
              return false;
            } else {
              window.location = item.find('a[class*=location]').attr('href');
            }
          }
        });
      }
    });
  
    $('.hover li a.sub-link').each(function(){
      var item = $(this);
      item.mouseover(function(){
        item.parent('li').attr('clickable', false);
      }).mouseout(function(){
        item.parent('li').attr('clickable', true);
      });
    });
  }
}

var clearInput = {
  init: function(){
    $('#newsletter-field, #search-field').focus(function() {
      var inputVal = $(this).val();
      if(inputVal == 'E-mailadres' || inputVal == 'Zoekterm:'){
        $(this).val('');
      }
    }).blur(function(){
        if ($(this).val() == ''){
          $(this).val($(this)[0].defaultValue);
        }
    });
    
    $('form').submit(function() {
      var inputField = $(this).find('#newsletter-field, #search-field').each(function(){
        var inputVal = $(this).val();
        if(inputVal == 'E-mailadres' || inputVal == 'Zoekterm:'){
          $(this).val('');
        }
      });
    });
  }
}

var autocomplete = {
  init: function(){
    $('input.search').autocomplete('../zoek/ajax');
  }
}

var tabs = {
  init: function(){
    $('.agenda-tab').hide();
    $('.agenda-button').addClass('inactive');
    $('.news-button').bind('click', function(){
      $('.news-button').removeClass('inactive');
      $('.agenda-button').addClass('inactive');
      $('.agenda-tab').hide();
      $('.news-tab').show();
    });
    $('.agenda-button').bind('click', function(){
      $('.agenda-button').removeClass('inactive');
      $('.news-button').addClass('inactive');
      $('.news-tab').hide();
      $('.agenda-tab').show();
    });
  } 
}

var slideshow = {
  slide: null,
  prev: null,
  next: null,
  slides: 0,
  timer: 7000,
  transition: 1000,
  animating: false,
  numberOfSlides: 0,
  currentSlide: 0,
  interval: null,
  defaultLeft: 0,

  init: function(){
  
    slideshow.slide = $('#carousel-holder');
    slideshow.prev = $('#carousel-prev');
    slideshow.next = $('#carousel-next');
    slideshow.slides = $('#carousel-holder li');
    slideshow.numberOfSlides = slideshow.slides.size();
    
    slideshow.defaultLeft = 0 - (slideshow.numberOfSlides * 225) + 125;
    
    if(slideshow.numberOfSlides < 3){
      slideshow.prev.remove();
      slideshow.next.remove();
      $('#carousel-overlay').remove();
      return false;
    }
    
    slideshow.cloneList();
    
    slideshow.prev.click(function(){
      if(!slideshow.animating) slideshow.prevSlide();      
    });
    
    slideshow.next.click(function(){
      if(!slideshow.animating) slideshow.nextSlide();
    });
    
    slideshow.setInterval();
  },
  
  setInterval: function(){
    if(slideshow.interval) clearInterval(slideshow.interval);
    
    slideshow.interval = setInterval(function(){
      slideshow.nextSlide();
    }, slideshow.timer + slideshow.transition);
  },
  
  cloneList: function(){
    slideshow.slides.clone().prependTo(slideshow.slide);
    slideshow.slide.css({
      'left': slideshow.defaultLeft
    });
    slideshow.slides = $('#carousel-holder li');
  },
  
  nextSlide: function(){
    slideshow.setInterval();
    slideshow.animateSlides(-1);
  },
  
  prevSlide: function(){ 
    slideshow.setInterval(); 
    slideshow.animateSlides(1);
  },
  
  animateSlides: function(amount){
    slideshow.animating = true;
    slideshow.slide.animate({
      'left': slideshow.defaultLeft + (amount * 225)
    }, {
      duration: slideshow.transition,
      complete: function(){
        if(amount < 0){
          slideshow.putFirstAsLast();
        }else{
          slideshow.putLastInFront();
        }
        slideshow.animating = false;
      }
    });
  },
  
  putLastInFront: function(){
    slideshow.slides.filter(':last').prependTo(slideshow.slide);
    slideshow.slides = $('#carousel-holder li');
    slideshow.slide.css({
      'left': slideshow.defaultLeft
    });
  },
  
  putFirstAsLast: function(){
    slideshow.slides.filter(':first').appendTo(slideshow.slide);
    slideshow.slides = $('#carousel-holder li');
    slideshow.slide.css({
      'left': slideshow.defaultLeft
    });
  }
}

var tooltip = {
  init: function(){
    $('#carousel-holder li a').each(function(){
      $(this).qtip({
        content: $(this).text(),
        position: {
          corner: {
            target: 'bottomMiddle',
            tooltip: 'topLeft'
          }
        }
      });
    });
  }
}

$(document).ready(function(){
  viewFilter.init();
  fontControls.init();
  hover.init();
  clearInput.init();
  autocomplete.init();
  tabs.init();
  slideshow.init();
  tooltip.init();
  $('a[rel*=external]').attr('target', '_blank');
});

