﻿

var tx = null;          //input text
var rp = null;          //search panel
var tmr = null;         //id timer
var tmr1 = null;        //id timer
var fmain = null;
var curr = -1;          //indice selzionato
var arrLI = null;


jQuery(function () {

  tx = document.getElementById("searchtext");
  rp = document.getElementById("searchpanel");

  jQuery(tx).click(function (e) { e.stopPropagation(); e.stopImmediatePropagation(); });
  rp.onclick = function (event) {
    var e = fmw.fixE(event);
    var h = e.srcElement.getAttribute("h");
    if (h == null) {
      var li = fmw.getParent(e.srcElement, "tagName", "LI");
      if (li) {
        h = li.getAttribute("h");
      }
    }
    if (h) {
      window.location.href = h;
    }
    event.stopImmediatePropagation();
    event.stopPropagation();
  }

  //  rp.onmouseover = function () {
  //                    var riga = fmw.getParent(e.srcElement, "tagName", "TR");
  //                  }
  //  rp.onmouseout = function () {
  //                    var riga = fmw.getParent(e.srcElement, "tagName", "TR");
  //                  }
  tx.onkeydown = function (event) {
    var e = fmw.fixE(event);
    switch (e.keyCode) {
      case 9:
        return false;
        //case 13:          
        //    searchValue(null);          
        //    return false;          
        //    break;          
      case 27:
        closeSearch();
        return false;
      case 40:
        selectItem(1);
        return false;
      case 38:
        selectItem(-1);
        return false;
    }
  }
  tx.onkeyup = function (event) {
    var e = fmw.fixE(event);
    switch (e.keyCode) {
      case 9:
      case 27:
      case 40:
      case 38:
        return false;
      case 13:
        searchValue(null);
        return false;
    }
    startSearch();
  }

  //    tx.onblur = function() {
  //        tmr1 = window.setTimeout(hideSearch, 300);
  //    }

  tx.onfocus = function () {
    if (tx.value != "") {
      openSearch();
    }
  }

  fmain = fmw.get("form1");
  //fmain.action = "ricerca.aspx?op=ra";
  //fmain.onsubmit = function() { return searchValue(null) };
  fmain.onsubmit = function () { return false };

  jQuery(document).click(function () {
    hideSearch();
  });
});

function searchValue(typesearch) {

    /* Se è stato selezionato un elemento, con l'invio faccio partire quello */
    if (curr != -1) {
        var h = arrLI[curr].getAttribute("h");
        if (h) {
            window.location.href = h;
            return;
        }            
    }            
    
    var values = [];
    switch (typesearch) {
        case "adv":
            values.push(encodeURIComponent('t') + '=' + encodeURIComponent(fmw.get("t").value));
            values.push(encodeURIComponent('p') + '=' + encodeURIComponent(fmw.get("p").value));
            values.push(encodeURIComponent('c') + '=' + encodeURIComponent(fmw.get("c").value));
            values.push(encodeURIComponent('rxp') + '=' + encodeURIComponent(fmw.get("rxp").value));
            //s += "&t=" + t.value + "&p=" + p.value + "&c=" + c.value + "&rxp=" + rxp.value;
            //s += "&" + values.join("&");
            break;
        default:
            /* la ricerca deve essere fatta per marca o titolo */
            values.push(encodeURIComponent('t') + '=' + encodeURIComponent(tx.value));
            values.push(encodeURIComponent('mt') + '=' + encodeURIComponent('1'));
            //s += "&t=" + tx.value + "&mt=1";
            //s += "&" + values.join("&");
            break;            
    }
    window.location.href = "ricerca.aspx?sez=RICERCA&op=ra&" + values.join("&");
    return false;                    
}

function startSearch() {

  if (tmr) {
    window.clearTimeout(tmr);
  }
  if (tx.value != '') {
    tmr = window.setTimeout(sendToServer, 100)
  }
  else {
    closeSearch();
  }
}

function indexList() {
    curr = -1;
    //arrLI = fmw.getNodesByAttributeValue("class", "searchpanel", rp. );
    arrLI = fmw.getNodesByAttributeValue("class", "searchpanel", rp.firstChild);
}

function openSearch() {

  rp.style.display = "block";
  rp.style.zIndex = 10000;
  
}

function selectItem(pos) {
                
    if (curr == -1) {
        curr = 0;
    }
    else {
        fmw.removeClass(arrLI[curr], "searchpanel_selected");    
        curr = curr + pos;
    }
    if (curr == arrLI.length) {
        curr = 0;
    }
    if (curr < 0) {
        curr = arrLI.length - 1;
    }

//    for (var i = 0; i < arrLI.length; i++) {
//        fmw.removeClass(arrLI[i], "searchpanel_selected");
//    }
    fmw.addClass(arrLI[curr], "searchpanel_selected");       
}

function hideSearch() {
  if (tmr1) {
    window.clearTimeout(tmr1);
  }
  rp.style.display = "none";
}

function closeSearch() {

  if (tmr) {
    window.clearTimeout(tmr);
  }
  //rp.innerHTML = "";
  hideSearch();
}

function sendToServer() {

  if (tmr) {
    window.clearTimeout(tmr);
  }
    
  if (tx.value != "") {    
  
//    var ricat = document.getElementsByName("riccat");
//    var nu;
//    for (var i=0; i<ricat.length; i++) {
//      if (ricat[i].checked === true) {
//        nu = ricat[i].value;
//        break;
//      }    
//    }
  
    var hr = fmw.HttpRequest();
    var action = "ricerca2.aspx?search=1&searchtext=" + encodeURIComponent(tx.value);
    hr.open("GET", action, true);
    hr.onreadystatechange = function() {
        if (hr.readyState == 4) {
            switch (hr.status) {
                case 200:
                    if (tx.value != "") {
                        rp.innerHTML = hr.responseText;
                        indexList();
                        openSearch();
                    }
                    break;
            }
        }
    }
    hr.send(null); 
  }
  else {
    hideSearch();
  }
}

/* salvo il tipo di ricerca che vuole effettuare l'utente sui nuovi/usati */
function aggiornaCriteri(el) {

  var hr = fmw.HttpRequest();
  var action = "ricerca.aspx?search=2&nu=" + el.value;
  hr.open("GET", action, true);
  hr.onreadystatechange = function() {
    if (hr.readyState == 4) {
      switch (hr.status) {
      case 200:
        window.location.reload(true);
      }
    }
  }
  hr.send(null);
}
