/** 
 * meltmocha sliding drawers
 * version 0.1.0
 * Author: Nick Crohn (nick@meltmedia.com)
 **/

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

meltmedia.widget.Drawer.prototype = {
    // Main Class name for flyout
    DRAWER_CLASS: "mm-drawer",

    inTransition: false,
    
    toString: function() {
	return "object meltmocha Drawer";
    }
};

meltmedia.widget.Drawer.prototype.init = function(el, userConfig) {
    el = (document.getElementById(el)) ? document.getElementById(el) : el;

    this.getEl = function() {
	return el;
    };

    // Copy the userConfig properties to ourself
    this.copyProperties(userConfig);

    // Parse the contents and store the new drawer objects
    this.parseDrawer();

    // Activate the drawers
    this.activate();
};

meltmedia.widget.Drawer.prototype.initEvents = function() {
    this.listen("mouseover", this.show);
};

meltmedia.widget.Drawer.prototype.copyProperties = function(o) {
    for ( var key in o ) {
	this[key] = o[key];
    }
};

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

meltmedia.widget.Drawer.prototype.parseDrawer = function() {
    // Create our container array
    this.drawers = [];

    // Grab all anchors
    var anchors = this.getEl().getElementsByTagName("A");
    for ( var i=0; i<anchors.length; i++ ) {
	if(anchors.item(i).className) {
	    if(anchors.item(i).className.indexOf(this.calloutLinkId) > -1) {
		anchors.item(i).id = "drawer-link-" + this.drawers.length;
		var c = this.getContent(anchors.item(i).href);
		var h = c.style.height;
		h = h.substring(0, h.length-2);
		if ( anchors.item(i).className.indexOf("closed") > -1 ) {
		    c.style.height = "0px";
		}

		this.drawers.push({
			link: anchors.item(i),
			content: c,
			height: h
		    });

	    }
	}
    }

    // Queue them all and start the animations
    if(this.startMode == "cycle") {
	for(var key in this.drawers) {
	    if(typeof this.drawers[key] == "object") {
		if(this.drawers[key].content.className.indexOf("open") > -1 &&
		   this.drawers[key].content.className.indexOf("start") > -1) {
		    if(this.drawers.length-1 == key) {
			this.direction = "asc";
			this.cycle(key);
		    } else {
			this.direction = "desc";
			this.cycle(key);
		    }
		}
	    }
	}
    } else if (this.startMode == "all") {
	var scope = this;
	var timeoutCallback = function() {
	    scope.closeAll();
	};
	for(var key in this.drawers) {
	    setTimeout(timeoutCallback, this.delay);
	}
    }
};

meltmedia.widget.Drawer.prototype.cycle = function(id) {
    var scope = this;
    var timeoutCallback = function() {
	scope.open(scope.next);
    };

    switch (this.direction) {
    case "asc":
	if(this.drawers[id-1]) {
	    this.next = this.drawers[id-1];
	    setTimeout(timeoutCallback, this.delay);
	} else {
	    this.direction = null;
	}
	break;
    case "desc":
	id = parseInt(id);
	if(this.drawers[id+1]) {
	    this.next = this.drawers[id+1];
	    setTimeout(timeoutCallback, this.delay);
	} else {
	    this.direction = null;
	}
	break;
    }
};

meltmedia.widget.Drawer.prototype.activate = function() {
    for ( var key in this.drawers ) {
	this.listen(this.drawers[key].link, "mouseover", this.rollOver);
	this.listen(this.drawers[key].link, "mousemove", this.rollOver);
	this.listen(this.drawers[key].link, "click", this.click);
    }
};

meltmedia.widget.Drawer.prototype.getContent = function(link) {
   /// var id = link.split("?")[1].split("=")[1];
   var id = link.split("#")[1];
    return document.getElementById(id);
};

meltmedia.widget.Drawer.prototype.getTransitioning = function(transition) {
    for ( var key in this.drawers ) {
	if ( this.drawers[key].transitioning ) {
	    switch (transition) {
	    case "opening":
	        if (this.drawers[key].link.className.indexOf("closed") > -1) return this.drawers[key];
   	        break;
	    case "closing":
	        if (this.drawers[key].link.className.indexOf("open") > -1) return this.drawers[key];
	        break;
	    default:
	        return true;
	        break;
	    }
	}
    }
    return false;
};

meltmedia.widget.Drawer.prototype.rollOver = function(ev) {
    var t = YAHOO.util.Event.getTarget(ev);
    var id = t.id.split("-").pop();
    var d = this.drawers[id];    
    this.open(d);
};

meltmedia.widget.Drawer.prototype.open = function(d) {
    // Check and make sure we aren't already open
    if ( d.content.className.indexOf("closed") > -1 && !this.inTransition) {
	// Close all drawers first
	this.close();

	// Open the new drawer
	d.transitioning = true;
	this.removeClass(d.content, "closed");
	this.addClass(d.content, "open");
	// Open animation
	this.animate(d.content, { to: d.height }, {
		onComplete: function(ev) {
		    var dx = this.getTransitioning("opening");
		    this.removeClass(dx.link, "closed");
		    this.addClass(dx.link, "open");
		    dx.transitioning = false;

		    if(!this.getTransitioning()) this.inTransition = false;

		    if(this.startMode == "cycle") {
			this.cycle(dx.link.id.split("-").pop());
		    } else if (this.startMode == "all") {
			// Do Nothing for now
		    }
		}
	    });
    }
};

meltmedia.widget.Drawer.prototype.close = function() {
    for ( var key in this.drawers ) {
	if ( typeof this.drawers[key] != "function" ) {
	    if ( this.drawers[key].link.className.indexOf("open") > -1 && !this.inTransition) {
		this.drawers[key].transitioning = true;
		this.removeClass(this.drawers[key].content, "open");

		// Close Animation
		this.animate(this.drawers[key].content, { to: 0 }, {
			onComplete: function(ev) {
			    var dx = this.getTransitioning("closing");
			    this.removeClass(dx.link, "open");
			    this.addClass(dx.link, "closed");
			    this.addClass(dx.content, "closed");
			    dx.transitioning = false;

			    if(!this.getTransitioning()) this.inTransition = false;
			}
		    });
	    }
	}
    }
};

meltmedia.widget.Drawer.prototype.closeAll = function() {
    for ( var key in this.drawers ) {
	if ( typeof this.drawers[key] != "function" ) {
	    if ( this.drawers[key].link.className.indexOf("open") > -1) {
		this.drawers[key].transitioning = true;
		this.removeClass(this.drawers[key].content, "open");

		// Close Animation
		this.animate(this.drawers[key].content, { to: 0 }, {
			onComplete: function(ev) {
			    var dx = this.getTransitioning("closing");
			    this.removeClass(dx.link, "open");
			    this.addClass(dx.link, "closed");
			    this.addClass(dx.content, "closed");
			    dx.transitioning = false;

			    if(!this.getTransitioning()) this.inTransition = false;
			}
		    });
	    }
	}
    }
};

meltmedia.widget.Drawer.prototype.click = function(ev) {
    YAHOO.util.Event.preventDefault(ev);
};

meltmedia.widget.Drawer.prototype.addClass = function(el, className) {
    YAHOO.util.Dom.addClass(el, className);
};

meltmedia.widget.Drawer.prototype.removeClass = function(el, className) {
    YAHOO.util.Dom.removeClass(el, className);
};

meltmedia.widget.Drawer.prototype.animate = function(el, param, callback) {
    var config = {
	height: param
    };

    var anim = new YAHOO.util.Anim(el, config, .75, YAHOO.util.Easing.easeInOut);
    for( var key in callback ) {
	anim[key].subscribe(callback[key], this, true);
    }
    if(!this.inTransition) this.inTransition = true;
    anim.animate();

};

