﻿/// <reference path="../Plugins/jquery-1.3.2-vsdoc.js" />
Type.registerNamespace("UserControls");
/*
* V1.3
* Contains different extensions to jquery
*/


/// <summary>
/// Submits the parent form without specify id so it will be easier to change id if needed
/// Depends:
/// jquery-1.4.js
///	ui.core.js
/// ExtendJquery.js
/// MicrosoftAjax.js
/// </summary>
jQuery.fn.submitParentForm = function() {
    $($(this[0]).parents('form')[0]).submit()
    return this;
}

jQuery.fn.bindSubmitOnChange = function() {
    this.each(function() {
        var type = $(this).attr("type");
        if (type == "button" || type == "file" || type == "image" || type == "radio" || type == "reset" || type == "submit" || type == "checkbox") {
            $(this).bind("click", function() { $(this).submitParentForm(); });
        }
        else {
            $(this).bind("change", function() { if ($(this).data("oldValue") != $(this).val()) { $(this).data("oldValue", $(this).val()); $(this).submitParentForm(); } }).each(function() { $(this).data("oldValue", $(this).val()) });
        }
    });
    return this;
}
jQuery.fn.value = function(setValue) {
    if (typeof (setValue) == "undefined") {
        var value = [];
        this.each(function() {
            var type = $(this).attr("type");
            //if ($(this).attr("disabled")) return;
            if (type == "radio" || type == "checkbox") {
                if ($(this).attr("checked")) value.push($(this).val());
            }
            else {
                value.push($(this).val());
            }
        });
        return (value.length == 0 ? null : (value.length == 1 ? value[0] : value));
    }
    else {
        if (!$.isArray(setValue)) {
            setValue = [setValue];
        }
        var i = 0;
        this.each(function() {
            var type = $(this).attr("type");
            //if ($(this).attr("disabled")) return;
            if (type == "radio" || type == "checkbox") {
                $(this).attr("checked", $.inArray($(this).attr("value"), setValue) > -1);
            }
            else {
                $(this).val(setValue[i]);
            }
            i++;
        });
        return this;
    }
}

jQuery.fn.tagName = function() {
    if (1 === this.length) {
        return this[0].tagName.toLowerCase();
    } else {
        var tagNames = [];
        this.each(function(i, el) {
            tagNames[i] = el.tagName.toLowerCase();
        });
        return tagNames;
    }
};
/// <summary>
/// Fixes a bugg in ie href i sometimes resolved to the full path even if its not in the html
/// </summary>
jQuery.fn.href = function() {
    var href = $(this[0]).attr("href");
    if (href.indexOf("https://") == 0 || href.indexOf("http://") == 0) {
        var i = href.indexOf($(this[0]).attr("pathname"));
        href = href.substr(i);
        if (href.charAt(0) != '/') {
            href = "/" + href;
        }

    }
    return href;


}

/// <summary>
/// Creates a event Delegate for a JQuery widget, this points to the widget 
/// Example of how to use	$("#buyButton").live("click",$.createDelegate(this, this.onBuyClick));
/// 	onBuyClick: function(element,event){alert(this);alert(event.type+":"+$(element).attr("class"));	}
/// </summary>
/// <param name="classInstance">The object</param>
/// <param name="method">The method</param>
jQuery.createEventDelegate = function(classInstance, method) {
    return function() {
        var a = new Array();
        a.push(this);
        for (i = 0; i < arguments.length; i++) {
            a.push(arguments[i]);
        }
        return method.apply(classInstance, a);
    }
}

/// <summary>
/// Registers a class as a JQuery widget
/// </summary>
/// <param name="widgetClass">The class</param>
jQuery.registerAsWidget = function(widgetClass) {
    if ($.ui.version < "1.8") {
        var o = new widgetClass();
        $.widget(widgetClass.getName(), o);
        var widget = $[widgetClass.getName().split(".")[0]][widgetClass.getName().split(".")[1]];
        widget.defaults = widgetClass.prototype.defaults;

        var getters = new Array();
        $.each(o, function(propertyName, property) {
            if ($.isFunction(property) && (propertyName.indexOf("get") == 0 || propertyName.indexOf("is") == 0)) {
                getters.push(propertyName);
            }
        }
	)
        widget.getter = getters;
    }
    else {
        var o = new widgetClass();
        if (typeof (o.options) == "undefined" && typeof (o.defaults) != "undefined")
            o.options = o.defaults;
        $.widget(widgetClass.getName(), o);
    }
}

/// <summary>
/// Makes logging to web console
/// </summary>
/// <param name="widgetClass">The message</param>
jQuery.log = function(message) {
    if (window.console) {
        if (console.log) {
            console.log(message);
        }
        if (console.debug) {
            console.debug(message);
        }
    } else {
        //        alert(message);
    }
};



jQuery.gotoUrl = function(url) {
    if ($.browser.opera || $.browser.msie) {
        // For IE and Opera. Needed to maintain referrer
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else {
        location.href = url;
    }
};


