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

var helpDoc = MM.HELP_jQueryButtonset;			

var BUTTONSET_ID;
var LIST_PANEL;
var UP_BTN;
var DOWN_BTN;
var ADD_BTN;
var DEL_BTN;
var LIST_INDEX = 0;

// ********************* API FUNCTIONS ***************************

//--------------------------------------------------------------------
// FUNCTION:
//   canInspectSelection
//
// DESCRIPTION:
//   This is a Property Inspector API function that gets called
//   whenever the selection in the document changes to decide whether
//   or not this property inspector should be displayed.
//
// ARGUMENTS:
//  None
//
// RETURNS:
//   true if the currently selected node is a Buttonset element,
//   false if it is not.
//--------------------------------------------------------------------

function canInspectSelection() 
{
	var bCanInspectSelection = false;
	var dom = dw.getDocumentDOM();
	var selectedNode = dom.getSelectedNode();
	
	if ( !selectedNode || !selectedNode.getTranslatedAttribute )
		return false;
	
	var attr = selectedNode.getTranslatedAttribute('buttonset');
  
	if ( attr && attr.length > 0 )
	{

		bCanInspectSelection = true;
	
		// If the widget manager is out of sync, run the translator
		var widgetMgr = JQuery.DesignTime.Widget.Manager.getManagerForDocument(dom); 
		
		if (!widgetMgr)
			return;
	
		if ( !widgetMgr.getWidget('buttonset', selectedNode.id ) )
		{ 
			dom.runTranslator("jQuery Widget");

			if ( !widgetMgr.getWidget('buttonset', selectedNode.id ) )
			{ 
			// Running the translator failed to create a design time object
			// for this widget. Either caInspectSelection() was called in the
			// middle of an edit operation, which prevents the translator from
			// running right now, or an error occurred during the translation.

			bCanInspectSelection = false;
			}
		}  
	}

	return bCanInspectSelection;
}

//--------------------------------------------------------------------
// FUNCTION:
//   initializeUI
//
// DESCRIPTION:
//   This is an internal utility function that searches through the
//   Property Inspector document to find all of the UI controls we
//   will programatically manipulate, and stores handles to them in
//   global variables which are used in some of the other functions
//   for this Property Inspector.
//
// ARGUMENTS:
//  None
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------

function initializeUI() 
{
	BUTTONSET_ID = document.getElementById("idInput");
	LIST_PANEL = new ListControl("panelList");
	ADD_BTN = document.getElementById("elemAdd");
	DEL_BTN = document.getElementById("elemDel");
	UP_BTN = document.getElementById("elemUp");
	DOWN_BTN = document.getElementById("elemDown");
}

//--------------------------------------------------------------------
// FUNCTION:
//   inspectSelection
//
// DESCRIPTION:
//   This is a Property Inspector API function that gets called
//   whenever the selection in the document has changed and it has
//   been decided that this Property Inspector should be displayed.
//   This function syncs up the Property Inspector UI with the
//   widget's design-time object so that it accurately reflects
//   what is in the widget HTML markup and its JS constructor.
//
// ARGUMENTS:
//  None
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------

function inspectSelection() 
{
	dw.logEvent(UT_JQUERY_BUTTONSET, UT_JQUERY_BUTTONSET_INSPECT);
	// Call initializeUI() here; it's how the global variables get
	// initialized. The onLoad event on the body tag is never triggered
	// in inspectors.
	initializeUI();

	var dom = dw.getDocumentDOM();
	var selectedNode = dom.getSelectedNode();
	
	if (!canInspectSelection())
		return;
		
	var divId = selectedNode.id;
	// Update the ID field in the PI.
	BUTTONSET_ID.value = divId;	
	
	var widgetMgr = JQuery.DesignTime.Widget.Manager.getManagerForDocument(dom); 
  
	if (!widgetMgr)
		return;
	
	var buttonset = widgetMgr.getWidget('buttonset', divId);
	
	if (!buttonset)
	{
		return;
	}
  
	var buttonsetLabels = [];
	var selIndex = LIST_INDEX;
		
	if ( buttonset )
		buttonsetLabels = buttonset.getLabels();
  
	// Set the list values.
	if (buttonsetLabels)
	{
		LIST_PANEL.setAll(buttonsetLabels, buttonsetLabels);
		if ( buttonsetLabels.length )
		{
			LIST_PANEL.setIndex(selIndex);
		}
	}
	else
	{
		LIST_PANEL.setAll([],[]);
	}	
	
	
	enableControls(buttonset);
}

//--------------------------------------------------------------------
// FUNCTION:
//   enableControls
//
// DESCRIPTION:
//   This internal utility function enables/disables the controls
//   in the Property Inspector based on the state of the widget
//   design-time object.
//
// ARGUMENTS:
//  buttonset - object - The widget design-time object.
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------

function enableControls(buttonset)
{
  if (LIST_PANEL.getLen() > 0)
       LIST_PANEL.enable();
  else
       LIST_PANEL.disable();

  var selIndex = LIST_PANEL.getIndex();
  
	ADD_BTN.removeAttribute("disabled");
	ADD_BTN.src = "../Shared/MM/Images/btnAddSmall.png";
	DEL_BTN.removeAttribute("disabled");
	DEL_BTN.src = "../Shared/MM/Images/btnDelSmall.png";
	UP_BTN.removeAttribute("disabled");
	UP_BTN.src = "../Shared/MM/Images/btnUpSmall.png";
	DOWN_BTN.removeAttribute("disabled");
	DOWN_BTN.src = "../Shared/MM/Images/btnDownSmall.png";
  
  if (selIndex <= 0)
  {
		UP_BTN.setAttribute("disabled", true);
    UP_BTN.src = "../Shared/MM/Images/btnUpSmall_dis.png";
  }
  if (selIndex >= LIST_PANEL.getLen()-1)
  {
		DOWN_BTN.setAttribute("disabled", true);
    DOWN_BTN.src = "../Shared/MM/Images/btnDownSmall_dis.png";
  }
  if (LIST_PANEL.getLen() == 0)
  {
		DEL_BTN.setAttribute("disabled", true);
    DEL_BTN.src = "../Shared/MM/Images/btnDelSmall_dis.png";
  }

}


//--------------------------------------------------------------------
// FUNCTION:
//   updateTag
//
// DESCRIPTION:
//   This function handles all of the user actions triggered by the
//   user from the Propery Inspector controls.
//
// ARGUMENTS:
//  action - string - The name of the action to perform.
//
// RETURNS:
//   N/A
//--------------------------------------------------------------------
function updateTag(action)
{
	var dom = dw.getDocumentDOM();
	var selectedNode = dom.getSelectedNode();
	if (!canInspectSelection())
		return;
		
	var selIndex = LIST_PANEL.getIndex();	
	var divId = selectedNode.id;
  
	var widgetMgr = JQuery.DesignTime.Widget.Manager.getManagerForDocument(dom); 
	var buttonset = widgetMgr.getWidget('buttonset', divId );
	
	if ( !buttonset )
		return;
  
	var buttonSetLabels = buttonset.getLabels();
	var buttonsetLength = 0;
	
	if (buttonSetLabels)	
		buttonsetLength = buttonSetLabels.length;
	
	if (action) 
	{
		switch (action)
		{
			case "id":
			{
				// Validate the new id.
				var newId = BUTTONSET_ID.value;
				if ( newId == divId )
				  return; // Nothing to change.
				
				if ( newId.length == 0 )
				{
				  alert(dw.loadString("jquery/widget/alert/need unique id"));
				  return;
				}
				
				if ( dom.getElementById(newId) )
				{
				  alert(dw.loadString("jquery/widget/alert/id already exists"));
				  return;
				}
				
				if ( !dwscripts.isValidID(newId) )
				{
				  alert(dw.loadString("jquery/widget/alert/id is invalid"));
				  return;
				}
				
				// Update the constructor.
				buttonset.updateWidgetId(newId);

				// Update the WidgetManager for the new ID.
				widgetMgr.setWidget('buttonset', newId, buttonset );
			}
			break;
			
			case "addButton":
			{
				// Add new panel after the current selection.
				buttonset.addNewButton(selIndex);
				
				// Add after current panel.
				selIndex = selIndex+1;
			}
			break;
      
			case "deleteButton":
			{
				if (selIndex < 0)
					break;
					
				buttonset.deleteButton(selIndex)
				if (selIndex >= buttonsetLength-1)
					selIndex = selIndex - 1;
			}
			break;
      
			case "moveButtonUp":
			{
				if (selIndex < 0)
					break;
				
				buttonset.moveButtonUp(selIndex);
				
				if (selIndex > 0)
					selIndex = selIndex - 1;
				else
					selIndex = 0;
			}
			break;
      
			case "moveButtonDown":
			{
				if (selIndex < 0)
					break;
				
				buttonset.moveButtonDown(selIndex);
				
				if (selIndex >= buttonsetLength-1)
					selIndex = buttonsetLength-1;
				else
					selIndex = selIndex +1;
			 
			}
			break;
		}
	}

	LIST_INDEX = selIndex;

	// All these edits modify the Buttonset. We need to recreate the JS Object to reflect those changes.
	buttonset.refresh();

	// Make sure selection stays on the input.
	dom.setSelectedNode(selectedNode); 
	inspectSelection();
}
