﻿function CheckBoxClickEvent(sender, selector) {
    var sibling = sender.parent().siblings(selector);
    if (sender.is(":checked")) {
        sibling.slideDown();
        sibling.find("span.validator").enableValidators();
    }
    else {
        sibling.slideUp();
        sibling.find("span.validator").disableValidators();
    }
}

function findPosition(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function findPositionX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
        } while (obj = obj.offsetParent);
    }
    return curleft;
}

function findPositionY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return curtop;
}

// Events to show and hide the hint callouts.
function InovexUserControlHintFocused(e) {
    if (!e.data.owner) {
        e.data.owner = $(this);
    }
    if (!e.data.width) {
        e.data.width = e.data.owner.outerWidth() + 15;
    }
    if (!e.data.height) {
        e.data.height = e.data.owner.outerHeight() * -1;
    }
    if (!e.data.htmlContent) {
        e.data.htmlContent = $(this).siblings(".hint").html();
    }
    if (!e.data.orient) {
        e.data.orient = "left";
    }
    if (!e.data.align) {
        e.data.align = "left";
    }
    // Width of the callout
    if (!e.data.hintWidth) {
        e.data.hintWidth = 175;
    }

    // Remove any current callouts
    var calloutItems = $("body > div.jQueryCalloutCorners, body > div.jQueryCallout");
    calloutItems.remove();

    if (jQuery.trim(e.data.htmlContent) != "") {
        e.data.owner.callout({
            orient: e.data.orient,
            cornerRadius: 5,
            className: "hintCallout",
            text: e.data.htmlContent,
            align: e.data.align,
            nudgeHorizontal: e.data.width,
            nudgeVertical: e.data.height,
            width: e.data.hintWidth
        });
    }
}
function InovexUserControlHintLostFocus(e) {
    if (!e.data.owner) {
        e.data.owner = $(this);
    }
    e.data.owner.closeCallout();
}

function ReloadInovexUserControlEvents() {
    if (typeof (LoadInovexCheckBoxEvents) != "undefined") {
        LoadInovexCheckBoxEvents();
    }
    if (typeof (LoadInovexDateEvents) != "undefined") {
        LoadInovexDateEvents();
    }
    if (typeof (LoadInovexDropDownListEvents) != "undefined") {
        LoadInovexDropDownListEvents();
    }
    if (typeof (LoadInovexLabelEvents) != "undefined") {
        LoadInovexLabelEvents();
    }
    if (typeof (LoadInovexRadioButtonListEvents) != "undefined") {
        LoadInovexRadioButtonListEvents();
    }
    if (typeof (LoadInovexTextBoxEvents) != "undefined") {
        LoadInovexTextBoxEvents();
    }
}

// Check and uncheck a checkbox
jQuery.fn.extend({
    check: function() {
        return this.each(function() { this.checked = true; });
    },
    uncheck: function() {
        return this.each(function() { this.checked = false; });
    }
});

// Disables or enables the specified validators. If disabled,
// then the control's and the page's validators are reset. If enabled,
// then it is hidden.
jQuery.fn.extend({
    enableValidators: function() {
        $(this).disableValidators(false);
    },
    disableValidators: function(disabled) {
        // If disabled is not defined, then it's defaulted to true.
        if (typeof disabled == "undefined") {
            disabled = true;
        }
        $(this).each(function(i) {
            this.enabled = !disabled;
            if (disabled) {
                ValidatorValidate(this);
                ValidatorUpdateIsValid();
            }
            else {
                this.style.display = "none";
            }
        });
    }
});

function noConfirm() {
    requireConfirmation = false;
}

function returnFalse() {
    return false;
};

$.fn.disableLink = function() {
    $(this).click(returnFalse);
};

$.fn.enableLink = function() {
    $(this).unbind("click", returnFalse);
};

// Vertically aligns an element to the middle.
$.fn.alignMiddle = function() {
    this.parent().css("position", "relative");
    this.css("position", "absolute");
    this.css("top", (this.parent().height() / 2) - (this.height() / 2) + "px");
};

// Gets the maximum height from each of the elements.
$.fn.getMaximumHeight = function() {
    var maxHeight = 0;
    for (var i = 0; i < this.length; i++) {
        var height = this.eq(i).height();
        if (height > maxHeight) {
            maxHeight = height;
        }
    }
    return maxHeight;
};

$.fn.scrollToObject = function(duration) {
    if (typeof duration == "undefined") {
        duration = 250;
    }
    var x = y = 0;
    if (this.length > 0) {
        y = findPositionY(this[0]);
        $.scrollTo({ top: y, left: x }, duration);
    }
};

$.fn.in_ShiftDown = function(pixels, settings, callback) {
    var duration = 200;
    if (settings != null && settings.duration != null) {
        duration = settings.duration;
    }
    return this.animate({ top: pixels }, { duration: duration, queue: false, complete: callback });
};

$.fn.in_Resize = function(w, settings, callback) {
    var duration = 200;
    if (settings != null && settings.duration != null) {
        duration = settings.duration;
    }
    return this.animate({ width: w }, { duration: duration, queue: false, complete: callback });
};

// Returns whether the item is visible within its scrollable container.
$.fn.in_isVisible = function(container) {
    if (container.length > 0) {
        var _left = $(this).position().left;
        var containerBegin = container.scrollLeft();
        var containerEnd = container.scrollLeft() + container.width();
        return (_left >= containerBegin && _left < containerEnd) ? true : false;
        //return (_left > 0 && _left < container.outerWidth()) ? true : false;
    }
    else {
        return true;
    }
};

jQuery.extend({
    isNullOrEmpty: function(a) {
        if (a == "" || a == null) {
            return true;
        }
        else {
            return false;
        }
    }
});

/*
* String extensions
*/
String.prototype.beginsWith = function(t, i) {
    if (i == false) {
        return t == this.substring(0, t.length);
    }
    else {
        return t.toLowerCase() == this.substring(0, t.length).toLowerCase();
    }
};

String.prototype.endsWith = function(t, i) {
    if (i == false) {
        return t == this.substring(this.length - t.length);
    }
    else {
        return t.toLowerCase() == this.substring(this.length - t.length).toLowerCase();
    }
};
