﻿/**
* Javascript text field toggle
* Copyright (c) 2008 Dan Ladds <dan@danladds.com>
*
* Usage:
* 1) Include JQuery
* 2) Include this file
* 3) Call init with config params, in form:
* 
* swapper.init([{name : "name of first field", value : "initial value"}, {name : "second", value : "second value"}]);
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/

// Textbox swapper static class
var swapper = {

    items: null,

    // Should be called with config before pageload
    init: function(items) {
        this.items = items;
    },

    // Runs after page load to place listeners on fields
    activate: function() {

        // Place listeners
        for (var n in this.items) {

            var i = this.items[n];

            var input = $("input[name='" + i.name + "']");

            $(input).focus(function() {

                if (this.value == i.value) this.value = "";
            });

            $(input).blur(function() {

                if (this.value == "") this.value = i.value;
            });
        }
    }
};

// Swapper activate
$(document).ready(function() {
    swapper.activate();
});