//jquery extention function to add delayed callback execution on bindings.
//example, a function that does an ajax request on keyup. If you use this function it will only call the ajax request when the user finishes typing or
//there is a pause in their typing that is longer then the timeout
(function($) {
    jQuery.fn.bindWithDelay = function( type, data, fn, timeout ) {
        var wait = null;
        var that = this;

        if ( $.isFunction( data ) ) {
            timeout = fn;
            fn = data;
            data = undefined;
        }

        var cb = function(e) {
            clearTimeout(wait);
            var cachedEvent = $.extend({}, e);

            wait = setTimeout(function() {
                fn.apply(that, [cachedEvent]);
            }, timeout);
        }

        return this.bind(type, data, cb);
    }
})(jQuery);

jQuery.fn.forceNumericOnly = function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

jQuery.fn.confirmActionDialog = function(options) {
     return this.each(function() {
       var self = this;
         
       var defaultOptions = {
            "description": "Are you sure you want to proceed?",
            "resizable": false,
			"height": 140,
            "autoOpen": false,
			"modal": true,
			"buttons": {
				"Confirm": function() {
					window.location.href = $(self).attr("href");
				},
                "Close": function() {
					$(this).dialog("close");
				}
            }
        };
         
        jQuery.extend(defaultOptions, options) ;
       
        var content = $("<p></p>")
            .attr("title", "Are you sure?")
            .html(defaultOptions.description);
       
        content.dialog( "destroy" );
        content.dialog(defaultOptions);
        
        $(this).click(function(e) {
            content.dialog("open");
            return false;
        });
    });
};

jQuery.fn.autoCompleteTextbox = function (id, param, url) {

    $('#' + id + '_search').autocomplete({
        source:function (request, response) {
            var defaultTerm = {'term':request.term};

            $.extend(defaultTerm, param);
            $.ajax(
                {
                    url:url,
                    data:defaultTerm,
                    type:'POST',
                    dataType:'json',
                    success:function (data) {
                        response($.map(data, function (item) {
                            return {
                                id:item.id,
                                label:item.label,
                                value:item.value
                            }
                        }));
                    }
                });
        },
        minLength:1,
        sroll:true,
        maxRows:20,
        select:function (event, ui) {
            $('#' + id).val(ui.item.id);
        }
    });
};


/*** Pop up extensions ***/
jQuery.fn.picturePopup = function(options) {
    var defaultOptions = {
        "type": "image",
        "autoScale": true,
        "overlayColor": "#333",
        "titleShow":  false,
        "width": "auto",
        "height": "auto",
        "scrolling": "no",
        "enableEscapeButton": true,
        "hideOnContentClick": true,
        "onComplete": function(e) {
            $("#fancybox-wrap").hover(function() {
                $(this).css("cursor", "pointer");
            }, function() {
                $(this).css("cursor", "default");
            });
        }
    };

    $.extend(defaultOptions, options);
    
    return this.each(function() {
        $(this).fancybox(defaultOptions);
    });
};

jQuery.fn.iframePopup = function(options) {
    var defaultOptions = {
        "type" : "iframe",
        "overlayColor" : "#333",
        "titleShow" : false,
        "autoDimensions" : false, //iframe content should always have this set to false
        "autoScale" : false,
        "enableEscapeButton" : true,
        "scrolling" : "auto",
        "onComplete": function() {
            $("#fancybox-overlay").height($(document).height());
            $("#fancybox-overlay").width($(document).width());
            $(window).bind('resize', function () {
                $("#fancybox-overlay").height($(document).height());
                $("#fancybox-overlay").width($(document).width());
            });
        },
        "onClosed": function() {
            $(window).unbind('resize');
        }
    } ;

    $.extend(defaultOptions, options);
    
    return this.each(function() {
        $(this).fancybox(defaultOptions);
    });
};

jQuery.fn.inlinePopup = function(options) {

    var defaultOptions = {
        "overlayColor": "#333",
        "titleShow": false,
        "autoScale": true,
        "width": "auto",
        "height": "auto",
        "enableEscapeButton": true,
        "scrolling": "auto",
        "onComplete": function() {
            $(this).resize();
        }
    };

    $.extend(defaultOptions, options);
    return this.each(function() {
        $(this).fancybox(defaultOptions);
    });
};

//global function in the jquery namespace
jQuery.closePopup = function () {
  parent.$.fancybox.close();
};

jQuery.fn.wfbDatePicker = function(options) {
    return this.each(function() {
        $(this).datepicker(options);
    });
};
