(function($){
    $.fn.easyTooltip = function(options) {
        var defaults = { 
            xOffset: 10,
            yOffset: 25,
            tooltipId: "hint",
            clickRemove: false,
            content: "",
            useElement: ""
        };
        
        var options = $.extend(defaults,options);
        var content;
        
        this.each(function(){
            var title = $(this).attr("title");
            $(this).hover(function(e){
                content = (options.content != "")? options.content:title;
                content = (options.useElement != "")? $("#"+options.useElement).html() : content;
                $(this).attr("title", "");
                if(content != "" && content != undefined) {
                    $("body").append("<div id='" + options.tooltipId + "'>" + content + "</div>");
                    $("#" + options.tooltipId).css("position","absolute").css("top",(e.pageY - options.yOffset) + "px").css("left",(e.pageX + options.xOffset) + "px").css("display","none").fadeIn("slow")
                }
            }, function(){
                $("#" + options.tooltipId).remove();
                $(this).attr("title", title)
            });
            
            $(this).mousemove(function(e) {
                $("#" + options.tooltipId).css("top",(e.pageY - options.yOffset) + "px").css("left",(e.pageX + options.xOffset) + "px").fadeIn("slow");
            });
            
            if (options.clickRemove) {
                $(this).mousedown(function(e) {
                    $("#"+options.tooltipId).remove();
                    $(this).attr("title",title)
                })
            }
        })
    }
})(jQuery);

var abstrWindow = function(el) {
    var win = el;
    var msie = (jQuery.browser.msie && jQuery.browser.version == "6.0");
    var eff = !msie;
    this.init = function() { jQuery('body').append(win); }
    this.show = function(hdl) { eff? win.fadeIn(hdl) : win.show(hdl); }
    this.to_center = function() { 
        win.css({'opacity': 1, 'position': 'fixed', 'left': (jQuery(window).width() / 2) - (win.width() / 2), 'top': (jQuery(window).height() / 2) - (win.height() / 2)}); 
        if (msie) win.css({'position': 'absolute'}); 
    }
    this.hide = function(hdl) { eff? win.fadeOut(hdl) : win.hide(hdl); }
    this.getWin = function() { return win; }
    this.init();
    return this;
}

// сплешскрин
var splScreen;
var getSplScreen = function() {
   if (!splScreen) {
        var el = jQuery('<div id="spl_scr"></div>').css({'position': 'fixed',
                'background': '#fff', 'z-index': 20, 'opacity': 0.6, 'width': '100%',
                'height': '100%', 'top': 0, 'left': 0}).hide().click(function(){jQuery('.close').trigger('click')});

        splScreen = new abstrWindow(el); 
   }
   return splScreen;
}

// подсказака
function customWin(options) {

    var win = new abstrWindow($('<div class="dialog"><div class="bg"></div><div class="close"></div><div class="title"></div><div class="content"></div></div>'));
    
    var close = function() {
        win.hide(true, getSplScreen().hide(true));
        options.close_handler();
        return false;
    }

    var defaults = {
        'content': false,
        'class_name': false,
        'title': 'Custom Window',
        'confirm_btn_name': '',
        'cancel_btn_name': '',
        'confirm_btn_action': function(){},
        'cancel_btn_action': close,
        'close_handler': function(){},
        'open_handler': function(){}
    }

    var options = jQuery.extend({}, defaults, options);

    this.init = function() {
        /*var el = $('#show_block').clone();
        $('body').append(el);
        win = new abstr_window(el);*/
        if (options.class_name) win.getWin().addClass(options.class_name);
        update();
    }
    
    this.setContent = function(ctn) {
         win.getWin().find('.content').html(ctn);
         return this;
    }

    var setContent = this.setContent;

    this.open = function() {
        getSplScreen().show(function(){
            win.to_center();
            win.show();   
        });
        options.open_handler();
    }

    this.setOption = function(name, value) {
        options[name] = value;
        update();
        return this;
    }

    var update = function() {
        if (options.content) this.setContent(options.content);
        else {
            win.getWin().find('.title').html(options.title);
            //win.getWin().find('#show_bl_conf').html(options.confirm_btn_name).unbind('click').click(options.confirm_btn_action);
            //win.getWin().find('#show_bl_cancl').html(options.cancel_btn_name).unbind('click').click(options.cancel_btn_action);
            //if (!options.confirm_btn_name.length) win.get_win().find('#show_bl_conf').hide();
            //else win.get_win().find('#show_bl_conf').show();
        }

        getSplScreen().getWin().click(close);
        win.getWin().find('.close').click(close);
    }

    this.close = close;
    this.init();
}

var error, message;
$(document).ready(function(){
    
    $("a").easyTooltip()
    $("img").easyTooltip()
    
    
    var errorWin = null; 
    error = function(text) {
        if (errorWin == null) errorWin = new customWin({title: 'Ошибка', class_name: 'error-dialog'});
        var content = '<div class="error-content">' + text + '</div>';
        errorWin.setContent(content).open();
    }

    var messageWin = null; 
    message = function(text) {
        if (messageWin == null) messageWin = new customWin({title: 'Сообщение'});
        messageWin.setContent(text).open();
    }

});

