var objTimer;
var objDrop;
var objMenuLast;

// Detect Opera browser
IsOpera = /opera/i.test(navigator.userAgent);

// Detect if we're in IE
IsIE = ( /msie/i.test(navigator.userAgent) && !IsOpera );

// Detect if it's IE5
IsIE5 = ( IsIE && /msie 5\.0/i.test(navigator.userAgent) );

// Variable for id counter
IDCount = 0;

// Create iFrame tags that sit underneath each menu span, so that select boxes don't show through the menus
function CreateMenuIFrame(element) {
	var f = null;
	element = element || window.self.document.body;
	if (IsIE && !IsIE5) {
		var filter = 'filter:progid:DXImageTransform.Microsoft.alpha(style=0,opacity=0);';
		var id = "SpanIFrame_" + (++IDCount);
		element.insertAdjacentHTML
			('beforeEnd', '<iframe id="' + id + '" scroll="no" frameborder="0" ' +
			 'style="z-index:0;position:absolute;visibility:hidden;' + filter +
			 'border:0;top:0;left:0;width:0;height:0;" ' +
			 'src="javascript:false;"></iframe>');
		f = window.self.document.getElementById(id);
	}
	return f;
};

// Sets the dimensions of an iFrame underneath a menu span
function SetUpIFrame(f, x, y, w, h) {
	if (f) {
		var s = f.style;
		(typeof x != "undefined") && (s.left = x + "px");
		(typeof y != "undefined") && (s.top = y + "px");
		(typeof w != "undefined") && (s.width = w + "px");
		(typeof h != "undefined") && (s.height = h + "px");
		s.visibility = "inherit";
	}
};


//shows menu
function appear(objMenu, XOffset) {
	//remove any old timers	
	clearTimeout(objTimer);
		
	// if we're on a new menu then hide the last immediatly
	if (objMenu != objMenuLast) doDisappear();

	// reset the previously selected menu item's background color
	if (objMenuLast) objMenuLast.className = "navHeader";
	
	// change the background color
	objMenu.className = "navHeaderActive"
	objMenuLast = objMenu;

	// our drop down is the first child node of the menu object passed in
	objDrop = objMenu.childNodes[1];	
	
	// set reference to the navbar object
	objNavBar = document.all("navbarmain")
	
	//get the menu node
	if (objDrop) {
		if (!XOffset) XOffset = 0;
		// Make sure the drop down starts at the same left position as it's header menu
		objDrop.style.left = parseInt(findPosX(objMenu) + XOffset) + 'px';	//get left position
		// Make sure the drop down pops up right under the main nav bar
		objDrop.style.top = objNavBar.offsetTop + objNavBar.offsetHeight;
		// Set the minimum width of the drop down to be as wide as the menu header above it
		//if (objDrop.style.width < objMenu.offsetWidth) { objDrop.style.width = objMenu.offsetWidth; }
		objDrop.style.display = 'inline';
	
		// Create an IFrame under this drop-down, if not already made
		if (!objDrop._SubIFrame) { objDrop._SubIFrame = CreateMenuIFrame(objDrop); }
		objDrop._SubIFrame.style.zIndex = -1;

		// Get the drop-down menu's left and top values
		var subMenuBorderLeft, subMenuBorderTop;
		if (typeof objDrop.clientLeft != 'undefined') { // IE & Opera
			subMenuBorderLeft = objDrop.clientLeft;
			subMenuBorderTop = objDrop.clientTop;
		} else { // Mozilla
			subMenuBorderLeft = (objDrop.offsetWidth - objDrop.clientWidth) / 2;
			subMenuBorderTop = (objDrop.offsetHeight - objDrop.clientHeight) / 2;
		}

		// Setup the iFrame to the same dimensions as the drop-down menu
		SetUpIFrame(objDrop._SubIFrame, -subMenuBorderLeft, -subMenuBorderTop, objDrop.offsetWidth, objDrop.offsetHeight);
	}

}

//hides a menu
function disappear(objMenu) {
	objMenuLast = objMenu;
	objDrop = objMenu.childNodes[1];	
	//get the menu node
	objTimer = setTimeout('doDisappear();', 600);
}

//internal function for hiding menu
function doDisappear() {
	if (objMenuLast) objMenuLast.className = "navHeader";
	if (objDrop) objDrop.style.display = 'none';
}

//return screen X position
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

//return screen Y position
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


//Turn the textarea editor on for a piece of HTML content
function edittag(sTagName){
	//document.body.onselectstart="";
	document.all("divEditor_"+sTagName).style.display="block";
	//document.all("txtEditor_"+sTagName).value=document.all(sTagName).innerHTML;
	document.all(sTagName).style.display="none";
	document.all("btnEdit_"+sTagName).style.display="none";
	document.all("btnSaveCancel_"+sTagName).style.display="block";
}

//Cancel edits to a piece of HTML content
function canceledittag(sTagName){	
	document.all("divEditor_"+sTagName).style.display="none";
	document.all("divPreview_"+sTagName).style.display="none";
	document.all(sTagName).style.display="block";	
	document.all("btnSaveCancel_"+sTagName).style.display="none";
	document.all("btnEdit_"+sTagName).style.display="block";
}

function clearedits(sTagName) {
	var bConfirm = confirm("Are you sure you want to clear the content above?  (NOTE: if you clear it by accident, you can always hit cancel to revert to your previously saved changes.)");
	if (bConfirm==true) document.all("txtEditor_" + sTagName).value="";
	

}

//Save changes to edits of a piece of HTML content
function saveedittag(sTagName){
	try { 
	document.all(sTagName).innerHTML=document.all("txtEditor_"+sTagName).value; 
	canceledittag(sTagName);
	frmPostContent.pagehtml.value = document.all("txtEditor_"+sTagName).value;
	frmPostContent.submit();
	}
	catch(e) { if (e.number!=0) { alert("Your HTML may not be correctly formed; your page could not be saved."); } }
}

//Preview changes to a piece of HTML content
function previewtag(sTagName){
	document.all("divPreview_"+sTagName).style.display="none";
	try { document.all("divPreview_"+sTagName).innerHTML = document.all("txtEditor_"+sTagName).value 
		document.all("divPreview_"+sTagName).style.display="block";
}
	catch(e) { if (e.number!=0) { alert("Your HTML may not be correctly formed; the preview could not be generated."); } }
}


//Called from the list display code
function sortby(s_field_name){
	frmList.pagingmove.value='First Page';
	frmList.sortby.value=s_field_name;
	frmList.submit();
}

