﻿
(function ($) {

    // Tabs
    $.fn.tabThis = function (settings) {

        settings = jQuery.extend({
            baseTabOn: 'h2',
            tabClass: 'tab',
            activeClass: 'active'
        }, settings);

        return this.each(function () {

            var tabContainer = $(this);

            var tabs = $(tabContainer).find('.' + settings.tabClass);

            createTabs(tabContainer, tabs.find(settings.baseTabOn));

            $(tabs).hide().first().show();

        });

        function createTabs(container, tabs) {

            var ul = $('<ul />');

            tabs.each(function (i) {

                var tab = $(this);

                var li = $('<li />');

                var id = (container).find('.' + settings.tabClass + ':eq(' + i + ')').attr('id');

                var a = $('<a />').html(tab.html());

                //a.attr('href', '#' + id);
                a.attr('href', 'javascript:void(0);');

                a.click(function () {

                    $(container).find('.' + settings.tabClass).hide();

                    $(container).find('ul li').removeClass(settings.activeClass);

                    $(ul).find('li:eq(' + i + ')').addClass('active');

                    $(container).find('.' + settings.tabClass + ':eq(' + i + ')').show();

                });

                a.appendTo(li);

                li.appendTo(ul);

                tab.remove();

            });

            $(ul).find('li:first').addClass(settings.activeClass);

            $(container).prepend(ul);

        }

    };

    // Modal popup
    $.fn.modalPopup = function (options) {

        var defaults = {
            closeTrigger: null,
            closeTriggerDisablePostback: true,
            enableDefaultClose: true,
            defaultCloseClass: 'modal-popup-close',
            enableModalBackground: true,
            modalBackgroundClass: 'modal-bg',
            modalClass: 'modal-popup',
            content: null,
            contentFirst: true,
            enableEscClose: true
        };

        var opts = $.extend(defaults, options);

        return this.each(function () {

            var el = $(this);

            if (!el.hasClass(opts.modalClass))
                el.addClass(opts.modalClass);

            if (opts.enableModalBackground)
                showModalBackground();

            if (opts.content != null && opts.content != '') {

                var iframe = $('<iframe />');
                iframe.css({ border: 'none', width: '100%', height: '100%', overflow: 'auto', margin: '0', padding: '0' });
                iframe.attr('src', opts.content);

                // Add iframe before or after
                if (opts.contentFirst)
                    iframe.prependTo(el);
                else
                    el.append(iframe);

            }

            var close = $('<a class="modal-popup-close" />');

            // Handle default close
            if (opts.enableDefaultClose) {

                close.attr('href', 'javascript:void(0);');
                el.parent().append(close);

                close.bind('click', function () {

                    el.hide();
                    close.remove();
                    hideModalBackground();

                    if (opts.closeTriggerDisablePostback)
                        return false;

                });

            }

            show(el);

            if (opts.enableDefaultClose)
                show(close);

            position(el);

            if (opts.enableDefaultClose)
                positionClose(close, el);

            // Handle close trigger
            if (opts.closeTrigger != null) {

                $(opts.closeTrigger).click(function () {

                    el.hide();

                    if (close)
                        close.remove();

                    hideModalBackground();

                    if (opts.closeTriggerDisablePostback)
                        return false;

                });

            }

            // Handle resize
            $(window).resize(function () {
                if (el.css('display') != 'none') {
                    position(el);
                    if (opts.enableDefaultClose)
                        positionClose(close, el);
                }
            });

        });

        function show(el) {
            el.css({ 'display': 'block', 'position': 'fixed' });
        }

        function showModalBackground() {
            var bg = $('.' + opts.modalBackgroundClass);
            if (bg.length > 0)
                bg.show();
            else
                $('<div/>').addClass(opts.modalBackgroundClass).appendTo(document.body);
        }

        function hideModalBackground() {
            var bg = $('.' + opts.modalBackgroundClass);
            if (bg.length > 0)
                bg.hide();
        }

        function position(el) {
            var x = ($(window).width() / 2) - (el.width() / 2);
            if (x < 0) x = 0;
            var y = ($(window).height() / 2) - (el.height() / 2);
            if (y < 0) y = 0;
            el.css({ 'top': y + 'px', 'left': x + 'px' });
        }

        function elementSize(el) {

            var paddingLeft = parseInt(el.css('padding-left'), 10);
            var paddingRight = parseInt(el.css('padding-left'), 10);
            var paddingTop = parseInt(el.css('padding-left'), 10);
            var paddingBottom = parseInt(el.css('padding-left'), 10);

            return { width: el.width() + paddingLeft + paddingRight, height: el.height() + paddingTop + paddingBottom };

        }

        function positionClose(el, modal) {

            var x = ($(window).width() / 2) + (elementSize(modal).width / 2) + ((el.width() / 2) / 2) - 15;
            if (x < 0) x = 0;
            var y = ($(window).height() / 2) - (elementSize(modal).height / 2) + (el.height() / 2) + 6;
            if (y < 0) y = 0;
            el.css({ 'top': y + 'px', 'left': x + 'px' });
        }

    };

    // Popup
    $.fn.popup = function (options) {

        var defaults = {
            url: '',
            name: '',
            scrollbars: false,
            toolbar: false,
            status: false,
            location: false,
            menubar: false,
            directories: false,
            resizable: true,
            width: 675,
            height: 500,
            showmodal: false
        };

        if (defaults.name == '')
            defaults.name = defaults.url;

        var opts = $.extend(defaults, options);

        var features = 'toolbar=' + ((opts.toolbar) ? 'yes' : 'no');
        features += ',status=' + ((opts.status) ? 'yes' : 'no');
        features += ',scrollbars=' + ((opts.scrollbars) ? 'yes' : 'no');
        features += ',location=' + ((opts.location) ? 'yes' : 'no');
        features += ',menubar=' + ((opts.menubar) ? 'yes' : 'no');
        features += ',directories=' + ((opts.directories) ? 'yes' : 'no');
        features += ',resizable=' + ((opts.resizable) ? 'yes' : 'no');
        features += ',width=' + (opts.width);
        features += ',height=' + (opts.height);

        if (opts.showmodal) {

        } else {
            window.open(opts.url, opts.name, features);
        }

    };

    // Notification script
    $.notify = function (options) {

        var defaults = {
            fadeOutTime: 500,
            showTimeOut: 2000,
            type: 'success',
            message: ''
        };

        var opts = $.extend(defaults, options);

        var notifyContainer = $('<div id="notify-container" />');

        var notifyMessage = $('<div class="message"/>');

        notifyMessage.appendTo(notifyContainer);

        notifyMessage.html(opts.message);

        notifyContainer.addClass(opts.type);

        notifyContainer.appendTo('body');

        notifyContainer.stop().effect('shake', { times: 7, distance: 1, direction: 'down' }, 30, function () {

            var t = setTimeout(function () {
                $(notifyContainer).fadeOut(opts.fadeOutTime, function () {
                    $(notifyContainer).remove();
                })
            }, opts.showTimeOut);

        });

    };

    // Fix element
    $.fn.fixElement = function (options) {

        return this.each(function () {

            var el = $(this);

            var top = el.offset().top - parseFloat(el.css('marginTop').replace(/auto/, 0));

            $(window).scroll(function (event) {

                var y = $(this).scrollTop();

                if (y >= top)
                    el.addClass('fixed');
                else
                    el.removeClass('fixed');

            });

        });

    };

    // Extended title
    $.fn.extendedTitle = function (options) {

        var defaults = {
            titleClass: 'extendedTitle',
            titleRelativePosition: { x: 20, y: -70 }
        };

        var opts = $.extend(defaults, options);

        return this.each(function () {

            var el = $(this);

            var text = el.attr('title');

            if (text != null && text != undefined) {

                text = text.replace(/\n/g, "<br />");

                el.removeAttr('title');

                var titleEl = $('<div />');
                titleEl.addClass(opts.titleClass);
                titleEl.html(text);

                // Append to container
                el.parent().append(titleEl);

                el.parent().mouseover(function () {
                    titleEl.show();
                });

                el.parent().mousemove(function (e) {
                    titleEl.css('top', e.pageY + opts.titleRelativePosition.y + 'px');
                    titleEl.css('left', e.pageX + opts.titleRelativePosition.x + 'px');
                });

                el.parent().mouseout(function (e) {
                    titleEl.hide();
                });

            }

        });

    };

    // Accordion
    $.fn.accordion = function (options) {

        var defaults = {
            elementIdToSlideToggle: '',
            toggleSpeed: 'slow',
            activeClass: 'active'
        };

        var opts = $.extend(defaults, options);

        return this.each(function () {

            var el = $(this);

            el.click(function () {
                $(opts.elementIdToSlideToggle).slideToggle(opts.toggleSpeed);
                el.toggleClass(opts.activeClass);
            });

        });
    };

    // Watermark
    $.fn.watermark = function (options) {

        var defaults = {
            watermarkClass: 'watermarked'
        };

        var opts = $.extend(defaults, options);

        return this.each(function () {

            var element = $(this);

            // Set initial values.
            if (element.val() == '' || element.val() == this.title) {
                element.val(this.title);
                element.addClass(opts.watermarkClass);
            }

            // On focus.
            element.focus(function () {

                if (element.val() == this.title) {
                    element.removeClass(opts.watermarkClass);
                    element.val('')
                }
                else
                    return;
            });

            // When losing focus.
            element.blur(function () {

                if ($.trim(element.val()) == '') {

                    if (!element.hasClass(opts.watermarkClass)) {
                        element.addClass(opts.watermarkClass);
                    }

                    element.val(this.title);
                }
                else {
                    element.removeClass(opts.watermarkClass);
                    return;
                }
            });

            // Clear the watermarks before form submission.
            $('form:first').submit(function () {

                if (element.hasClass(opts.watermarkClass)) {
                    element.val('');
                }

            });

        });

    };

})(jQuery);
    
