jQuery(document).ready(function($){

  // home page
  $('section#latest-news article .date').corner('round bottom 5px');
  $('#home-top #home-tabs ul').tabs('#home-top #home-panes > .pane', 
    {rotate: true, effect: 'fade', fadeOutSpeed: 100}).slideshow(
    {autoplay: true, clickable: false, interval: 5800});


  // fix menu to occupy full width. uses css padding-left / right
  var width = 0;
  $('nav ul li').each(function(i, dom) {
    width += $(dom).width();
  });
  if(width < 900) {
    var pad = $('nav li:last-child a').css('padding-left');
    pad = pad.substr(0, pad.length-2) * 1;
    var w = 900 - width;
    w = Math.floor(w/2);
    w = pad + w;
    $('nav li:last-child a').css({'padding-left': w+'px', 'padding-right' : w+'px'});
    $('nav li:last-child').css({position:'absolute', right: '0px'}); // mainly for ff
  }
  
  
  // scrollable behaviour 
  $('.scrollable').scrollable();
  

  // use title as value when entering data to newsletter - makes form compact
  $('#newsletter :input[type=text][title]').each(function() {
    var $this = $(this);
    if($this.val() === '') {
      $this.val($this.attr('title'));
    }
    $this.focus(function() {
      if($this.val() === $this.attr('title')) {
        $this.val('');
      }
    });
    $this.blur(function() {
      if($this.val() === '') {
        $this.val($this.attr('title'));
      }
    });
  });

  // check newsletter form and submit using ajax / check errors
  $('#newsletter form').submit(function() {
    $('#newsletter #errordesc').hide();        
    var oks = true;          
    $('#newsletter :input[type=text][title]').each(function() {
      $this = $(this);
      if($this.val() == '' || $this.val() == $this.attr('title')) {
        $('#newsletter #errordesc').show();
        oks = false;
      }
    });
    if(oks) { // send it
      var f = $('#newsletter form');
      $.post(f.attr('action'), f.serialize(), function(data) {
        $('#newsletter #submitokdesc').show();
      }, 'html');
    }
    return false; // always submit via ajax
  });


});
