// OLC 2.5 - new function
// Display the '+' or '-' icon according to the visibility of the specified object
// spath would be used if refObj is shown, hpath otherwise
function updateDisplay(obj, refObj, spath, hpath) {
	obj.src = (refObj.style.display == '')? spath : hpath;
}

// OLC 2.5 - drag and drop function
// Source code was modified for OLC.
//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.
var browser = new Browser();		// See script.js

// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 0;

function portalCellDragStart(event, id)
{
	var el;
	var x, y;

	dragObj.elNode = document.getElementById(id);
	dragObj.ObjID = dragObj.elNode.id.substring(3);     // Holds the ID of the portal cell being dragged
	dragObj.detectWinScroll = true;
	dragObj.detectPCReposition = true;
	dragObj.lastDragID = "";
	dragObj.relocateMarker = document.getElementById('RelocateMarker').style;

	// Get cursor position with respect to the page.
	if (browser.ie) {
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.ns) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
	dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

	// Update element's z-index.
	//dragObj.elNode.style.zIndex = ++dragObj.zIndex;

	// Capture mousemove and mouseup events on the page.
	if (browser.ie) {
		document.attachEvent("onmousemove", portalCellDragGo);
		document.attachEvent("onmouseup",   portalCellDragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.ns) {
		document.addEventListener("mousemove", portalCellDragGo,   true);
		document.addEventListener("mouseup",   portalCellDragStop, true);
		event.preventDefault();
	}

	// Change the style of the cursor to 'move'
	dragObj.relocateMarker.cursor = 'move';

	// Change Opacity of the portal cell going to be moved
	if (browser.ie) {
		dragObj.elNode.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=50,Style=0)";
	}
	if (browser.ns) {
		dragObj.elNode.style.MozOpacity = 0.5;
	}

	// Change the relocate marker to visible and same size as the dragObj
	dragObj.relocateMarker.left = x - (parseInt(dragObj.relocateMarker.width) / 2);
	dragObj.relocateMarker.top = y - (parseInt(dragObj.relocateMarker.height) / 2);
	dragObj.relocateMarker.display="";

}

function portalCellDragGo(event)
{
	var x, y;

	// Get cursor position with respect to the page.
	if (browser.ie) {
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.ns) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Code to handle cursor out of browser window case
	// ...

	// Move relocate marker by the same amount the cursor has moved.
	dragObj.relocateMarker.left = x - (parseInt(dragObj.relocateMarker.width) / 2);
	dragObj.relocateMarker.top = y - (parseInt(dragObj.relocateMarker.height) / 2);

	// Move drag element by the same amount the cursor has moved.
	//dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	//dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

	// Scroll browser window and refresh reposition bar if necessary
	if (dragObj.detectWinScroll) {
		scrollWindow(event);
		if (dragObj.detectPCReposition) detectPCReposition(event);
	}

	if (browser.ie) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.ns) event.preventDefault();
}

function portalCellDragStop(event)
{
	// Stop capturing mousemove and mouseup events.
	if (browser.ie) {
		document.detachEvent("onmousemove", portalCellDragGo);
		document.detachEvent("onmouseup",   portalCellDragStop);
	}
	if (browser.ns) {
		document.removeEventListener("mousemove", portalCellDragGo,   true);
		document.removeEventListener("mouseup",   portalCellDragStop, true);
	}


	// Hide reposition bar
	if (dragObj.lastDragID != "") document.getElementById('PCReposition_'+dragObj.lastDragID).style.visibility="hidden";

	dragObj.lastDragID = "";

	// Change the style of the cursor back to normal
	dragObj.elNode.style.cursor = '';

	// Change Opacity
	if (browser.ie) {
		dragObj.elNode.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=100,Style=0)";
	}
	if (browser.ns) {
		dragObj.elNode.style.MozOpacity = 1.0;
	}

	// Hide relocation marker
	dragObj.relocateMarker.display = "none";

	// Move the portal cell
	widgetID = theWidgetID(event);

	if (widgetID!="0") {
		moveWidget(widgetID);
		/* For future use - save position
				var col1,col2,col3;
				col1=col1Serv.join(",");	
				col2=col2Serv.join(",");	
				col3=col3Serv.join(",");	
				if (col1==""||col1==null) col1="NULL";
				if (col2==""||col2==null) col2="NULL";
				if (col3==""||col3==null) col3="NULL";
				window.frames["widget_reload"].location.href="/core/savelayout.psp?set%3Auser/nss/"+cobrand+"/pages/"+pageID+"/services1="+col1+"&set%3Auser/nss/"+cobrand+"/pages/"+pageID+"/services2="+col2+"&set%3Auser/nss/"+cobrand+"/pages/"+pageID+"/services3="+col3;
		*/
	}

}


// Helper function - Scroll browser window
function scrollWindow(event)
{
	dragObj.detectWinScroll = false;

	if (browser.ie) {
		cX = window.event.clientX;
		cY = window.event.clientY;
	}
	if (browser.ns) {
		cX = event.clientX;
		cY = event.clientY;
	}

	b = document.body;
	browserHeight = b.clientHeight;
	browserWidth = b.clientWidth;
	yScroll = browserHeight/2 - (browserHeight/3);

	if ( (cY > browserHeight - yScroll && b.scrollTop + browserHeight < b.scrollHeight) || (cY < yScroll + 50 && b.scrollTop != 0) ) {
		yInt=25;
		if (cY < yScroll + 50) yInt=-25; 
		scrollBy(0,yInt);
	}

	dragObj.detectWinScroll = true;
}


// Helper function - Detect and show / hide reposition marker
function detectPCReposition(event)
{
	dragObj.detectPCReposition = false;

	widgetID = theWidgetID(event);

	if (widgetID!=0)
	{
		if (dragObj.lastDragID != "") document.getElementById('PCReposition_'+dragObj.lastDragID).style.visibility="hidden";
		col = whichColumn(widgetID);

		document.getElementById('PCReposition_'+widgetID).style.visibility="visible";
		dragObj.lastDragID = widgetID;
	}

	setTimeout("dragObj.detectPCReposition=true", 10);

	return;
}

// Detect the portal cell being dragged and return its ID.
function theWidgetID(event)
{
	// Determine which portal cell do the mouse is on top of (ie)
	if (browser.ie)
	{
		elementHTML = window.event.srcElement;

		while (true) {
	 		if (elementHTML.id.indexOf('PC_') != -1) {
				widgetID = elementHTML.id.substring(3); break;
			} else if (elementHTML.id.indexOf('PortalPageColumn')!=-1) {
				widgetID = "0"; break;
			} else if (elementHTML.id.indexOf('PortalPage')!=-1) {
				widgetID = "0"; break;
			} else if (elementHTML.id.indexOf('RelocateMarker') != -1) {
				widgetID = "0"; break;
			} else {
				elementHTML = elementHTML.parentElement;
			}
		}
	}

	// Determine which portal cell do the mouse is on top of (ns)
	if (browser.ns)
	{
		elementHTML = event.target;

		while (elementHTML && (elementHTML.nodeType != 1 || elementHTML.id.indexOf('PC_') == -1))
		{
			elementHTML = elementHTML.parentNode;
			if (elementHTML.nodeType == 9) {
				widgetID="0";
				return widgetID;
			}
		}

		if (elementHTML.id.indexOf('PC_')!=-1) {
			widgetID = elementHTML.id.substring(3);
		} else if (elementHTML.nodeType == 9) {
			widgetID = 0;
		}
	}

	// Make no effect if the portal cell is itself
	if (widgetID == dragObj.ObjID) widgetID = '0';

	return widgetID;
}

// Helper function - Determine the column which the id belongs to
function whichColumn(id)
{
	for (i=0; i<PortalColumnDef.length; i++) {
		for (j=0; j<PortalColumnDef[i].length; j++) {
				if (PortalColumnDef[i][j] == id) return i;
		}
	}

	return 1;
}

// Helper function - Determine the position of the portal cell within the column
function whichPosition(id)
{
	col = whichColumn(id);
	for (i=0;i<PortalColumnDef[col].length;i++) {
		if (PortalColumnDef[col][i] == id) {pos = i; break;};
	}
	return pos;
}

// Helper function - Move the portal cell below the specified
function moveWidget(moveBelowThisWidget)
{
	col = whichColumn(moveBelowThisWidget);

	if (browser.ie) {
		html = document.getElementById('PC_' + dragObj.ObjID).outerHTML;
		document.getElementById('PC_' + dragObj.ObjID).outerHTML="";
		document.getElementById('PC_' + moveBelowThisWidget).insertAdjacentHTML("AfterEnd",html)
	}

	if (browser.ns) {
		theTarget = document.getElementById('PC_' + moveBelowThisWidget);
		var theSource = document.getElementById('PC_' + dragObj.ObjID);
		if (theTarget.nextSibling){
			theTarget.parentNode.insertBefore(theSource, theTarget.nextSibling);
		} else {
			theTarget.parentNode.appendChild(theSource);
		}
	}
}
// OLC 2.5 - drag and drop function ends
