jQuery.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                jQuery(self).dequeue();
            }, time);
        });
    };

var Core = {};

ge = function(id) {return typeof id == 'string' ? document.getElementById(id) : id;}

Core.mask = {};
Core.mask.instance = null;
Core.mask.show = function()
{
    var wheight=(window.innerHeight)?window.innerHeight:
        ((document.all)?document.body.offsetHeight:null);
    if (this.instance == null)
    {
        var wrapper = ge('wrapper');
        var mask = document.createElement('div');
        mask.style.position = 'absolute';
        mask.style.left = '0px';
        mask.style.top = '0px';
        mask.style.width = '100%';
//        mask.style.height = '100%';
        mask.style.opacity = '0.5';
        mask.style.display = 'none';
        mask.style.background = '#ffffff';
        mask.innerHTML = '<img style="position: relative; top: 50%; left: 50%;" src="' + WEB_ROOT + 'images/ajax-loader.gif" alt="Загрузка" />';
        this.instance = mask;
        wrapper.appendChild(mask);
    }

    this.instance.style.height = wheight + 'px';

    this.instance.style.display = 'block';
}

Core.mask.hide = function()
{
    if (this.instance != null)
    {
        this.instance.style.display = 'none';
    }
}

Core.request = function(url, successMessage)
{
    Core.mask.show();
    $.ajax({
        url: url,
        type: 'get',
        cache: false,
        success: function(msg) {
            Core.mask.hide();
            if (msg != '')
                alert(msg);
            else
                alert(successMessage)
        }
    });
}

Core.load = function(url, method, data, blockId)
{
    $('#'+ blockId).html('<img src="' + WEB_ROOT + 'images/ajax-loader.gif" alt="Загрузка" />');
    var dataString = '';
    if (typeof data == 'object')
    {
        for (var i in data)
        {
            if (data[i] != null)
            {
                dataString += (i + '=' + data[i] + '&');
            }
        }
    }
    else if (typeof data == 'string')
        dataString = data;
    else
        dataString = '';

    $.ajax({
        url: url,
        type: method,
        data: dataString,
        cache: false,
        success: function(msg) {
            $('#'+ blockId).html(msg);
        }
    })
}

Core.wordHighlight = function(words, selector, subselectors, makeBold, color)
{
    color = color ? color : '#00ee00';
    makeBold = makeBold ? makeBold : false;

    for (var k = 0; k < subselectors.length; k++)
    {
        $(subselectors[k], selector).each(function(){
            var text = $(this).html();
            var textParts;
            for (var i = 0; i < words.length; i++)
            {
                textParts = text.split(/[<>]/);
                text = '';
                for (var k = 0; k < textParts.length; k += 2)
                {
                    textParts[k] = textParts[k].replace(new RegExp('(' + words[i] + ')', 'ig'), '<span ' + Core.createStyles(makeBold, color) + '>$1</span>');
                    text += textParts[k];
                    if (k + 1 < textParts.length)
                        text += '<' + textParts[k + 1] + '>';
                }
                $(this).html(text);
            }
        });
    }
};

Core.createStyles = function (bold, color)
{
    var style = '';
    if (bold)
    {
        style += 'font-weight: bold;';
    }
    else
    {
        style += 'font-weight: normal;';
    }

    if (color)
    {
        style += ('color: ' + color + ';');
    }

    return 'style="' + style + '"';
}

Core.moveInnerHtmltoValue = function (from, to)
{
    if (from && from.innerHTML && to)
        to.value = from.innerHTML;
}

//$(document).ready(function(){
//    Core.wordHighlight(new Array('new', 'eng'), '#search-result', new Array('.message-caption', '.message-body'), true);
//    Core.wordHighlight(new Array('mess', 'with'), '#search-result', new Array('.message-caption', '.message-body'), true, '#55aa55');
//});
