function Eventer(){
    this.loaded =  new Array();
    this.hiding = new Array();
    this.boneHtml =
    '<div id="$name" name="div_dialog" style="position : absolute;">\n\
        <div class="shadow" name="div_dialog">\n\
            <div id="$name_child" class="content" style="" name="div_dialog" >\n\
                $text\n\
            </div>\n\
        </div>\n\
    </div>';
    this.errorBoneHtml =
    '<div id="$name" name="div_dialog" style="position : absolute;hidden:hidden;">\n\
        <div class="attention noprint " name="div_dialog">\n\
            <div id="$name_child" class="errors" name="div_dialog" >\n\
                $text\n\
            </div>\n\
        </div>\n\
    </div>';
    this.boneHtmlBlackScreen =
    '<div id=\"dialog_div_back\" class=\"dialog_back\"></div>\n\
    <div id=\"dialog_div\" class=\"dialog_div\">$content</div>';
    this.boneHtmlBlackScreen1 =
    '<div id=\"dialog_div_back\" class=\"dialog_back\"></div>';
    this.showBlackScreen = function(){
        $("body").append(this.boneHtmlBlackScreen1);
        $("#dialog_div_back").width($(document).width());
        var hlim = 4000;
        var h =$(document).height();
        h = (h>hlim)?hlim:h;
        $("#dialog_div_back").height(h);
        $('#dialog_div_back').css('filter', 'alpha(opacity=60)');
        $("#dialog_div_back").fadeIn("1000");
    }
    this.hideBlackScreen = function(){
        $("#dialog_div_back").fadeOut("1000",function(){
		   $("#dialog_div_back").remove();
	    });
    }
    this.showBlackScreenDialog = function (link){
			var t = this;
            $.getJSON(link,function (data){
            var html = t.putArgs(t.boneHtmlBlackScreen,{"content" : data.html});
            $("body").append(html);
            var hlim = 4000;
            var h =$(document).height();
            h = (h>hlim)?hlim:h;
            $("#dialog_div_back").height(h);
            $('#dialog_div_back').css('filter','alpha(opacity=60)');
            $("#dialog_div").css("top",$(window).scrollTop()+200);
            $("#dialog_div").css("left","50%");
            $("#dialog_div").css("margin-left","-"+($("#dialog_div").width()/2)+"px");
            $("#dialog_div_back").fadeIn("1000");
            $("#dialog_div").fadeIn("1000");
        });
    }
	this.hideBlackScreenDialog = function (){
        $("#dialog_div_back").fadeOut("1000",function(){
		   $("#dialog_div_back").remove();
	    });
	    $("#dialog_div").fadeOut("1000",function(){
		   $("#dialog_div").remove();
	    });
    }
	this.showBlackScreenDialog2 = function (link){
			var t = this;
            $.getJSON(link,function (data){
			var html = '';
			if(data.errMsg !== undefined){
				html = t.putArgs(t.boneHtmlBlackScreen,{"content" : '<div class="attention"><span class="errors">Klaida!</span></div>'});	
			}else{
				html = t.putArgs(t.boneHtmlBlackScreen,{"content" : data.html});
			}	
				$("body").append(html);
				var hlim = 4000;
				var h =$(document).height();
				h = (h>hlim)?hlim:h;
				$("#dialog_div_back").height(h);
				$('#dialog_div_back').css('filter','alpha(opacity=60)');
				$("#dialog_div").css("top","10%");
				$("#dialog_div").css("left","50%");
				$("#dialog_div").css("margin-left","-"+(($("#dialog_div").width())/2)+"px");
				$("#dialog_div_back").fadeIn("1000");
				$("#dialog_div").fadeIn("1000");
			if(data.errMsg !== undefined) setTimeout(function() {t.hideBlackScreenDialog()} , 2000);
        });
    }
    this.showFolowMouse = function (name,msg,element){
        if(this.showIfExists(name)){return;};
        var html = this.putArgs(this.boneHtml,{"name" : name,"text" : msg});
        $("body").prepend(html);
        $(element).hover(
        function(){},
        function (event){
            $("#"+name).fadeOut();
        });
        $(element).mousemove(function (e){
            $("#"+name).css({"top":e.pageY+15,"left":e.pageX+15});
        })
    }
    this.showToPos = function (name,msg,element,x,y){
        if(this.showIfExists(name)){return;};
        var html = this.putArgs(this.boneHtml,{"name" : name,"text" : msg});
        $("body").prepend(html);
        var t =this;
        $(element).hover(
        function(){},
        function (){
            t.hide(name);
        });
        $("#"+name).css({"top":x,"left":y});
        this.show(name);
    }
    this.showNearElement = function (name,msg,element,position,d,stay,type,options){
        if(this.showIfExists(name)){return;};
        var html = "";
        if(type!=null){
            switch (type)
            {
                case 'error':
                    html = this.errorBoneHtml;
                break;
                default:
                    html = this.boneHtml;
                break;
            }
        }else{
            html = this.boneHtml;
        }
        html = this.putArgs(html,{"name" : name,"text" : msg});
        if(stay){
            $(element).append("<td>"+html+"</td>");
        }else{
            $("body").append(html);
        }
        this.loaded[name] = true;
        var t =this;
        $("#"+name).hover(
            function(){},
            function (e){
                if(options != null && options.delay!= undefined){
                    t.hiding[name] = true;
                    t.hide(name,options.delay);
                }else{
                    t.hide(name);
                }
        });
        $(element).hover(
            function(){},
            function (e){
                if(options != null && options.delay!= undefined){
                    t.hiding[name] = true;
                    t.hide(name,options.delay);
                }else{
                    t.hide(name);
                }
        });
        var el = $(element).offset().left;
        var et = $(element).offset().top;
        var ew = $(element).width();
        var eh = $(element).height();
        var nw = $("#"+name+"_child").width();
        var nh = $("#"+name+"_child").height();
        if(nw>400 || (nw/nh)>10 || (nh/nw)>10){$("#"+name).css("width", "400");nw=400;}
        if(nw == 0)$("#"+name).css("width", "400");
        if(nh == 0)$("#"+name).css("height", "100");
        if(!d){
            d = 5;
        }
        var nl = 0;
        var nt = 0;
        if(!position){
            position = 'bottom';
        }
        switch (position){
            case 'top':
                nl = el + (ew/2-nw/2);
                nt = et - (d + nh);
                $("#"+name).css("padding-bottom",d);
            break;
            case 'right':
                nl = el + (d + ew);
                nt = et + (eh/2 - nh/2);
                $("#"+name).css("padding-left",d);
            break;
            case 'bottom':
                nl = el + (ew/2-nw/2);
                nt = et + (d + eh);
                $("#"+name).css("padding-top",d);
            break;
            case 'left':
                nl = el - (d + nw);
                nt = et + (eh/2 - nh/2);
                $("#"+name).css("padding-right",d);
            break;
            default:
                console.error("Pozicija \""+position+"\" nėra aprašyta.");
                return false;
            break;
        }
        $("#"+name).css({"top":nt,"left":nl});
		$("#"+name).css("z-index",3);
        this.show(name);
    }
    this.show = function (name){
        this.hiding[name] = false;
        $("#"+name).show();
    }
    this.hide = function (name,delay){
        if(delay!= null){
            var t = this;
            setTimeout(function(){
                if(t.hiding[name]){
                    $("#"+name).hide();
                    t.hiding[name] = false;
                }
            },delay)
        }else{
            $("#"+name).hide();
        }
    }
    this.showIfExists = function(name){
         if($("#"+name).length != 0){
             this.show(name);
             return true;
         }else{
            return false;
         }
    }
    this.putArgs = function (html,args){
        for(var i in args){
            var re = new RegExp("\\$"+i, 'gi');
            html = html.replace(re,args[i]);
        }
        return html;
    }
    this.labelBoneHtml = '<div class="dialog_label" id="$name">$content</div>'
    this.showLabel = function(name,content,element,side,position,attr){
        if(this.showIfExists(name)){return;};
        var html = this.putArgs(this.labelBoneHtml,{name:name,content : content});
        var x = 0,y =0;
        var h = "15";
        switch(side){
            case 'top':
                    switch(position){
                        case 'left':
                            x = $(element).offset().left;
                            y = $(element).offset().top - h;
                            break;
                        default :
                            console.error("Opcijos position="+position+" veikymas dar neaprašytas.");
                            break;
                    }
                break;
            default :
                console.error("Opcijos side="+side+" veikymas dar neaprašytas.");
                break;
        }
        $("body").prepend(html);

        $.each(attr,function(k,v){
            $("#"+name).css(k,v);
        });
        $("#"+name).css({left:x,top:y,height:h});
        $("#"+name).show();

    }
    this.hideLabel = function(name){
        $("#"+name).hide();
    }
    this.throwError = function (msg,id,fade){
        $("#"+id)
        .html("<div id=\"error_uzkl\" class=\"attention noprint\">\n\
                    <div class=\"errors\">"+msg+"</div>\n\
                </div>");
        if(fade){
            $("#error_uzkl").fadeOut(3500,function(){
                $("#error_uzkl").remove();
            });
        }
    }
    this.throwSuccess = function(msg,id,fade){
        $("#"+id)
        .html("<div id=\"success_uzkl\" class=\"attention noprint\">\n\
                    <div class=\"good\">"+msg+"</div>\n\
                </div>");
        if(fade){
            $("#success_uzkl").fadeOut(3500,function(){
                $("#success_uzkl").remove();
            });
        }
    }
}


