def openEQPopup(objectName, tagPath):
	""" Called from equipment template view. Sends a message to the parent view using the messageId 'sendEQPopupMsg'
	"""
	system.perspective.sendMessage('eqPopupMsg', payload={
		'objectName': objectName,
		'tagPath': tagPath,
		'modal': False,
		'resizable': True,
		'draggable': True,
		'position': {
#			'left':'0',
#			'top':'0',
			'width': '1000px',
			'height': '835px'
		}
	})
	
def eqPopup(self, payload):
	""" Opens the Equipment Popup view. 
	"""
	viewPath = 'Library/Templates/Base'
	popupId = 'eqPopup'
	params = {
		'objectName': payload['objectName'],
		'tagPath': payload['tagPath']
	}
	system.perspective.openPopup(
		popupId, viewPath, params=params, position=payload['position'],
		showCloseIcon=False, draggable=payload['draggable'], resizable=payload['resizable'], 
		modal=payload['modal'], overlayDismiss=False, viewportBound=True
	)
	
def eqPopupMsg(self, payload):
	""" When called from inside an iFrame, will send a message to the outer view to call eqPopup() and open the popup outside of the iFrame. Otherwise will just call eqPopup() and open the popup.
	"""
	try:
		pageId = self.view.params.pageId
	except:
		pageId = ""
	if pageId == "":
		eqPopup(self, payload)
	else:
		system.perspective.sendMessage('eqPopup', payload=payload, scope='page', pageId=pageId)

#======================================================================================================================================================

def openLLSPopup(viewPath,title):
	""" Called from equipment template view. Sends a message to the parent view using the messageId 'sendEQPopupMsg'
	"""
	system.perspective.sendMessage('LLSPopupMsg', payload={
		'viewPath': viewPath,
		'title': title,
		'modal': False,
		'resizable': True,
		'draggable': True,
		'position': {
#			'left':'0',
#			'top':'0',
			'width': '1000px',
			'height': '835px'
		}
	})
	
def LLSPopup(self, payload):
	""" Opens the Equipment Popup view. 
	"""
	viewPath = payload['viewPath']
	title = payload['title']
	popupId = 'LLSPopup'

	system.perspective.openPopup(
		popupId, viewPath,title=title, position=payload['position'],
		showCloseIcon=False, draggable=payload['draggable'], resizable=payload['resizable'], 
		modal=payload['modal'], overlayDismiss=False, viewportBound=True
	)
	
def LLSPopupMsg(self, payload):
	""" When called from inside an iFrame, will send a message to the outer view to call eqPopup() and open the popup outside of the iFrame. Otherwise will just call eqPopup() and open the popup.
	"""
	try:
		pageId = self.view.params.pageId
	except:
		pageId = ""
	if pageId == "":
		LLSPopup(self, payload)
	else:
		system.perspective.sendMessage('LLSPopup', payload=payload, scope='page', pageId=pageId)


	
#def openEQPopup(objectName, tagPath, modal=False, resizable=True, draggable=True, pageId=None):
#	""" OLD VERSION
#	Opens an equipment popup based on an objectName and tagPath. 
#	"""
#	viewPath = 'Library/Templates/Base'
#	popupId = 'eqPopup'
#	params = {
#		'objectName': objectName,
#		'tagPath': tagPath
#	}
#	position = {
#		'left':'0',
#		'top':'0',
#		'width': '1000px',
#		'height': '800px'
#	}
#	
#	system.perspective.openPopup(
#		popupId, viewPath, params=params, position=position,
#		showCloseIcon=False, draggable=draggable, resizable=resizable, 
#		modal=modal, overlayDismiss=False, viewportBound=True
#	)
	
# developing
#def openEQPopupCentered(objectName, tagPath, modal=True, resizable=False, draggable=False):
#	"""
#	Opens an equipment popup based on an objectName and tagPath. 
#	"""
	
#	viewPath = 'Library/Templates/Base'
#	popupId = 'eqPopup'
#	params = {
#		'objectName': objectName,
#		'tagPath': tagPath
#	}
#	position = {
#		'width': '1000px',
#		'height': '800px'
#	}
# system.perspective.openPopup(
#		popupId, viewPath, params=params, position=position,
#		showCloseIcon=False, draggable=draggable, resizable=resizable, 
#		modal=modal, overlayDismiss=True, viewportBound=True
#	)

def iFrameNavigate(self, payload):
	"""
	
	"""
	try:
		pageId = self.view.params.pageId
	except:
		pageId = ""
	system.perspective.print("%s"%(payload))
	params =  payload["params"]
	target = payload["target"]
	breadcrumb = payload.get("breadcrumb")
	if pageId == "":
		system.perspective.navigate(view = target, params= params)
	else:
		system.perspective.sendMessage('iFrameNav', payload=payload, scope='page', pageId=pageId)

def _nav(view, params, breadcrumb= None):
	if breadcrumb is None:
		breadcrumb = ""
	system.perspective.sendMessage('updateBreadcrumb', {'target':breadcrumb}, scope="page")
	system.perspective.sendMessage('updateNavSelection', {'breadcrumb':breadcrumb}, scope="page")
	
	system.perspective.navigate(view=view,params=params['zoom'])

def openL4AlarmPopup(srcPath):
	""" Use this to determine the UDT or navigation path
		srcPath: string ex: prov:Ignition_SATB1_IO1_default:/tag:Datahall 1/Development/UT HS1/Alarms/Vacuum Fault Interrupter_VF-1 Tripped:/alm:Alarm
	"""
	def adjustObjName(objName):
		""" Manually correct the varying udt to view
		"""
		if "AnalogIn" in objName:
			objName = "AnalogIn"
		elif "SEL" in objName:
			objName = "SEL-751"
		elif "LV Feeder" in objName:
			objName = "LV Feeder"
		return objName
	
	objName = ""
	tagPath = ""
	
	parts = srcPath.split(':/')
	prov_part = parts[0].split(':')[1]  # Extract after "prov:"
	tag_part = parts[1].split(':')[1]   # Extract after "tag:"	
	
	constrPath = ""
	for p in tag_part.split("/"):
		constrPath+= (p+"/")
	#	print constrPath
		tConf= system.tag.getConfiguration("[%s]%s"%(prov_part, constrPath), False)
		if tConf[0]["tagType"].toString() == "UdtInstance":
			objName = adjustObjName(str(tConf[0]["typeId"]).split("/")[-1])
			tagpath = tConf[0]["path"].toString()
	
	return {"objectName":objName, "tagPath":tagpath}