/*
 * jQuery ABBsLabelIn plug-in version 1.0
 * Last Update : July 28, 2008
 * New features:
 *      Added ability for custom text to be specified instead of using an attribute value
 *
 * Bug Fixed:
 *
 * Copyright (c) 2008 Dean Layton-James
 *   http://www.layton-james.co.uk
 * Copyright (c) 2008 Digital Marmalade Ltd
 *   http://www.digitalmarmalade.co.uk
 *
 * Based on the Original LabelIn by
 * Copyright (c) 2007 E-wave web design
 *   http://www.ewave.com.au/
 *
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @requires jQuery v 1.2.1 or later
 * @name	ABBsLabelIn
 * @usage	$('#selector').ABBsLabelIn();
 *
 * HTML
 * <form id="form1">
 * <input id="input1" type="text" name="Label"></input>
 * <input type="submit" value="Submit>
 * </form>
 *
 * Description
 * Display info to specify content of input tag, on focus info is removed.
 * Also Apply a class to the input tag
 *
 *
 * @param ali_class
 * 	this class is added to input tag when empty
 *
 * @param text
 * 	display the text when tag is empaty
 *
 * @param attribute
 * 	display the content of this attribute when tag is empty
 *  only used if text is blank
 *
 */

// function to clear default text for a selector
// cleared by submit function
function clearABBsLabelIn()
{
    // alert( 'in clearABBsLabelIn' ) ;
    // loop through each array element and then remove default text
    for(var sSelector in aaFieldGuideText) {

        // select all matching elements
        $( '#' + sSelector ).each(function(){

            // for each one in turn make it $input
            $input = $(this);

            // is it equal to the default text
            if($.trim($input.val()) == aaFieldGuideText[sSelector]) {
                // clear the default text ready for form submission
                $input.val("");
            } // end if
        }) ;
    } // end for
} // end function


// ABBs version of Label In
jQuery.fn.ABBsLabelIn = function(settings) {

    var iSelector = this;

    settings = jQuery.extend({
                    version		: '1.0',
                    ali_class	: 'ABBsLabelIn',
                    text	    : 'Enter value here',
                    attribute	: 'name'
                }, settings);


   initABBsLabelIn( settings );

    // init the ABBsLabelIn for this element
    function initABBsLabelIn(settings) {
        // alert( 'in initABBsLabelIn') ;
         if($.trim($(iSelector).val()) == "") {
            $(iSelector).addClass(settings["ali_class"]);
            if( settings['text'] != "" ) {
                $(iSelector).val(settings['text']);
            } else {
                $(iSelector).val($(iSelector).attr(settings['attribute']));
            } // end if / else
        }// end if
    } // end function init

    // attach a focus event to element
    return iSelector.focus(function() {
        // remove the class
        $(iSelector).removeClass(settings["ali_class"]);
        // if we have text in the attributes array
        if( settings['text'] != "" ) {
            // if the field contains the default text then blank the field
            if($(iSelector).val() == settings['text']) {
                $(iSelector).val("");
            } // end if
        } else {  // if we are using an element attribute
            if($(iSelector).val() == $(iSelector).attr(settings['attribute'])) {
                // clear if text matches attribute
                $(iSelector).val("");
            } // end if
        } // end if / else

    // attach a blur event to the element
    }).blur(function() {
        if($.trim($(iSelector).val()) == "") {
            $(iSelector).addClass(settings["ali_class"]);
            if( settings['text'] != "" ) {
                $(iSelector).val(settings['text']);
            } else {
                $(iSelector).val($(iSelector).attr(settings['attribute']));
            } // end if / else
        } else {
            $(iSelector).removeClass(settings["ali_class"]);
        } // end if
    });
};

