﻿/**
* SearchField
* 
* PLY standard search field functionality. Allows custom styles on focus & blur, and allows enter to work correctly.
*
* @author: PLY Interactive
* @dependency: jquery.js
**/

if (typeof PLY != 'object') PLY = {};
if (!PLY.UI) PLY.UI = {};

PLY.UI.SearchField = function(fieldId, customOptions)
{
    //class default config values
    var config =
    {
        'buttonId': null
        , 'fnOnSubmit': null
        , 'defaultText': ''
        , 'oBlurStyles': null
        , 'oFocusStyles': null
        , 'blurClass': null
        , 'focusClass': null
        , 'bSubmitOnEmptySearch': true
		, 'bClearDefaultTextOnEdit' : true
    };

    //override with custom config where provided
    for (var option in customOptions)
    {
        config[option] = customOptions[option];
    }

    var IsFocused = false;

    this.init = function()
    {
        $(fieldId).focus(function()
        {
            //remove any blur classes
            if (config['blurClass'] != null)
            {
                $(this).removeClass(config['blurClass']);
            }

            //add the focus class
            if (config['focusClass'] != null)
            {
                $(this).addClass(config['focusClass']);
            }

            //add any focus styles
            if (config['oFocusStyles'] != null)
            {
                for (var key in config['oFocusStyles'])
                {
                    $(this).css(key, config['oFocusStyles'][key]);
                }
            }

            //reset the value of its the same as the default text
            if (config['bClearDefaultTextOnEdit'] && $(this).val() == config['defaultText'])
            {
                $(this).val('');
            }

            //set this search field as focused
            IsFocused = true;
        });

        //search field restore
        $(fieldId).blur(function()
        {
            if ($(this).val() == '')
            {
                //remove any focus classes
                if (config['focusClass'] != null)
                {
                    $(this).removeClass(config['focusClass']);
                }

                //add the blur class
                if (config['blurClass'] != null)
                {
                    $(this).addClass(config['blurClass']);
                }

                //add any blur styles
                if (config['oBlurStyles'] != null)
                {
                    for (var key in config['oBlurStyles'])
                    {
                        $(this).css(key, config['oBlurStyles'][key]);
                    }
                }

                //reset the val to the default text
                $(this).val(config['defaultText']);
            }

            //set this search field as not focused
            IsFocused = false;
        });

		$(fieldId).keypress(function(event)
		{
			if (event.which == '13' && IsFocused)
			{
				return submit();
			}
		});

		if (config['buttonId'] != null)
		{
			$(config['buttonId']).click(function()
			{
				return submit();
			});
		}
    }

    var submit = function()
    {
        var val = $(fieldId).val();
        val = (config['bClearDefaultTextOnEdit'] && val == config['defaultText']) ? '' : val;
        
		if (config['fnOnSubmit'] != null && (config['bSubmitOnEmptySearch'] || val != ''))
        {
            var fn = config['fnOnSubmit'];
            return fn(val);
        }
		else
		{
			return (config['bSubmitOnEmptySearch'] || val != '');
		}
    }
};
