var ContextMenuVisible = false;
var CurrentDesignSelector = false;
function DesignSelectorMenuAction( menu, caption, style, onexecute ) 
{
	this.menu = menu;
	
	this._domObj = null;
	this.tdIcon = null;
	this.tdCaption = null;
	
	this.caption = caption;
	this.style = style;
	this.onexecute = onexecute;
	this.icon = arguments[4];
	
	
	var self = this;
	
	this.draw = function( menuDom ) {
		if ( !this._domObj ) {
			this._domObj = CrossDom.createElement( "TR" );
			this._domObj.style.cursor = "hand";
			this._domObj.style.height = "20px";
			this._domObj.style.color =  "ButtonText";
			this._domObj.style.fontSize = "12px";
			this._domObj.unselectable = "on";
			
			CrossDom2.appendChild( menuDom, this._domObj );
			
			this.tdCaption = CrossDom.createElement( "TD", {valign:"middle"} );
			
			
			this.tdCaption.onmousemove = this.menu.cancelHide;
			this.tdCaption.onmouseout = this.menu.resetHide;
			this.tdCaption.contextAction = this;
			this.tdCaption.unselectable = "on";
			this.tdCaption.style.cssText = this.style;
			CrossDom2.appendChild( this._domObj, this.tdCaption );
			
			this.tdCaption.innerHTML = this.caption;
			
			this._domObj.onclick = this.execute;
			this._domObj.contextAction = this;
		}
		if ( this.menu.origstyle == this.style ) {
			this.tdCaption.style.border = "2px solid green";
		}
		else {
			this.tdCaption.style.border = "";
		}
	}
	
	this.execute = function() {
		self.menu.hide();
		if ( self.menu.origstyle != self.style && menu.trackChange ) {
			track_change(self.menu.designfield);
		}
		self.menu.designfield.value = self.style;
		return false;
	}
	
	this.lolite = function() {
		this._domObj.style.backgroundColor = "Menu";
		this._domObj.style.color =  "ButtonText";
		if ( this.tdCaption ) this.tdCaption.style.color =  "ButtonText";
	}
	
	this.hilite = function() {
		this._domObj.style.backgroundColor = "Highlight";
		this._domObj.style.color =  "HighlightText";
		if ( this.tdCaption ) this.tdCaption.style.color =  "HighlightText";
	}
}

function DesignSelectorMenu()
{
	this._domObj = null;
	this._table = null;
	this.items = new Array();
	this.timer = null;
	this.designfield = "";
	this.origstyle = "";
	this.trackChange = false;
	
	var self = this;
	
	this.addItem = function( caption, style ) {
		this.items.push( new DesignSelectorMenuAction( this, caption, style ) );
	}
	
	this.show = function( obj, designField, origStyle ) {
		this.designfield = designField;
		this.origstyle = designField.value;
		if ( CurrentDesignSelector ) CurrentDesignSelector.hide();
		if ( !this._domObj ) {
			this._domObj = CrossDom.createElement( "DIV" );
			this._domObj.unselectable = "on";
			this._domObj.style.width = "200px";
			this._domObj.style.position = "absolute";
			this._domObj.style.backgroundColor = "#FFFFFF";
			this._domObj.style.zIndex = "10000";
			
			this._domObj.style.borderLeft = "1px solid ThreeDLightShadow";
			this._domObj.style.borderTop = "1px solid ThreeDLightShadow";
			this._domObj.style.borderRight = "1px solid ThreeDDarkShadow";
			this._domObj.style.borderBottom = "1px solid ThreeDDarkShadow";
			
			var table = CrossDom.createElement( "TABLE" );
			table.setAttribute("border", "0");
			table.setAttribute("cellSpacing", "0");
			table.setAttribute("cellPadding", "0");
			table.style.width = "100%";
			CrossDom.appendChild( this._domObj, table );
			
			this._table = CrossDom.createElement( "TBODY" );
			CrossDom.appendChild( table, this._table );
		}
		
		for ( var i=0; i < this.items.length; i++ ) {
			this.items[i].draw( this._table );
		}

		this._domObj.style.left = CrossDom.absX(obj)+10;
		this._domObj.style.top = CrossDom.absY(obj)+10;
		this._domObj.style.display = "";
		CrossDom.appendChild( document.body, this._domObj );
		ContextMenuVisible = true;
		CurrentDesignSelector = this;
		
		this.resetHide();
	}
	
	this.clear = function() {
		if ( this._domObj ) {
			while( this._table.childNodes.length > 0 ) {
				this._table.childNodes[0].removeNode( true );
			}
			this.items.length = 0;
			
		}
	}
	
	this.cancelHide = function(e) {
		if ( window.event || e ) {
			var evt = CrossDom.getEvent(e);
			
			if ( evt.target.contextAction ) {
				for ( var i=0; i < self.items.length; i++ ) {
					self.items[i].lolite();
				}
				evt.target.contextAction.hilite();
			}
		}
		
		if ( self.timer ) clearTimeout( self.timer );
	}
	
	this.resetHide = function(e) {
		self.cancelHide();
		self.timer = setTimeout( self.hide, 1500 );
	}
	
	this.hide = function() {
		self._domObj.style.display = "none";
		ContextMenuVisible = false;
	}
	
}

var DesignSelector = new DesignSelectorMenu();
