YAHOO.CMA.yah = {
    timer:null,
    data:null,
    init:function(el, o) {
        if (YAHOO.lang.isObject(el) == false) {
            throw "Missing Parameter: Element";
        }
        if (YAHOO.lang.isObject(o)) {
            this.data = o;
            if (YAHOO.lang.isObject(this.data.categories)) {
                var yahContent = createElement("DIV", {
                    "class":"musicology"
                    , "style":"margin-right:10px;"
                });
                el.innerHTML = "";
                this.buildHTML(yahContent, this.data.categories, 1);
                el.appendChild(yahContent);
                if (YAHOO.lang.isObject(this.window)) {
                    this.window.setBody(el.innerHTML);
                    this.window.render(document.body);
                    this.window.cfg.setProperty("xy", [
                        parseInt(this.window.cfg.getProperty("x")) + 2
                        , parseInt(this.window.cfg.getProperty("y")) - (parseInt($('yahMusic').offsetHeight)/2) + (parseInt($(this.window.oid).offsetHeight)/2)
                    ]);
                }
            } else {
                var compID = this.data.composerID;
                if (YAHOO.lang.isNumber(compID)) {
                    var imgLoading = createElement("img", {
                        "src":"/images/treeview-loading.gif"
                    })
                    el.innerHTML = "";
                    el.appendChild(imgLoading);
                    el.appendChild(document.createTextNode(" Loading ..."));
                    YAHOO.util.Connect.asyncRequest('GET', "/ajax/yah/"+compID+".json", {
                        success:function(o) {
                            try {
                                o.argument.o.categories = YAHOO.lang.JSON.parse(o.responseText);
                                this.init(o.argument.el, o.argument.o);
                            } catch(x) {
                                YAHOO.CMA.dMsg('error','Error!','Sorry, but there was a problem parsing this page',x);
                                throw "Bad Data: "+x;
                                return;
                            }
                        }
                        , failure:function(o) {
                            YAHOO.CMA.dMsg('error','Error!','An error has occured. Please try again. If the error persists please contact us. [Code: '+o.status+']');
                        }
                        , scope:this
                        , argument:{ "el":el, "o":o }
                    }, '');
                } else {
                    throw "Missing Parameter: Element";
                }
            }
        } else {
            throw "Missing Parameter: Object";
        }
    },
    buildHTML:function(el, o, lvl) {
        var yahCats = null;
        for (var key in o) {
            var cats = o[key];
            if (YAHOO.lang.isArray(cats)) {
                for (var i = 0 ; i < cats.length ; i++) {
                    if (YAHOO.lang.isNull(yahCats)) {
                        yahCats = createElement("UL", {
                            "class":"std"
                        });
                    }

                    var cat = cats[i];
                    if (YAHOO.lang.isObject(cat)) {
                        var yahCat = createElement("LI");
                        if (YAHOO.lang.isString(cat.title) && YAHOO.lang.isNumber(cat.id)) {
                            var link = null;
                            if (YAHOO.lang.isNumber(this.data.composerID) && YAHOO.lang.isNumber(this.data.selectedCategoryID)) {
                                link = createElement("A", {
                                    "href":"/composer/" + this.data.composerID + ".html#category" + cat.id
                                });
                                if (this.data.selectedCategoryID == cat.id) {
                                    yahCat.style.fontWeight = "bold";
                                }
                            } else {
                                link = createElement("A", {
                                    "href":"javascript:yahAdjustView('cat0_" + cat.id + "');"
                                });
                            }
                            link.appendChild(document.createTextNode(cat.title));
                            yahCat.appendChild(link);
                        } else if (YAHOO.lang.isString(cat.title)) {
                            yahCat.appendChild(document.createTextNode(cat.title));
                        }
                        if (YAHOO.lang.isObject(cat.categories) && lvl < 2) {
                            this.buildHTML(yahCat, cat, lvl+1);
                        }
                        yahCats.appendChild(yahCat);
                    }
                }
            }
        }
        if (YAHOO.lang.isObject(yahCats)) {
            el.appendChild(yahCats);
        }
    },
    show:function(oid, action, parentObj) {
        if (!YAHOO.lang.isObject(parentObj)) {
            parentObj = this;
        }
        $(oid).onmouseout = function() {
            parentObj.hide();
        };
        $(oid).onclick = function() {
            parentObj.hide('now');
        };
        if (!YAHOO.lang.isString(action)) { // prevent unintented info calls - start timer
            if (!YAHOO.lang.isNull(parentObj.timer)) {
                clearTimeout(parentObj.timer);
                parentObj.timer = null;
            }
            var sFunc = function() {
                parentObj.show(
                    YAHOO.lang.isString(oid) ? oid : null
                    , 'now'
                    , YAHOO.lang.isObject(parentObj) ? parentObj : null
            )};
            parentObj.timer = setTimeout(
                    sFunc
                    , 100
            );
        } else if (action == 'now') { // ok to make ajax request
            YAHOO.CMA.yah.window = new YAHOO.widget.Overlay('yahMusic', {
                context:[oid,'tr','tl']
                , visible:false
                , width:"400px"
                , zIndex:"1000"
            });
            YAHOO.CMA.yah.window.oid = oid;
            YAHOO.CMA.yah.window.setBody($('yahData').innerHTML);
            YAHOO.CMA.yah.window.render(document.body);
            YAHOO.CMA.yah.window.cfg.setProperty("xy", [
                parseInt(YAHOO.CMA.yah.window.cfg.getProperty("x")) + 2
                , parseInt(YAHOO.CMA.yah.window.cfg.getProperty("y")) - (parseInt($('yahMusic').offsetHeight)/2) + (parseInt($(YAHOO.CMA.yah.window.oid).offsetHeight)/2)
            ]);
            YAHOO.util.Dom.addClass('yahCmpsr','on');
            YAHOO.CMA.yah.window.show();
            $('yahMusic').onmouseover = function() {
                clearTimeout(parentObj.timer);
                parentObj.timer = null;
            };
            $('yahMusic').onmouseout = function() {
                parentObj.hide();
            };

            YAHOO.CMA.yah.windowBridge = new YAHOO.widget.Overlay("yahBridge", {
                context:[oid,'tl','tl']
                , visible:false
                , width:"2px"
                , height:(parseInt($(YAHOO.CMA.yah.window.oid).offsetHeight)-2)+"px"
                , zIndex:"1001"
            } );
            if (YAHOO.lang.isObject(YAHOO.CMA.yah.windowBridge)) {
                YAHOO.CMA.yah.windowBridge.setBody('');
                YAHOO.CMA.yah.windowBridge.render(document.body);
                YAHOO.CMA.yah.windowBridge.cfg.setProperty("x", parseInt(YAHOO.CMA.yah.windowBridge.cfg.getProperty("x")) + 1);
                YAHOO.CMA.yah.windowBridge.show();
            }
        }
    },
    hide:function(action, parentObj) {
        if (!YAHOO.lang.isObject(parentObj)) {
            parentObj = this;
        }
        clearTimeout(parentObj.timer);
        parentObj.timer = null;
        if (action == 'now') {
            if (YAHOO.lang.isObject(YAHOO.CMA.yah.window)) {
                YAHOO.CMA.yah.window.hide();
                if (YAHOO.lang.isObject(YAHOO.CMA.yah.windowBridge)) {
                    YAHOO.CMA.yah.windowBridge.hide();
                }
                YAHOO.util.Dom.removeClass('yahCmpsr','on');
            }
        } else {
            var hFunc = function() {parentObj.hide(
                    'now'
                    , YAHOO.lang.isObject(parentObj) ? parentObj : null
            )};
            parentObj.timer = setTimeout(
                    hFunc
                    , 300
            );
        }
    },
    clone:function() {
        var clone = {};
        clone.prototype = this.prototype;
        for (property in this) clone[property] = this[property];
        return clone;
    }
}

YAHOO.util.Event.onAvailable('yahCmpsr', function(){
    YAHOO.util.Event.addListener('yahCmpsr', 'mouseover', function(){
        if (YAHOO.lang.isObject(YAHOO.CMA.yah.data)) {
            YAHOO.CMA.yah.show(this.id);
        } else {
//            YAHOO.CMA.loadImg;
            throw "TO DO : SHOW LOADING GIF HERE"
        }
    });

    YAHOO.util.Event.addListener('yahCmpsr', 'mouseout', function(){
        if (YAHOO.lang.isObject(YAHOO.CMA.yah.data)) {
            YAHOO.CMA.yah.hide(this.id);
        } else {
//            YAHOO.CMA.loadImg;
            throw "TO DO : SHOW LOADING GIF HERE"
        }
    });
}, this);
