"use strict";
function openDashboard() {
  $('#thedarkness .ajax_loading').fadeOut('fast');
  var target_width = $('#dashboard-content').width();
  var target_height = $('#dashboard-content').height();

  $('#dashboard-content').css({'width': '0px', 'height':'10px', 'overflow':'hidden'});
  $('#dashboard-content').show();

    $('#dashboard-content').animate({'width': target_width+'px'}, 'fast', function () {
      $('#dashboard-content').animate({'height': target_height+'px'}, 'fast', function () {
          $('#dashboard-content').css({'overflow':'auto', 'height':''});
      });
    });

    $('#main .reveal').hide();
}

window.cached_dashboard = false;

function loadDashboard(e) {
  try {
    e.preventDefault();
  } catch (err){}
  var target_url = $(this).attr('href');

  $('#thedarkness .ajax_loading').show();
  $("#thedarkness").fadeIn('fast');

  if (!cached_dashboard) {
      $.get(target_url, function(data) {
        $("#dashboard-content").html(data);
        openDashboard();
      });
      cached_dashboard = true;
  } else {
      openDashboard();
  }
  return false;
};

function closeDashboard() {
  $('#dashboard-content').css({'overflow':'hidden'});

  var target_width = $('#dashboard-content').width();
  var target_height = $('#dashboard-content').height();

  $('#dashboard-content').animate({'height': '10px'}, 'fast', function () {
     $('#dashboard-content').animate({'width': '0px'}, 'fast', function () {
        $('#thedarkness').fadeOut('fast');
        $('#dashboard-content').hide();
        $('#dashboard-content').css({'width': '','height': '', 'overflow': ''});
     });
  });

  // show hidden content
  $('#main .reveal').show();
}

function expandNewsReports() {
  var newssection = $(this).parent();
  $(newssection).toggleClass('expanded');
  var reports = $('.reports', newssection);
  reports.stop(true, true);
  reports.toggleClass('expanded', 200);
  return false;
}

function hoverExapndNews() {
    var newssection = $(this).parent();
    var reports = $(this);
    if (!newssection.hasClass('expanded')) {
        if (reports.hasClass('expanded')) {
            reports.stop(true, true);
            reports.toggleClass('expanded', 200);
        } else {
            setTimeout(function () {
                if (reports.is(':hover')) {
                    reports.stop(true, true);
                    reports.toggleClass('expanded');
                }
            }, 500)
        }
    }
}

function dashboardFailure(e, xhr, settings, error) {
  $('#ajaxError .errormsg').html(xhr.status + ' ' + xhr.statusText);
  $('#ajaxError').dialog({
    close: function() { $('#thedarkness').fadeOut('fast'); }
  });
}

$(function () {
  $('body').ajaxError(function(event, request, settings){
        if (request && request.status==403 && request.responseText){
            window.location = request.responseText; 
        }
      return false;
    });

  $("#dashboard-link a").click(loadDashboard);
  // Causes more trouble than it actually solves...
  //$("#dashboard-link a").ajaxError(dashboardFailure);
  $("#thedarkness").click(closeDashboard);
  $("#dashboard .close").live('click', closeDashboard);
  $("a.showall").live('click', expandNewsReports);

  if (!$.browser.msie) {
      $(".reports").live('hover', hoverExapndNews);
  }
});

