/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2012 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/

// *********** GLOBAL VARS *****************************
var gCanvasTag;
var gCanvasHTMLTag;
var gCanvCurID;
var gCurWidth;
var gCurHeight;
var helpDoc = MM.HELP_objHTML5Canvas;
// ******************** API ****************************
function canInspectSelection()
{
  return true;
}

function inspectSelection(){

	gCanvasTag = null;
	gCanvCurID = '';
	gCurWidth = '';
	gCurHeight = '';

	var theObj = getSelectedNode(); 
	if(theObj)
	{
		// Get the value id. 	
		var cID = theObj.getAttribute('id'); 
		
		// update all the fields
		if( cID )	
			document.topLayer.document.canvasID.value = cID;
		else	
			document.topLayer.document.canvasID.value = "";
		gCanvCurID = cID;
	
		var width = theObj.getAttribute('width');
		if( width )
		{
			width = width.replace('%','');
			document.topLayer.document.widthAttr.value = width;
		}
		else
			document.topLayer.document.widthAttr.value = ""; 
		gCurWidth = width;	
	
			
		var height = theObj.getAttribute('height');
		if( height )
		{
			height = height.replace('%','');
			document.topLayer.document.heightAttr.value = height;
		}
		else
			document.topLayer.document.heightAttr.value = "";
		gCurHeight = height;
	}
}



// ******************** LOCAL FUNCTIONS ****************************

function getSelectedNode()
{
	// Get the DOM of the current document. 
    var theDOM = dw.getDocumentDOM();     

	// Get the selected node. 
    var theObj;

	if( theDOM )
		theObj  = theDOM.getSelectedNode(); 	

	// return the selected node
	return theObj;
}

// passing null to this will remove the attribute
// from the canvas tag.

function setOrRemoveAttribute(attrName, attrValue)
{
	// set the attribute name. if the attrValue
	// is null then remove that attribute from the canvas tag.

	if( attrName )
	{
		var theNode = getSelectedNode();
		if( theNode ){
			// Set the attribute value if present or
			// remove the attribute if not present.
			if( attrValue )
			{
				theNode.setAttribute(attrName,attrValue);
				modifyCanvasTag();
			}
			else
				theNode.removeAttribute(attrName);
		}
	}
}

function IsCanvasIdExists(idStr)
{
	var dom = dw.getDocumentDOM();
	var elements = dom.getElementById(idStr);
	//alert(elements);
	if(!elements || elements.length == 0)
		return false;
	else
		return true;
}

function setCanvasID()
{
    var cID = document.topLayer.document.canvasID.value; 

    // Set the value of the direction attribute to theDirection. 
	if(cID == gCanvCurID || !IsCanvasIdExists(cID))
	{
		setOrRemoveAttribute('id', cID); 
	}
	else
	{
		if(confirm(dw.loadString("insertbar/HTML5/canvas/dupID")))
		{
			setOrRemoveAttribute('id', cID); 
		}
		else
		{
			if(gCanvCurID)
				document.topLayer.document.canvasID.value = gCanvCurID;
			else
				document.topLayer.document.canvasID.value = '';
		}
	}
}

function IsPositiveNumber(str)
{
    return /^[0-9]+(\.[0-9]+)?$/.test(str);
}

function setWidth()
{
//	alert('setWidth');
	var width = document.topLayer.document.widthAttr.value; 

    // Set the value of the direction attribute to theDirection. 
	if(!width || IsPositiveNumber(width))
	{
		setOrRemoveAttribute('width',width); 
	}
	else
	{
		alert(dw.loadString("insertbar/HTML5/canvas/widthInvVal"));	
		if(gCurWidth)
			document.topLayer.document.widthAttr.value = gCurWidth;
		else
			document.topLayer.document.widthAttr.value = '';	
	}
}

function setHeight()
{
	var height = document.topLayer.document.heightAttr.value; 

    // Set the value of the direction attribute to theDirection. 
	if(!height || IsPositiveNumber(height))
	{	
		setOrRemoveAttribute('height',height); 
	}
	else
	{
		alert(dw.loadString("insertbar/HTML5/canvas/heightInvVal"));
		if(gCurHeight)
			document.topLayer.document.heightAttr.value = gCurHeight;
		else
			document.topLayer.document.heightAttr.value = '';
	}
}

function fillAttributes()
{
	var selectedNode = getSelectedNode();
	if(selectedNode)
	{
		var strout = "";
		var temp = getSelectedNode().getAttribute('id');
		if( temp && temp!="")
		{
			strout = strout + 'id="' + temp + '"';
		}		
		temp = selectedNode.getAttribute('width');
		if(temp && temp!="")
		{
			strout = strout + ' width="' + temp + '"';
		}
		temp = getSelectedNode().getAttribute('height');
		if( temp && temp!="")
		{
			strout = strout + ' height="' + temp + '"';
		}
		return strout;
	}
	return "";
}

function modifyCanvasTag()
{
	var strout = "<canvas ";
	strout = strout + fillAttributes();
	strout = strout + ">" ;
	
	for(var i=0 ; i< getSelectedNode().childNodes.length ; i++ )
	{
		strout = strout + getSelectedNode().childNodes[i].outerHTML;
	}
	strout = strout + "</canvas>";
		
	getSelectedNode().outerHTML = strout;
	
}

