$(function()
{
    $(":input[id]")
        .focus(function()
        {
            $("label[for='" + this.id + "']").addClass("focus");
        })
        .blur(function()
        {
            $("label[for='" + this.id + "']").removeClass("focus");
        });

    $(".hasDatePicker").datepicker({ dateFormat:'yy-mm-dd'});

    $("dd").hover(
        function()
        {
            $(this).prev("dt").addClass("hover");
        },
        function()
        {
            $(this).prev("dt").removeClass("hover");
        });

    $("dt").hover(
        function()
        {
            $(this).next("dd").addClass("hover");
        },
        function()
        {
            $(this).next("dd").removeClass("hover");
        });

    $("li")
        .click(function(event)
        {
            if (event.target != this)
            {
                return;
            }

            var targets = $(":input, a", this);
            if (targets.length == 1)
            {
                target = targets.eq(0);
                if (target.is(":checkbox, :radio"))
                {
                	target.click();
                	target.focus();
                }
                else if (target.is(":input"))
                {
                    target.focus();
                }
            }
        });
    
    $("tr.unselected")
        .click(function(event)
        {
        	$(this).toggleClass('unselected');
        	$(this).siblings().addClass('unselected');
        });
    $("tbody tr:even").addClass("even");
    
    $(".msg_wrapper").click(function()
    {
    	if (!$(this).hasClass("expanded")) {
    		$(this).html('less...');
    		$(this).prev('p').show();
    		$(this).prev('p').prev('p').hide();
    	} else {
    		$(this).html('more...');
    		$(this).prev('p').hide();
    		$(this).prev('p').prev('p').show();
    	}
    	$(this).toggleClass("expanded");
    });

});

(function($) {
	/**
	 * Specialized autocomplete to handle IMP service's autocomplete support.
	 *
	 * @param service string
	 * @param module string
	 * @param options string
	 */
	$.fn.servAutocomplete = function(service, module, options)
    {
        if (module == null) {
            module = 'system';
        }
        $(this).data('service', service);
        $(this).data('module', module);

        $(this).autocomplete({
            'source': function (request, formatCallBack)
            {
                var service = $(this.element).data('service');
                var module = $(this.element).data('module');
                
                $.get("/" + module + "/rest/" + service + "/meta/auto-comp", {
                    "q": request.term,
                    "limit":"10"
                }, autocompAjaxCallback, "json");
                
                function autocompAjaxCallback(json)
                {
                    var data = [];
                    for (var id in json)
                    {
                        data.push({"id": id, "label": json[id], "value:" : json[id] } );
                    }
                    //console.log(schoolAcDataCallBack.toString());
                    
                    // the method named here and in the params of schoolAcList is just a placeholder...
                    // the definition of what this method does is presumeably inside jquery somewhere 
                    formatCallBack(data);
                }
            }
        });
    };

})(jQuery);
