// laine@gaiaonline.com
// pair the anchor (clicking part) with the arrow image's id

YAHOO.namespace("gaia.app.Marketplace");
YAHOO.gaia.app.Marketplace.ShowHide = function() {

    // Replace 'yui_collapsed' with 'collapsed' in all elements that have it. This is done
    // so we can have a CSS class to control the style of collapsed (hidden) elements without
    // permanently hiding them from non-JS users
    var collapsed = YAHOO.util.Dom.getElementsByClassName('yui_collapsed', 'div', 'content');
    YAHOO.util.Dom.replaceClass(collapsed, 'yui_collapse', 'collapsed');
    
    // get the links we want to attach events to
    var triggers = YAHOO.util.Dom.getElementsByClassName('trigger_expandcollapse', 'a', 'content');
    YAHOO.util.Event.addListener(triggers, 'click', this.showHide, this);

}

/**
 * Toggles the display of the selected container
 **/
YAHOO.gaia.app.Marketplace.ShowHide.prototype.showHide = function(e, scope) {
    
    // Get the div name 
    var anchorname_parts = this.id.split("_"); //get postfix: 1, 2 , that uniquely identifies each anchor/detail log
    
    // Get the actual elements
    var thedetail   = document.getElementById( "details_"+anchorname_parts[1] );
    var thearrow    = document.getElementById( "arrow_"+anchorname_parts[1] );
    
    // If the container is already showing, hide it and vice versa, changing the
    // toggle image as necessary
    if ( YAHOO.util.Dom.getStyle( thedetail, "display") == "block" ) {
        YAHOO.util.Dom.replaceClass(thedetail, 'expanded', 'collapsed');
        
        if (thearrow && YAHOO.util.Dom.hasClass( thearrow ,'triggerCollapse')) {
            YAHOO.util.Dom.replaceClass(thearrow, 'triggerCollapse', 'triggerExpand');
        }
        
        if (thearrow && YAHOO.util.Dom.hasClass( thearrow , 'triggerCollapseArrow')) {
            YAHOO.util.Dom.replaceClass(thearrow, 'triggerCollapseArrow', 'triggerExpandArrow');
        }
                    
    }
    else {
        YAHOO.util.Dom.replaceClass(thedetail, 'collapsed', 'expanded');
        
        if (thearrow && YAHOO.util.Dom.hasClass( thearrow , 'triggerExpand')) {
            YAHOO.util.Dom.replaceClass(thearrow, 'triggerExpand', 'triggerCollapse');
        }

        if (thearrow && YAHOO.util.Dom.hasClass( thearrow, 'triggerExpandArrow')) {
            YAHOO.util.Dom.replaceClass(thearrow, 'triggerExpandArrow', 'triggerCollapseArrow');
        }
    }
    
}

YAHOO.util.Event.onContentReady('details_wishlist', function() { new YAHOO.gaia.app.Marketplace.ShowHide(); });
YAHOO.util.Event.onContentReady('userstore_content', function() { new YAHOO.gaia.app.Marketplace.ShowHide(); });
