﻿/// <summary>
/// Function to Add hovering to an element
/// </summary>
$.fn.addHover = function () {
    $(this).hover(function () {
        $(this).addClass("hover");
    }, function () {
        $(this).removeClass("hover");
    });

    return this;
}

/// <summary>
/// Function to Center an element
/// </summary>
$.fn.center = function (options) {
    var options = $.extend({ // Default values
        inside: window, // element, center into window
        transition: 0, // millisecond, transition time
        minX: 0, // pixel, minimum left element value
        minY: 0, // pixel, minimum top element value
        vertical: true, // booleen, center vertical
        withScrolling: false, // booleen, take care of element inside scrollTop when minX < 0 and window is small or when window is big 
        horizontal: true, // booleen, center horizontal
        vertical: true,
        width: 0,
        height: 0
    }, options);

    return this.each(function () {
        var $this = $(this);
        var props = { position: "absolute" };
        
        if (options.vertical) {
            var top = ($(options.inside).height() - ($this.outerHeight() > 0 ? $this.outerHeight() : options.height)) / 2;
            if (options.withScrolling) {
                top += $(options.inside).scrollTop() || 0;
            }

            top = (top > options.minY ? top : options.minY);
            $.extend(props, { top: top + "px" });
        }
        
        if (options.horizontal) {
            var left = ($(options.inside).width() - ($this.outerWidth() > 0 ? $this.outerWidth() : options.width)) / 2;
            if (options.withScrolling) {
                left += $(options.inside).scrollLeft() || 0;
            }

            left = (left > options.minX ? left : options.minX);
            $.extend(props, { left: left + "px" });
        }

        if (options.transition > 0) {
            $this.animate(props, options.transition);
        }
        else {
            $this.css(props);
        }

        return $this;
    });
}

/// <summary>
/// Function to check if an element exists
/// </summary>
$.fn.exists = function () {
    return ($(this).length > 0);
}

$.fn.hasChildren = function (element) {
    return $(this).find(element).length > 0;
}

/// <summary>
/// Function to format a string (like the .net string.Format function)
/// </summary>
$.fn.format = function (text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; ++token) {
        //iterate through the tokens and replace their placeholders from the text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }

    return text;
};

/// <summary>
/// Function to check if a string Startswith the given text
/// </summary>
$.fn.startsWith = function (text) {
    if (this != null && text != null) {
        return (text.toLowerCase() == this.substring(0, text.length).toLowerCase());
    }

    return false;
}

/// <summary>
/// Function to add a Zebra element to a container
/// </summary>
$.fn.zebra = function () {
    var zebra = $(this);

    zebra.children(":not(.header)")
        .filter(":even").removeClass("odd").addClass("even").end()
        .filter(":odd").removeClass("even").addClass("odd").end()
        .addHover();
}

/// <summary>
/// Modal for the InfoPunt
/// </summary>
$.fn.modal = function (options) {
    var element = $(this);
    var body = $("body");
    this.options = $.extend({
        center: true,
        parent: false,
        title: "Modal dialog",
        width: 800,
        height: 600,
        iframe: false,
        url: ""
    }, options);

    var modal = $("#modaldialog");

    //Get the parent, if it is given
    if (this.options.parent && $(parent).exists() && parent.$("#modaldialog").exists()) {
        modal = parent.$("div#modaldialog");
        body = parent.$("body");
    }

    if (!modal.exists()) {
        var dialog = $("<div />");
        dialog.attr("id", "modaldialog");

        body.append(dialog);

        modal = $("#modaldialog");
    }

    if (modal.exists()) {
        modal.empty();

        var modalOverlay = $('<div id="modaloverLay" />').html("&nbsp;");

        modalOverlay.height($(document).height());
        //close of the Modal
        modalOverlay.click(function () {
            container.fadeOut();
            modal.hide();

            modalOverlay.hide();
        });

        //Add the Overlay
        modal.prepend(modalOverlay);

        var container = $("<div />").attr("id", "container");
        var header = $("<div />").attr("id", "header");
        $("<div />").attr("class", "title").html(this.options.title).appendTo(header);
        var close = $("<div />").attr("class", "close");
        close.text("X")

        //Add the Close functionality
        close.click(function () {
            modal.hide();

            modalOverlay.hide();
        });

        header.append(close);

        //Add functionality to a closebutton if it is present
        var closeButton = element.find("#closebutton");
        if (closeButton.exists()) {
            closeButton.click(function () {
                modal.hide();

                modalOverlay.hide();
            });
        }

        container.append(header);

        var showImage = false;

        //If it is an Iframe dialog, add the url to the Iframe
        if (this.options.iframe) {
            var iframe = $("<iframe />");
            iframe.attr("frameborder", "0").width(this.options.width).height(this.options.height);

            if (this.options.url.length > 0) {
                iframe.attr("src", this.options.url);
            } else {
                iframe.append(element);
            }
            container.append(iframe);
        } else {
            var img = element.find("img:first-child");

            if (img.exists()) {
                showImage = true;
                var image = new Image();
                image.src = img.attr("src");

                $(image).ready(function () {
                    var resize = false;
                    if (image.width > options.width || image.height > options.height) {
                        resize = true;
                    } else if (image.width < options.width || image.height < options.height) {
                        options.width = image.width;
                        options.height = image.height;

                        resize = true;
                    }

                    if (resize) {
                        img = img.resize({ width: options.width, height: options.height });
                    }
                });
            }

            container.append(element);
        }

        container.fadeIn().width(this.options.width + 2).height(this.options.height + 32);
        modal.append(container);

        if (this.options.center) {
            container.center({ width: this.options.width, height: this.options.height + 30, inside: this.options.parent ? parent.window : window });
        }

        modal.show();

        return modal;
    }
}

/// <summary>
/// Functions to Append a value to an Array
/// </summary>
function AppendToArray(array, value, add) {
    var position = jQuery.inArray(value, array);

    if (value) {
        //If the value doesn't exists and it must be added, add it to the Array
        if ((position == -1) || (add && position > -1)) {
            array.push(value);
        }
    }

    return array;
}
