addNamespace("Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement")

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActiveUrl_class = function(strObjectUri, strObjectId, strContentType, intEditMode)
{
	this.strObjectUri = strObjectUri
	this.strObjectId = strObjectId
	this.strContentType = strContentType
	this.intEditMode = intEditMode
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActionItem_class = function(strName, strTitle, fnAction, boolEnabled, strIconUrl, targetElement)
{
	this.intIconWidth = 20
	this.intIconHeight = 20
	this.strName = strName
	this.fnAction = fnAction
	this.boolEnabled = boolEnabled
	this.strIconUrl = strIconUrl	
	this.refElement = null
	this.targetElement = targetElement
	this.boolInitialized = false
	this.strTitle  = strTitle
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActionItem_class.prototype.GetElement = function()
{
	if(this.refElement == null)
	{
		this.refElement = document.createElement("div")
		
		this.refElement.refOwner = this
		
		if(MS.Browser.isIE)
			this.refElement.style.styleFloat="left"
		else
			this.refElement.style.cssFloat="left"
		
		this.refElement.style.width = this.intIconWidth + "px"
		this.refElement.style.height = this.intIconHeight + "px"
		this.refElement.style.margin = "1px"
		this.refElement.style.backgroundImage = "url(" + this.strIconUrl + ")"
		if (!this.boolEnabled)
		{
			this.refElement.style.backgroundPosition = "0px -" + (3*this.intIconHeight) + "px "	
			this.refElement.className="ActiveElementToolBoxActionDisabled"
		}
		else
		{
			Aspacts.Idios3.I_CAP.Scripting.Core.ImplementEvent(this.refElement)
			this.refElement.title = this.strTitle
			this.refElement.style.cursor= "pointer"
		
			this.refElement.setAttribute("strBackgroundPos_over", "0px -" + this.intIconHeight + "px")
			this.refElement.setAttribute("strBackgroundPos_default", "0px 0px")
			this.refElement.setAttribute("strBackgroundPos_down", "0px -" + (2*this.intIconHeight) + "px")
			this.refElement.addEventListener('mouseover', function(evt){this.style.backgroundPosition = this.getAttribute("strBackgroundPos_over")}, true)
			this.refElement.addEventListener('mouseout', function(evt){this.style.backgroundPosition = this.getAttribute("strBackgroundPos_default")}, true)
			this.refElement.addEventListener('mousedown', function(evt){this.style.backgroundPosition = this.getAttribute("strBackgroundPos_down")}, true)
			this.refElement.addEventListener('click', function(evt){this.refOwner.fnAction(this.refOwner.targetElement)}, true)
			this.refElement.className="ActiveElementToolBoxAction"
		}
		
		
		
		
	}
	if (!this.boolInitialized)
	{
		if (this.boolEnabled)
		{
			this.boolInitialized = true
			Aspacts.Idios3.I_CAP.Scripting.Core.ImplementEvent(this.refElement)
			this.refElement.addEventListener('click', function(evt){this.refOwner.targetElement.elementParser.HandleCtrlRelease(evt||event)}, false)
		}
		
	}
	return this.refElement
}



Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class = function(strParseTags)
{
	this.strParseTags = strParseTags;
	this.activeUrls = new Array();
	this.activeElements = new Array();
	this.customElementParsers = new Array();
	this.boolLoaded = false
	
	this.options = new Array()
	
	
	//Aspacts.Script.Utils.Core.ImplementEvent(window)
	Aspacts.Idios3.I_CAP.Scripting.Core.AddDirectEvent(window, 'load', this.PrepareElements.bind(this))
	
	this.layersVisible = false
	
	this.customElementParsers.push(this.DefaultElementParserContentObject.bind(this))
	this.toolBox = new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ToolBox_class(this)
	
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.SetOption = function(strOptionGroup, strOptionKey, strValue)
{
	if (this.options[strOptionGroup] == null)
		this.options[strOptionGroup] = new Array()
		
	this.options[strOptionGroup][strOptionKey] = strValue
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.GetOption = function(strOptionGroup, strOptionKey, strDefaultValue)
{
	
	if (this.options[strOptionGroup] == null || this.options[strOptionGroup][strOptionKey] == null)
	{
		if(this.options['default'] != null  && this.options['default'][strOptionKey] != null)
		{
			return this.options['default'][strOptionKey]
		}
		else
		{
			if (typeof(strDefaultValue) == 'undefined')
				return null
			else
				return strDefaultValue
		}
	}	
	else
	{
		if (this.options[strOptionGroup][strOptionKey] == null)
		{
			if (typeof(strDefaultValue) == 'undefined')
				return null
			else
				return strDefaultValue
		}
		return this.options[strOptionGroup][strOptionKey]
	}
	
}



Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.AddActiveUrl = function(strUrl, strObjectId, strContentType, intEditMode)
{
	this.activeUrls.push(new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActiveUrl_class(strUrl, strObjectId, strContentType, intEditMode))
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.PrepareElements = function()
{
	Aspacts.Idios3.I_CAP.Scripting.Core.ImplementEvent(document.documentElement)
	document.documentElement.addEventListener('keydown', this.HandleCtrlPress.bind(this), true)
	document.documentElement.addEventListener('keyup', this.HandleCtrlRelease.bind(this), true)
	
	
	
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.PrepareElementsForTag = function(strTag)
{
	
	var refElements = document.body.getElementsByTagName(strTag)
	for (var i = 0; i < refElements.length ; i++)
	{
			var elementItem = new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.CoverLayer_class(this, refElements[i])
			for (var j=0;j<this.customElementParsers.length;j++)
			{
				this.customElementParsers[j](elementItem)
			}
			
			if(elementItem.actions.length > 0)
			{
				this.activeElements.push(elementItem)
			}
	}
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.DefaultElementParserContentObject = function(activeElement)
{
	//CAP_IgnoreActiveElement
	var intIgnoreEditMode = 0
	if (activeElement.refElement.getAttribute("CAP_IgnoreEditMode") != null )
	{
		intIgnoreEditMode = activeElement.refElement.getAttribute("CAP_IgnoreEditMode")
		if (intIgnoreEditMode == -1)
			return 
	}
	
	for(var i=0;i<this.activeElements.length;i++)
	{
		if (Aspacts.Idios3.I_CAP.Scripting.Core.SubElementOf(this.activeElements[i].refElement, activeElement.refElement))
		{
			return
		} 
		if (Aspacts.Idios3.I_CAP.Scripting.Core.SubElementOf(activeElement.refElement, this.activeElements[i].refElement))
		{
			return 
		}
	}
	
	var strOptionGroup = activeElement.refElement.getAttribute("CAP_OptionGroup") || "default"
	var elementActiveUrls = this.GetActiveUrls(activeElement.refElement)
	var editMode = 0
	var validContentTypes = ['contentobject', 'category']
	var strBaseUri = ""
	for (var i = 0; i < elementActiveUrls.length ; i++)
	{
		if(validContentTypes.indexOf(elementActiveUrls[i].strContentType.toLowerCase()) > -1)
		{
			editMode = 	elementActiveUrls[i].intEditMode | editMode
			strBaseUri = 	elementActiveUrls[i].strObjectUri
		}
	}
	
	var strEditUrl = strBaseUri
	if(strEditUrl.indexOf("?") > -1)
		strEditUrl += "&btEditObject=1"
	else		
		strEditUrl += "?btEditObject=1"
		
	
	if (editMode & 4 && !(intIgnoreEditMode & 4))
	{
		//add
		activeElement.refElement.setAttribute("strContentAddUrl", strEditUrl+"&btAddNewDocument=1")
		
		var action = new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActionItem_class('ContentAdd', this.GetOption(strOptionGroup, 'strContentAddTitle'), function(activeElement){location.href=activeElement.refElement.getAttribute("strContentAddUrl")}, true, this.GetOption(strOptionGroup, 'strContentAddIcon'), activeElement)
		action.intIconWidth = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeWidth', 20))
		action.intIconHeight = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeHeight', 20))
		
		
		activeElement.actions.push(action)
		
			
	
	}
	if (editMode & 1 && !(intIgnoreEditMode & 1))
	{
		//edit
		activeElement.refElement.setAttribute("strContentEditUrl", strEditUrl)
		
		var action = new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActionItem_class('ContentEdit', this.GetOption(strOptionGroup, 'strContentEditTitle'), function(activeElement){location.href=activeElement.refElement.getAttribute("strContentEditUrl")}, true, this.GetOption(strOptionGroup, 'strContentEditIcon'), activeElement)
		action.intIconWidth = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeWidth', 20))
		action.intIconHeight = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeHeight', 20))
		
		activeElement.actions.push(action)
		
	}
	if (editMode & 2 && !(intIgnoreEditMode & 2))
	{
		//delete
		activeElement.refElement.setAttribute("strContentDeleteUrl", strEditUrl + "&btDeleteDocument=1&strReturnUrl=" + escape(location.href))
		activeElement.refElement.setAttribute("strConfirmDelete", this.GetOption(strOptionGroup, 'strContentDeleteConfirm'))
		
		var action = new Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.ActionItem_class('ContentDelete', this.GetOption(strOptionGroup, 'strContentDeleteTitle'), function(activeElement){if(confirm(activeElement.refElement.getAttribute("strConfirmDelete"))){location.href=activeElement.refElement.getAttribute("strContentDeleteUrl")}}, true, this.GetOption(strOptionGroup, 'strContentDeleteIcon'), activeElement)
		action.intIconWidth = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeWidth', 20))
		action.intIconHeight = parseInt(this.GetOption(strOptionGroup, 'intToolBoxIconSizeHeight', 20))
		
		activeElement.actions.push(action)
	}
	
	

	
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.GetActiveUrls = function(refElement)
{
	var returnvalue = new Array()
	
	var strContentType = refElement.getAttribute("CAP_ObjectType")
	
	var strUri = refElement.getAttribute("CAP_ObjectUri") || refElement.href
	if (strUri)
	{
		if (strUri.indexOf("?strFilter") >= 0)
		{
			strUri = strUri.substring(0, strUri.indexOf("?strFilter"))		
		}
		for (var i = 0; i < this.activeUrls.length ; i++)
		{
			if ((strContentType == null || strContentType.toLowerCase() == this.activeUrls[i].strContentType.toLowerCase()) && this.activeUrls[i].strObjectUri.toLowerCase() == strUri.toLowerCase())
			{
				returnvalue.push(this.activeUrls[i])
			}
		}
	}
	var strObjectId = refElement.getAttribute("CAP_ObjectId")
	if (strObjectId)
	{
		for (var i = 0; i < this.activeUrls.length ; i++)
		{
			if ((strContentType ==null || strContentType.toLowerCase() == this.activeUrls[i].strContentType.toLowerCase()) && this.activeUrls[i].strObjectId.indexOf(strObjectId) == 0)
			{
				returnvalue.push(this.activeUrls[i])
			}
		}
	}
	return returnvalue
}


Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.HandleCtrlPress = function(evt)
{
	evt = evt||event
	if(evt.keyCode == 17 && !this.layersVisible)
	{
		if(!this.boolLoaded)
		{
			for (var i=0; i< this.strParseTags.length;i++)
			{
				this.PrepareElementsForTag(this.strParseTags[i])
			}
		}
		
		//reverse to prevent issues with the toolbox when elements slightly overlap.
		for (var i = this.activeElements.length-1; i >=0  ; i--)
		{
			if(!this.boolLoaded)
			{	
				this.activeElements[i].BuildCoverLayer()
				document.body.insertBefore(this.activeElements[i].refCoverLayer, document.body.firstChild)
			}
			this.activeElements[i].Show()
		}
		
		if(!this.boolLoaded)
		{
			this.toolBox.PrepareElement()
		}
			
		this.layersVisible = true
		this.boolLoaded = true
	}
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.HandleCtrlRelease = function(evt)
{
	evt = evt||event
	if(evt.keyCode == 17 && this.layersVisible)
	{
		for (var i = 0; i < this.activeElements.length ; i++)
		{
			this.activeElements[i].Hide()
		}
		this.toolBox.Hide()
		this.layersVisible = false
	}
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.HandleElementMouseOver = function(evt, callingElement)
{
	
	if(this.currentElement != null)
		this.currentElement.Highlight(false)
	this.currentElement = callingElement
	this.currentElement.Highlight(true)
	
	this.toolBox.Show(this.currentElement)
	
	
}

Aspacts.Idios3.I_CAP.ClientInteraction.ActiveElement.Parser_class.prototype.HandleElementMouseOut = function(evt, callingElement)
{
	if(!Aspacts.Idios3.I_CAP.Scripting.Core.SubElementOf(evt.relatedTarget || evt.toElement, callingElement))
	{
		if(!Aspacts.Idios3.I_CAP.Scripting.Core.SubElementOf(evt.relatedTarget || evt.toElement, this.toolBox.elementToolBox))
		{
			callingElement.Highlight(false)
			this.toolBox.Hide(callingElement)
		}
	}
		
}

