﻿function PhSearch( anchor_element, data_source )
{
	this.m_bIdle			= true;
	
	this.m_oAjax			= new PhAjaxJson();
	this.m_strDataSource	= data_source;
	this.m_oParams			= new Array();
	this.m_oAnchor			= (typeof( anchor_element ) == 'string')? document.getElementById( anchor_element ) : anchor_element;
	
	this.InitObject();
}

PhSearch.prototype.InitObject = function()
{
	var me = this;
	
	this.OnSearchResultsReturned = null
	
	if( this.m_oAnchor != null )
	{
		g_oClient.AttachEvent( this.m_oAnchor, 'keyup', function( e ){me.HandleKeyUp( e?e:event );} );
		//g_oClient.AttachEvent( this.m_oAnchor, 'blur', function( e ){me.HandleBlur( e?e:event );} );
	}
}

PhSearch.prototype.HandleKeyUp = function( evt )
{	
	//if( evt.keyCode == 13 )
	//	this.RefreshDataSource( evt );
}

PhSearch.prototype.RunSearch = function( keywords, show_all )
{
	var me		= this;
	var params	= '';
	var i		= 0;
	
	if( this.m_bIdle && keywords.length > 3 )
	{
		this.m_oAjax.SendRequest( this.m_strDataSource, 'show_all='+show_all+'&keywords='+keywords, null, function( obj ){me.RefreshDataSourceCallback( obj );} );
		this.m_bIdle = false;
	}
}

PhSearch.prototype.RefreshDataSource = function( evt )
{
	var me		= this;
	var src_elm = (evt.target)? evt.target : evt.srcElement;
	var params	= '';
	var i		= 0;
	
	document.getElementsByTagName( 'body' )[0].style.cursor = 'wait';
	
	if( this.m_bIdle && src_elm.value.length > 3 )
	{
		this.m_oAjax.SendRequest( this.m_strDataSource, 'keywords='+src_elm.value, null, function( obj ){me.RefreshDataSourceCallback( obj );} );
		this.m_bIdle = false;
	}
}

PhSearch.prototype.RefreshDataSourceCallback = function( obj )
{
	var me		= this;
	var item	= null;
	var option	= null;
	var html	= '';
	var i		= 0;
	
	if( this.OnSearchResultsReturned != null )
		this.OnSearchResultsReturned( obj );
			
	this.m_bIdle = true;
}

PhSearch.prototype.HandleBlur = function( evt )
{
}