/* jquery targetpop -- makes popup windows out of the 'target' attribute of
 * the matched tags.
 *
 * Only works on <a> tags for now. If jQuery(this).is('a') fails, then nothing
 * is done.
 *
 * (C) 2008 Jeff Shell, Bottlerocket MFG.
 *
 * Example:
 *
 * <a href="..." target="mywin-400x300-resizable-scrollbars" class="targeted"
 *  >...</a>
 *
 * jQuery('a.targeted').targetpop();
 *
 * The above would produce a 400 wide by 300 high window named 'mywin' with
 * resizable and scrollbars set to true.
 *
 * Height can be left off. Other example targets are:
 *
 * `tour-400x600` : 400 by 600, fixed size, all features disabled.
 * `newpage-800`  : 800 by 800, fixed size
 * `newpage-800-resizable-statusbar-menubar` : 800 by 800, resizable, etc.
 * `news-400x600-resizable-scrollbars` : guess.
 */
jQuery.fn.targetpop = function() {
    this.each(function() {
        var link = jQuery(this);
        if(!(link.is('a'))) {
            return null;
        }

        var href = link.attr('href'),
            target = link.attr('target');

        if(!target) {
            return null;
        }

        var target_elements = target.split('-');
        // First two elements should be window name and dimensions:
        //      tour-400x600
        //      newpage-300
        //      news-400x600-resizable-menubar
        var window_name = target_elements.shift(),
            dimensions = target_elements.shift().split('x');

        // If the dimensions are only a single number, duplicate it for height.
        if(dimensions.length == 1)
            dimensions.push(dimensions[0]);

        // The remaining window options map to setting that option on the
        // window.open call.
        var window_options = jQuery.map(target_elements, function(option) {
            return option+'=1';
        });
        // In first incarnation, scrollbars were always true. Should allow
        // for defaults in 'targetpop()' call.
        // window_options.push('scrollbars=1');

        var option_string = "width="+dimensions[0]+",height="+dimensions[1];
        option_string += ','+window_options.join(',');

        link.attr('target', '')
            .click(function() {
                window.open(this.href, window_name, option_string);
                return false;
            });
    });
};

