$package('difc.navigator',
    $class('NavigatorEvent').$extends(ejs.events.Event).$define(
       $public({
            construct : function(){
                this.$super.construct(arguments[0]);
            }
        }),
       $static({
            OVER : 'over',
            OUT : 'out'
        })
    )
);

$package('difc.navigator',
    $class('NavigatorItem').$implements(ejs.events.IEventDispatcher).$define(
        $public({
            id:'',
            host:null,
            mgr:null,
            overStyle:'',
            outStyle:'',
            division:null,
            construct:function(item){
                this.id = item.id;
                this.host = (item.host || {offsetLeft:0, offsetTop:0, offsetHeight:0});
                this.mgr = item.mgr;
                this.overStyle = item.overStyle;
                this.outStyle = item.outStyle;
                this.division = item.division;

                this.addEventListener(difc.navigator.NavigatorEvent.OVER, this.onOverHandler);
                this.addEventListener(difc.navigator.NavigatorEvent.OUT, this.onOutHandler);
                ejs.addEventListener(this.host, 'mouseover', function(self){
                    return function(){
                        self.dispatchEvent(new difc.navigator.NavigatorEvent(difc.navigator.NavigatorEvent.OVER));
                    };
                }(this));
                ejs.addEventListener(this.host, 'mouseout', function(self){
                    return function(){
                        self.dispatchEvent(new difc.navigator.NavigatorEvent(difc.navigator.NavigatorEvent.OUT));
                    };
                }(this));
            },
            onOverHandler:function(event){
                if(this.mgr && this.mgr.currentItem){
                    this.mgr.currentItem.dispatchEvent(new difc.navigator.NavigatorEvent(difc.navigator.NavigatorEvent.OUT));
                }
                this.host.className = this.overStyle;
                if(this.division){
//                    alert(ejs.browser.version);
                    this.division.className = 'pn-navigator-item show';
                    var coordinate = this.host.getBoundingClientRect(),
                        _scrollLeft = ejs.isStandardMode?document.documentElement.scrollLeft:document.body.scrollLeft,
                        _scrollTop = ejs.isStandardMode?document.documentElement.scrollTop:document.body.scrollTop,
                        parentCoordinate = this.host.parentNode.getBoundingClientRect(),
                        offsetHeight = this.host.offsetHeight, 
                        edge = parentCoordinate.left + this.host.parentNode.offsetWidth,
                        _offsetLeft = (ejs.browser.msie)?-2:0, 
                        _offsetTop = (ejs.browser.msie)?-2:0,
                        divWidth = this.division.offsetWidth;
                    this.division.style.left = [_scrollLeft + coordinate.left + _offsetLeft, 'px'].join(''); + 'px';
                    this.division.style.top = [_scrollTop + coordinate.top + this.host.offsetHeight + _offsetTop, 'px'].join('');
					var childCoordinate = this.division.getBoundingClientRect();
                    divWidth = childCoordinate.left + divWidth;
                    if(edge < divWidth){
                        this.division.style.left = (childCoordinate.left - (divWidth - edge)) + 'px';
					}
                }
                this.mgr && (this.mgr.currentItem = this);
            },
            onOutHandler:function(event){
                this.host.className = this.outStyle;
                this.division && (this.division.className = 'pn-navigator-item hide');
            }
        })        
    )        
);

$package('difc.navigator',
    $class('NavigatorManager').$define(
        $public({
            currentItem : null,
            items : {},
            construct : function(/*array*/items){
                ejs.utils.ArrayUtil.foreach(items, this.addItem, this);
            },
            addItem : function(/*difc.navigator.NavigatorItem*/item){
                if(item instanceof difc.navigator.NavigatorItem){
                    item.mgr = this;
                    this.items[item.id] = item;
                }
            },
            removeItem : function(/*string*/id){
                if(this.items[id]){
                    delete this.items[id];
                }
            },
            dispatch : function(/*string*/id, /*string*/eventType){
                if(this.items[id]){
                    this.items[id].dispatchEvent(new difc.navigator.NavigatorEvent(eventType));
                }
            }
        })    
    )      
);

