// $Id: global.js 268 2008-09-08 14:42:14Z marloes $

/**
 * Initialize the Plag object.
 */
Plag = {
    init: function() {
        Plag.tagInputElements();
        Plag.collapsibleFieldset();
        Plag.flagRequiredFields();
    }
}

/**
 * Add class to input elements.
 * We use this for cross browser compatiblity... (IE doesn't allow type
 * selectors).
 */
Plag.tagInputElements = function() {
    $('input').each(function() {
        $(this).addClass('input-'+ this.type);
    });
};

/**
 * Extends the current object with the parameter. Works recursively.
 */
Plag.extend = function(obj) {
    for (var i in obj) {
        if (this[i]) {
            Plag.extend.apply(this[i], [obj[i]]);
        }
        else {
            this[i] = obj[i];
        }
    }
};

/**
 * Make fieldset expand- & collapsible.
 *
 * If the browser doesn't support JS this won't be executed, so they wil just
 * see a normal expanded fieldset element.
 *
 */
Plag.collapsibleFieldset = function() {
    $('fieldset.collapsible')
        // find legend so we only expand/collapse when legend is clicked
        .find('legend')
            .click(function() {
                $(this)
                    .parent().toggleClass('expanded')
                    .find('.collapse-wrapper').slideToggle();
            })
            .end()
        // now hide the collapsible content
        .find('.collapse-wrapper').hide();
}

/**
 * make sure all required fields get their *
 *
 */
Plag.flagRequiredFields = function() {
    $('form .required').each(function() {
        label = $(this).find('label');

        // check if the required field is already flagged
        if (!$(label).find('span').hasClass('flag-required')) {
            $(label).append('<span class="flag-required">*</span>');
        }
    });
}

// Run any/all init functions.
$(function() { Plag.init() });