var InterimPartners = {
  flashDisplay: function(contents, width, height) {
    $('#flash_container').empty();
    $('#flash_container').prepend('<div class="notice">' + contents + '</div>');
    $('#flash_container').dialog({ 
      modal: true,
      width: width, 
      height: height
    });
  },
  flashNotice: function(notice) {
    $('#flash_container').empty();
    $('#flash_container').prepend('<div class="notice">' + notice + '</div>');
    $('#flash_container').dialog({
      modal: true,
      buttons: { 
        'Continue': function() {
          $(this).empty();
					$(this).dialog('close');
				}
		  }
    });
  },
  flashError: function(error) {
    $('#flash_container').empty();
    $('#flash_container').prepend('<div class="error">' + error + '</div>');
    $('#flash_container').dialog({
      modal: true,
      buttons: { 
        'Continue': function() {
          $(this).empty();
					$(this).dialog('close');
				}
		  }
    });
  }, 
  flashConfirmation: function(message, onConfirmation) {
    $('#flash_container').empty();
    $('#flash_container').prepend('<div class="notice">' + message + '</div>');
    $('#flash_container').dialog({
      modal: true, 
      buttons: {
        'Continue': function() {
          $(this).empty();
          $(this).dialog('close');
          onConfirmation();
        },
        'Cancel': function() {
          $(this).empty();
          $(this).dialog('close');
        }
      }
    });
  }, 
  loadPartial: function(url, onComplete) {
    $.get(url, onComplete); 
  }, 
  setupControls: function() {
    $('button, input:submit').button();
    $('.accordion').accordion({
      autoHeight: false, 
      collapsible: true,
      active: false, 
      icons: { 'header': 'ui-icon-triangle-1-s', 'headerSelected': 'ui-icon-triangle-1-n' }
    });
    $('.tooltip').tipsy({fade: true, gravity: 's', html: true});
    
    $('a.login').live('click', function() {
      $('#flash_container').empty();
      $('#flash_container').append($('#inline_login').clone().show());
      $('#flash_container').dialog({
        modal: true
      });
      return false;
    });
    
    $('a.back').live('click', function() {
      history.go(-1);
      return false;
    });
    
    $('a.expander').live('click', function() {
      var element = $(this).attr('href');
      $(element).fadeIn();
    });
    
    $('a.contractor').live('click', function() {
      var element = $(this).attr('href');
      $(element).fadeOut();
    });
    
    InterimPartners.loadPartial('/partials/login', function(data) {
      $('#login_panel').empty();
      $('#login_panel').append(data);
    });
    
    if (window.location.href.search('for-candidates') == -1 && window.location.href.search('for-clients') == -1) {
      $('a#set_home_page').remove();
      $('a#reset_home_page').remove();
    }
  },
  setupFooterTabs: function() {
    // set up the tabs in the footer
    $('#quick_links a').click(function() {
      InterimPartners.showFooterTab('#quick_links');
      return false;
    });
    $('#contacts a').click(function() {
      InterimPartners.showFooterTab('#contacts');
      return false;
    });
    $('#brightpool a').click(function() {
      InterimPartners.showFooterTab('#brightpool');
      return false;
    });
    setInterval("InterimPartners.setTime()", 1000);
  }, 
  setupShortlists: function() {
    // set up shortlist buttons
    $('.shortlist_buttons a.remove').click(function() {
      $.ajax({
        url: $(this).attr('href'), 
        type: 'POST', // not all browsers support type=delete so we pass it as a form parameter
        data: '_method=delete', 
        success: function() {
          window.location.reload();
        }
      });
      return false;
    });
    $('.shortlist_buttons a.add').click(function() {
      $.ajax({
        url: $(this).attr('href'), 
        type: 'POST', 
        data: 'rails=needsdata', 
        success: function() {
          window.location.reload();
        }
      });
      return false;
    });
    $('#clear_list_button').click(function() {
      $.ajax({
        url: $(this).attr('href'), 
        type: 'POST', 
        data: 'rails=needsdata', 
        success: function() {
          window.location.reload();
        }
      });
      return false;
    });
  },
  showFooterTab: function(tabName) {
    $('#tabs li.current').removeClass('current');
    $('#footer .footer_panel').hide();
    $(tabName + '_panel').show();
    $(tabName).addClass('current');
  }, 
  cycleContents: function(selector) {
    $(selector).cycle( { fx: 'fade', cleartype: true, cleartypeNoBg: true, timeout: 10000 } );
  },
  fourDigits: function(number) {
    return (number < 1000) ? number + 1900 : number;
  }, 
  setTime: function() {
    var curtime = new Date();
    var curhour = curtime.getHours();
    var curmin = curtime.getMinutes();
    var cursec = curtime.getSeconds();
    var time = "";
    var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  
    var date = ((curtime.getDate()<10) ? "0" : "")+ curtime.getDate();
    dateString =  days[curtime.getDay()] + ", " +
       months[curtime.getMonth()] + " " +
       date + ", " +
       (InterimPartners.fourDigits(curtime.getYear()));
  
    if(curhour == 0) curhour = 12;
    timeString = (curhour > 12 ? curhour - 12 : curhour) + ":" +
           (curmin < 10 ? "0" : "") + curmin + ":" +
           (cursec < 10 ? "0" : "") + cursec + " " +
           (curhour > 12 ? "PM" : "AM");
  
    $('#time').text(dateString + ' ' + timeString);
  }, 
  hideQuickLinks: function() {
    if (window.location.href.substr(-5, 5) == '.com/') {
      $('#header .links').hide(); 
    }
  }
}

$(function() {
  InterimPartners.hideQuickLinks();
  InterimPartners.setupControls();
  InterimPartners.setupFooterTabs();
  InterimPartners.setupShortlists();
});
