// overwrite jQuery's fading functions

jQuery.fn.fadeIn = function(speed, callback) {
  return this.animate({opacity: 'show'}, speed, function(){
    if (jQuery.browser.msie)
      this.style.removeAttribute('filter');
    if (jQuery.isFunction(callback))
      callback();
  });
};

jQuery.fn.fadeOut = function(speed, callback) {
  return this.animate({opacity: 'hide'}, speed, function(){
    if (jQuery.browser.msie)
      this.style.removeAttribute('filter');
    if (jQuery.isFunction(callback))
      callback();
  });
};

jQuery.fn.fadeTo = function(speed,to,callback) {
  return this.animate({opacity: to}, speed, function(){
    if (to == 1 && jQuery.browser.msie)
      this.style.removeAttribute('filter');
    if (jQuery.isFunction(callback))
      callback();
  });
};

// bfs namespaced functions

var bfs = window.bfs || {
  log: function(m){
    if ($('#messages').length == 0) {
      $(document.body).append('<div id="messages"><ul></ul></div><a id="messages_toggle"><img src="img/clear.gif" width="26" height="26" alt="(x)" /></a>');
      $('#messages').hide();
      $('#messages_toggle').click(function(e){
         e.preventDefault();
         $('#messages').toggle('fast');
      });
      $('#messages').show('fast');
    }
    $('#messages ul').append('<li>'+m+'</li>');
  },
  fixfoot: function(){
    var hh = $(window).height();
    var bh = $('body').height();
    var fh = $('#footer').outerHeight();
    if (bh > hh) {
      $('#footer').css({'position': 'relative'});
      $('body').height('auto');
      $('#body2').css({'margin-bottom': '20px'});
    } else {
      $('#footer').css({'position': 'fixed'});
      $('body').height(hh);
      $('#body2').css({'margin-bottom': (fh+20)+'px'});
    }
  },
  thisPanel: 0,
  panels: function(){
    if (document.getElementById('panels')) {
      $('#dots a').click(function(e){
        e.preventDefault();
        bfs.jump(parseInt(this.href));
      });
      $('.backward').click(function(e){
        e.preventDefault();
        if (bfs.thisPanel == 0) return;
        bfs.jump(bfs.thisPanel-1);
      });
      $('.forward').click(function(e){
        e.preventDefault();
        if (bfs.thisPanel == 5) return;
        bfs.jump(bfs.thisPanel+1);
      });
      $('#panels').css({position: 'relative', top: 0, left: 0, overflow: 'hidden'});
      $('.panel').css({position: 'absolute', top: 0, left: 0}).hide();
      $($('.panel')[bfs.thisPanel]).show();
      $('a[href="#'+bfs.thisPanel+'"]').addClass('current');
      $('.backward').addClass('disabled');
    }
  },
  jump: function(i){
    var p = $('.panel');
    $('a[href="#'+bfs.thisPanel+'"]').removeClass('current');
    $('a[href="#'+i+'"]').addClass('current');
    $(p[bfs.thisPanel]).css({zIndex:0}).fadeOut('slow');
    $(p[i]).css({zIndex:1}).fadeIn('slow');
    bfs.thisPanel = i;
    if (i == 0) {
      $('.backward').addClass('disabled');
      $('.forward').removeClass('disabled');
    } else if (i == 5) {
      $('.forward').addClass('disabled');
      $('.backward').removeClass('disabled');
    } else {
      $('.forward').removeClass('disabled');
      $('.backward').removeClass('disabled');
    }
  },
  formCheck: function(){
    var e = $('*[name^="your"]');
    if ($(e[0]).val().length > 0 && $(e[1]).val().length > 0 && $(e[2]).val().length > 0) {
      $('#send').fadeTo('fast', 1.0);
      $('#send').attr('disabled', false);
    } else {
      $('#send').fadeTo('fast', 0.5);
      $('#send').attr('disabled', true);
    }
  }
};

$(function(){
  $('#messages').hide();
  $('#messages_toggle').click(function(e){
     e.preventDefault();
     $('#messages').toggle('fast');
  });
  bfs.panels();
  $('*[name^="your"]').keyup(bfs.formCheck).keyup();
});

$(window).load(function(){
  $('#messages_toggle').click();
  setTimeout(bfs.fixfoot, 10);
});

$(window).wresize(function(){
  setTimeout(bfs.fixfoot, 10);
});
