// file:		slib-3.0.js
// last modified:	2009-10-14 15:22 (SBF)

// ____________________________________________________________________________________________________
function open_window(url, name, features) {
    window.open(url, name, features);
}

// ____________________________________________________________________________________________________
function now() {
    return new Date().getTime();
}

// ____________________________________________________________________________________________________
function extract_function_name(function_data) {
    var name = function_data;
    name = name.substr('function '.length);
    name = name.substr(0, name.indexOf(')') + 1);
    return name;
}

// ____________________________________________________________________________________________________
function display_group() {
    this.members = new Array;
    for (var i = 0; i < arguments.length; i++) {
        this.members.push(arguments[i]);
    }
    this.current = 0;
}

display_group.prototype.show = function(name) {
    for (var i = 0; i < this.members.length; i++) {
        if (this.members[i] === name) {
            $(this.members[i]).show();
        }
        else {
            $(this.members[i]).hide();
        }
    }
}

// ____________________________________________________________________________________________________
function new_modal(init) {

    for (x in init) {
        var found = false;
        ["height", "width", "zIndex"].each(function(attribute) {
            if (x === attribute) {
                init[x] = init[x] * 1;
                found = true;
            }
        });
        ["className", "destroyOnClose", "recenterAuto"].each(function(attribute) {
            if (x === attribute) {
                found = true;
            }
        });
        if (! found) {
            alert("warning: slib.js new_modal() attribute " + x + " not checked");
        }
    }

    if (init.destroyOnClose === undefined) {
        init.destroyOnClose = true;
    }

    if (init.recenterAuto === undefined) {
        init.recenterAuto = false;
    }

    return new Window(init);
}

// ____________________________________________________________________________________________________
function add_handler(element, event_name) {
    if (! event_name) {
        event_name = "click";
    }

    if ($(element)) {
        $(element).observe(event_name, event_handler);
    }
}

// ____________________________________________________________________________________________________
function push_handler(id) {
    if (holder.push_handler === undefined) {
        holder.push_handler = new Array();
    }

    holder.push_handler.push(id);
}

// ____________________________________________________________________________________________________
function pop_handlers() {
    if (holder.push_handler !== undefined) {
        while (holder.push_handler.length > 0) {
            add_handler(holder.push_handler.pop());
        }
    }
}

// ____________________________________________________________________________________________________
function dimmer_on() {
    if (! holder.modal) {
        holder.modal = {};
    }
    holder.modal.dimmer = new_modal(holder.snippet.dimmer.control);
    holder.modal.dimmer.getContent().update(holder.snippet.dimmer.dimmer);
    holder.modal.dimmer.showCenter(true);
}

// ____________________________________________________________________________________________________
function dimmer_off() {
    holder.modal.dimmer.close();
}

// ____________________________________________________________________________________________________
function prepare_template(template) {
    return new Template(template.replace(/#%7B([a-zA-Z0-9_-]+)%7D/gi, "#{$1}"));
}

// ____________________________________________________________________________________________________
function check_for_duplicate_ids() {
    var countId = 0;
    var countDuplicate = 0;

    var oCount = new Object;
    getLikeElements("", "id").each(function(element) {
        countId++;
	if (oCount[element.id]) {
            countDuplicate++;
            oCount[element.id]++;
	}
        else {
	    oCount[element.id] = 1;
        }
    });

    console.log('examined ' + countId + ' IDs, ' + countDuplicate + ' duplicates');

    var duplicateId = new Array();
    for (thisElementId in oCount) {
        if (oCount[thisElementId] > 1) {
	    duplicateId.push(thisElementId);
	}
    }

    var duplicateIdSorted = duplicateId.sort();
    for (var count = 0; count < duplicateIdSorted.length; count++) {
        console.log(duplicateIdSorted[count] + '    (' + oCount[duplicateIdSorted[count]]  + ')');
    }
}

// ____________________________________________________________________________________________________
function getLikeElements(tagName, attrName, attrValue) {
    var startSet;
    var endSet = new Array( );
    if (tagName) {
        startSet = document.getElementsByTagName(tagName);    
    } else {
        startSet = (document.all) ? document.all : 
            document.getElementsByTagName("*");
    }
    if (attrName) {
        for (var i = 0; i < startSet.length; i++) {
            if (startSet[i].getAttribute(attrName)) {
                if (attrValue) {
                    if (startSet[i].getAttribute(attrName) == attrValue) {
                        endSet[endSet.length] = startSet[i];
                    }
                } else {
                    endSet[endSet.length] = startSet[i];
                }
            }
        }
    } else {
        endSet = startSet;
    }
    return endSet;
}

// ____________________________________________________________________________________________________
/**
* from: http://xavisys.com/2007/03/using-prototype-javascript-to-get-the-value-of-a-radio-group/
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function XXX_$RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

//You can pass it either a form (object or id) and a radio group name, or a radio button (object or id).
//var value = $RF('radio_btn_id');
//var value = $RF('form_id', 'radio_grp_name');

// ____________________________________________________________________________________________________
