var objPortletDragged = null, intPortletDraggedX, intPortletDraggedY;
var intPortletDragStartX, intPortletDragStartY;
var fnOldDocumentOnMouseDown = null, fnOldDocumentOnMouseUp = null, fnOldDocumentOnMouseMove = null;
var bAllowPortletRepositioning = false, bAllowPortletShowHide = false;
var strPortletInitString = "";
var divTrackMenu = Array(), divTrackActiveItem = Array();
var cmeMenuHideTimer = null;
var menuMac = navigator.userAgent.toLowerCase().indexOf("mac")!=-1;

// This function is called by the WYSIWYG editor to enable client-side editing of portlet controls.
// It may also be called by live-site pages (e.g. in a script block at the end of the HTML) to switch
// on live-site portal editing.
function cmeSetEditState(bAllowPortletEdits) {
	var objStyle;
	
	bAllowPortletRepositioning = bAllowPortletEdits;
	bAllowPortletShowHide = bAllowPortletEdits;
	
	if(bAllowPortletShowHide) {
		// Make portlet togglers visible
		// alert(document.styleSheets(0).href);
		/*
		objStyle = document.createElement("style");
		objStyle.setAttribute("type","text/css");
		objStyle.nodeValue = "a.cmeportletvisibilitytoggle { display: inline; }";
		//alert(document.childNodes(0).childNodes(0).tagName);
		document.childNodes(0).childNodes(0).appendChild(objStyle);
		*/
	}
}

// This function must be called at the end of an HTML page which uses dynamically editable portlets
// in order to restore the position/visibility of the portlets from a state string.
function cmePortletsInitialise(strInit) {
	var intIndex, arrPortlets, arrPortlet;
	var objPortlet;
	
	strPortletInitString = String(strInit).toString();
	arrPortlets = strPortletInitString.split(";");
	for(intIndex=0;intIndex<arrPortlets.length;intIndex++) {
		arrPortlet = arrPortlets[intIndex].split(":");
		if(arrPortlet.length==2) {
			objPortlet = document.getElementById(arrPortlet[0]);
			objPortlet.parentElement.removeChild(objPortlet);
			document.getElementById(arrPortlet[1]).appendChild(objPortlet);
		}
	}
}

// Store state information for the given portlet.
function cmePortletsPersist(objPortlet) {
	var intIndex, arrPortlets;
	
	arrPortlets = strPortletInitString.split(";");
	for(intIndex=0;intIndex<arrPortlets.length;intIndex++) {
		if(arrPortlets[intIndex].indexOf(objPortletDragged.id + ":")==0) {
			arrPortlets[intIndex] = objPortletDragged.id + ":" + objPortletDragged.parentElement.id;
			arrPortlets[intIndex] = "";
			strPortletInitString = arrPortlets.join(";").replace(";;","");
			break;
		}
	}
	strPortletInitString += ";" + objPortletDragged.id + ":" + objPortletDragged.parentElement.id;
}

function cmeTogglePortletVisibility(imgToggle, divPortletBody) {
	if(bAllowPortletShowHide) {
		if(divPortletBody.style.display=="none") divPortletBody.style.display = "";
		else divPortletBody.style.display = "none";
	}
}

function getPortletBlob(el) {
	if (el.className == "cmeportlettitle") return el.parentElement;
	else return el;
}

function cme_onmousedown() {
	if(bAllowPortletRepositioning) {
		el = getPortletBlob(window.event.srcElement)
		if (el.className == "cmeportletblob") {
			objPortletDragged = el;
			
			intPortletDragStartX = window.event.clientX;
			intPortletDragStartY = window.event.clientY;
			intPortletDraggedX = intPortletDragStartX - getPortletLeftPos(objPortletDragged);
			intPortletDraggedY = intPortletDragStartY - getPortletTopPos(objPortletDragged);
						
			window.event.returnValue = false;
			window.event.cancelBubble = true;
		} else objPortletDragged = null;
	}	
	if(fnOldDocumentOnMouseDown) fnOldDocumentOnMouseDown();
}

function cme_onmouseup() {
	var intIndex;
	var objDivs = document.getElementsByTagName("DIV");
	var objDiv, bMoved = false;
	
	if(objPortletDragged && bAllowPortletRepositioning) {
		objPortletDragged.style.left = "";
		objPortletDragged.style.top = "";
		objPortletDragged.style.position = "";
		objPortletDragged.style.filter = "";
		bMoved = window.event.clientX >= 0 && window.event.clientY >= 0
			&& (window.event.clientX!=intPortletDragStartX || window.event.clientY != intPortletDragStartY);
		if(bMoved) {
			for(intIndex=0;intIndex<objDivs.length;intIndex++) {
				objDiv = objDivs(intIndex);
				if(objDiv.className=="cmeportletcontainer" && objDiv.componentFromPoint(window.event.clientX, window.event.clientY)=="") {
					objPortletDragged.parentElement.removeChild(objPortletDragged);
					objDiv.appendChild(objPortletDragged);
					
					// Store to string for persistence later
					cmePortletsPersist(objPortletDragged, objDiv);
					
					objPortletDragged = null;
					return;
				}
			}
		}
		objPortletDragged = null;
	}
	
	if(fnOldDocumentOnMouseUp) fnOldDocumentOnMouseUp();
}

function cme_onmousemove() {
	if (objPortletDragged && bAllowPortletRepositioning) {
		bMoved = window.event.clientX >= 0 && window.event.clientY >= 0
			&& (window.event.clientX!=intPortletDragStartX || window.event.clientY != intPortletDragStartY);
		if(bMoved) {
			objPortletDragged.style.position = "relative";
			objPortletDragged.style.left = window.event.clientX - intPortletDraggedX;
			objPortletDragged.style.top = window.event.clientY - intPortletDraggedY;
			objPortletDragged.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=75)";
		}
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
	if(fnOldDocumentOnMouseMove) fnOldDocumentOnMouseDown();
}

function getPortletLeftPos(el) {
	return el.style.pixelLeft;
}

function getPortletTopPos(el) {
	return el.style.pixelTop;
}

function cmeMenuShow(divMenu, elementAlignTo, elementSelectItem, alignDirection, level, specialClassID) {
	var l=0, t=0, e = elementAlignTo;
    
	// Hide an existing menu if one is visible
	cmeMenuClearTimer();
	cmeMenuHide(level);
	elementSelectItem.className = "popupmenu2hover" + specialClassID;
    
	if((new String(divMenu.alreadyPositioned))!="done") {
		if(menuMac) {
			t = e.offsetParent.offsetParent.style.pixelTop;
			do {
				if(e.tagName.toLowerCase()=="div") {
					l += e.clientLeft-6;
				}
				if(e.tagName.toLowerCase()=="td") {				
					l += e.clientLeft;
					t += e.clientTop;
				}
				e = e.offsetParent;
			} while(e!=null)
			if(alignDirection==0) {
				// NOT YET IMP
			} else if(alignDirection==1) {
				// Fit menu to the right of the item elementAlignTo
				divMenu.style.left = l + elementAlignTo.offsetWidth;
				divMenu.style.top = t;
			} else if(alignDirection==2) {
                // Fit menu above the item elementAlignTo
				divMenu.style.left = l;
				divMenu.style.top = t + elementAlignTo.offsetHeight;
            } else {
				// Not yet implemented.
			}
			// Next line is a fudge to avoid having to specify a fixed width for the menu in pixels when the menu DIV is created, but yet
			// still have a right-aligned ">" symbol without menu line wrapping.
			// divMenu.childNodes(0).style.width = divMenu.childNodes(0).offsetWidth + 16;
			divMenu.all(0).style.width = divMenu.all(0).offsetWidth + 16;
		} else {
			do {
				l += e.offsetLeft;
				t += e.offsetTop;
				e = e.offsetParent;
			} while(e!=null)

			if(alignDirection==0) {
				// Fit menu below the item elementAlignTo
				divMenu.style.left = l;
				divMenu.style.top = t + elementAlignTo.offsetHeight + 1;
			} else if(alignDirection==1) {
				// Fit menu to the right of the item elementAlignTo
				divMenu.style.left = l + elementAlignTo.offsetWidth;
				divMenu.style.top = t;
			} else if(alignDirection==2) {
				// Fit menu below the item elementAlignTo
				divMenu.style.left = l;
				divMenu.style.top = t + elementAlignTo.offsetHeight;
            } else {
				// Not yet implemented: fit menu above or to one side of item elementAlignTo
			}
			// Next line(s) are a fudge to avoid having to specify a fixed width for the menu in pixels when the menu DIV is created, but yet
			// still have a right-aligned ">" symbol without menu line wrapping.
        
			// divMenu.firstChild.style.width = (divMenu.firstChild.offsetWidth + 16);
			/*
            if(divMenu.childNodes[0].tagName==undefined) {
				// Netscape
				// Why doesn't "firstChild" work in Netscape 7?
				divMenu.childNodes[1].style.width = divMenu.childNodes[1].offsetWidth+16;
			} else {
				// IE
				divMenu.childNodes[0].style.width = divMenu.childNodes[0].offsetWidth+16;
			}
            */
		}		
		divMenu.alreadyPositioned = "done";
	}

	// Show menu item and start tracking.
	elementSelectItem.menuSpecialClass = specialClassID;
	divTrackMenu[level] = divMenu;
	divTrackActiveItem[level] = elementSelectItem;
	divMenu.style.display = "";
}        

function cmeMenuClearTimer()
{
	if(cmeMenuHideTimer!=null) {
		clearTimeout(cmeMenuHideTimer);
		cmeMenuHideTimer = null;
	}
}

function cmeMenuHide(level)
{
	for(var i=level;i<divTrackMenu.length;i++) {
		if(divTrackMenu[i]!=null) {
			divTrackMenu[i].style.display = "none";
			divTrackActiveItem[i].className = "popupmenu2" + divTrackActiveItem[i].menuSpecialClass;
			divTrackMenu[i] = null;
			divTrackActiveItem[i] = null;
		} else break;
	}
}

function cmeAddBookmark()
{
	var url = document.location.href;
	var title = document.title;
	
    if (window.sidebar) window.sidebar.addPanel(title, url, "");
    else if(document.all) window.external.AddFavorite(url, title);
    else if(window.opera && window.print) return true;
}

// MacroMedia Dreamweaver support functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Date function
function writeDate() {
	var dtDate = new Date();
	var arrDays = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var arrMonths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	document.write(arrDays[dtDate.getDay()] + " " + dtDate.getDate() + " " + arrMonths[dtDate.getMonth()] + " " + dtDate.getFullYear());
}

// Hook events up to the HTML objects.
fnOldDocumentOnMouseDown = document.onmousedown;
fnOldDocumentOnMouseUp = document.onmouseup;
fnOldDocumentOnMouseMove = document.onmousemove;
document.onmousedown = cme_onmousedown;
document.onmouseup = cme_onmouseup;
document.onmousemove = cme_onmousemove;