$(function(){
  // Document is ready
  
  $('.tool-tip').tooltip();
  
  // zone three button insert
  if ($('#zone-three-holder').length == 1) {
    $.post('/zone-three-check/', {}, function(data) {
      $('#zone-three-holder').replaceWith(data);
    });
  }
  
  // if there is a news div and there is only one (which there should never be more than one, but hey)
  if ($('#news').length == 1) {
    
    // find div's for tabs
    var tabs = $('#news div.tab');
    
    // add a tabs div before the first tab
    $(tabs[0]).before("<div id=\"tabs\"></div>");
    
    // grab the tab div we just inserted
    var tab_div = $('#tabs');
    
    // find the headers and create a tab from each then hide them
    tabs.find('h2').each(function() {
      var tab_id = $(this).parents('div.tab')[0].id;
      tab_div.append('<a href="#'+tab_id+'" class="'+tab_id+'">'+$(this).text()+'</a>');
    }).hide();
    
    // grab all the anchors
    var as = tab_div.find('a');
    
    // make the tab links show/hide the tabs as needed
    as.click(function(e) {
      var tab_id = this.href.split('#')[1];
      tabs.hide();
      $('#'+tab_id).show();
      
      as.removeClass('selected');
      $(this).addClass('selected');
      
      // if it's the first tab, then show the roundy corner
      if (tab_id == 'news-room') {
        $('#roundy-corner-for-tabs').show();
      } else {
        $('#roundy-corner-for-tabs').hide();      
      } 
      // if it's the last tab, then show the end corner
	    if (tab_id == 'events') {
        $('#end-corner-for-tabs').show();
      } else {
        $('#end-corner-for-tabs').hide();      
      } 
	  
      return false; // stop propigation
    });
    
    // hide all but the first tab
    tabs.hide();
    $(tabs[0]).show();
    $(as[0]).addClass('selected');
    $('#roundy-corner-for-tabs').show();
	  $('#end-corner-for-tabs').hide();
  }
  
  // for event attending and such
  $('#attend-button').click(function(e) {
    
    var submit_ok = true;
    var form_data = {};
    
    if ($('.custom_required').length > 0) {
      $('.custom_required').each(function() {
        if ($(this).val() == '') {
          submit_ok = false;
        } else {
          form_data[$(this).attr('name')] = $(this).val(); // serialize the custom fields into an object
        }
      });
    }
    
    if (!submit_ok) {
      alert('You must fill in all required info before attending.');
      return;
    }
    
    $(this).attr('disabled', true);
    
    $.post('/company/events/' + $('input[name=event_id]').val() + '/add/', form_data, function(data) {
      $('#attend-button').parents('p.attend-button').replaceWith(data);
    });
    
  });
  
  $('#unsubscribe-button').click(function(e) {
    
    $(this).attr('disabled', true);
    
    $.post('/company/events/' + $('input[name=event_id]').val() + '/remove/', {}, function(data) {
      $('#unsubscribe-button').parents('p.unsubscribe-button').replaceWith(data);
    });
    
  });
  
  $('#international').hover(
    // over
    function() {
      $('#fake-search').show();
      $('#search').hide();
      $('#international').addClass('hover');
    },
    // out
    function() {
      $('#fake-search').hide();
      $('#search').show();
      $('#international').removeClass('hover');
    }
  );
  
  // Date stuff
  
  // These helpful arrays and such are borrowed from the awesome Matt Kruse (javascripttoolbox.com)
  
  // Utility function to append a 0 to single-digit numbers
  Date.LZ = function(x) {return(x<0||x>9?"":"0")+x};
  // Full month names. Change this for local month names
  Date.monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  // Month abbreviations. Change this for local month names
  Date.monthAbbreviations = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  // Full day names. Change this for local month names
  Date.dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  // Day abbreviations. Change this for local month names
  Date.dayAbbreviations = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  // If the getFullYear() method is not defined, create it
  if (!Date.prototype.getFullYear) { 
  	Date.prototype.getFullYear = function() { var yy=this.getYear(); return (yy<1900?yy+1900:yy); } ;
  }
  
  // By Nathan Herald, Nov 2008
  Date.prototype.format_wimba = function() {
    var output = '';
    
    var y=this.getFullYear();
    
    var M=this.getMonth()+1;
    var d=this.getDate();
    var H=this.getHours();
    var m=this.getMinutes();
    var h;
    var a;
    
    if (H == 0) {
      h = 12;
    } else if (H > 12) {
      h = H - 12;
    } else {
      h = H;
    }
    
    if (H > 11) { 
      a = "pm"; 
    } else { 
      a = "am"; 
    }
    
    // for time zones
    var time_string = this.toString(); // [... (EST)]
    var parenth = time_string.lastIndexOf('('); // ... [(]EST)
    var time_zone = time_string.substr(parenth+1, 3); // ... ([EST])
    
    // in IE, the datetimes look different
    if ($.browser.msie) {
      time_zone = time_string.substr(time_string.length-8, 3); // ... [EST] 2008
    }
    
    // do a regex to see if it's three uppercase letters
    // if it's not then look for three words in parenthasees and grab the first letter of each of the three words
    // else just don't show anything
    
    if (! time_zone.match(/[A-Z]{3}/)) {
      // blank it out, cause it's not right
      time_zone = '';
      
      // look for the words
      var words = this.toString().match(/\(([A-Za-z ]*)\)/);
      var words = words[1].split(' ');
      
      if (words.length == 3) {
        // if it's three words, then just save the first letter of each
        time_zone = words[0].substring(0, 1) + words[1].substring(0, 1) + words[2].substring(0, 1);
      }
    }
    
    // January 7, 2008 6:05 am EST
    output += Date.monthNames[M-1]; // Month - January
    output += ' ';
    output += d; // day - 7
    output += ', ';
    output += y; // year - 2008
    output += ' ';
    output += h; // hour - 6 (not military time)
    output += ':';
    output += Date.LZ(m); // minute - 05
    output += ' ';
    output += a; // am
    output += ' ';
    output += time_zone;
    
    return output;
  }
  
  // jQuery plugin
  $.fn.extend({
    make_date_local: function() {
      return this.each(function() {
        var $this = $(this);
        
        var gmt_time = $this.attr('rel');
        var new_time = Date.parse(gmt_time);
        
        // if it's a valid time
        if (new_time) {
          var this_time = new Date(new_time);
          $this.text(this_time.format_wimba()); // replace the element's contents with the new time
        }
        
      }); // end of return
    } // end of make_date_local
  }); // end of fn.extend
  
  // do it
  $('span.time').make_date_local();
  
});



