// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function qeelementloading(element) {
  $(element).innerHTML='<img alt="Wait" src="/images/shared/loading.gif" class="tab-loading" />';
}

function qeloaded() {
  $('dialog-loading').setStyle({visibility:'hidden'});
  qedialog();
}

function qedialogoff() {
  $('veil').setStyle({visibility:'hidden'});
  $('dialog').setStyle({visibility:'hidden'});
}

function qeloading() {
  $('veil').setStyle({display:'block'});
  $('veil').setStyle({visibility:'visible'});
  $('dialog-loading').setStyle({display:'block'});
  $('dialog-loading').setStyle({visibility:'visible'});
  // Should be in CSS but Windows IE7 bug would then stop the animation
  $('dialog-loading').setStyle({background: "#f7f7f7 url(/images/shared/loading.gif) center 40px no-repeat"});
  qecenterdialog('dialog-loading');
}

function qedialog() {
  $('veil').setStyle({display:'block'});
  $('veil').setStyle({visibility:'visible'});
  $('dialog').setStyle({display:'block'});
  $('dialog').setStyle({visibility:'visible'});
  qecenterdialog('dialog');
}

function qecenterdialog(id){
  var viewportDims = document.viewport.getDimensions();
  var viewportOffs = document.viewport.getScrollOffsets();
  var horiz_middle = viewportDims.width / 2;
  var vert_middle = viewportDims.height / 2;
  var scrollFromTop = viewportOffs.top;
  var docHeight = getDocHeight();

  width = $(id).getWidth();
  height = $(id).getHeight();
  left_pos = horiz_middle - (parseInt(width) / 2);
  top_pos = Math.max(vert_middle + scrollFromTop - (parseInt(height) / 2), 0);
  $('veil').setStyle({top: scrollFromTop + 'px'});
  $('veil').setStyle({height: docHeight + 'px'});
  $(id).setStyle({top: top_pos + 'px'});
  $(id).setStyle({left: left_pos + 'px'});
}

function getDocHeight() {
  var D = document;
  return Math.max(
    Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
    Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
    Math.max(D.body.clientHeight, D.documentElement.clientHeight)
  );
}

function qeHideAdvancedSearch() {
  $j('#qeAdvancedSearchBox').hide('slow');
  $j('#qeActionBox').show('slow');
  $j('.right_column').show();
  $j('#qeSearchBox').show('slow');
}

function qeShowAdvancedSearch() {
  $j('#qeSearchBox').hide('slow');
  $j('#qeActionBox').hide('slow');
  $j('#qeAdvancedSearchBox').show('slow');
  $j('#content').addClass('sub_menu_expand')
}

function qeDockBoxMenu(id) {
  areaId = $j(id)
  areaId.find('.sortDockClose').hide();
  areaId.find('.sortDockOpen').show();
  areaId.find('.itemSorter').removeClass('undocked');
  areaId.find('.itemSorter').addClass('docked');
}

function qeUndockBoxMenu(id) {
  areaId = $j(id)
  areaId.find('.sortDockOpen').hide();
  areaId.find('.sortDockClose').show();
  areaId.find('.itemSorter').addClass('undocked');
  areaId.find('.itemSorter').removeClass('docked');
}

function qeSingleOverlapCheck(comment_top) {
  var last_nutshell = $j('.nutshell:last');
  var nutshell_bottom = last_nutshell.height() + last_nutshell.offset().top;
  var space = comment_top - nutshell_bottom;
  if(space < 6){
    last_nutshell.remove();
  };
  return space;
}
 
function qeCheckOverlaps() {
  var comment_top = $j('.comment_body:first').offset().top;
  var space = 0;
  if(comment_top != 0) {
    while((space = qeSingleOverlapCheck(comment_top)) < 6);
  };
  if (space > 30 || comment_top == 0) {
    try{ qeAddToHistory(); }
    catch(err){}
  };
}

function qeShowHistoryItems(){
  var bottom = $j('.comment_body:first').offset().top;
  var top = $j('.history').height() + $j('.history').offset().top;
  var items = $j('#history_extend .nutshell');

  if (bottom == 0) {
    bottom = $j('#content').offset().top + $j('#content').height() - 40;
  }
 
  var space = bottom - top;

  items.each(function (i){
    this.hide();
  });
  $j('#history_extend').show();
  items.each(function (i){
    if(space - $j(this).outerHeight() > 30) {
      $j(this).show().click(function() {
        window.location = $j(this).attr('url');
        $j(this).prependTo('.history');
        $j("*").css("cursor", "progress");
      });
      space = space - $j(this).outerHeight();
    } else {
      return;
    };
  });
}

function slowShow(id){
  $j(id).show(150);
}

function qeScrollTo(element) {
  $j('html, body').animate({
    scrollTop: $j(element).offset().top
  }, 1000);
}

function qeFlasher(id){
  $j(id).effect("highlight", {}, 5000);
}

function hideSubMenus(){
  jQuery('.sub_menu').hide();
}

function showSubMenu(id) {
  hideSubMenus();
  $(id).show();
}

function qeDockContextMenu(){
  var menu = $j('#extra_menu');

  // $j('#content').addClass('sub_menu_expand');
  menu.appendTo(jQuery('#outsideBox'));
  $j('#dock_close').hide();
  $j('#dock_open').show('slow', function(){
    $j('#headerDockLink').show();
  });
}

function qeUndockContextMenu(){
  var menu = $j('#extra_menu');

  menu.appendTo(jQuery('#insideBox'));
  $j('#dock_open').hide();
  $j('#dock_close').show('slow', function(){
    $j('#headerDockLink').hide();
  });
}

function qeSortEntries(id, klass, subclass, sortDesc) {
  var list = jQuery(id);
  var listitems = list.children(klass).get();

  listitems.sort(function(a, b) {
    var compA = jQuery(a).find(subclass).text().toUpperCase();
    var compB = jQuery(b).find(subclass).text().toUpperCase();
    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
  })
  if(sortDesc) listitems.reverse();
  jQuery.each(listitems, function(idx, itm) { list.append(itm); });
}

var qeToolTipsInitialized = false; 

function qeHideToolTips() {
  $j('#qeMenuShowToolTips').show();
  $j('#qeMenuHideToolTips').hide();
  $j('.tipBM').qtip("hide");
  $j('.tipBR').qtip("hide");
  $j('.tipBL').qtip("hide");
  $j('.tipTM').qtip("hide");
  $j('.tipML').qtip("hide");
  $j('.tipMR').qtip("hide");
}
function qeShowToolTips() {
  $j('.tipBM').qtip("show");
  $j('.tipBR').qtip("show");
  $j('.tipBL').qtip("show");
  $j('.tipTM').qtip("show");
  $j('.tipML').qtip("show");
  $j('.tipMR').qtip("show");
}

function qeInitializeToolTips() {
  $j('#qeMenuShowToolTips').hide();
  $j('#qeMenuHideToolTips').show();
  if (qeToolTipsInitialized) {
    qeShowToolTips();
    return;
  }
  qeCreateToolTipStyles();
  qeToolTipsInitialized = $j('.tipBM').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeBottomMiddle' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' } }
  });
  $j('.tipBR').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeBottomRight' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'bottomRight', tooltip: 'topLeft' } }
  });
  $j('.tipBL').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeBottomLeft' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'bottomLeft', tooltip: 'topRight' } }
  });
  $j('.tipTM').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeTopMiddle' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } }
  });
  $j('.tipML').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeMiddleLeft' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'leftMiddle', tooltip: 'rightMiddle' } }
  });
  $j('.tipMR').qtip({
     tip: true,
     show: { ready: true },
     style: { name: 'qeMiddleRight' },
     // show: 'mouseover',
     hide: 'mouseout',
     position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } }
  });
}

function qeCreateToolTipStyles(){
  $j.fn.qtip.styles.qeBottomMiddle = { 
    width: 108,
    padding: 2,
    background: '#444',
    color: '#eee',
    textAlign: 'center',
    border: {
      width: 1,
      radius: 1,
      color: '#ffbe00'
    },
    tip: 'topMiddle',
    name: 'dark' // Inherit the rest of the attributes
  };
  $j.fn.qtip.styles.qeBottomRight = { 
    tip: 'topLeft',
    name: 'qeBottomMiddle' // Inherit the rest of the attributes
  };
  $j.fn.qtip.styles.qeBottomLeft = { 
    tip: 'topRight',
    name: 'qeBottomMiddle' // Inherit the rest of the attributes
  };
  $j.fn.qtip.styles.qeTopMiddle = { 
    tip: 'bottomMiddle',
    name: 'qeBottomMiddle' // Inherit the rest of the attributes
  };
  $j.fn.qtip.styles.qeMiddleLeft = { 
    width: 70,
    tip: 'rightMiddle',
    name: 'qeBottomMiddle' // Inherit the rest of the attributes
  };
  $j.fn.qtip.styles.qeMiddleRight = { 
    width: 70,
    tip: 'leftMiddle',
    name: 'qeBottomMiddle' // Inherit the rest of the attributes
  };
}

$j = jQuery.noConflict();
$j(document).ready(function() {
  $j(".history_item").click(function() {
    window.location = $j(this).attr('url');
    $j(this).prependTo('.history');
    $j("*").css("cursor", "progress");
  });
});

$(function() {
  $("a.rjs").click( function() {
    $.ajax({
        url: this.href,
        dataType: "script",
        beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
    });
    return false;
  });
  // requires jQuery.form plugin
  $("form.rjs").ajaxForm({
    dataType: 'script',
    beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
    resetForm: true
  });
});
