/*
* 简易导航插件
*/ 
(function($){
	var easy = this.easyNavigator = function(){
		this.easyNavigator.apply(this, arguments);
	};
	easy.prototype = new baseNavigator();
	
	$.extend(true, easy.prototype, {
		constructor : baseNavigator,
		$super : baseNavigator.prototype,
		$parent : baseNavigator,
		direction : 'down', //up or down
		easyNavigator : function ()
		{
			this.addListener('mouseover', this.mouse_over);
			this.addListener('mouseout', this.mouse_out);
		},	//end function
		handle : function (/*array*/ $element)
		{
			var data = {};
			var $plugin = this;
			$element.each(function(){
				var profile = JSON.parse($(this).attr('profile'));
				profile.plugin = $plugin;
				profile.element = this;
				data[profile.id] = profile;
				$(this).attr('class', profile.out);
				$('#'+this.id+'_child')
					.css('display','none')
					.css('position','absolute')
					.appendTo(document.body);

				this.onmouseover = function(){ $plugin.dispatchEvent('mouseover', profile.id); return false; };
				this.onmouseout = function(){ $plugin.dispatchEvent('mouseout', profile.id); return false; };
			});
			this.data(data);
		},	//end function
		__selected__ : null,
		selected : function (/*string*/ $dom_id, /*bool*/$dispatched)
		{
			if(arguments.length>0 && $dom_id)
			{
				this.__selected__ = $dom_id;
				if($dispatched)
					this.dispatchEvent('mouseover', $dom_id);
				return this;
			}
			return this.__selected__;
		},	//end function
		mouse_over : function (/*string*/ $dom_id)
		{
			var profile = this.data()[$dom_id];
			if(profile){				
				$(profile.element).attr('class', profile.over);
				if(profile.fid)
				{
					this.dispatchEvent('mouseover', profile.fid);
				}			
				if(profile.cid && $('#'+profile.cid).css('display')=='none')
				{
					var pos = $(profile.element).offset();
					var edge = $(this.owner()).offset().left+$(this.owner()).width();
					var child_size = pos.left+$('#'+profile.cid).width();
					
					$('#'+profile.cid)
						.css('left', child_size>edge?pos.left-(child_size-edge):pos.left)
						.css('top', this.direction=='up'?
							pos.top-$('#'+profile.cid).height():
							pos.top+$(profile.element).height());
						
					$('#'+profile.cid).css('display', 'block');
				}
				
			}
		},	//end function
		mouse_out : function (/*string*/ $dom_id)
		{
			if(this.selected()==$dom_id)
				return;
			var profile = this.data()[$dom_id];
			if(profile){
				$(profile.element).attr('class', profile.out);
				if(profile.fid)
				{
					this.dispatchEvent('mouseout', profile.fid);
				}
				if(profile.cid && $('#'+profile.cid).css('display')!='none')
				{
					$('#'+profile.cid).css('display', 'none');
				}
			}
		}	//end function
	} );

})(jQuery);
