//
//  QuickSearchBox
//  LightStorage - Simple MP3 search engine
//  (P) 2010
//  Pou Le Serg
//

var AutoRequestBoxID = 'AutoRequestBox';                                                // ID of div to place results
var InputOnProcessID = 'getmp3';                                                        // ID of input
var AboveThisChoice = '';                                                               // current choice by keys
var MaxSearchCount = 9;                                                                 // results count from server [0..COUNT]
var ServerResult = '';                                                                  // results are stored here
var IsBoxOpened = false;                                                                // is search box opened
var TCount = 0;                                                                         // init of count of received results

// --- keys route

function MouseOverSearchBox () {
  if (AboveThisChoice !== '') {
    RemoveClass ('CS' + AboveThisChoice, 'AnderChoice');
  }
}

function MarkCurrentThisChoice () {
  ReceiveDataFromServer ();
  if ((AboveThisChoice !== '') && (IsBoxOpened)) {
    AddClass ('CS' + AboveThisChoice, 'AnderChoice');
  }
}

function ProcessDocumentKeyUp (evt) {
  evt = (evt) ? evt : window.event;
  if (evt) {
    var KCode = GetKeyCode (evt);
    switch (KCode) {
      case 38:                          // UP
        if (AboveThisChoice !== '') {
          AboveThisChoice --;
          if (AboveThisChoice < 0) AboveThisChoice = '';
        }
        MarkCurrentThisChoice ();
      break;
      case 40:                          // DOWN
        if (AboveThisChoice !== '') {
          AboveThisChoice ++;
          if (AboveThisChoice > MaxSearchCount) AboveThisChoice = MaxSearchCount;
        } else if (MaxSearchCount >= 0) {                                                           // UPD
          AboveThisChoice = 0;
        }
        MarkCurrentThisChoice ();
      break;
      case 27:                          // ESC
        HideSearchBox ();
      break;
      case 13:                          // SUBMIT
        if (IsBoxOpened) {
          PutThisIntoForm (ServerResult [AboveThisChoice]);
        }
      break;
    }
  }
}

function GetKeyCode (evt) {
  return ((evt.charCode) ? evt.charCode : evt.keyCode);
}

// --- main

function PutThisIntoForm (InName) {
  if (InName != undefined) {
    $ (InputOnProcessID).value = InName;
    $ (InputOnProcessID).focus ();
  }
  HideSearchBox ();
}

function HighlightSearchRequest (InSStr) {
  SearchReq = trim ($ (InputOnProcessID).value);
  return InSStr.replace (new RegExp (SearchReq, 'gi'), "<span class=\"HighlightSearchStr\">" + SearchReq + "</span>");
}

function ReceiveDataFromServer (WeGotIt) {
  if (WeGotIt != undefined) {
    ServerResult = eval (WeGotIt);
    AboveThisChoice = '';
    TCount = ServerResult.length;
    MaxSearchCount = TCount - 1;
  }
  IsBoxOpened = false;
  ChangeSBoxStyle ('none');
  if (TCount != 0) {
    $ (AutoRequestBoxID).innerHTML = '';
    for (ic = 0; ic < TCount; ic++) {
      $ (AutoRequestBoxID).innerHTML += '<div id="CS' + ic + '" class="selector" onclick="PutThisIntoForm (ServerResult [' + ic + ']);">' + HighlightSearchRequest (ServerResult [ic]) + '</div>';
    }
    LeftX = getXYOffset ($ (InputOnProcessID)) ['x'];
    TopY = getXYOffset ($ (InputOnProcessID)) ['y'];
    WidthX = RealValue (InputOnProcessID, 'width');
    HeightY = RealValue (InputOnProcessID, 'height');
    ModifierY = 7;
    ModifierX = 6;
    ModifySBoxCoords (LeftX, TopY + HeightY + ModifierY, WidthX + ModifierX);
    ShowSearchBox ();
    return true;
  } else {
    return false;
  }
}

function CheckInputData (evt) {
  SearchString = $ (InputOnProcessID).value;
  if (SearchString.length > 2) {
    // block control keys (UP, DOWN, ENTER, ESC)
    evt = (evt) ? evt : window.event;
    if (evt) {
      var KCode = GetKeyCode (evt);
      if ((KCode == 38) || (KCode == 40) || (KCode == 13) || (KCode == 27)) return false;
    }
    RequestString = encodeURIComponent (trim (SearchString));
    TransferAJAXData ("qsearch.exec.php?us=" + RequestString, "ReceiveDataFromServer");
  } else {
    ReceiveDataFromServer ('[]');
    HideSearchBox ();
  }
}

// ---

function TestOpenedChoiceForm () {
  return !IsBoxOpened;
}

function ShowSearchBox () {
  ChangeSBoxStyle ('block');
  IsBoxOpened = true;
}

function HideSearchBox () {
  // if time is too small - doesn`t works "PutThisIntoForm ()"
  IsBoxOpened = false;
  setTimeout ("ChangeSBoxStyle ('none');", 250);
}

function ModifySBoxCoords (psx, psy, psw) {
  $ (AutoRequestBoxID).style.left = psx + 'px';
  $ (AutoRequestBoxID).style.top = psy + 'px';
  $ (AutoRequestBoxID).style.width = psw + 'px';
}

function ChangeSBoxStyle (WhatsNew) {
  $ (AutoRequestBoxID).style.display = WhatsNew;
}

function SetupSearchBox () {
  if ($ (InputOnProcessID) == null) return 0;
  $ (InputOnProcessID).onkeyup = CheckInputData;
  $ (InputOnProcessID).onblur = HideSearchBox;
  $ (AutoRequestBoxID).onmouseover = MouseOverSearchBox;
  document.onkeyup = ProcessDocumentKeyUp;
}

window.onload = function() {
  PutFocusIntoForm ();
  SetupSearchBox ();
}

//
//  DByte64
//
