// Copyright 2012-2013 Adobe Systems Incorporated.  All rights reserved.

//--------------------------------------------------------------------
// CLASS:
//   JQuery.DesignTime.Widget.Autocomplete
//
// DESCRIPTION:
//   This class is used by the Autocomplete property inspector to manage
//   and manipulate the design-time state of the widget's markup in the
//   design view.
//--------------------------------------------------------------------

//--------------------------------------------------------------------
// FUNCTION:
//   Autocomplete
//
// DESCRIPTION:
//	 This is the constructor method for this design time Autocomplete object.
//	 It calls the base widget constructor which helps us inherit all base methods
//	 like addClass, removeClass, ensureValidElements etc
//	 
//   It also manages the Autocomplete's widget markup in the design view. This constructor
//   is registered in the JQuery Widget translator found at:
//
//       Configuration/Translators/jQueryWidget.htm
//	 
//
// ARGUMENTS:
//   dom - object - The DOM used by the document in design view.
//   element - object - The top-level DOM element for our widget markup.
// RETURNS:
//   N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete = function(dom, element, consArgs)
{
  JQuery.DesignTime.Widget.Base.call(this, dom, element);

	this.opts = {};
	this.opts = consArgs;
  this.init(element);
};

// JQuery.DesignTime.Widget.Autocomplete derives from our JQuery.DesignTime.Widget.Base
// class, so create a Base object and use it as our prototype so we inherit all of its
// methods.
JQuery.DesignTime.Widget.Autocomplete.prototype = new JQuery.DesignTime.Widget.Base();

// Reset the constructor property back to Autocomplete for our newly created prototype
// object so callers know that our object was created with the Autocomplete constructor.
JQuery.DesignTime.Widget.Autocomplete.prototype.constructor = JQuery.DesignTime.Widget.Autocomplete;

//--------------------------------------------------------------------
// FUNCTION:
//   init
//
// DESCRIPTION:
//   Initializes the design-time object's properties. This  method is
//   called from the constructor and refresh() methods.
//
// ARGUMENTS:
//   element - object - The widget's top-level DOM element.
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.init = function(element)
{
  this.element = this.getElement(element);
	this.myListArray = [];
	this.atListArray = [];
	this.collisionListArray = [];
	this.initMyListArray();
	this.initAtListArray();
	this.initCollisionListArray();
	
  this.canRefresh = true;
};

//--------------------------------------------------------------------
// FUNCTION:
//  	getSourceValue
//
// DESCRIPTION:
//  	This method returns the value of the source attribute in the widget constructor.
//		If user changes the source value in the code. This is where we 'read' that updated 
//		value from the object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.getSourceValue = function()
{
	if( this.opts && this.opts.source != null && typeof this.opts.source != 'undefined' )
	{
		if (this.opts.source.constructor.toString() == Array.toString())
			return '["' + this.opts.source.join('", "') + '"]';
		else
			return this.opts.source;
	}
	return "";
}

JQuery.DesignTime.Widget.Autocomplete.prototype.recalculateOpts = function()
{
	this.opts = {};
	var consArgs = this.getConstructorArgs(this.widgetType);
	this.opts = consArgs;
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setSourceValue
//
// DESCRIPTION:
//  	When a new value is attained for the source attribute from the PI
//		we have to update our opts object with this new source value and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setSourceValue = function(sourceValue)
{	
	if( sourceValue == null || typeof sourceValue == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
		
	var oldSourceValue = this.opts.source;	
		
	if( sourceValue == "" )
	{
		this.opts.source = null;
	}	
	else
	{
		this.opts.source = sourceValue;
	}
	
	if( oldSourceValue != this.opts.source )
		this.updateOptions(this.widgetType, this.opts);	
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setSourceArray
//
// DESCRIPTION:
//  	When a new array value is attained for the source attribute from the PI
//		we have to update our opts object with this new source array and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setSourceArray = function(sourceArray)
{	
	if( sourceArray == null || typeof sourceArray == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
		
	var oldSourceValue = "";
	
	if (this.opts.source)
		oldSourceValue = this.opts.source.toString();	
		
	this.opts.source = sourceArray;
	
	var newSourceValue = "";
	
	if (this.opts.source)
		newSourceValue = this.opts.source.toString();
	
	if( oldSourceValue != newSourceValue )
		this.updateOptions(this.widgetType, this.opts);	
}


//--------------------------------------------------------------------
// FUNCTION:
//  	getMinLengthValue
//
// DESCRIPTION:
//  	This method returns the value of the minlength attribute in the widget constructor.
//		If user changes the minlength value in the code. This is where we 'read' that updated 
//		value from the object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	String: The value of the minlength attribute as string
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.getMinLengthValue = function()
{
	if( this.opts && this.opts.minLength != null && typeof this.opts.minLength != 'undefined' )
	{
		return this.opts.minLength.toString();
	}
	return "";
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setMinlengthValue
//
// DESCRIPTION:
//  	When a new value for minLength is attained for the minLength attribute from the PI
//		we have to update our opts object with this new minLength value and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setMinLengthValue = function(minLengthValue)
{	
	if( minLengthValue == null || typeof minLengthValue == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
	
	var oldMinLength = this.opts.minLength;
	
	if( minLengthValue == "" || minLengthValue == "1" || parseInt(minLengthValue).toString() == 'NaN' )
	{
		this.opts.minLength = null;
	}	
	else
	{
		this.opts.minLength = parseInt(minLengthValue);
	}
	
	if( oldMinLength != this.opts.minLength )
		this.updateOptions(this.widgetType, this.opts);	
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getDelayValue
//
// DESCRIPTION:
//  	This method returns the value of the delay attribute in the widget constructor.
//		If user changes the delay value in the code. This is where we 'read' that updated 
//		value from the object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	String: The value of the delay attribute as string
//--------------------------------------------------------------------


JQuery.DesignTime.Widget.Autocomplete.prototype.getDelayValue = function()
{
	if( this.opts && this.opts.delay != null && typeof this.opts.delay != 'undefined' )
	{
		return this.opts.delay.toString();
	}
	return "";
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setDelayValue
//
// DESCRIPTION:
//  	When a new value for delay is attained for the delay attribute from the PI
//		we have to update our opts object with this new delay value and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setDelayValue = function(delayValue)
{	
	if( delayValue == null || typeof delayValue == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
		
	var oldDelayValue = this.opts.delay;	
		
	if( delayValue == "" || delayValue == "300" || parseInt(delayValue).toString() == 'NaN' )
	{
		this.opts.delay = null;
	}	
	else
	{
		this.opts.delay = parseInt(delayValue);
	}
	
	if( oldDelayValue != this.opts.delay )
		this.updateOptions(this.widgetType, this.opts);	
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getAppendValue
//
// DESCRIPTION:
//  	This method returns the value of the appendTo attribute in the widget constructor.
//		If user changes the appendTo value in the code. This is where we 'read' that updated 
//		value from the object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	String: The value of the appendTo attribute as string
//--------------------------------------------------------------------


JQuery.DesignTime.Widget.Autocomplete.prototype.getAppendValue = function()
{
	if( this.opts && this.opts.appendTo != null && typeof this.opts.appendTo != 'undefined' )
	{
		return this.opts.appendTo.toString();
	}
	return "";
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setAppendValue
//
// DESCRIPTION:
//  	When a new value for appendTo is attained for the appendTo attribute from the PI
//		we have to update our opts object with this new appendTo value and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setAppendValue = function(appendToValue)
{	
	if( appendToValue == null || typeof appendToValue == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
	
	var oldAppendValue = this.opts.appendTo;
	
	if( appendToValue == "" || appendToValue == "body" )
	{
		this.opts.appendTo = null;
	}	
	else
	{
		this.opts.appendTo = appendToValue;
	}
	
	if( oldAppendValue != this.opts.appendTo )
		this.updateOptions(this.widgetType, this.opts);	
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getAutoFocusValue
//
// DESCRIPTION:
//  	This method returns the value of the autofocus attribute in the widget constructor.
//		If user changes the autofocus value in the code. This is where we 'read' that updated 
//		value from the object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	Boolean: The value of the autofocus attribute as boolean
//--------------------------------------------------------------------


JQuery.DesignTime.Widget.Autocomplete.prototype.getAutoFocusValue = function()
{
	if( this.opts && this.opts.autoFocus != null && typeof this.opts.autoFocus != 'undefined' )
	{
		return (this.opts.autoFocus == true);
	}
	return false;
}

//--------------------------------------------------------------------
// FUNCTION:
//  	setAutoFocusValue
//
// DESCRIPTION:
//  	When a new value for autoFocus is attained for the autoFocus attribute from the PI
//		we have to update our opts object with this new autoFocus value and initiate an update cycle
//		for this change to be reflected in code.
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	None
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.setAutoFocusValue = function(autoFocusValue)
{	
	if( autoFocusValue == null || typeof autoFocusValue == "undefined" )
		return;

	if (!this.opts)
		this.opts = {};
	
	var oldAutoFocusValue = this.opts.autoFocus;
	
	if( autoFocusValue == true )
	{
		this.opts.autoFocus = true;
	}	
	else
	{
		this.opts.autoFocus = null;
	}
	
	if( oldAutoFocusValue != this.opts.autoFocus )
		this.updateOptions(this.widgetType, this.opts);
}
//--------------------------------------------------------------------
// FUNCTION:
//   getAssets
//
// DESCRIPTION:
//   Static function that returns the assets to be applied to page
//
// ARGUMENTS:
//   None
//
// RETURNS:
//   (array of objects)
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.getAssets = function()
{
	var assets = new Array();
	
	assetObject = new Object();
	assetObject.fullPath = jQAutocompleteImagesPath;
	assetObject.file = jQAutocompleteImagesFile;
	assetObject.type = "";
	assets.push(assetObject);
	
	assetObject = new Object();
	assetObject.fullPath = jQCoreCssPath;
	assetObject.file = jQCoreCssFile;
	assetObject.type = "link";
	assets.push(assetObject);

	assetObject = new Object();
	assetObject.fullPath = jQCssThemePath;
	assetObject.file = jQCssThemeFile;
	assetObject.type = "link";
	assets.push(assetObject);
	
	assetObject = new Object();
	assetObject.fullPath = jQAutocompleteCssWidgetPath;
	assetObject.file = jQAutocompleteCssWidgetFile;
	assetObject.type = "link";
	assets.push(assetObject);
    
    assetObject = new Object();
	assetObject.fullPath = jQMenuCssPath;
	assetObject.file = jQMenuCssFile;
	assetObject.type = "link";
	assets.push(assetObject);
	
	assetObject = new Object();
	assetObject.fullPath = jQMainPath;
	assetObject.file = jQMainFile;
	assetObject.type = "javascript";
	assets.push(assetObject);
	
	assetObject = new Object();
	assetObject.fullPath = jQAutocompleteJsPath;
	assetObject.file = jQAutocompleteJsFile;
	assetObject.type = "javascript";
	assets.push(assetObject);
	
	return (assets);
	
};

//--------------------------------------------------------------------
// FUNCTION:
//   refresh
//
// DESCRIPTION:
//   None
//
// ARGUMENTS:
//   Syncs up the internal state of the design-time widget object
//   with the markup that currently exists in the design view.
//
//   This method gets called from the translator if a design-time
//   object already exists for a specific widget ID, and from various
//   methods in the Autocomplete property inspector.
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.refresh = function()
{
  if (!this.canRefresh)
	return;
	
	this.opts = {};
	var consArgs = this.getConstructorArgs(this.widgetType);
	this.opts = consArgs; 
  this.init(this.element_id);  
};


//--------------------------------------------------------------------
// FUNCTION:
//   JQuery.DesignTime.Widget.Autocomplete.getNewAutocompleteSnippet
//
// DESCRIPTION:
//   Static utility function that returns a string containing the
//   markup for a complete Autocomplete.
//
// ARGUMENTS:
//  id - string - The string to use as the widget's id attribute.
//
// RETURNS:
//   String that is the HTML markup fragment for the panel.
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.getNewAutocompleteSnippet = function(id)
{
  var dpSnippet = '<input type="text" id="' + id + '">';
  return dpSnippet;
};

//--------------------------------------------------------------------
// FUNCTION:
//   JQuery.DesignTime.Widget.Autocomplete.getNewAutocompleteConstructorSnippet
//
// DESCRIPTION:
//   Static utility function that returns a string that contains the
//   constructor snippet for creating a JQuery widget with the specified id.
//
// ARGUMENTS:
//  id - string - The id of the widget markup to insert into the
//                constructor snippet.
//
// RETURNS:
//   String containing JS that is the constructor call to create the
//   widget.
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.getNewAutocompleteConstructorSnippet = function(id)
{
  return ' $(function() {\n\t$( "#' + id + '" ).autocomplete(); \n});';
 /* $(function() {
		$( "#Autocomplete1" ).Autocomplete();
	});*/
};

//--------------------------------------------------------------------
// FUNCTION:
//  	initMyListArray
//
// DESCRIPTION:
//  	Initializes the myList array with predefined values. 
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.initMyListArray = function()
{
	this.myListArray.push("left top");
	this.myListArray.push("top");
	this.myListArray.push("center");
	this.myListArray.push("bottom");
	this.myListArray.push("left");
	this.myListArray.push("right");
}

//--------------------------------------------------------------------
// FUNCTION:
//  	initAtListArray
//
// DESCRIPTION:
//  	Initializes the atList array with predefined values. 
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.initAtListArray = function()
{
	this.atListArray.push("left bottom");
	this.atListArray.push("top");
	this.atListArray.push("center");
	this.atListArray.push("bottom");
	this.atListArray.push("left");
	this.atListArray.push("right");
}

//--------------------------------------------------------------------
// FUNCTION:
//  	initCollisionListArray
//
// DESCRIPTION:
//  	Initializes the collisionList array with predefined values. 
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.initCollisionListArray = function()
{
	var noneString = dw.loadString("jquery/widgets/autocomplete/none")
	this.collisionListArray.push(noneString);
	this.collisionListArray.push("flip");
	this.collisionListArray.push("fit");
	this.collisionListArray.push("fit flip");
	this.collisionListArray.push("fit none");
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getMyListArray
//
// DESCRIPTION:
//  	Return the myList Array containing all possible values for 'my'
//		attribute of the position object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.getMyListArray = function()
{
	var mergeArray = [];
	
	for ( var i = 0; i < this.myListArray.length; i++)
		mergeArray[i] = "my:" + this.myListArray[i];
	
	return mergeArray;
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getAtListArray
//
// DESCRIPTION:
//  	Return the atList Array containing all possible values for 'at'
//		attribute of the position object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------

JQuery.DesignTime.Widget.Autocomplete.prototype.getAtListArray = function()
{
	var mergeArray = [];
	
	for ( var i = 0; i < this.atListArray.length; i++)
		mergeArray[i] = "at:" + this.atListArray[i];
	
	return mergeArray;
}

//--------------------------------------------------------------------
// FUNCTION:
//  	getCollisionListArray
//
// DESCRIPTION:
//  	Return the collisionList Array containing all possible values for 'collision'
//		attribute of the position object
//
// ARGUMENTS:
//  	None
//
// RETURNS:
//  	N/A
//--------------------------------------------------------------------


JQuery.DesignTime.Widget.Autocomplete.prototype.getCollisionListArray = function()
{
	var mergeArray = [];
	mergeArray[0] = this.collisionListArray[0];
	
	for ( var i = 1; i < this.collisionListArray.length; i++)
		mergeArray[i] = "collision:" + this.collisionListArray[i];
	
	return mergeArray;
}

JQuery.DesignTime.Widget.Autocomplete.prototype.getMyListIndex = function()
{
	if( this.opts && this.opts.position != null && typeof this.opts.position != 'undefined' )
	{
		if( this.opts.position.my && typeof this.opts.position.my != 'undefined')
		{
			for( var i = 0; i < this.myListArray.length; i++ )
			{
				if( this.opts.position.my.toString() == this.myListArray[i] )
					return i;
			}
		}
	}
	return 0;
}
JQuery.DesignTime.Widget.Autocomplete.prototype.setMyValue = function(listIndex)
{
	if( listIndex == null || typeof listIndex == "undefined" )
		return;
			
	if (!this.opts)
		this.opts = {};
		
	if (!this.opts.position)
		this.opts.position = {};
		
	var oldMyValue = this.opts.position.my;	
	
	if( listIndex == 0 )
		this.opts.position.my = null;
	else
		this.opts.position.my = this.myListArray[listIndex];	
		
	if( this.opts.position && !this.opts.position.my && !this.opts.position.at && !this.opts.position.collision )	
	{
		this.opts.position = null;
		this.updateOptions(this.widgetType, this.opts);	
	}
	else if( oldMyValue != this.opts.position.my )	
		this.updateOptions(this.widgetType, this.opts);	
}
JQuery.DesignTime.Widget.Autocomplete.prototype.getAtListIndex = function()
{
	if( this.opts && this.opts.position != null && typeof this.opts.position != 'undefined' )
	{
		if( this.opts.position.at && typeof this.opts.position.at != 'undefined')
		{
			for( var i = 0; i < this.atListArray.length; i++ )
			{
				if( this.opts.position.at.toString() == this.atListArray[i] )
					return i;
			}
		}
	}
	return 0;
}
JQuery.DesignTime.Widget.Autocomplete.prototype.setAtValue = function(listIndex)
{
	if( listIndex == null || typeof listIndex == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
		
	if (!this.opts.position)
		this.opts.position = {};
		
	var oldAtValue = this.opts.position.at;	
	
	if( listIndex == 0 )
		this.opts.position.at = null;
	else
		this.opts.position.at = this.atListArray[listIndex];	
		
	if( this.opts.position && !this.opts.position.my && !this.opts.position.at && !this.opts.position.collision )	
	{
		this.opts.position = null;
		this.updateOptions(this.widgetType, this.opts);	
	}
	else if( oldAtValue != this.opts.position.at )	
		this.updateOptions(this.widgetType, this.opts);	
}
JQuery.DesignTime.Widget.Autocomplete.prototype.getCollisionListIndex = function()
{
	if( this.opts && this.opts.position != null && typeof this.opts.position != 'undefined' )
	{
		if( this.opts.position.collision && typeof this.opts.position.collision != 'undefined')
		{
			for( var i = 0; i < this.collisionListArray.length; i++ )
			{
				if( this.opts.position.collision.toString() == this.collisionListArray[i] )
					return i;
			}
		}
	}
	return 0;
}
JQuery.DesignTime.Widget.Autocomplete.prototype.setCollisionValue = function(listIndex)
{
	if( listIndex == null || typeof listIndex == "undefined" )
		return;
		
	if (!this.opts)
		this.opts = {};
		
	if (!this.opts.position)
		this.opts.position = {};
	
	var oldCollisionValue = this.opts.position.collision;
	
	if( listIndex == 0 )
		this.opts.position.collision = null;
	else
		this.opts.position.collision = this.collisionListArray[listIndex];	
		
	if( this.opts.position && !this.opts.position.my && !this.opts.position.at && !this.opts.position.collision )	
	{
		this.opts.position = null;
		this.updateOptions(this.widgetType, this.opts);	
	}
	else if( oldCollisionValue != this.opts.position.collision )
		this.updateOptions(this.widgetType, this.opts);	
}