﻿if (!rentcycle) var rentcycle = {};
if (!rentcycle.interactive) rentcycle.interactive = {};

rentcycle.interactive = {
    blockElement: function (elementSelector) {
        rentcycle.interactive.blockElement_with_options(elementSelector, '');
    },

    blockElement_with_options: function (elementSelector, message) {
        var template = '<div class="block-element-container"><div class="loader-graphic"><img src="/content/images/icons/loaders/ajax-loader_square-circle.gif" /></div><div class="loader-graphic">{0}</div></div>';

        // update message content
        var messageContent = String.format(template, message);

        $(elementSelector).block({
            message: messageContent,
            overlayCSS: {
                backgroundColor: '#FFF',
                opacity: 0.6
            },
            css: { border: 'none', backgroundColor: 'none' }
        });
    },

    unblockElement: function (elementSelector) {
        $(elementSelector).unblock();
    },

    loadModalAjaxContent: function (targetFile, callback) {
        $.ajax({
            url: targetFile,
            cache: true,
            success: function (html) {
                $("#modContainer").html(html);
                if ((callback != null) && (callback != 'undefined')) callback(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },

    reloadParent: function () {
        parent.location.href = parent.location.href;
    },

    notify: function (message, target_block, positive) {
        var span_open = "<span>";
        if (positive) {
            span_open = "<span class='positive'>";
        }
        $(target_block).hide().html(span_open + message + "</span>").fadeIn(500);
        setTimeout('$("' + target_block + '").fadeOut(500).html("");', 5000);
    },

    appMessage: function (title, message) {

        $.blockUI({
            message: '<div class="app-message-content"><h1>' + title + '</h1>' + '<h2>' + message + '</h2></div>',
            fadeIn: 700,
            fadeOut: 700,
            timeout: 4000,
            showOverlay: false,
            centerY: false,
            css: {
                width: '350px',
                top: '10px',
                border: 'none',
                padding: '5px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                'border-radius': '10px',
                opacity: .8,
                color: '#fff'
            }
        });

    },

    showLessMoreText: function (containingElementSelector, characterCount) {
        var container = $(containingElementSelector)
        var text = container.text();
        if (text.length > characterCount) {
            var shortened = container.text().substring(0, text.indexOf(' ', characterCount) + 1);
            container.text(shortened + '... ');

            var showMore = $('<a href="javascript:;"></a>').html("(see more)");

            showMore.click(function (event) {

                $(container).text(text);
                showMore.remove();
            });

            container.append(showMore);
        }
    }
};

//
// MARKETPLACE NOTIFICATION BAR
//

if (!rentcycle.interactive.mkt_notify) rentcycle.interactive.mkt_notify = {};
rentcycle.interactive.mkt_notify = {
    icon_fail: "/content/images/icons/cancel.png",
    icon_success: "/content/images/icons/accept.png",
    icon_neutral: "/content/images/icons/information.png",
    notify_bar: null,
    opened: false,
    show: function (status, msg) {
        if (this.notify_bar == null) {
            this.notify_bar = $('#rc_mkt_notifybar');
        }
        if (this.opened) {

            this.notify_bar.slideToggle('750', function () {
                rentcycle.interactive.mkt_notify.show(status, msg);
            });

            this.opened = false;
        }
        else {

            this.notify_bar.removeClass();

            if (status == -1) {
                this.notify_bar.addClass('notify_failure');
            }
            else if (status == 1) {
                this.notify_bar.addClass('notify_success');
            }

            $('#rc_mkt_notifybar_text').html(msg);

            this.notify_bar.slideToggle();
            this.opened = true;

            $('#rc_mkt_notifybar_close').unbind('click').click(function () {
                rentcycle.interactive.mkt_notify.opened = false;
                rentcycle.interactive.mkt_notify.notify_bar.slideToggle('750', function () { rentcycle.interactive.mkt_notify.notify_bar.removeClass(); });
            });
        }
    },
    close: function () {
        if (rentcycle.interactive.mkt_notify.opened) {
            rentcycle.interactive.mkt_notify.opened = false;
            this.notify_bar.slideToggle('750', function () { rentcycle.interactive.mkt_notify.notify_bar.removeClass(); });
        }
    }
};
