(function($)
{
  var _popupTracker = {}
  var _popupCounter = 0;
  $.fn.openPopupWindow = function(options)
  {
    var defaults = {
      height: 600,
      width: 600,
      toolbar: 0,
      scrollbars: 1,
      status: 0,
      resizable: 1,
      left: 0,
      top: 0,
      center: 0,
      createnew: 0,
      location: 0,
      menubar: 0 
    };

    var options = $.extend(defaults, options);

    var obj = this;

    // center the window
    if (options.center == 1)
    {
      options.top = (screen.height - (options.height + 110)) / 2;
      options.left = (screen.width - options.width) / 2;
    }

    var parameters = "location=" + options.location +
    ",menubar=" + options.menubar +
    ",height=" + options.height +
    ",width=" + options.width +
    ",toolbar=" + options.toolbar +
    ",scrollbars=" + options.scrollbars +
    ",status=" + options.status +
    ",resizable=" + options.resizable +
    ",left=" + options.left +
    ",screenX=" + options.left +
    ",top=" + options.top +
    ",screenY=" + options.top;

    // target url
    var target = obj.attr("href");       

    // test if popup window is already open, if it is, just give it fokus.        
    var popup = _popupTracker[target];
    if (options.createnew == 0 && popup !== undefined && !popup.closed)
    {            
      popup.focus();
    } 
    else
    {
      var name = "PopupWindow" + _popupCounter;
      _popupCounter++;

      // open window
      popup = window.open(target, name, parameters);            
      _popupTracker[target] = popup;
      _popupTracker[target].focus();
    }

    return false;
  };        
})(jQuery);


$(document).ready(function () {
  
  // console.log("test")
  
  // initialize tabs
  $("div#sample tr.title_for_tab_content").hide();
  $("div#sample h4").addClass("active_tabs");
  $("ul#sample_tabs").show();
  $("div.tab_content").hide();
  $("div.tab_content:first").show();
  $("ul#sample_tabs li:first").addClass("active")
  
  // initialize date drop downs
  $("ul#date_list").addClass("active")
  $("ul#date_list li").addClass("closedItem")

  // initialize date and calendar
  $("div#calendar").show()
  $("h5.title").show()
  $("h5.degraded_title").hide()
  
  $("a.email").click(function () {
    $(this).openPopupWindow({
      height: 550,
      width: 670,
      center: 1,
      createnew: 1
    });
    return false
  });
  $("a.email_inline").click(function () {
    $(this).openPopupWindow({
      height: 550,
      width: 670,
      center: 1,
      createnew: 1
    });
    return false
  });

  $("a.noArrow").click(function () {
    $(this).openPopupWindow({
      height: 768,
      width: 1024,
      center: 1,
      createnew: 1
    });
    return false
  });

  // tabs
  $("ul#sample_tabs li a").click(function() {
    
    tabToLoad = $(this).attr("href").split('#')[1]

    $("ul#sample_tabs li").removeClass("active")
    $(this).parent().addClass("active")

    $("div.tab_content").hide();

    if (tabToLoad == "all_tabs") {
      $("div.tab_content").show();
      $("div.tab_content").addClass("all_tabs_showing")
      $("div#sample tr.title_for_tab_content").show();
      $("div#sample tr.title_for_tab_content").addClass("ie_bg_fix");
    } else {
      $("div.tab_content").removeClass("all_tabs_showing")
      $("div#" + tabToLoad).show()
      $("div#sample tr.title_for_tab_content").hide()
      $("div#sample tr.title_for_tab_content").removeClass("ie_bg_fix");
    }
    return false
  });

  // date drop downs
  $("ul#date_list li").toggle(function() {
    $(this).removeClass("closedItem")
    $(this).addClass("openItem")
  }, function() {
    $(this).removeClass("openItem")
    $(this).addClass("closedItem")
  });


  // get next event, open that drop down.

  var currentDate = new Date()
  jQuery.each($("li.important_date"), function(i, element) {
     var date = $(this).attr("title")
     // element.elementDate = new Date()
      
     if (new Date(date) < currentDate) {
       $(element).removeClass("futureEvent")
       $(element).addClass("pastEvent")
     //keep date function coloring working for nested hyperlinks
	   $('li.a') .addClass("pastEvent");
     } else {  
       
       var dateWriter = new Date(date);
  
       // had to make an array of months because the Date() obj only returns the months number
       var month=new Array(12);
       month[0]="January";
       month[1]="February";
       month[2]="March";
       month[3]="April";
       month[4]="May";
       month[5]="June";
       month[6]="July";
       month[7]="August";
       month[8]="September";
       month[9]="October";
       month[10]="November";
       month[11]="December";
  
       $("h6#month_name").text(month[dateWriter.getMonth()])
       $("div#date_content span").text(dateWriter.getDate())
  
       $(element).next().show()
       $(element).removeClass("closedItem")
       $(element).addClass("openItem")
       var calendar_p = $(element).children("p").html()
       $("p#details").html(calendar_p)
       
       return false
     }

     // hide the calendar if it's after May 13
     
     var nowDate = new Date()
     var end_date = new Date('May 13, 2010')
     if (nowDate > end_date) {
       $("div#calendar").hide()
       $("h5.title").hide()
       $("h5.degraded_title").show()
     }

   });
   
 });