//
// some general initialization here
//
if (typeof jQuery != "undefined") {
    $(function() {
        if ($.isFunction($.blockUI)) {
            // set default cursor for popup
            $.blockUI.defaults.css.cursor = "default";

            // enable overlay opacity for FF/Linux
            $.blockUI.defaults.applyPlatformOpacityRules = false;
        }
    });
}

/**
 * Redirects to url
 *
 * @param url Url to redirect
 */
function redirect(url) {
    window.location.href = url;
}

/**
 * Shows specified anchor
 *
 * @param name Name of anchor to show
 */
function goToAnchor(name) {
    var url = window.location.href;
    var parts = url.split("#");

    window.location.href = parts[0] + "#" + name;
}

/**
 * Convert new line symbols to html br
 *
 * @param value input string to convert
 * @return string
 */
function nl2br(value) {
    return value.replace(/\r\n/g, "<br />")
        .replace(/\n/g, "<br />")
        .replace(/\r/g, "<br />");
}

/**
 * Removes html entities from string
 *
 * @param value input string
 * @return string
 */
function stripTags(value) {
    return value.replace(/<\/?[^>]+>/gi, '');
}

/**
 * Returns alias way formatted string
 *
 * @param input
 * @return string
 */
function getAliasString(input) {
    return input.toLowerCase()
        .replace(/[^\w\d]+/g, '-')
        .replace(/-{2,}/g, '-')
        .replace(/^-/, '')
        .replace(/-$/, '');
}

/**
 * Creates alias string from one field and put it to another field
 *
 * @param fromElSelector
 * @param toElSelector
 */
function aliasCreator(fromElSelector, toElSelector) {
    var changeAlias = function() {
        var alias = $(fromElSelector).val();
        $(toElSelector).val(getAliasString(alias));
    };

    $(fromElSelector).keyup(changeAlias);
    $(fromElSelector).blur(changeAlias);
}

/**
 * Extends one class from another
 * 
 * @param child
 * @param parent
 */
function extendClass(child, parent) {
	var F = function() { };
	F.prototype = parent.prototype;
	
    child.prototype = new F();
	child.prototype.constructor = child;
	child.superclass = parent.prototype;
}

/**
 * Class for validation purposes
 */
var Validator = {
    isEmpty: function(value) {
        return value == "" || value == null || value == undefined;
    },
    isEmail: function(value) {
        return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(value);
    }
};

var tooltipTimeout = null;

/**
 * Shows modal popup with message. Redirects to url if specified
 *
 * @param message Message to show
 * @param url Url to redirect (optional)
 */
function showTooltipMessage(message, url) {
    if (typeof jQuery != "undefined" && $.isFunction($("body").dialog)) {
        if ($("#message-dlg").length == 0) {
            $(document.body).append("<div id=\"message-dlg\" style=\"display: none;\"></div>")
        }

        $("#message-dlg").html(message);
        $("#message-dlg").dialog({
            title: "Message",
            minHeight: 0,
            modal: true,
            resizable: false,
            buttons: {
                "Ok": function() {
                    $(this).dialog("close");

                    if (url) {
                        redirect(url);
                    }
                }
            },
            close: function(event, ui) {
                clearTimeout(tooltipTimeout);

                if (url) {
                    redirect(url);
                }
            }
        });

        tooltipTimeout = setTimeout(function() {
            $("#message-dlg").dialog("close");

            if (url) {
                redirect(url);
            }
        }, 3000);
    } else {
        alert(message);
    }
}

/**
 * Shows confirmation modal popup with message
 *
 * @param message Message to show
 * @param okCallback
 * @param cancelCallback
 */
function showConfirmDlg(message, okCallback, cancelCallback) {
    if (typeof jQuery != "undefined" && $.isFunction($("body").dialog)) {
        if ($("#confirm-dlg").length == 0) {
            $(document.body).append("<div id=\"confirm-dlg\" style=\"display: none;\"></div>")
        }

        $("#confirm-dlg").html(message);
        $("#confirm-dlg").dialog({
            title: "Confirm",
            minHeight: 0,
            modal: true,
            resizable: false,
            buttons: {
                "Ok": function() {
                    $(this).dialog("close");

                    if ($.isFunction(okCallback)) {
                        okCallback.call(this);
                    }
                },
                "Cancel": function() {
                    $(this).dialog("close");

                    if ($.isFunction(cancelCallback)) {
                        cancelCallback.call(this);
                    }
                }
            }
        });
    } else {
        if (confirm(message)) {
            okCallback.call(this);
        } else {
            cancelCallback.call(this);
        }
    }
}

/**
 * Shows popup with video
 *
 * @param type Type of video popup. Can be 'home', 'widget', 'portfolio' or 'microsite-rd'
 */
function showVideoPopup(type) {
    var id = "video-popup";
    var selector = "#" + id;
    var videoEl = $(selector);
        
    if (videoEl.length == 0) {
        $(document.body).append("<div id=\"" + id + "\" style=\"display: none;\"></div>");
    } else {
        videoEl.empty();
    }
    
    if (!type) {
        type = "home";
    }
    
    $.ajax({
        url: '/ajax/video-popup',
        data: {type: type},
        success: function(data) {
            $(selector).html(data);
            
            $.blockUI({
                message: $(selector),
                css: {
                    top: '50%',
                    left: '50%',
                    width: '714px',
                    height: '560px',
                    marginLeft: '-357px',
                    marginTop: '-280px'
                }
            });

            var escapeBtnHandler = function(e) {
                var code = e.keyCode ? e.keyCode : e.which;

                if (code == 27) {
                    $.unblockUI();
                }
            };

            // setup esc button to exit from popup
            $(document).unbind('keyup', escapeBtnHandler);
            $(document).keyup(escapeBtnHandler);
        },
        error: function() {
            showTooltipMessage(Config.generalErrorMessage);
        }
    })
}

/**
 * Make specified element's text expandable
 *
 * @param symbols Number of symbols to show before "More" link (optional)
 * @param selector Selector for element(s) (optional)
 */
function makeExpandableText(symbols, selector) {
    if (typeof jQuery == "undefined" || !$.isFunction($("body").expander)) {
        return;
    }

    if (!symbols) {
        symbols = 180;
    }
    
    if (!selector) {
        selector = ".sys-expandable-text";
    }
    
    $(selector).expander({
        slicePoint: symbols,
        expandText: "Read More",
        userCollapseText: "Less"
    });
}

