/** 
 * meltmocha flyout menu
 * version 0.1.1
 * Author: Nick Crohn (nick@meltmedia.com)
 **/

meltmedia.widget.FlyOut = function(el, userConfig) {
    if(el) {
	this.init(el, userConfig);
    } else {
	// Do Nothing
    }
};

meltmedia.widget.FlyOut.prototype = {
    // Flyout Widget Version
    version: "0.1.1",
    // Main Class name for flyout
    FLYOUT_CLASS: "mm-flyout-menu",
    
    toString: function() {
	return "mm-flyout menu Object [ " + this.firstChild.id + " " + this.tagName + " ]";
    }
};

meltmedia.widget.FlyOut.prototype.init = function(el, userConfig) {
    // Create our new container
    var div = document.createElement("DIV");
    try {
	el.parentNode.appendChild(div);
	el.parentNode.removeChild(el);
	div.appendChild(el);
	el.style.display = "block";
	div.style.zIndex = "9000";
    } catch (e) {
	// Do Nothing
    }

    this.getDiv = function() {
	return div;
    };

    // Set the users configuration
    for (var prop in userConfig) {
	this[prop] = userConfig[prop];
    }

    // Extend the flyout's properties from the element
    this.copyProperties(div);

    // Add the listeners to the event
    this.initEvents();

    // Add the entity classes
    this.addClass(this.FLYOUT_CLASS);
    this.hide();
};

meltmedia.widget.FlyOut.prototype.initEvents = function() {
    this.listen("mouseover", this.hover);
    this.listen("mouseout", this.hide);
};

meltmedia.widget.FlyOut.prototype.addClass = function(cn) {
    YAHOO.util.Dom.addClass(this.getDiv(), cn);
};

meltmedia.widget.FlyOut.prototype.copyProperties = function(o) {
    for (var key in o) {
	// Do this to ensure we don't override existing properties
	if(!this[key]) {
	    this[key] = o[key];
	}
    }
};

meltmedia.widget.FlyOut.prototype.copy = function(el) {
    var node = document.createElement(el.nodeName);
    node.innerHTML = el.innerHTML;
    this.copyProperties(node);
    return node;
};

meltmedia.widget.FlyOut.prototype.previousSibling = function() {
    var sibling = this.getDiv();
    var previous = this;

    var parent = this.getDiv().parentNode;
    for (var i=0; i<parent.childNodes.length; i++) {
	if(parent.childNodes.item(i).nodeType == 1) {
	    if(parent.childNodes.item(i).id == this.id) sibling = previous;
	    previous = parent.childNodes.item(i);
	}
    }
    return sibling;
};

meltmedia.widget.FlyOut.prototype.listen = function(type, call) {
    YAHOO.util.Event.addListener(this.previousSibling(), type, call, this, true);
};

meltmedia.widget.FlyOut.prototype.hide = function() {
    this.getDiv().style.display = "none";
    if(navigator.userAgent.indexOf("MSIE") > -1) this.getDiv().parentNode.style.marginBottom = "-1px";
};

meltmedia.widget.FlyOut.prototype.hover = function(ev) {
    var t = YAHOO.util.Event.getTarget(ev);

    // Check for any other menus matching our FLYOUT_CLASS and hide them
    var menus = YAHOO.util.Dom.getElementsByClassName(this.FLYOUT_CLASS);
    for (var key in menus) {
	if(menus[key].style) {
	    menus[key].style.display = "none";
	}
    }

    this.show();
    this.position(YAHOO.util.Dom.getXY(t));
    
    YAHOO.util.Event.addListener(this.getDiv(), "mouseover", this.flyout, this, true);
    YAHOO.util.Event.addListener(this.getDiv(), "mouseout", this.hide, this, true);
};

meltmedia.widget.FlyOut.prototype.flyout = function(ev) {
    this.show();
};

meltmedia.widget.FlyOut.prototype.show = function() {
    this.getDiv().style.display = "block";
    if(navigator.userAgent.indexOf("MSIE") > -1) this.getDiv().parentNode.style.marginBottom = "-5px";
};

meltmedia.widget.FlyOut.prototype.position = function(xy) {
    for( var key in xy ) {
	xy[key] += this.offset[key];
    }
    YAHOO.util.Dom.setXY(this.getDiv(), xy);
};