/*--------------------------------------------------------------------------*/
/*  Western North Carolina GPS Mapper                                       *
 *  (c) 2008 WNCOutdoors.info                                               *
 *  All rights reserved.                                                    *
/*--------------------------------------------------------------------------*/

var throttlePeriod = 1000;
var lastSentListQuery = '';

function hasValue(v){return(typeof(v)!='undefined'&&typeof(v)!==undefined&&v!=null&&v!='')?true:false;}

function toggleToolbox(contents){
  $('tbstatus').update();
  $('toolbox').show();
  var url = hostServer + 'ui.php';
  var pars = 'display=' + contents;
  var myAjax = new Ajax.Updater('tbcontent', url, {method: 'get', parameters: pars, evalScripts: true});
}

function showResultLoop(){
  if( typeof( $('wtfSearch') ) == 'undefined' || $('wtfSearch') == null ){ return; }
  if( $('wtfSearch').value != lastSentListQuery && 
      $('wtfSearch').value != ''){
    // Send the requst 
    showResult($('wtfSearch').value);
    lastSentListQuery = $('wtfSearch').value;
  }
  setTimeout('showResultLoop();',throttlePeriod);
}

function showResult(searchText){
  var submittedFrom = (top.location.href.match("map.html") ? 'map' : 'search'); 
  var URL = hostServer + '/search.php';
  var pars = 'searchText=' + searchText;
  $('searchResults').update();
  var myAjax = new Ajax.Request(URL,{
    method: 'get',
    parameters: pars,
    onSuccess: function(transport){
     var fallsArr = transport.responseText.evalJSON(true);
     if(typeof(fallsArr['error']) != 'undefined'){
       $('searchResults').update(fallsArr['error']);
     } else {
       if(submittedFrom == 'map'){
         href = 
         $('searchResults').insert(mapResultFormat(fallsArr));
       } else {
         $('searchResults').insert(searchResultFormat(fallsArr));
       }
       
     }
    }
  });
}

function searchResultFormat(fallsArr){
  var resultPara = '';
  for (var i=0; i<fallsArr.length; i++){
    var wfall = fallsArr[i];
    var urlName = wfall.name.replace(/\s/g,'_');
    var wfallUrl = "waterfall/" +
       wfall.id + '/' + urlName;
    resultPara += '<p><a href="' + wfallUrl + '">' + wfall.name +
      '</a>';
    if(wfall.height != null){
      resultPara += ' - ' + wfall.height;
    }
    if(wfall.stream != null){
      resultPara += ' (' + wfall.stream + ')';
    }
    if(wfall.landowner != null){
      resultPara += '<br>' + wfall.landowner;
    }
    resultPara += '</p>';
  }
  return resultPara;
}

function mapResultFormat(fallsArr){
  var resultPara = '';
  for (var i=0; i<fallsArr.length; i++){
    var wfall = fallsArr[i];
    resultPara += '<p><a href="#" onClick="myclick(\'' + wfall.id + '\')">' + wfall.name +
      '</a>';
    if(wfall.height != null){
      resultPara += ' - ' + wfall.height;
    }
    if(wfall.stream != null){
      resultPara += ' (' + wfall.stream + ')';
    }
    if(wfall.landowner != null){
      resultPara += '<br>' + wfall.landowner;
    }
    resultPara += '</p>';
  }
  return resultPara;
}

