var _ddmbar1x=false,_ddmbar1y=false,_ddmbar1=false;
var _ddmbasepath=false;
var _default_ddm_bar=false;


var _currentDropdown=false;
var _onbar=false,_onitem=false;

var DropdownMenu=Class.create();
function ddmWatch(){
	if(!_onbar && !_onitem){
	if(_currentDropdown)
	_currentDropdown.style.display='none';	
}
setTimeout('ddmWatch()',300);
}

DropdownMenu.prototype={
    initialize:function(menuId,containerId,basePath,defaultBar,struct){
	var ddm_conf=struct;
        this.container=$(containerId);
        this.container.className='spddmenuholder';
        this.basePath=basePath;
        var _div=document.createElement('div');
        _div.className='ddmenu';
        this.container.appendChild(_div);
        this._ph=_div;
        for(var i=0;i<ddm_conf.length;i++){
            if(i>0){
			    var sep=document.createElement('div');
			    sep.className='seperator';
			    _div.appendChild(sep);
            }
            var div=document.createElement('div');
            div.className='bar';
            div.unselectable='on';
            if(defaultBar && defaultBar-1==i) {
                div.className='barOn';
                _default_ddm_bar=div;
            }
            if(!ddm_conf[i].TextMode){
                var img=document.createElement('img');
                img.src=basePath+'menu/img/'+ddm_conf[i].ImageNormal;
                //img.style.align='absmiddle';
                img.style.marginTop='5px';
                div.appendChild(img);
            }else{
                var span=document.createElement('span');
                span.appendChild(document.createTextNode(ddm_conf[i].ItemText));
                div.appendChild(span);
            }
            /*
            div.onmouseover=function(){this.style.backgroundColor='#859cb8';}
	        div.onmouseout=function(){this.style.backgroundColor='#20497c';}
            */
            div.ItemLink=ddm_conf[i].ItemLink;
            div.Children=ddm_conf[i].Children;
		    _div.appendChild(div);
            var me=this;
            Event.observe(div,'mouseover',function(e){_onbar=true;me.barOver(e);Event.stop(e)});
            div.onmouseout=function(e){_onbar=false;if(this!=_default_ddm_bar){this.className='bar';}}
            div.onclick=function(e){
                var _url=this.ItemLink;
                if(_url) window.location.href=me.basePath+_url;
            }
            if(i==0) _ddmbar1=div;

        }
		setTimeout('ddmWatch()',300);
    },
    barOver:function(e){
        var src=e.srcElement || e.target;
        while(src.tagName!='DIV') src=src.parentNode;
        src.className='barOn';
        this._createDropdown(src);
        if(_currentDropdown) _currentDropdown.style.display='none';
        if(src.Dropdown){            
            src.Dropdown.style.display='';
            _currentDropdown=src.Dropdown;
        }

    },
    _createDropdown:function(src){    
        var _children=src.Children;
		var me=this;
        if(_children && _children.length>0){
            var position=Position.cumulativeOffset(src);
            if(!src.Dropdown){
                var _dropdown=document.createElement('div');
                _dropdown.className='dropdown';
                _dropdown.unselectable='on';
                this.container.appendChild(_dropdown);
                for(var i=0;i<_children.length;i++){
                    var _item=document.createElement('div');
                    _item.className='menuitem';
                    _item.onmouseover=function(e){this.className='menuitemOn';}
                    _item.onmouseout=function(e){this.className='menuitem';}
                    var _span=document.createElement('span');
                    _span.appendChild(document.createTextNode(_children[i].ItemText));
                    _item.appendChild(_span);
                    _dropdown.appendChild(_item);
                    _item.Link=_children[i].ItemLink;
                    _item.Target=_children[i].Target;
                    _item.onclick=function(e){
                        var _url=this.Link;
                        if(this.Target.length>0) window.open(me.basePath+this.Link);
                        else window.location.href=me.basePath+this.Link;
                    }
                }

                src.Dropdown=_dropdown;
        		Event.observe(_dropdown,'mouseout',function(e){me.mouseout(e,_dropdown);});
			    _dropdown.onmouseover=function(){_onitem=true;}
            }
            src.Dropdown.style.left=position[0]+'px';
            src.Dropdown.style.top=position[1]+23+'px';
        }        
    },   
    mouseout:function(e,target){
        var checkTarget=Event.isMouseLeaveOrEnter(e,target);
        if(checkTarget){
		_onitem=false;
        }
    },
 
    _ph:false,
    basePath:false
}
