/*
	BLT+A menu class ver0.01 alpha 01
	copyright BLT+A
		http://www.blt-a.com/
		info@blt-a.com
*/

function Menu( name ){
	
	this.name = name;
	this.size;
	this.sPosition;
	this.div;
	this.attache;
	this.cssClass;
	this.bgcolor;
	
	this.childDiv;
	this.childBgcolor;
	
	this.sRect;
	this.eRect;
	this.count;
	
	this.menues = new Array();
	
}

var eventobj = null;

function Menu_member(){
	
	var z = 100;
	
	var M  = Menu;
	var MP = M.prototype;
	
	MP.addMenuItem = function( str, link ) {
		var ePosition = this.sPosition.copy();
		ePosition.add( new Point2D( this.div.x, this.div.y * ( this.menues.length + this.attache ) ) );
		this.menues[this.menues.length] = ( new MenuItem( 'menu' + this.name + this.menues.length, this.menues.length, str, link, this.cssClass, this.size, this.bgcolor, this.sPosition, ePosition, z-- ) );
	};
	MP.addChildItem = function( str, link ) {
		var sPosition = this.childDiv.copy();
		sPosition.add( this.sPosition );
		
		var ePosition = sPosition.copy();
		ePosition.add( new Point2D( this.div.x, this.div.y * ( this.menues.length + this.attache ) ) );
		
		this.menues[this.menues.length] = ( new MenuItem( 'menu' + this.name + this.menues.length, this.menues.length, str, link, this.cssClass, this.size, this.childBgcolor, this.sPosition, ePosition, z-- ) );
	};
	
	MP.output = function()  {
		for( var i=0; i<this.menues.length; i++ ){
			this.menues[i].output();
		}
		this.sRect.x += this.sPosition.x;
		this.eRect.x += this.sPosition.x + this.size.x;
		
		if( this.div.y < 0 ) {
			this.sRect.y += this.sPosition.y + this.div.y * this.menues.length;
			this.eRect.y += this.sPosition.y + this.size.y;
		} else {
			this.sRect.y += this.sPosition.y;
			this.eRect.y += this.sPosition.y + this.div.y * this.menues.length;
		}
	};
	
	MP.show = function() {
		if( eventobj ) {
			if( eventobj.name == this.name ){
				
				return false;
				
			} else {
				//alert( eventobj.name );
				for( var i=0; i<eventobj.menues.length; i++ ){
					eventobj.menues[i].qhide();
				}
				eventobj = null;
			}
		}
		
		eventobj = this;
		if( C.DOM || C.IE ){
			document.onmousemove = this.mousemove;
		} else {
			window.captureEvents( Event.MOUSEMOVE )
			window.onmousemove = this.mousemove;
		}
		
		for( var i=0; i<this.menues.length; i++ ){
			this.menues[i].show();
			this.menues[i].setMover( this.menues[i].getPosition(), this.menues[i].ePosition , 25, 200  );
			this.menues[i].move();
		}
	};
	
	MP.mousemove = function( nn ){
		if( !eventobj ) return false;
		
		var cx,cy;
		
		if( C.IE ){
			cx = event.clientX;
			cy = event.clientY;
		} else {
			cx = nn.pageX;
			cy = nn.pageY;
		}
		
		if( ( ( eventobj.sRect.x < cx )&&( eventobj.eRect.x > cx ) )
		   && ( eventobj.sRect.y < cy )&&( eventobj.eRect.y > cy ) ) {
			
		} else {
			if( C.DOM || C.IE ) {
				document.onmousemove = null;
			} else {
				window.onmousemove = null;
			}
			
			if( eventobj.count ) {
				for( var i=0; i<eventobj.menues.length; i++ ){
					eventobj.menues[i].qhide();
				}
				eventobj = null;
			} else {
				eventobj.hide();
			}
		}
	};
	
	MP.hide = function() {
		eventobj.count = this.menues.length;
		for( var i=0; i<this.menues.length; i++ ){
			
			this.menues[i].setMover( this.menues[i].getPosition(), this.menues[i].sPosition, 25, -200  );
			//this.menues[i].move();
			this.menues[i].hide();
		}
	};
	
}
Menu_member();

function MenuItem( name, count, str, link, cssClass, size, bgcolor, sPosition, ePosition, z ){
	
	this.name = name;
	this.count = count;
	this.str = str;
	this.link = link;
	
	this.cssClass = cssClass;
	this.size = size;
	this.bgcolor = bgcolor;
	this.sPosition = sPosition;
	this.ePosition = ePosition;
	this.z = z;
	
	this.menues = new Array();
	this.element;
	this.mover;
	menuItemesMover[this.z] = null;
}

function MenuItem_member() {
	
	menuItemesMover = new Array();
	
	
	var I  = MenuItem;
	var IP = I.prototype;
	
	IP.output = function() {
		
		var t = "";
		t += '<div id="' + this.name + '" class="d">\n';
		t += '	<table border="0" cellpadding="0" cellspacing="0" width="' + this.size.x + '" height="' + this.size.y + '">\n';
		t += '		<tr align="center">\n';
		t += '			<td bgcolor="' + this.bgcolor + '"><a class="' + this.cssClass + '" href="' + this.link + '">' + this.str + '</a></td>\n';
		t += '		</tr>\n';
		t += '	</table>\n';
		t += '</div>\n';
		document.write( t );
		
		this.element = new Element( this.name );
		
		this.element.setPosition( this.sPosition );
		this.element.setPositionZ( this.z );
	};
	
	IP.setMover = function( sPosition, ePosition, divCount, edging ) {
		
		this.mover = new Mover( sPosition, ePosition, divCount, edging );
	};
	
	IP.getPosition = function() {
		
		return this.element.getPosition();
		
	};
	
	IP.show = function() {
		this.element.setVisibility( true );
	};
	
	IP.hide = function() {
		if( eventobj ) {
			if( this.mover.count() ){
				this.element.setPosition( this.mover.position() );
				if( C.WIN ) {
					menuItemesMover[this.z] = setTimeout( function($o){ return function(){ $o.hide(); } }(this), 10 );
				} else{
					menuItemesMover[this.z] = setTimeout( eventobj.name + ".menues[" + this.count + "].hide();", 10 );
				}
			} else {
				this.element.setVisibility( false );
				eventobj.count--;
				if( eventobj.count < 1 ) {
					eventobj = null;
				}
			}
		}
	};
	
	IP.qhide = function() {
		this.element.setVisibility( false );
		this.element.setPosition( this.sPosition );
	};
	
	IP.move = function() {
		if( eventobj ) {
			if( this.mover.count() ){
				this.element.setPosition( this.mover.position() );
				if( C.WIN ) {
					setTimeout( function($o){ return function(){ $o.move(); } }(this) , 10 );
				} else {
					menuItemesMover[this.z] = setTimeout( eventobj.name + ".menues[" + this.count + "].move();", 10 );
				}
			}
		}
	};
	
	
}
MenuItem_member();