
// ------------------------------------------------------------
// Dihedrals Webascent Enabler.
// Hook added to all pages that support the Webascent system.
// (c) 2007 by Dihedrals. All rights reserved. www.dihedrals.com
// ------------------------------------------------------------

// globals
var gDWAToolboxWindRef = null;
var gDWAToolboxLoc = '/_webascent/code/toolbox.htm';
//if ( DEV_OR_lIVE === 'development' ) {
//	gDWAToolboxLoc = '/_webascent/code/toolbox.htm?dev';
//}



// Create the cookie that keeps track of whether or not the Toolbox
// has already been opened or not.
// Cookie name, Cookie value, days valid (0 means session cookie).
if ( readCookie( 'isToolboxOpen?' ) === null ) {
	createCookie( 'isToolboxOpen?', 'false', 0 );
}


//-------------------------------------------------------------
// Have this window tell the Toolbox that its currently active.
// Toolbox then installs DWAEdit Elements into this page dynamically.
//-------------------------------------------------------------
window.onload = function() {
	// If this window has been reloaded, the gDWAToolboxWindRef var will be
	// set to null again. Try and see if the Toolbox is already open and avail,
	// if yes, then grab a reference to it.
	//alert( 'start of window.onload()' );
	var isTBOpen = readCookie( 'isToolboxOpen?' );
	//alert( 'after readCookie()' );
	if ( isTBOpen === 'true' && gDWAToolboxWindRef === null ) {
		// Toolbox is open -- grab a reference to it!
		openToolbox();
	}
	//noGo();
};

//-------------------------------------------------------------
// Tell the Toolbox to do any needed cleaning up from this Web Page.
//-------------------------------------------------------------
/*
window.onunload = function() {
	// If this window has been reloaded, the gDWAToolboxWindRef var will be
	// set to null again. Try and see if the Toolbox is already open and avail,
	// if yes, then grab a reference to it.
	var isTBOpen = readCookie( 'isToolboxOpen?' );
	if ( isTBOpen == 'true' && gDWAToolboxWindRef != null ) {
		// Toolbox is open -- grab a reference to it!
		//gDWAToolboxWindRef.gToolbox.unRegisterWebPage();
	}
};
*/

//-------------------------------------------------------------
// Trigger point for firing up the Dihedrals.com WebAscent online
// editing suite.
//-------------------------------------------------------------
function openDWAToolbox()
{
	
	/**
	 * Need to catch IE users here, to stop ugly errors, as we don't
	 * support IE (and never will).
	 */
	var IE = /*@cc_on!@*/ false;
	if ( IE ) {
		alert( "Sorry! The Webascent Toolbox doesn't support Internet Explorer. \nPlease use the latest version of Chrome, Safari or Firefox." );
		return false;
	}
	
	var isTBOpen = readCookie( 'isToolboxOpen?' );
	
	if ( isTBOpen === null ) {
		// This browser doesn't allow Cookies...
		alert( 'Not able to open the Webascent Toolbox, as this browser is not allowing Cookies to be used...' );
		// Maybe some browser sniffing stuff to show different help files on
		// enabling Javascript & cookies...
	}
	
	if ( isTBOpen == 'false' ) {
		openToolbox();
		gDWAToolboxWindRef.focus();
	} else {
		//
	}
	
	return false;
}


//-------------------------------------------------------------
// Called when this page has finished loading.
//-------------------------------------------------------------
function openToolbox()
{
	
	gDWAToolboxWindRef = window.open( gDWAToolboxLoc, 'DWAToolbox', 'left=3,top=1,width=580,height=410,menubar=0,status=1,,location=0,toolbar=0,resizable=1,scrollbars=1,alwaysRaised=1' );

}




//-------------------------------------------------------------
// Cookie management functions.
//-------------------------------------------------------------
function createCookie( name, value, days ) {
	var expires = "";
	if ( days ) {
		var date = new Date();
		date.setTime( date.getTime()+( days*24*60*60*1000 ) );
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie( name ) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for( var i=0; i < ca.length; i++ ) {
		var c = ca[i];
		while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
		if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
	}
	return null;
}



var SimpleContextMenu = {

	// private attributes
	_attachedElement : null,
	_menuElement : null,


	// public method. Sets up whole context menu stuff..
	setup : function () {

		if ( document.all && document.getElementById && !window.opera ) {
			SimpleContextMenu.IE = true;
		}

		if ( !document.all && document.getElementById && !window.opera ) {
			SimpleContextMenu.FF = true;
		}

		if ( document.all && document.getElementById && window.opera ) {
			SimpleContextMenu.OP = true;
		}

		if ( SimpleContextMenu.IE || SimpleContextMenu.FF ) {
			document.oncontextmenu = SimpleContextMenu._show;
			document.onclick = SimpleContextMenu._hide;
		}
        
        // debug -- Safari not reacting anymore...
        //alert( 'IE?: ' + SimpleContextMenu.IE + "\n" + 'FF?: ' + SimpleContextMenu.FF + "\n" + 'OP?: ' + SimpleContextMenu.OP );
        
	},


	// public method. Attaches context menus to specific class names
	attachNew : function ( attachtoThisElement, menuElement ) {

		SimpleContextMenu._attachedElement = attachtoThisElement;
		SimpleContextMenu._menuElement = menuElement;

	},

	// private method. Shows context menu
	_show : function (e) {
		
		SimpleContextMenu._hide();
		
		
		// Need to make sure that we're only handling a click on our Element Editor
		// button, and not just anywhere on the page...
		var t = null;
		var drawMenuFlag = false;
		
		if (SimpleContextMenu.IE) {  
			//SimpleContextMenu._attachedElement = event.srcElement;
			t = event.srcElement;
		} else {  
			t = e.target;
		} 
		
		//alert( 't.className: ' + trim( t.className ) );
		if ( trim( t.className ) == 'DWAElementEditLabel' ) {
			// Need to make sure that the ElEditButton that contains this label, is the
			// Button that is currently Selected.
			if ( trim( t.parentNode.className ) == 'DWAElementEditButtonSelected' ) {
				// things are cool.
				drawMenuFlag = true;
			}
		}
		if ( trim( t.className ) == 'DWAElementEditButtonSelected' ) {
			// things are cool.
			drawMenuFlag = true;
		}
		
		//alert( 'drawMenuFlag: ' + drawMenuFlag );
		if ( drawMenuFlag ) {
			var m = SimpleContextMenu._getMousePosition(e);
			var s = SimpleContextMenu._getScrollPosition(e);
			SimpleContextMenu._menuElement.style.left = m.x + s.x + 'px';
			SimpleContextMenu._menuElement.style.top = m.y + s.y + 'px';
			SimpleContextMenu._menuElement.style.display = 'block';
			
			
			if ( SimpleContextMenu.IE === true ) {
				//alert('Exploder');
				//console.info( 'Context Menu: Exploder' );
				event.cancelBubble = true;
			} else if ( SimpleContextMenu.FF === true ) {
				//alert('Firefox');
				//console.info( 'Context Menu: Firefox/Safari' );
				e.stopPropagation();
			} else {
				//alert('other');
				//console.info( 'Context Menu: Other browser' );
			}
			
			return false;		// stop the browser from displaying its context menu as well!
		} else {
			return true;		// allow the browser's normal context menu to be displayed...
		}
		
	},


	// private method. Hides context menu
	// OK.
	_hide : function () {

		if (SimpleContextMenu._menuElement) {
			SimpleContextMenu._menuElement.style.display = 'none';
		}

	},


	// private method. Returns mouse position
	// OK.
	_getMousePosition : function (e) {

		e = e ? e : window.event;
		var position = {
			'x' : e.clientX,
			'y' : e.clientY
		};

		return position;

	},


	// private method. Get document scroll position
	// OK.
	_getScrollPosition : function () {

		var x = 0;
		var y = 0;

		if( typeof( window.pageYOffset ) == 'number' ) {
			x = window.pageXOffset;
			y = window.pageYOffset;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}

		var position = {
			'x' : x,
			'y' : y
		};

		return position;

	}

};

// Trim() functions.
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function noGo() {
	if ( !document.getElementById('dwa') ) return;
	document.getElementById('dwa').addEventListener('focus',first,true);
}
function first() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a")[0].focus();
}






















