var fMenuUp;

fMenuUp = false;

L_MinimizeTip_Text = "Minimizar";
L_ExpandTip_Text = "Expandir";

var m_fMouseDown = false;	// mouse down flag
var m_fMouseMoved = false;	// mouse moved flag
var m_cxOffset = 0;			// x offset of mouse from left of drag part
var m_cyOffset = 0;			// y offset of mouse from top of drag part
var m_prtSrc = null;		// ref to source of the part drag
var m_prtDrag = null;		// ref to drag part
var m_zoneLast = null;		// last active zone
var m_idxInsert = 0;		// position at which to insert dropped part

// This function resets any table attributes
// which may have been lost during browser reload.
function CleanUpTables()
{ 	
	document.all.OuttermostContentBodyTable.width = "40px";	 // 40px is an arbitrary number
	document.all.OuttermostContentBodyTable.width = "100%";
}

function Document_OnMouseDown()
{
	if (window.event.srcElement.className == 'LayoutWebPart')
	{
		m_fMouseDown = true;

		m_cxOffset = window.event.clientX - GetAbsoluteLeft(window.event.srcElement);
		m_cyOffset = window.event.clientY - GetAbsoluteTop(window.event.srcElement);

		m_prtSrc = GetPartElement(window.event.srcElement);
		m_prtDrag = m_prtSrc.cloneNode(true);

		m_prtDrag.style.position = 'absolute';
		m_prtDrag.style.filter = 'alpha(opacity=50)';
		m_prtDrag.style.left = (window.event.x - m_cxOffset) + 'px';
		m_prtDrag.style.top = (window.event.y - m_cyOffset) + 'px';
		m_prtDrag.style.width = m_prtSrc.offsetWidth;

		document.body.insertAdjacentElement('beforeEnd', m_prtDrag);

		divMouse.setCapture();
	}
}

function DivMouse_OnMouseMove()
{
	if (m_fMouseDown)
	{
		var zoneTarget;

		m_fMouseMoved = true;
		
		m_prtDrag.style.left = window.event.x - m_cxOffset + 'px';
		m_prtDrag.style.top = window.event.y - m_cyOffset + 'px';

		zoneTarget = GetZoneFromPoint(window.event.x, window.event.y, m_prtDrag, tblIBeam);

		if (FIsZone(zoneTarget))
		{
			// We are over a zone
			// If the class name is just "Zone" this is a new zone
			// and we need to switch the last drop zone
			if (m_zoneLast)
			{
				SetAsDropTarget(m_zoneLast, false);
			}

			SetAsDropTarget(zoneTarget, true);
			
			PositionIBeam(window.event.x, window.event.y, tblIBeam, zoneTarget);
		}
		else
		{
			// Not over a zone at all. Reset last zone and clear it
			if (m_zoneLast)
			{
				SetAsDropTarget(m_zoneLast, false);
			}
			tblIBeam.style.display = 'none';
		}
	}        
}

function DivMouse_OnMouseUp()
{
	if (m_fMouseDown)
	{
		if (m_fMouseMoved)
		{
			var zoneTarget;
			var prtTemp;

			zoneTarget = GetZoneFromPoint(window.event.x, window.event.y, m_prtDrag, tblIBeam);
			if (zoneTarget)
			{
				switch (m_idxInsert)
				{
				case -1:
					{
					zoneTarget.insertAdjacentElement('afterBegin', m_prtSrc.removeNode(true));
					break;
					}
				case zoneTarget.children.length:
					{
						// inserting part at the end of the zone
						prtTemp = zoneTarget.children(zoneTarget.children.length - 1);

						// check to make sure the source and target are not the same part
						if (prtTemp != m_prtSrc)
						{
							prtTemp.insertAdjacentElement('afterEnd', m_prtSrc.removeNode(true));
						}
						break;
					}
				default:
					{
						// inserting part before indexed child
						prtTemp = zoneTarget.children(m_idxInsert);

						// check to make sure the source and target are not the same
						if (prtTemp != m_prtSrc)
						{
							prtTemp.insertAdjacentElement('beforeBegin', m_prtSrc.removeNode(true));
						}
					}
				}
				saveToServer.partLayout.value = '<Dashboard>';
				AddPartsInfoForSave(TopPane, "TopPane");
				AddPartsInfoForSave(LeftPane, "LeftPane");
				AddPartsInfoForSave(ContentPane, "ContentPane");
				AddPartsInfoForSave(RightPane, "RightPane");
				AddPartsInfoForSave(BottomPane, "BottomPane");
				AddPartsInfoForSave(DeletedPane, "DeletedPane");
				saveToServer.partLayout.value += '</Dashboard>';
			}

			tblIBeam.style.display = 'none';

		}
		// Grabar si no es de la zona de delete
		m_prtDrag.style.position = '';
		m_prtDrag.removeNode(true);
		m_prtDrag = null;

		if (m_zoneLast)
		{
			SetAsDropTarget(m_zoneLast, false);
		}

		divMouse.releaseCapture();
		m_fMouseDown = false;
		m_fMouseMoved = false;
				saveToServer.submit();
	}
}

function AddPartsInfoForSave(node, zone)
{
	var nIndex;
	var partCount = node.children.length;
	for (nIndex = 0; nIndex < partCount; nIndex++)
	{
		saveToServer.partLayout.value += 
			'<WebPart>' +
			'  <WebPartID>' + node.children[nIndex].id.substr(0) + '</WebPartID>' +
			'  <Zone>' + zone + '</Zone>' +
			'  <PartOrder>' + nIndex + '</PartOrder>' +
			'</WebPart>';
	}
}

// Positions the IBeam element at the right insertion point
function PositionIBeam(x, y, elemIBeam, zoneTarget)
{
	var nParts;
	var prtFirst;
	var prtTemp;
	var idxPart;

	nParts = zoneTarget.children.length;
	if (nParts > 0)
	{
		// get the first part in the zone
		prtFirst = zoneTarget.children(0);

		// set the ibeam width to the part width
		elemIBeam.style.width = prtFirst.offsetWidth + 1

		// get the part before which we should display the ibeam
		for(m_idxInsert = 0; m_idxInsert < nParts; m_idxInsert++)
			{
				prtTemp = zoneTarget.children(m_idxInsert);
				if (y <= (GetAbsoluteTop(prtTemp) + ((prtTemp.offsetHeight + (parseInt(prtTemp.cellSpacing) * 2)) / 2)))
				{
					break;
				}
			}

		// if the part index is beyond the last part,
		// move the ibeam to after the last part
		if (m_idxInsert >= nParts)
		{
			m_idxInsert = nParts;
			prtTemp = zoneTarget.children(m_idxInsert - 1);
			elemIBeam.style.top = String(((GetAbsoluteTop(prtTemp) + 
				(prtTemp.offsetHeight + (parseInt(prtTemp.cellSpacing) * 2))) - 
				(elemIBeam.offsetHeight / 2))) + 'px';
			elemIBeam.style.left = String(GetAbsoluteLeft(prtTemp)) + 'px';
		}
		else
		{
			prtTemp = zoneTarget.children(m_idxInsert);
			elemIBeam.style.top = String((GetAbsoluteTop(prtTemp) - 
				(elemIBeam.offsetHeight / 2) + 1)) + 'px';
			elemIBeam.style.left = String(GetAbsoluteLeft(prtTemp)) + 'px'
		}

		// show it
		elemIBeam.style.display = 'inline';
	}
	else
	{
		elemIBeam.style.display = 'none';
		m_idxInsert = -1;
	}
}

// Navigates up from an element until it finds the element
// of type LayoutWebPartFrame (the whole table representing the WebPart)
function GetPartElement(subElem)
{
	var partFrame = subElem
	while (partFrame.className != 'LayoutWebPartFrame')
	{
		partFrame = partFrame.parentElement;
	}
	return partFrame;
}

// Gets real Top value with respect to client area
function GetAbsoluteTop(elem)
{
	var topPosition = 0;

	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		topPosition += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return topPosition;
}

// Get real Left value with respect to client area
function GetAbsoluteLeft(elem)
{
	var leftPosition = 0;
		
	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		leftPosition += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return leftPosition;
}

// Returns true if the given element is one of our zones.
function FIsZone(elem)
{
	var fRet = false;
	var strClass;
	var ich;
	
	if (elem)
	{
		strClass = elem.className;
		ich = strClass.indexOf(' ');
		if (ich != -1)
		{
			strClass = strClass.substr(0, ich);
		}
		
		fRet = strClass == 'LayoutZoneTop' || 
				strClass == 'LayoutZoneLeft' || 
				strClass == 'LayoutZoneCenter' || 
				strClass == 'LayoutZoneRight' || 
				strClass == 'LayoutZoneBottom';
	}

	return fRet;
}

// Sets or clears elem as the drop target.
function SetAsDropTarget(elem, fSet)
{
	var strClass;
	var ich;
		
	if (fSet)
	{
		strClass = elem.className;
		if (strClass.indexOf(' LayoutDropZone') == -1)
		{
			elem.className = strClass + ' LayoutDropZone';
			m_zoneLast = elem;
		}
	}
	else
	{
		strClass = elem.className;
		ich = strClass.indexOf(' LayoutDropZone');
		if (ich != -1)
		{
			elem.className = strClass.substr(0, ich);
			m_zoneLast = null;
		}
	}
}


// Gets the zone underneath the x,y or returns Nothing if not over a zone
function GetZoneFromPoint(x, y, prtDrag, elemIBeam)
{
	// This is totally hoaky. Hopefully someone on DHTML talk can
	// give me a better idea. The problem is that elementFromPoint will
	// return the top-most element under the point, which is the part
	// being dragged, not the zone underneath it. Setting zIndex to -1
	// temporarily seems to work, but also seems like a lot of overhead.

	var prtDragZIndexOld = prtDrag.style.zIndex;
	var elemIBeamZIndexOld = elemIBeam.style.zIndex;

	prtDrag.style.zIndex = -1;
	elemIBeam.style.zIndex = -1;
		
	var zone = document.elementFromPoint(x, y);
	prtDrag.style.zIndex = prtDragZIndexOld;
	elemIBeam.style.zIndex = elemIBeamZIndexOld;

	// Now the element we just got might be another part sitting in
	// the zone, so check to see if the class name is "LayoutWebPart". If it
	// is then go up the parent chain until we get its zone

	if ((zone.className == 'LayoutWebPart') || (zone.className == 'LayoutWebPartFrame'))
	{
		while ((!FIsZone(zone)) && 
				(zone.className != 'LayoutDropZone') &&
				(zone.tagName != 'BODY'))
		{
			zone = zone.parentElement;
		}
	}

	// Last check is that we might be over the body or something
	// else that is not a zone at all. If the current return pointer
	// is not of className = "Zone" or "LayoutDropZone" then return Nothing
	if ((!FIsZone(zone)) && (zone.className != 'LayoutDropZone'))
	{
		zone = null;
	}
	return zone;
}
			
function MinimizarRestaurar(partElem)
{
	var varProp;
	// Determine the new value for the FrameState property.
	if ( partElem == null )
	{
		return;
	}
	if (partElem.style.display == "none")
	{
		partElem.style.display = "";
		document.activeElement.children.item(0).src = "images/ig_tblminus.gif";
		document.activeElement.children.item(0).title = L_MinimizeTip_Text;
	}
	else
	{
		partElem.style.display = "none";
		document.activeElement.children.item(0).src = "images/ig_tblplus.gif";
		document.activeElement.children.item(0).title = L_ExpandTip_Text;
	}
}

function MinimizarRestaurar2(partElem,imgMin,imgMax)
{
	var varProp;
	// Determine the new value for the FrameState property.
	if (partElem.style.display == "none")
	{
		partElem.style.display = "";
		document.activeElement.children.item(0).src = imgMin;
		document.activeElement.children.item(0).title = L_MinimizeTip_Text;
	}
	else
	{
		partElem.style.display = "none";
		document.activeElement.children.item(0).src = imgMax;
		document.activeElement.children.item(0).title = L_ExpandTip_Text;
	}
}

function OcultarSup(obj,obj2)
{
	var objBtn = document.getElementById(obj);
	var objLat = document.getElementById('TableBanner');
	var objOWC = document.getElementById(obj2);	
	var objFC  = document.getElementById('nvMenu_div');	
	var alto = 100;
	var sw;	
	
	if (typeof objLat != "undefined" || objLat != null) alto = objLat.clientHeight;		
	
	if (objBtn.value == 'Mostrar'){			
		sw = 1;
		objBtn.value = 'Ocultar';
				
		if (typeof objOWC != "undefined" || objOWC != null){
			objOWC.style.height = Number(objOWC.Alto);
			objOWC.style.width = Number(objOWC.Ancho);
		}
	}
	else{
		sw = 0;
		objBtn.value = 'Mostrar';
		
		if (typeof objOWC != "undefined" || objOWC != null){		
			if (objOWC.Ancho == 1) objOWC.Ancho = objOWC.offsetWidth;
			if (objOWC.Alto == 1)objOWC.Alto = objOWC.offsetHeight;
			if (objFC != null) objOWC.style.width = objOWC.offsetWidth + Number(objFC.clientWidth) - 15;
			objOWC.style.height = objOWC.offsetHeight + alto;
		}
	}	
	
	btnMaxMin('btnImgMaxMin','Images/hide.gif','Images/show.gif','Maximizar', 'Minimizar', sw);
	OcultarMostrarSup('TableBanner','imgME','Images/hide.gif','Images/show.gif','Esconder', 'Mostrar', sw);
	
	var objLateral = document.getElementById('TablaMenu');

	if (objLateral != null) OcultarMostrarLat('TablaMenu','imgLat','Images/FlechasIzq.gif','Images/FlechasDer.gif','Esconder', 'Mostrar', sw);
	
	return false;
}

function btnMaxMin(imageId,image1Url, image2Url, alt1, alt2,Accion)
{	
	var itemImage = document.getElementById(imageId);	
	
	if (Accion == 1){		
		itemImage.src = image1Url;
		itemImage.alt = alt1;
		}
	else{		
      	itemImage.src = image2Url;
		itemImage.alt = alt2;		
		}
}

function OcultarMostrarSup(itemId,imageId,image1Url, image2Url, alt1, alt2,Accion)
{
	var item = document.getElementById(itemId);
	var itemImage = document.getElementById(imageId);
	if (Accion == 1){
		item.style.display = 'inline';
		itemImage.src = image1Url;
		itemImage.alt = alt1;
		}
	else{
		item.style.display = 'none';
      	itemImage.src = image2Url;
		itemImage.alt = alt2;
		}
}	

function OcultarMostrarLat(itemId,imageId,image1Url, image2Url, alt1, alt2,Accion)
{
	var item = document.getElementById(itemId);
	var itemImage = document.getElementById(imageId);
	if (Accion == 1){		
		item.style.display = 'inline';
		itemImage.src = image1Url;
		itemImage.alt = alt1;
		}
	else{
		item.style.display = 'none';
      	itemImage.src = image2Url;
		itemImage.alt = alt2;
		}
}

function MostrarDiv(ControlDiv,strRuta){	
	if (strRuta == null){
		PantallaFull=dhtmlmodal.open('PantallaFull', 'div', ControlDiv, 'Sitio Web Externo', 'width=' + getAncho() + ',height=' + getAlto() + ',center=1,resize=0,scrolling=1');
		PantallaFull.moveTo('middle','middle');
		ResizeVentana();
		var ctrl = document.getElementById(ControlDiv);
		ctrl.style.display='';
	}else{
		PantallaFull=dhtmlmodal.open('PantallaFull', 'iframe', strRuta, 'Reporte', 'width=' + getAncho() + ',height=' + getAlto() + ',center=1,resize=0,scrolling=1');
		PantallaFull.moveTo('middle','middle');
		ResizeVentana();		
	}
	
}


function ResizeVentana(){		
	var win = document.getElementById('PantallaFull');
	if (win != null){	
		win.isResize(false);
		win.setSize(getAncho(),getAlto());
		win.moveTo('middle','middle');				
	}
}

function getAlto(){return document.body.offsetHeight - (document.body.offsetHeight * 0.06)}
function getAncho(){return document.body.offsetWidth - (document.body.offsetWidth * 0.02)}

function Acoplar(a){
//alert(a);
	var c = document.getElementById("DIV_"+a); 
	var i = document.getElementById("IMG_"+a); 
	if (c.style.display == 'none'){
		c.style.display = 'inline';				
		i.src="images/uparrows_white.gif"
		}
	else{
		c.style.display = 'none';
		i.src="images/downarrows_white.gif"
		}
}

		function ToggleItem(itemId,imageId,image1Url, image2Url, alt1, alt2)
		{
			var item = document.getElementById(itemId);
			var itemImage = document.getElementById(imageId);
			if (item.style.display == 'none'){
				item.style.display = 'inline';
				itemImage.src = image1Url;
				itemImage.alt = alt1;
				}
			else{
				item.style.display = 'none';
      			itemImage.src = image2Url;
				itemImage.alt = alt2;
				}
		}
		
		function RefreshPanelWithValue(controlId, strValue){
			//var theForm = document.Default;
			var theForm = document.forms(0);
			var theControl = document.getElementById(controlId);
			theControl.value = strValue;
			__doPostBack('theForm','');
			popUp.close();
		} 

		function RefreshOriginalForm(){
			//var theForm = document.Default;
			var theForm = document.forms(0);
			__doPostBack('theForm','');
			popUp.close();
		} 

		function OpenPopUp(url){
			popUp = window.open(url, 'test', 'width=580,height=300,left=0,top=0,scrollbars=yes');
			popUp.moveTo(10,10);
		}

		function DisableEmptyTabs()
		{
			var objObjeto = document.all("Banner_PanelTabs_empty");
			if ( objObjeto != null )
			{
				objObjeto.style.display = "none";
			}
		}	
