var yuiDom = YAHOO.util.Dom;
var yuiE   = YAHOO.util.Event;
var yuiC   = YAHOO.util.Connect;

//###################################################################

var App = {
  
  session : null,
  run     : false,
  
  //#################################################################
  //## 
    
  //#################################################################
  
  init : function() {
    App.initSessionLinks();
    $( '#btnSearch' ).click( App.searchRequest );
    
    //*
    //###############################################################
    // AutoComplete authors
    
    var dsAuthors = new YAHOO.widget.DS_JSArray( App.authors );
    
    var acAuthors = new YAHOO.widget.AutoComplete( 'authorsSearch', 'authorsContainer', dsAuthors );
    acAuthors.delimChar = "";
    acAuthors.queryDelay = 0;
    acAuthors.maxResultsDisplayed = 20;

    acAuthors.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults) { 
      var pos = YAHOO.util.Dom.getXY(oTextbox); 
      pos[1] += YAHOO.util.Dom.get(oTextbox).offsetHeight; 
      YAHOO.util.Dom.setXY(oContainer,pos); 
      return true; 
    };
    
    acAuthors.itemSelectEvent.subscribe( 
      function( event, args ) {
        App.sorWybrany = args[2];
      }
    );
    
    //###############################################################
    // AutoComplete countries
    
    var dsCountries = new YAHOO.widget.DS_JSArray( App.countries );
    
    var acCountries = new YAHOO.widget.AutoComplete( 'countriesSearch', 'countriesContainer', dsCountries );
    acCountries.delimChar = "";
    acCountries.queryDelay = 0;
    acCountries.maxResultsDisplayed = 20;

    acCountries.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults) { 
      var pos = YAHOO.util.Dom.getXY(oTextbox); 
      pos[1] += YAHOO.util.Dom.get(oTextbox).offsetHeight; 
      YAHOO.util.Dom.setXY(oContainer,pos); 
      return true; 
    };
    
    acCountries.itemSelectEvent.subscribe( 
      function( event, args ) {
        App.sorWybrany = args[2];
      }
    );
    
    //#############################################################*/
    
  },
  
  //#################################################################
  
  initSessionLinks : function() {
    $( 'div.session a' ).click( App.getPresentationList );
    return true;
  },
  
  //#################################################################
  
  /**
   * Initiates XHR request to get presentations list from the server.
   * Scope - clicked <a> element 
   * 
   * @param {Object} e Click event
   */
  
  getPresentationList : function( e ) {
    var sessionId = this.getAttribute( 'session_id' );
    
    var url = 'presentations/ajax.handler.php?cmd=sessionlist&sessionid=' + sessionId;
    
    var callback = {
      success: App.showPresentationList,
      failure: function() { 
        alert( "Request failure!" );
        App.session = null;                 //-- clear stored session
        $( '#presentation-title' ).html();  //-- clear session title on page
      },
      scope  : App,
      timeout: 5000
    };
    
    var con = yuiC.asyncRequest( 'GET', url, callback );
    
    //--- store chosen session link element
    
    App.session = {
      linkElement : this,
      title       : $( this ).html(),
      id          : sessionId
    }
    
    return false;
  }, 
  
  //#################################################################
  
  /**
   * Handle XHR response - show presentation list.
   * 
   * @param {Object} o Response object.   
   */
  
  showPresentationList : function( o ) {
    $( '#presentations-list' ).empty().html( o.responseText ).show();
    
    if ('' != App.session.title)
      $( '#presentation-title' ).html( App.session.title );
  },
  
  //#################################################################
  
  searchRequest : function( e ) {
    var author  = $( '#authorsSearch' )[0].value; 
    var country = $( '#countriesSearch' )[0].value; 
    var word    = $( '#wordsSearch' )[0].value; 
    
    if ('' != author)  author  = '&author=' + encodeURI( author );
    if ('' != country) country = '&country=' + encodeURI( country );
    if ('' != word)    word    = '&word=' + encodeURI( word );
    
    
    var url = 'presentations/ajax.handler.php?cmd=search' + author + country + word;
    
    var callback = {
      success: App.searchHandleResults,
      failure: function() { 
        alert( "Request failure!" );
        App.session = null;                 //-- clear stored session
        $( '#presentation-title' ).html();  //-- clear session title on page
      },
      scope  : App,
      timeout: 5000
    };
    
    var con = yuiC.asyncRequest( 'GET', url, callback );
    
    //--- store chosen session link element
    
    App.session = null;
    
    return false;
    
  },
  
  //#################################################################
  
  searchHandleResults : function( o ) {
    $( '#presentations-list' ).empty().html( o.responseText ).show();
    
    $( '#presentation-title' ).html( "Search results" );    
  }
  
  //#################################################################
  
};

//###################################################################
//###################################################################


yuiE.addListener( window, "load", App.init );

