String.prototype.s_replace=function(arr1,arr2){
	var $$=this;
	if(typeof arr1=="object"){
		for(var i=0;i<arr1.length;i++){
			if(typeof arr1[i]!='undefined' && typeof arr2[i]!='undefined'){
				$$=$$.replace(new RegExp(arr1[i],"gi"),arr2[i]);
			}
		}
	}
	return $$;
}
var ui={};
Class({
	name:"window_decorator",
	pack:ui,
	implements:[tools.browser],
	parent:tools.eventDispatcher,
	constructor:function(replace_object){
		this.Super();
		if(replace_object){
			for(var key in replace_object){
				this.html=this.html.replace(new RegExp("\\{"+key+"\\}","gi"),replace_object[key]);
			}
		}
		this.html=this.html.replace(/\{class\}/gi,this.cssClass);
		if(this.html){
			this.winJquery=$(this.html);
			this.winJquery.hide();
			this.winJquery.appendTo("body");
		}
		if(this.positionTop || this.positionLeft){
			this.positionMode=false;
		}
	},
	public:{
		drag:"",
		positionTop:"",
		positionLeft:"",
		shadow:true,
		cssClass:"standart_window",
		drag_box:"",
		winJquery:false,
		open:function(){
			if(this.winJquery){
				this.winJquery.removeAttr("style");
// 				if(!this.positionTop){
				this.positionTop=$(window).scrollTop()+Math.abs(Math.round(($(window).height()/2)-(this.winJquery.height()/2)));
// 				}
// 				if(!this.positionLeft){
				this.positionLeft=$(window).scrollLeft()+Math.abs(Math.round(($(window).width()/2)-(this.winJquery.width()/2)));
// 				}
				this.winJquery.css({
					top:this.positionTop,
					left:this.positionLeft,
					zIndex:1000
				});
				this.shadowMake();
				this.winJquery.show();
				this.dispatch("OPEN");
				this.winJquery.find("*.close").click(this.close);
				this.winJquery.find("*.ok").click(this.ok);
				this.winJquery.find("*.cancel").click(this.cancel);
			}
		},
		close:function(){
			this.destroy();
			this.dispatch("CLOSE");
		},
		ok:function(){
			var array = [];
			var object = {};
			this.winJquery.find("input[type=text], input[type=checkbox]:checked, input[type=radio]:checked, textarea, select").each(function(){
				array.push($(this).val());
				if(!object[$(this).attr("name")]){
					object[$(this).attr("name")] = $(this).val();
				}
				else{
					if(typeof object[$(this).attr("name")]=="object"){
						object[$(this).attr("name")].push($(this).val());
					}
					else{
						object[$(this).attr("name")]=[object[$(this).attr("name")],$(this).val()];
					}
				}
			});
			this.destroy();
			this.dispatch("OK",[object,array]);
		},
		remove:function(){
			this.winJquery.remove();
		},
		cancel:function(){
			var array = [];
			var object = {};
			this.winJquery.find("input[type=text], input[type=checkbox]:checked, input[type=radio]:checked, textarea, select").each(function(){
				array.push($(this).val());
				object[$(this).attr("id")] = $(this).val();
			});
			this.destroy();
			this.dispatch("CANCEL",[object,array]);
		}
	},
	protected:{
		eventList:["CLOSE","OPEN","OK","CANCEL"],
		html:"",
		inBox:"",
		innerHTML:"",
		position:"",
		positionMode:true,
		shadowMake:function(){
			if(this.shadow){
				$("#alert_shadow").remove();
				$("<div id='alert_shadow'>&nbsp;</div>").css({
					position : "absolute",
					top : "0px",
					left : "0px",
					width : $(document).width(),
					height : $(document).height(),
					zIndex : "100",
					background : "#000",
					opacity : .8
				})
				.appendTo("body");
			}
		},
		destroy:function(){
			if(this.shadow){
				$("#alert_shadow").remove();
			}
			this.winJquery.hide();
		},
		shadowRemove:function(){
			if(this.shadow){
				$("#alert_shadow").remove();
			}
		}
	}
});
Class({
	name:"admin_window",
	pack:ui,
	parent:ui.window_decorator,
	constructor:function(){
		!!(arguments[0]) ? this.Super(arguments[0]) : this.Super();
	},
	public:{
		cssClass:"admin_window",
		drag_box:"~'div.'+this.cssClass+'_title'~",
		open:function(clear){
			$(document).unbind("keydown.admin_window");
			$(document).bind("keydown.admin_window",this.shortcurts);
			if(!clear){
				this.winJquery.find("input[type=text],textarea").val("");
			}
			if(!this.one){
				this.winJquery.find("div."+this.cssClass+"_close,input."+this.cssClass+"_cancel").click(this.close);
				this.winJquery.find("input."+this.cssClass+"_ok").click(this.ok);
				this.winJquery.find("div.admin_window_button input").classHover("input_hover");
				this.winJquery.draggable({ handle:this.drag_box });
				this.one=true;
			}
			this.parent.open();
			$(this.winJquery.find("input[type=text],textarea")[0]).focus();
		},
		close:function(){
			$(document).unbind("keydown.admin_window",this.shortcurts);
			this.parent.close();
		},
		templates:{
			one_input:"<div class='line'><label for='{name}'>{label} </label><input type='text' name='{name}' id='{name}' class='text' value='{value}'/></div>",
			textarea:"<div><label>{label} </label><br/><div style='clear:both;margin:0px 0px 0px 8px;'><textarea name='{name}'>{textarea}</textarea></div></div>",
			checkbox:"<div class='line'><label for='{name}'>{label} </label><input type='checkbox' name='{name}' id='{name}' {checked} class='checkbox' value='{name}'/></div>"
		}
	},
	protected:{
		html:
		'<div class="{class}" style="display:none;">'+
			'<div class="{class}_header">'+
				'<div class="{class}_title">{title}</div>'+
				'<div class="{class}_close {class}_cancel">&nbsp;</div>'+
			'</div>'+
			'<div class="{class}_content">{content}</div>'+
			'<div class="{class}_button">'+
				'<input type="button" value="Ok" class="{class}_ok"/>'+
				'<input type="button" value="Cancel" class="{class}_cancel"/>'+
			'</div>'+
		'</div>'
	},
	private:{
		one:false,
		shortcurts:function(e){
			if(e.keyCode==13){
				this.ok();
			}
			if(e.keyCode==27){
				this.close();
			}
		}
	}
});
