function API(){
    var currZ = 100;
    this.showWindow = function(p){
        
        if(typeof p.width != "undefined") WWidth=p.width;
        else WWidth = 600;
        
       /* if(typeof p.titleBgColor != "undefined") titleBgColor=p.titleBgColor;
        else titleBgColor = "#EFEFEF";*/
        
        var count=jQuery(".window").length;
	    
        var windowtmp='<div class="window" id="W'+(count+1)+'">'+
				'<div class="border">'+
					'<div class="close" onclick="API.windowClose(\'W'+(count+1)+'\')">'+
						'<img src="/images/icons/closeWin.jpg" border="0">'+
					'</div>'+
					'<div class="title" >'+p.title+'</div>'+
					'<div id="output" class="output" ></div>'+
					'<div class="content">'+p.content+
					'</div>'+
				'</div>'+
			'</div>';
	    var tmp=jQuery(windowtmp).appendTo("body");
	    /*tmp.find(".title").css({
	    	"background-color": titleBgColor
	    })*/
	    tmp = jQuery("#W"+(count+1)); //какой-то дикий косяк у переменной tml lenght =2 ???????
    	var optDrag={
    	    handle: ".title",
    	    cursor: "move"   
    	}
    	
    	if(typeof p.height != "undefined"){
    	    tmp.find(".content").css({
		         "overflow-x" : "auto",
		         "overflow-y" : "scroll",
		         "height" : p.height
    	    })
		}
    	
    	if(typeof tmp.draggable != "undefined"){
    	    tmp.draggable(optDrag);
    	    tmp.find(".border > .title").css("cursor" , "move");
    	}
    	
    	 
    	tmp.css('width',WWidth);
    	w= jQuery(window).width();   // returns width of browser viewport)
    	h=jQuery(window).height()/2;
    	h+=jQuery(document).scrollTop();	
    	topp=h-tmp.height()/2-0;
    	leftt=w/2-tmp.width()/2-0;
    	if(leftt<0) leftt=100;
    	if(topp < jQuery(document).scrollTop()) topp=jQuery(document).scrollTop()+30;
    	tmp.css({'left':leftt+"px",'top':topp+"px"});
    	
    	
    	var params=p;
    	
    	if(typeof p.modal != "undefined"){
    	    tmp.data({'modal':'true'})
    	    jQuery("body").append("<div style='position:absolute;top:0px;left:0px;' class='documentBgShade' id='documentBgShade_"+tmp.attr("id")+"'></div>");
    	    
            jQuery("#documentBgShade_"+tmp.attr("id")).css({
                opacity: 0.5,
                width : jQuery(document).width(),
                height : jQuery(document).height(),
                "background-color" :"black",
                "z-index" : API.newCurZ(),
                "display" : "block"})
        }
        tmp.css('z-index',API.newCurZ());
    	tmp.fadeIn(300,function(){
 	
    	})
    	
    	if(typeof(params) != "undefined"){
		  if(typeof(params.editorHeight)!='undefined') editorHeight=params.editorHeight;
            else editorHeight=200; 
		}else editorHeight=200; 
		API.afterLoad({'id':tmp.attr("id"),"editorHeight":editorHeight});
    	return tmp.attr("id");
    	
    }
    
    this.windowClose = function(id){
        jQuery("#"+id).fadeOut(300,function(){
            if(typeof(jQuery(this).data()) != "undefined"){
                jQuery("#documentBgShade_"+id).remove();
            }
            jQuery(this).remove();
        })
    }
    
    this.windowCloseAll = function(){
        jQuery(".window").fadeOut(300,function(){
            jQuery(".documentBgShade").remove();
            jQuery(this).remove();
        })
    }
    
    this.showMessage = function(message,type,options){
	    if (typeof type == "undefined") type=1;
    	var start=0;
    	var callback=0;
    	var duration=1500;
    	var durationIn= 100;
    	var durationOut= 100;
    	if (typeof options != "undefined")    	{
    		if (typeof options.start != "undefined") start=options.start;
    		if (typeof options.duration != "undefined") duration=options.duration;
    		if (typeof options.durationIn != "undefined") durationIn=options.durationIn;
    		if (typeof options.durationOut != "undefined") durationOut=options.durationOut;
    		if (typeof options.callback != "undefined") callback=1;
    	}
    	
    	if(start==0 || start==1)
    	{
    		var DClass;
    		switch(type)
    		{
    			case 1:
    			  DClass="M_accept";
    	 			 break;
    			case 0:
    	  			DClass="M_error";
    	  		break;
    	  		/*case 2:
    	  			DClass="M_loading";
    	  		break;*/
    			default:
    	  			DClass="M_default";
    		}
    		w= jQuery(window).width();   // returns width of browser viewport)
    		h=jQuery(window).height()/2;
    		h+=jQuery(document).scrollTop();
    	
    		var tmp=jQuery('<div class="Message '+DClass+'" style="display:none;"><div class="M_ico">'+message+'</div></div>').appendTo("body");
    		/*if(type==2)
    			tmp.append('<img src="/images/loading.gif">');*/
    		
    		jQuery(".Message").css({'left':w/2-tmp.width()/2+"px",'top':h-tmp.height()/2+"px"});
    		if(start==0){
    			tmp.fadeIn(400).delay(duration).fadeOut(300,function(){
    				tmp.remove();
    				if (callback == 1) options.callback();
    			});
    		}
    		else{
    			tmp.fadeIn(400);
    		}
    	}
    	else{
    		var tmp=jQuery('.Message').fadeOut(300,function(){
    			tmp.remove();
    			if (callback == 1) options.callback();
    		});
    	}
    	
    }
    
    this.afterLoad = function(p){
       if(typeof(p)!='undefined') id="#"+p.id;
       else{
           p= {};
           id='этот чтобы жкувери не нашел нихрена!!!'; 
       }
      
       if(typeof(p.editorHeight)=='undefined') p.editorHeight = 300;
    	
    	if(typeof jQuery(id+' textarea.editor').elrte != "undefined"){
    	     var opts = {
        		lang : 'ru',
        		styleWithCss : false,
        		//height  : p.editorHeight,
        		height : p.editorHeight,
        		toolbar : 'basic',
        		fmAllow  : true,
        		fmOpen   : function(callback) {
        			jQuery('<div />').elfinder({
        				url : '/libs/elfinder/connectors/php/connector.php',
        				lang : 'ru',
        				dialog : { width : 900, modal : true },
        				editorCallback : callback
        			});
        		}
        	}
    	    jQuery(id+' textarea.editor').elrte(opts);
    	}
    	
    	if(typeof jQuery(this).acordion  != "undefined"){
    	   jQuery(id+" div.accordion").each(function(){
        	    jQuery(this).accordion({
                    autoHeight: false,
                    active: 2
                });
        	})
    	}
    	
    	if(typeof jQuery(this).datepicker  != "undefined"){
    		//console.log(id)
    	    jQuery(id+' .datepicker').datepicker({
    			dateFormat: "dd.mm.yy",
    			currentText: "Now"
    		});
    		
    	}
    	
    	
    	if(typeof jQuery(this).lightBox  != "undefined"){
    		
    	    jQuery(id+' .lightboxed').lightBox();
    	}
    	
    	
    	if(typeof jQuery(id+" .treeview").treeview  != "undefined"){
    	   
    	    jQuery(id+" .treeview").treeview({
        		collapsed: true,
        		persist: "location",
        		animated: "fast"
        	});
    	}
    	
    	if(typeof jQuery(id+" .tabs").tabs  != "undefined"){
    	   
    	    jQuery(id+" .tabs").tabs({ cookie: { expires: 30 } })
    	}
    	
    	if(typeof jQuery(id+" .phone").mask != "undefined")
    	   jQuery(".phone").mask("+9 (999) 999-99-99");
    	
    	jQuery(id+' .float_numbers').keypress(function (e)  
	    { 
	      //Если символ - не цифра, ввыодится сообщение об ошибке, другие символы не пишутся
	       if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)  && e.which!=44 && e.which!=46)
	        return false;  
	    });
	    
	    jQuery(id+' .int_numbers').keypress(function (e)  
	    { 
	      //Если символ - не цифра, ввыодится сообщение об ошибке, другие символы не пишутся
	         if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
	              return false;  
	    });
    }
    
    this.showLoading = function(){
        if(jQuery("#ajax_loading").length == 0){
            dh=jQuery(document).height();
            wh=jQuery(window).height();
            ww=jQuery(window).width();
            wst=jQuery(document).scrollTop();
            
            //alert(wh+"-"+ww+"-"+dh+"-"+wst);
           tmp=jQuery('<div id="ajax_loading" style=";z-index:10000;position:absolute;top:0;left:0;width:'+ww+'px;height:'+dh+'px;background: black;opacity:0;filter:alpha(opacity=0);>'+
               '<a href="javascript:void(0)" onclick="jQuery(\'#ajax_loading\').remove()">'+
               '<img style="position:absolute" src="/images/loading.gif"></a></div>').appendTo("body");
            img=jQuery("#ajax_loading img");
            jQuery("#ajax_loading img").load(function(){
        	    jQuery(this).css({
                    'position':'absolute',
                    'top': wst+wh/2-jQuery(this).width()/2,
                    'left': ww/2-jQuery(this).height()/2
                })
        	})
        	tmp.delay(300).animate({opacity:0.7});
            
        }
    }
    this.hideLoading = function(){
        jQuery("#ajax_loading").remove();
    }
    
    this.newCurZ = function(){
        currZ++;
        return currZ;
    }
}

var API = new API();
