/* jQuery timepicker
 * replaces a single text input with a set of pulldowns to select hour, minute, and am/pm
 *
 * Copyright (c) 2007 Jason Huck/Core Five Creative (http://www.corefive.com/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 1.0
 */

(function($) {
    jQuery.fn.timepicker = function() {
        this.each(function() {
            // get the ID and value of the current element
            var i = this.id;
            var v = $(this).val();


            // the options we need to generate
            var hrs = new Array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
            var mins = new Array('00', '15', '30', '45');
            var ap = new Array('AM', 'PM');

            // default to the current time
            var d = new Date;
            var h = d.getHours();
            var m = d.getMinutes();
            var p = (h >= 12 ? 'PM' : 'AM');

            // adjust hour to 12-hour format
            if (h > 12) h = h - 12;

            // round minutes to nearest quarter hour
            for (mn in mins) {
                if (m <= parseInt(mins[mn], 10)) {
                    m = parseInt(mins[mn], 10);
                    break;
                }
            }

            // increment hour if we push minutes to next 00
            if (m > 45) {
                m = 0;

                switch (h) {
                    case (11):
                        h += 1;
                        p = (p == 'AM' ? 'PM' : 'AM');
                        break;

                    case (12):
                        h = 1;
                        break;

                    default:
                        h += 1;
                        break;
                }
            }

            // override with current values if applicable
            if (v.length == 7 || v.length == 8) {
                h = parseInt(v.substr(0, 2), 10);
                m = parseInt(v.split(':')[1].substr(0, 2), 10);
                //m = parseInt(v.substr(2, 2));
                p = v.substr(5);
                p = p.replace(' ', '');
            }

            // build the new DOM objects
            var output = '';

            output += '<select id="h_' + i + '" class="h timepicker">';
            for (hr in hrs) {
                output += '<option value="' + hrs[hr] + '"';
                if (parseInt(hrs[hr], 10) == h) output += ' selected';
                output += '>' + hrs[hr] + '</option>';
            }
            output += '</select>';

            output += '<select id="m_' + i + '" class="m timepicker">';
            for (mn in mins) {
                output += '<option value="' + mins[mn] + '"';
                if (parseInt(mins[mn], 10) == m) output += ' selected';
                output += '>' + mins[mn] + '</option>';
            }
            output += '</select>';

            output += '<select id="p_' + i + '" class="p timepicker">';
            for (pp in ap) {
                output += '<option value="' + ap[pp] + '"';
                if (ap[pp] == p) output += ' selected';
                output += '>' + ap[pp] + '</option>';
            }
            output += '</select>';

            // hide original input and append new replacement inputs
            $(this).after(output);
        });

        $('select.timepicker').change(function() {
            var i = this.id.substr(2);
            var h = $('#h_' + i).val();
            var m = $('#m_' + i).val();
            var p = $('#p_' + i).val();
            var v = h + ':' + m + ' ' + p;
            $('#' + i).val(v);
        });

        return this;
    };
})(jQuery);



/* SVN: $Id: jquery.timepicker.js 456 2007-07-16 19:09:57Z Jason Huck $ */
(function($) {
    $.fn.popupWindow = function(instanceSettings) {

        return this.each(function() {

            $(this).click(function() {

                $.fn.popupWindow.defaultSettings = {
                    centerBrowser: 0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
                    centerScreen: 0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
                    height: 500, // sets the height in pixels of the window.
                    left: 0, // left position when the window appears.
                    location: 0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
                    menubar: 0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
                    resizable: 0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
                    scrollbars: 0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
                    status: 0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
                    width: 500, // sets the width in pixels of the window.
                    windowName: null, // name of window set from the name attribute of the element that invokes the click
                    windowURL: null, // url used for the popup
                    top: 0, // top position when the window appears.
                    toolbar: 0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
                };

                settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});

                var windowFeatures = 'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status +
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

                settings.windowName = this.name || settings.windowName;
                settings.windowURL = this.href || settings.windowURL;
                var centeredY, centeredX;

                if (settings.centerBrowser) {

                    if ($.browser.msie) {//hacked together for IE browsers
                        centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120) / 2) - (settings.height / 2)));
                        centeredX = window.screenLeft + ((((document.body.offsetWidth + 20) / 2) - (settings.width / 2)));
                    } else {
                        centeredY = window.screenY + (((window.outerHeight / 2) - (settings.height / 2)));
                        centeredX = window.screenX + (((window.outerWidth / 2) - (settings.width / 2)));
                    }
                    window.open(settings.windowURL, settings.windowName, windowFeatures + ',left=' + centeredX + ',top=' + centeredY).focus();
                } else if (settings.centerScreen) {
                    centeredY = (screen.height - settings.height) / 2;
                    centeredX = (screen.width - settings.width) / 2;
                    window.open(settings.windowURL, settings.windowName, windowFeatures + ',left=' + centeredX + ',top=' + centeredY).focus();
                } else {
                    window.open(settings.windowURL, settings.windowName, windowFeatures + ',left=' + settings.left + ',top=' + settings.top).focus();
                }
                return false;
            });

        });
    };
})(jQuery);
