$(document).ready(function() {
  pageController = new pageController();

  //pageController.prepareNavLinks();
  pageController.prepareSiteSearch();
  pageController.prepareWidgetNavLinks();
  //pageController.activateScrollableDIV();

});

String.prototype.formatHeader = function() {
  var pieces = this.toLowerCase().split(' ');
  return pieces.join('_');
}

/**
 * This is a page controller that will allow us to work with page elements,
 * including AJAX calls, much easier.
 */
function pageController() {

  var $this = $(this);

  var siteSearch    = {};

  // Navigation elements.
  var topLinks      = $("#innerWrapper table.topNav ul li a, #innerWrapper table.lowerNav ul li a");
  var sideLinks     = $("#sideNav table.sideMenus a");
  var widgetNav     = $("#sideNav ul#widgetNav li a");
  var widgetContent = $("#sideNav div.inner_widget");


  // Page elements.
  siteSearch.element = $("#innerWrapper table.topNav input[name=siteSearch]");
  siteSearch.value   = siteSearch.element.val();
  this.mainContent    = $("#mainContent");


  // Methods to prepare element behaviors
  this.prepareNavLinks = function() {
    // Prepare top links first.
    topLinks.click(function() {
      $this = $(this);

      $this.addClass('selected');

      // Make sure the selected class isn't active on any of the other elements.
      topLinks.not($this).removeClass('selected');
      // Make sure none of the side links are active.
      sideLinks.removeClass('active');

      return false;
    });

    // Prepare side links
    sideLinks.click(function() {
      $this = $(this);

      $this.addClass('selected');

      // Make sure none of the other side links are active.
      sideLinks.not($this).removeClass('selected');
      // Make sure none of the top links are active.
      topLinks.removeClass('selected');

      return false;
    });

    // Prepare widget links
    widgetNav.click(function() {
      $this = $(this);

      $this.addClass('selected');

      // Make sure none of the other widget links are active.
      widgetNav.not($this).removeClass('selected');

      return false;
    });
  };

  this.prepareSiteSearch = function() {
    siteSearch.element.focus(function() {
      this.value = '';
    }).blur(function() {
      this.value = siteSearch.value;
    });
  };

  this.prepareWidgetNavLinks = function() {
    widgetNav.click(function() {
      $(this).addClass('selected');
      widgetNav.not(this).removeClass('selected');

      var content = $(this).text().formatHeader();

      //widgetContent.load(base_url+'ajax/changeWidgetNavTo/'+content);

      $.ajax({
        url: base_url+'ajax/changeWidgetNavTo/'+content,
        beforeSend: function() {
          widgetContent.html('<img src="'+base_url+'/images/ajax-wait.gif" alt=""/>');
        },
        success: function(response) {
          widgetContent.html(response);
        },
        dataType: 'html'
      });

      return false;
    });
  };

  this.activateScrollableDIV = function() {
    var mainFlashGraphic = $("#mainFlashGraphic");
    if (mainFlashGraphic.length > 0) {
      $("div.scrollable").scrollable({
        easing: 'easeInOutSine',
        size: 1,
        clickable: false
      }).bind('click', function(event) {
        //event.preventDefault();
      }).circular().autoscroll({
        interval: 10000
      });

      //var api = $("div.scrollable").scrollable();
      //alert(api.length);

    }
  };

}
