// WS GUI-related functions

/**
 * Toggle a specified DIV element via Javascript call on a Subhead title click
 *
 * NOTE: if div_id does not match any valid DIV element id, then this function
 *   exits gracefully (fails silently) and nothing happens
 */
WS.gui.togglesubhead = function(div_id, on_icon, off_icon) {
    if(div_id.indexOf('#') < 0) {
        div_id = '#' + div_id;
    }
    if($(div_id) == null) { return; }

    if($(div_id).css('display') == 'none') {
        $(div_id+'_icon').attr('src', off_icon);
    } else {
        $(div_id+'_icon').attr('src', on_icon);
    }
    $(div_id).toggle('blind',{}, 500);
    return false;
};

//This function will take a row classname and convert all rows of that classname 
// to the Odd/Even scheme everywhere else in the system
WS.gui.fixRowColors = function(className){
    $("tr." + className + ":visible").removeClass("even").removeClass("odd");
    $("tr." + className + ":visible:odd").addClass("even");
    $("tr." + className + ":visible:even").addClass("odd");
}

WS.gui.get$ = function(elem) {
    if(typeof(elem) == 'string' && elem.substring(0,1) != '#') {
        elem = '#' + elem;
    }
    return $(elem);
};

WS.gui.getDimensions = function(elem) {
    var dims = {};
    var $elem = WS.gui.get$(elem);
    dims.h = $elem.height();
    dims.w = $elem.width();
    dims.height = dims.h;
    dims.width = dims.w;
    return dims;
}
// utility function to get coords in both top/left and x/y pairs,
// for compatibility with jQuery and other 3rd party libs
WS.gui.getPosition = function(elem) {
    var pos = WS.gui.get$(elem).offset();
    pos.x = pos.left;
    pos.y = pos.top;
    return pos;
};

WS.gui.setPosition = function(elem, pos) {
    var left = pos.x || pos.left || 0;
    var top = pos.y || pos.top || 0;
    WS.gui.get$(elem).css('left', left + 'px').css('top', top + 'px');
}

WS.gui.appendChildNodes = function(node /*, nodes...*/) {
    var args = $.merge([],arguments);
    var parent = args.shift();
    $(parent).append(args);
}

WS.gui.resizeFlintboxContent = function(target, adjust, tweak){
    var dynamic_content = (target) ? target : $("#dynamic_content");
    var alt_toolbox = (adjust) ? adjust : $("#alt_toolbox");
    var push = (tweak) ? tweak : 10;
    dynamic_content.css('width', (dynamic_content.parent().outerWidth() - alt_toolbox.outerWidth() - push)+'px');
}