﻿/**
* @author Paul Chan / KF Software House 
* http://www.kfsoft.info
*
* Version 0.5
* Copyright (c) 2010 KF Software House
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/

(function ($) {

    var _options = new Array();

    jQuery.fn.MySearchBox = function (options) {
        _options[_options.length] = $.extend({}, $.fn.MySearchBox.defaults, options);

        var idx = _options.length - 1;
        var opt = _options[idx];
        $(this).attr("idx", idx);

        $(this).each(function () {
            /*
            GLOBAL SEARCH FORM GENERATION
            <form id="globalSearchForm" action="" method="">
            <input type="text" id="txtGlobalSearch" name="txtGlobalSearch" class="searchbox" placeholder="keywords" value="" />
            <button type="submit" class="btnGlobalSearch" id="btnGlobalSearch">Search</button>
            </form>			
            */

            var formTemplate = "<form id='" + opt.searchFormId + "' action='" + opt.searchFormAction + "' method='" + opt.searchFormMethod + "'>";
            formTemplate += "<input type='text' id='" + opt.searchboxId + "' name='" + opt.searchboxId + "' class='searchbox' placeholder='" + opt.placeholderText + "' value='' />";
            formTemplate += "<button type='submit' class='searchbutton' id='" + opt.searchbuttonId + "'>Search</button></form>";

            $(this).append(formTemplate);

            /* Event Subscription and placholder preparation */
            var txtSearch = $(this).find("#" + opt.searchboxId);

            if (txtSearch) {
                var v = $(txtSearch).val();
                if (v == "" || v == opt.placeholderText) {
                    $(txtSearch).val(opt.placeholderText)
                    $(txtSearch).addClass("placeholder");
                }
                else {
                    $(txtSearch).removeClass("placeholder");
                }
            }

            $(txtSearch).focus(function (e) {
                var v = $(txtSearch).val();
                if (v == "" || v == opt.placeholderText) {
                    $(txtSearch).val("");
                    $(txtSearch).removeClass("placeholder");
                    $(txtSearch).addClass("editMode");
                }
            }).blur(function (e) {
                var v = $(txtSearch).val();
                if (v == "" || v == opt.placeholderText) {
                    $(txtSearch).val(opt.placeholderText)
                    $(txtSearch).addClass("placeholder");
                    $(txtSearch).removeClass("editMode");
                }
            });
        });
    }

    //default values
    jQuery.fn.MySearchBox.defaults = {
        placeholderText: "Search",
        searchFormId: "globalSearchForm",
        searchFormAction: "",
        searchFormMethod: "get",
        searchboxId: "txtGlobalSearch",
        searchbuttonId: "btnGlobalSearch"
    };

})(jQuery);
