function searchGallery( )
{
      var albumIdNode = dojo.byId( 'albumId' );
      var serchForNode = dojo.byId( 'searchFor' );
      
      if( albumIdNode.value.length > 0 )
	  self.location = '/galleries/show/'+serchForNode.value+'/album_id='+albumIdNode.value;
      else
	  self.location = '/galleries/show/'+serchForNode.value+'/search_for='+serchForNode.value;
      

}



function autoSuggetionInit()
{
 
if( dojo.byId( 'searchFor' ) )
	
 new autoSuggestion(
    {
      webserviceURL: '/webservices/childrenAlbumsSearcher.php?searchWord=',
      minLength: 3,
      targetObj: {
	  label : dojo.byId( 'searchFor' ),
	  value : dojo.byId( 'albumId' )
	},
      resultRowParser: function( row ) 
      {
	return '<img src="'+row.thumb_path+'" /><span>'+row.label+' ('+row.member_nick+')</span>';
      },
      onChoice : function() { searchGallery(); }
      
    },
     dojo.byId( 'searchFor' )
  );

}


function autoSuggestion ( params,  sourceObj )
{
	

  if( !params.webserviceURL )
    console.error( 'ERROR webserviceURL is  not defined' );
  
  this.defaultResultRowParser = function( row )
  {
    return '<span>'+row.label+'</span>';
  }


  this.resultRowParser = ( params.resultRowParser ) ? params.resultRowParser : _this.defaultResultRowParser ;
  this.onChoice = ( params.onChoice ) ? params.onChoice : function() {} ;

  this.webserviceURL = params.webserviceURL;  
  this.minLength = ( params.minLength ) ? params.minLength : 3;
  this.sourceObj = sourceObj;
  this.searchWord ='';
  this.dropDownObj = null;
  this.isDropDownVisible = false;
  this.dropDownItems = new Array();
  this.selectedDropDownItem = null;
  this.targetObj = new Object();
  this.targetObj.label = params.targetObj.label;
  if( params.targetObj.value )
  this.targetObj.value = params.targetObj.value;
  this.denyBlurEvent = false;
  var _this = this;

  this.navigationController = function( e )
  {
   // console.log( 'navigationController' );
    switch( e.charOrCode ){
        case dojo.keys.UP_ARROW:
	    _this.moveUp();
	    break;
	case dojo.keys.DOWN_ARROW:
	    _this.moveDown();
	    break;
	case dojo.keys.ENTER:
	    _this.saveAndHide();
	    break;
     }
  }

  this.dataController = function() 
  {
   // console.log( 'dataController' );
    if( _this.searchWord.length != _this.sourceObj.value.length )
    {
      
      _this.searchWord = _this.sourceObj.value;
      if( _this.minLength <= _this.searchWord.length )
      {
	_this.getDataAndCreateDropDown( _this.searchWord )  ;
      }
      else
      {
	_this.closeAndClean();
      }
     }
	
   }

   this.getDataAndCreateDropDown = function( searchWord ) 
   {
     // console.log( 'getDataAndCreateDropDown' );
      dojo.xhrGet(
            {
                'url': _this.webserviceURL+searchWord,
                'preventCache': true,
                'handleAs': 'json',
                'timeout': 3000,
                'load': function( data )
                {
		   if( data.length > 0  )
		   {
                    _this.createDropDown( data );
		    _this.showDropDown( );
		   }
		   else
		   {
		    _this.hideDropDown();
		   }
                }

            }
            );
    }

  this.createDropDown = function( data ) 
  {
    //console.log( 'createDropDown' );
    if( _this.dropDownObj == null )
    {
	
      var sourceObjPosition = dojo.coords( _this.sourceObj, true );
      _this.dropDownObj = dojo.doc.createElement( 'div' );
	  dojo.connect( _this.dropDownObj , 'onmouseenter', function(){ _this.denyBlurEvent = true; } );
	  dojo.connect( _this.dropDownObj , 'onmouseleave', function(){ _this.denyBlurEvent = false; } );
      dojo.addClass( _this.dropDownObj, "autoSuggestionDropDownContainer" );
      dojo.attr( _this.dropDownObj,
		 { style:{ top: sourceObjPosition.y+sourceObjPosition.h+'px',
			   left: sourceObjPosition.x+'px'
			  }
		  }
      );

      var body = dojo.doc.body;
      body.appendChild( _this.dropDownObj );
     }
     else
     {
	_this.cleanDropDown();
     }
     _this.dropDownItems = new Array();
	// console.log( data );
     for( i in data )
	{
	  _this.dropDownAddRow( data[ i ] );
	}
      if( data.length > 0 )
	dijit.scrollIntoView( _this.dropDownItems[ 0 ].node );
  }
  
  this.dropDownAddRow = function( rowData )
  {
	var rowNode = dojo.doc.createElement( 'div' );
	dojo.addClass( rowNode, "row" );
	rowNode.innerHTML = _this.resultRowParser( rowData );
	_this.dropDownObj.appendChild( rowNode );
	   
	var tmp = new Object()
	tmp.node = rowNode;
	tmp.selected = false;
	tmp.value = rowData.value;
	tmp.label = rowData.label;
	_this.dropDownItems.push( tmp );
	dojo.connect( rowNode, 'onclick', _this, function() { _this.saveAndHide() });
	dojo.connect( rowNode, 'onmouseover', _this, function() { _this.selectDropDownItem( rowNode ) } );
	}
  
  this.cleanDropDown = function() 
  {
   // console.log( 'cleanDropDown' );
    if( _this.dropDownObj != null )
    {
     dojo.empty( _this.dropDownObj);
      _this.selectedDropDownItem = null;
     }
  }

  this.showDropDown = function() 
  {
    //console.log( 'showDropDown' );
    if( _this.dropDownObj != null )
    {
    dojo.attr( _this.dropDownObj,
		{ style: { display: 'block' 
			  }
		}
	);
    }
    _this.isDropDownVisible = true;
  }

  this.hideDropDown = function() 
  {
   // console.log( 'hideDropDown' );
    if( _this.dropDownObj != null )
    {
    
    dojo.attr( _this.dropDownObj,
		{ style: { display: 'none' 
			  }
		}
	);
    
    }
    _this.isDropDownVisible = false;
  }

  this.closeAndClean = function()
  {
   // console.log( 'closeAndClean' );
    if( ! _this.denyBlurEvent )
    {
      _this.hideDropDown();
      _this.cleanDropDown();
    }
  }

  this.verifyValue = function()
  {
   // console.log( 'verifyValue' );
    if( _this.searchWord != _this.sourceObj.value )
      _this.sourceObj.value = ''
  }

  this.moveDown = function()
  {
    //console.log( 'moveDown' );
    if( _this.isDropDownVisible )
    {
      if( _this.selectedDropDownItem != null && _this.dropDownItems[ 1 + parseInt( _this.selectedDropDownItem ) ]  )
      {
	_this.selectDropDownItem( _this.dropDownItems[ _this.selectedDropDownItem + 1 ].node  );
      }
      else
      {
	_this.selectDropDownItem( _this.dropDownItems[ 0 ].node  );
      }
    }
  }

  this.moveUp = function()
  {
   // console.log( 'moveUp' );
    if( _this.isDropDownVisible )
    {
      if( _this.selectedDropDownItem != null && _this.dropDownItems[ _this.selectedDropDownItem - 1 ] )
      {
	_this.selectDropDownItem( _this.dropDownItems[ _this.selectedDropDownItem - 1 ].node  );
      }
      else
      {
	_this.selectDropDownItem( _this.dropDownItems[ _this.dropDownItems.length - 1 ].node  );
      }
    }
  }

  this.selectDropDownItem = function( node )
  {
    for( key in _this.dropDownItems )
    {
      if( _this.dropDownItems[ key ].node == node )
      {
	if( _this.selectedDropDownItem != null )
	{
	  _this.dropDownItems[ _this.selectedDropDownItem ].node.selected = false;
	  dojo.removeClass( _this.dropDownItems[ _this.selectedDropDownItem ].node, 'selected' );
	}
	_this.selectedDropDownItem = parseInt( key );
	_this.dropDownItems[ _this.selectedDropDownItem ].node.selected = true;
	dojo.addClass( _this.dropDownItems[ _this.selectedDropDownItem ].node, 'selected' );
	dijit.scrollIntoView( _this.dropDownItems[ _this.selectedDropDownItem ].node );
	return true;
      }
    }
  }

  this.saveAndHide = function() 
  {
		
	//console.log( 'saveAndHide' );
	
	if( _this.selectedDropDownItem != null )
	{
	 _this.searchWord = _this.dropDownItems[ _this.selectedDropDownItem ].label;
	 _this.targetObj.label.value = _this.dropDownItems[ _this.selectedDropDownItem ].label;
	 if( _this.targetObj.value )
		_this.targetObj.value.value = _this.dropDownItems[ _this.selectedDropDownItem ].value;
      _this.onChoice();
      _this.hideDropDown();
    
	}
  }

  try{
     dojo.connect( sourceObj, "onfocus", this, this.verifyValue );
     dojo.connect( sourceObj, "onblur", this, this.closeAndClean );
     dojo.connect( sourceObj, "onkeyup", this, this.dataController );
     dojo.connect( sourceObj, "onkeypress", this, function( e ){ this.navigationController(e); } );
  }
  catch( e )
  {
    console.log( e );
  }



}

function serchScriptInit() 
{
  if( dojo.byId( 'searchFor' ) ) 
  dojo.connect(
    dojo.byId( 'searchFor' ),
    'onkeypress',
    function( e ){ searcheAlbum( e ); }
  );
}

function searcheAlbum( event )
{
    if( event.charOrCode ==  dojo.keys.ENTER )
    {
      var albumIdNode = dojo.byId( 'albumId' );
      var serchForNode = dojo.byId( 'searchFor' );
      
      if( albumIdNode.value.length > 0 )
	  self.location = '/galleries/show/'+serchForNode.value+'/album_id='+albumIdNode.value;
      else
	  self.location = '/galleries/show/'+serchForNode.value+'/search_for='+serchForNode.value;
      
    }
}


dojo.addOnLoad( autoSuggetionInit );
dojo.addOnLoad( serchScriptInit );
