/*
 * WNCOutdoors.info UI

 * This script is free and unencumbered software released into the
 * public domain. It comes without any restrictions on what you may
 * do with it, and with no warranty whatsoever. Assume nothing works,
 * and you may be pleasantly surprised; and when it breaks, you get
 * to keep both pieces.
 * @requires: prototype.js
 * @requires: livePipe UI
*/
( function( WNCO, $, undefined ) {
  
  var SEARCH_THROTTLE_PERIOD = 1000;
  var lastSentQuery = "";
  
  // Public
  WNCO.Ui = {
    newPlainWindow: function( url ) {
      plainwindow = window.open(url,'newWindowName');
    },
    
    newWindow: function( url ) {
      imagewindow = window.open(url,'newWindowName','width=1000,height=700,toolbar=no,location=no,resizable=yes,scrollbars=yes,left=0,top=0')
      imagewindow.focus()
    },
    
    HelpWindow: function( help )   {
      helpwindow = window.open(help,'thiswindow','width=285,height=700,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left=0,top=0')
      helpwindow.focus()
    },
    
    openRatingWindow: function( url ) {
      hikeRideRatingWindow = window.open(url,'name','width=350,height=375,toolbar=no,location=no,resizable=yes,scrollbars=yes,left=0,top=0')
      hikeRideRatingWindow.focus()
    },
    
    mapLyteframe: function( dir ) {
      var standardMap = document.getElementById('standardMap');
      standardMap.style.display = 'None';
      
      var a = document.createElement("a");
      a.href = "http://maps.wncoutdoors.info/map.php?map=" + dir;
      a.rel = "lyteframe";
      a.title = "WNCOutdoors.info Interactive Map Browser";
      a.rev = "width:1000px; height:700px;";
      myLytebox.start( a, false, true);
    },
    
    trailSearchAheadResult: function( r ){
      $('trail-search-txt').removeClassName("search-loading").addClassName("search-waiting");
      $('trail-search-results').update("");
      if( r.responseJSON.success ){
        var res = r.responseJSON.result;
        if( res.length > 0 && $('trail-search-heading').childElements().length == 0 ){
          $('trail-search-heading').insert("<h2>Search Results</h2><p>Click a trail name for more details.</p>");
        } else if( res.length == 0 ){
          $('trail-search-heading').update("");
          $('trail-search-results').update("<h2>No results found.</h2>");
        }
        for( var i=0; i<res.length; i++ ){
          var resLine = new Element( 'p', { "class": "mediumHeading" } );
          var nameParent = resLine;
          if( res[i].trailheadslug != null && res[i].slug != null ){
            resLine.insert( new Element( 
              'a', { "href": [ "", "trails", res[i].trailheadslug, res[i].slug ].join( "/" ) }
            ).insert( res[i].name ) );
          } else {
            resLine.insert( res[i].name );
          }
          resLine.insert( '&nbsp;(' + res[i].trailheadname + ' Trailhead)' );
          $('trail-search-results').insert( resLine );
        }
      } else {
        $( 'trail-search-results' ).update( "<p>Ooops...we had an error. Please try again later.</p>" );
      }
    },
    
    trailSearchAheadRequest: function( value ){
      $( 'trail-search-results' ).update( "<p class='mediumHeading'>Searching for " + value + "...</p>" );
      var req = new Ajax.Request( "/data/trails", {
        method: 'GET',
        parameters: { "name": value, "site": WNCO.cfg.site },
        onSuccess: WNCO.Ui.trailSearchAheadResult,
        onException: function( request, e ) { }
      });
    },
    
    createSearchAheadMonitor: function( searchBox, callback ){
      return function(){
        if( typeof( $( searchBox ) ) != 'undefined' || $( searchBox ) != null ){
          if( $( searchBox ).getValue() != lastSentQuery &&
              $( searchBox ).getValue() != '' &&
              $( searchBox ).getValue().length >= 3 ){
            $('trail-search-txt').removeClassName("search-waiting").addClassName("search-loading");
            callback( $( searchBox ).getValue() );
            lastSentQuery = $( searchBox ).getValue();
          }
        }
        var nextFn = WNCO.Ui.createSearchAheadMonitor( searchBox, callback );
        var delayId = nextFn.delay( 0.5 );
      }
    }
    
  }; //Public
  
}( window.WNCO = window.WNCO || {}, $ ) );

