﻿/// <reference path="scripts/jquery-1.3.2-vsdoc.js" />
/// <reference path="scripts/jquery.validate.js" />
(function ($) {

    $.goTo = function (url) {
        top.location.href = url;
    }

    $.definedOrDefault = function (value, ifUndefined) {
        if (typeof value === "undefined")
            return ifUndefined;
        return value;
    }

    //Similar to $.ajax, but has a few extras:
    //1.  Meant for calling an asp.net web service passing JSON data
    //2.  Adds a default error message for the user
    //3.  Hides the "result.d" parameter that asp.net passes back in web services
    //4.  Introduces a "returned()" function in the opts that occurs before either error() or success()
    //5.  Automatically serializes the data (NOTE: opts.data should be a function so it gets computed after teh form submits, otherwise form elements will be wrong)
    $.wsAjax = function (opts, form) {
        if (form)
            form = $(form);
        //if the form is already being submitted (user pressed enter twice or hit the button twice), we ignore this submission
        if (form && form.data("submitting")) {
            return false;
        }

        var ajaxOps = jQuery.extend({}, opts); //make a shallow copy of opts because it can be used for multiple form submissions, and overwriting data the first time would screw things up the second time

        //data needs to be a fucntion so it gets evaluated as the form is validated, not during page load
        if (opts.data)
            ajaxOps.data = $.toJSON(opts.data());
        ajaxOps.type = "POST";
        ajaxOps.contentType = "application/json; charset=utf-8";
        ajaxOps.dataType = "json";
        if (form && !opts.url)
            ajaxOps.url = form[0].action;

        ajaxOps.error = function (XMLHttpRequest, textStatus, errorThrown) {
            if (opts.returned)
                opts.returned(false);
            if (form)
                form.data("submitting", false);
            //execute the specified 
            if (opts.error) {
                opts.error(XMLHttpRequest, textStatus, errorThrown);
            }
            else {
                if (XMLHttpRequest.statusText != "" && XMLHttpRequest.responseText != "")
                    alert('Uh oh!  There was a server error.');

                $("img.loadingImage").hide();
            }

        };

        ajaxOps.success = function (result) {
            if (result != null) {
                if (opts.returned)
                    opts.returned(true); //call our custom "returned" function and indicate success

                if (form)
                    form.data("submitting", false);

                if (opts.success) {
                    //asp.net returns a result.d that contains the actual results.  it's some security measure.  hide that little fact from our normal code
                    opts.success(result.d);
                }
            }
        };

        if (form)
            form.data("submitting", true);
        $.ajax(ajaxOps);

    };


    //Creates the submitHandler for submitting a form to a web service.  Just saves a few lines of code.
    $.wsFormSubmitHandler = function (opts) {
        return function (form) {
            $.wsAjax(opts, form);
            return false;
        }
    };

    /*faster and better waay to replace the innerHtml of an element*/
    $.fn.replaceHtml = function (val) {
        var stack = [];
        return this.each(function (i, el) {
            var oldEl = el;
            /*@cc_on // Pure innerHTML is slightly faster in IE
            oldEl.innerHTML = val;
            return oldEl;
            @*/
            var newEl = oldEl.cloneNode(false);
            newEl.innerHTML = html;
            oldEl.parentNode.replaceChild(newEl, oldEl);
            /* Since we just removed the old element from the DOM, return a 
            reference 
            to the new element, which can be used to restore variable 
            references. */
            stack.push(newEl);
        }).pushStack(stack);
    };

    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this)[0].height;
            var ph = 52;
            var mh = (ph - ah) / 2;
            $(this).css('margin-top', mh + "px");
        });
    }
;

})(jQuery);
