var speed=250;
var dspeed=speed*2;
var faster=speed+100;
var suggestion=-1;
var suggestions=0;
function setaffectdel()
{
 $("div[type=affect]").unbind("click").click(function() {
  $(this).remove();
 });
}
function getsuggestions(like,element,table,order,type)
{
 var field = element;
 var suggestboxhtml="<div style='display: none; position: absolute; z-index: 9999;' class='suggestions' id='suggestions'></div>";
 suggestboxhtml+="<div style='text-align: left; display: none; position: absolute; z-index: 9998;' class='suggestions-bg' id='suggestions-bg'>&nbsp;</div>";
 $("body").append(suggestboxhtml);
 $.ajax({url: "suggestions.cgi?like="+like+"&field="+element+"&table="+table+"&order="+order, cache: false, dataType: "xml", complete: function(data) {
  var suggestxml = $.xmlToJSON(data.responseXML);
  var suggesthtml="";
  if ( suggestxml.suggestions[0].Text == 0 ) { $("#suggestions").css({display: "none"}); $("#suggestions-bg").css({display: "none"}); }
  else {
   var j = 0;
   for(var i = 0; i < suggestxml.suggestion.length; i++) {
    if ( suggestxml.suggestion[i].Text != "deleted" ) {
     suggesthtml+="<div suggestion='"+j+"' type='clickfill' val='"+suggestxml.suggestion[i].Text+"' suggestionid='"+suggestxml.suggestion[i]._ID+"'>"+suggestxml.suggestion[i].Text+"</div>";
     j++;
    }
    suggestions = j-1;
   }
   if(type == 'affect') { element = 'affectedbox'; }
   $("#suggestions").html(suggesthtml).css({top: eval(eval(7 + $("#"+element).height()) + eval($("#"+element).offset().top))+"px", left: eval(2 + $("#"+element).offset().left)+"px", display: "block", width: $("#"+element).width()+"px"});
   $("#suggestions-bg").css({width: $("#suggestions").width()+"px", height: $("#suggestions").height()+"px", top: $("#suggestions").offset().top+"px", left: $("#suggestions").offset().left+"px", display: "block", opacity: "0.9"});
   $("[type=clickfill]").each(function() {
    if(type == 'affect') {
     var affectid=$(this).attr("suggestionid");
     var affect=$(this).attr("val");
     $(this).mousedown(function() {
      $("#affected").append("<div type='affect' affect='"+affectid+"' class='affect' title='Remove'>"+affect+"</div>");
      setaffectdel();
      setTimeout(function() { $("#"+element).val("").focus().select(); }, 100);
     });
    }
    else {
     var value=$(this).attr("val");
     $(this).mousedown(function() {
      $("#"+element).val(value);
     });
    }
   });
  }
 }});
}
function checksev(sev)
{
  if(sev == "0") {
   $("#closedfield").attr("disabled","true").val("");
  }
  else {
   $("#closedfield").removeAttr("disabled");
  }
}
$(document).ready(function() {
 $("#severity").change(function() { checksev($(this).val()); });
 checksev($("#severity").val());
 $("#affected").css({height: eval($("#affectedtd").height()-50)+"px"});
 $("input[name=random]").attr("name",Math.random());
 $("[dropdown=true]").each(function() {
  $("#"+$(this).attr("id")).keyup(function(e) {
   e.preventDefault();
   var element = $(this).attr("id");
   if(e.which == '38') {
    if(suggestion == -1) { suggestion = suggestions; }
    else { suggestion--; }
    if(suggestion == -1) {
     $("#editincidentform").unbind("submit");
    }
    else {
     $("#editincidentform").unbind("submit").submit(function(e) {
      $("#"+element).val($("div[type=clickfill][suggestion="+suggestion+"]").attr("val"));
      suggestion = -1;
      e.preventDefault();
      return 0;
     });
    }
    $("div[type=clickfill]").css({color: "#ddd", backgroundColor: "transparent"});
    $("div[type=clickfill][suggestion="+suggestion+"]").css({color: "#fff", backgroundColor: "#000"});
   }
   else if(e.which == '40') {
    if(suggestion == suggestions) {
     suggestion=-1;
     $("#editincidentform").unbind("submit");
    }
    else {
     suggestion++;
     $("#editincidentform").unbind("submit").submit(function(e) {
      $("#"+element).val($("div[type=clickfill][suggestion="+suggestion+"]").attr("val"));
      suggestion = -1;
      e.preventDefault();
      return 0;
     });
    }
    $("div[type=clickfill]").css({color: "#ddd", backgroundColor: "transparent"});
    $("div[type=clickfill][suggestion="+suggestion+"]").css({color: "#fff", backgroundColor: "#000"});    
   }
   else if (e.which == '27') {
    $("#suggestions").hide();
    $("#suggestions-bg").hide();
   }
   else {
    suggestion = -1;
    getsuggestions($(this).val(),$(this).attr("id"),$(this).attr("table"),$(this).attr("id")); e.preventDefault();
   }
  }).focus(function(e) {
   $(this).select(); getsuggestions($(this).val(),$(this).attr("id"),$(this).attr("table"),$(this).attr("id")); e.preventDefault();
  }).blur(function() {
   $("#suggestions").hide(); $("#suggestions-bg").hide();
  });
 });
 $("#affectedbox").keyup(function(e) { getsuggestions($(this).val(),'affect',$(this).attr("table"),'type asc, affect asc','affect'); }).focus(function(e) { getsuggestions($(this).val(),'affect',$(this).attr("table"),'type asc, affect asc','affect'); }).blur(function(e) { $("#suggestions").hide(); $("#suggestions-bg").hide(); });
 if ( search != "" ) {
  $("div[searchable=true]").highlight(search);
 }
 if ( $("#[focusme=true]").length == 0 ) { $("#search").focus().select(); }
 else { $("#[focusme=true]").focus().select(); }
 $("#showlogin-form").ajaxForm();
 $("#showlogin").click(function() { $("#search-form").hide(); $("#login").show(); $("#username").focus().select(); $(this).hide(0); });
 $("#hidelogin").click(function() { $("#showlogin").show(); $("#search-form").show(); $("#login").hide(); $("#search").focus().select(); });
 $("#itype").change(function() {
  if ( $(this).val() == "Info" ) {
   $("#started").val("");
   $("#ETA").val("");
   $("#nextupdate").val("");
  }
 });
 $("#hidebanner").click(function() {
  $.ajax({url: "hidebanner.cgi", complete: function() { $("#thewholebanner").hide(500); $("#showbannercont").show(); }});
 });
 $("#showbanner").click(function() {
  $.ajax({url: "showbanner.cgi", complete: function() { $("#thewholebanner").show(500); $("#showbannercont").hide(); }});
 });
 $("#admin-link").css({padding: eval($("#topmenu").height()-$("#admin-link").height())/2+"px"}).mouseover(function() {
  $("#admin-links").show().css({top: eval($("#topmenu").height()-1)+"px", left: $("#admin-link").offset().left+"px"});;
 }).mouseout(function() { $("#admin-links").hide(); });
 $().scroll(function() {
  if ( $().scrollTop() == 0 ) { $("#scrolltotop").hide(); }
  else { $("#scrolltotop").show(); }
 });
 $("#scrolltotop").click(function(event) {
  $("html,body").animate({scrollTop: "0px"}, eval($().scrollTop()/2));
  event.preventDefault();
 });
 $("[type=logentry]").click(function() {
  $(this).hide();
  $("#full-log-"+$(this).attr("entry")).show();
 });
 $("[type=full-logentry]").click(function() {
  $(this).hide();
  $("#log-"+$(this).attr("entry")).show();
 });
 if(toincident != '') {
  $("html,body").animate({scrollTop: $("#inc-"+toincident).offset().top+"px"}, $("#inc-"+toincident).offset().top/2, function() {
   $("div[type=more-info][infoid="+toincident+"]").show(faster).attr("showing","true");
  });
 }
 $("div[type=infotitle]").click(function() {
  var infoid=$(this).attr("infoid");
  var showing=$("div[type=more-info][infoid="+infoid+"]").attr("showing");
  if(showing == 'false') {
   $("div[type=more-info][infoid="+infoid+"]").show(faster).attr("showing","true");
  }
  else {
   $("div[type=more-info][infoid="+infoid+"]").hide(faster).attr("showing","false");
  }
 });
 $("a[href=showall]").click(function(e) {
  e.preventDefault();
  $("div.statusdiv").show(speed).attr("showing","true");
 });
 $("a[href=hideall]").click(function(e) {
  e.preventDefault();
  $("div.statusdiv").hide(speed).attr("showing","false");
 });
 $("a[href=hideallok]").click(function(e) {
  e.preventDefault();
  $("div.statusdiv[status=green]").hide(speed).attr("showing","false");
  $("div.statusdiv[status!=green]").show(speed).attr("showing","true");
 });
 $("div.statusheader").click(function() {
  var itype=$(this).attr("itype");
  if($("div.statusdiv[itype="+itype+"]").attr("showing") == "false") {
   $("div.statusdiv[itype="+itype+"]").attr("showing","true");
   $("div.statusdiv[itype="+itype+"]").show(speed);
  }
  else {
   $("div.statusdiv[itype="+itype+"]").attr("showing","false");
   $("div.statusdiv[itype="+itype+"]").hide(speed);
  }
 });
 $("#orderby-select").change(function() {
  var val=$(this).val();
  if(val == 'updated' || val == 'started' || val == 'closed') {
   $("#order-select").val("desc").hide();
  }
  else {
   $("#order-select").show();
  }
 });
});

