try{SERVICE_ENABLED;}
catch(e){if(e){
alert('widget.js: Для корректной работы класса Widget необходимо подключить файл service.js!');
}}
try{AJAX_ENABLED;}
catch(e){if(e){
alert('widget.js: Для корректной работы класса Widget необходимо подключить файл ajax.js!');
}}

WIDGET_ENABLED=true;

WIDGET_Widget = function(){
	this.windows=new Array();
}

WIDGET_Widget.prototype.openWindow=function(name,content,style,tpl){
	!name?name=getUniqueId():null;
	if (this.windows[name]){
		alert ('Widget: Окно "'+name+'" уже существует. Имя окна должно быть уникально!')
		return;
	}
	this.windows[name]=new WIDGET_Window(name,content,style,tpl);
	this.windows[name].show();
}

WIDGET_Widget.prototype.closeWindow=function(name){
	this.windows[name].close();
	delete(this.windows[name]);
}

WIDGET_Widget.prototype.dragWindow=function(name,evt){
	this.windows[name].drag(evt);
}

WIDGET_Widget.prototype.dropWindow=function(name,evt){
	this.windows[name].drop(evt);
}



WIDGET_Window = function(name,content,style,tpl){
	!name?this.name=getUniqueId():this.name=name;
	!content?this.content='':this.content=content;
	!style?this.style={}:this.style=style;
	this.tpl=tpl;
	this.dragDiffX=null;
	this.dragDiffY=null;
}

WIDGET_Window.prototype.show=function(){
	oDiv=document.createElement('div');
	oDiv.style.position='absolute';
	for (var key in this.style){
		oDiv.style[key]=this.style[key];
	}
	oDiv.id='WINDOW_CONTAINER_'+this.name;
	oDiv.innerHTML=this.prepare();
	document.body.appendChild(oDiv);
}

WIDGET_Window.prototype.close=function(){
	$('WINDOW_CONTAINER_'+this.name).parentNode.removeChild($('WINDOW_CONTAINER_'+this.name));
}

WIDGET_Window.prototype.prepare=function(){
	if (!this.tpl){
		return(this.content);
	}
	return(this.tpl.replace('{%close%}',"$Widget.closeWindow('"+this.name+"')").replace('{%content%}',this.content).replace('{%drag%}',"$Widget.dragWindow('"+this.name+"',event)"));
}

WIDGET_Window.prototype.drag=function(evt){
	if(this.dragDiffX==null){
		var pos=getElementPosition('WINDOW_CONTAINER_'+this.name);
		var mouse=getMousePosition(true);
		this.dragDiffX=mouse.x-pos.left;
		this.dragDiffY=mouse.y-pos.top;
		$Event.add('document','onmousemove','$Widget.windows["'+this.name+'"].dragging(event)')
		$Event.add('document','onmouseup','$Widget.windows["'+this.name+'"].drop(event)')
	}
}

WIDGET_Window.prototype.drop=function(evt){
	$Event.unset('document','onmousemove','$Widget.windows["'+this.name+'"].dragging(event)');
	$Event.unset('document','onmouseup','$Widget.windows["'+this.name+'"].drop(event)')
	this.dragDiffX=null;
	this.dragDiffY=null;
}

WIDGET_Window.prototype.dragging=function(evt){
	//alert(Number(this.getContainer().style.top.replace('px',''))+Number(diffY));
	var mouse=getMousePosition(true);
	this.getContainer().style.top=(mouse.y-this.dragDiffY)+'px';
	//alert(this.getContainer().style.top);
	this.getContainer().style.left=(mouse.x-this.dragDiffX)+'px';
}

WIDGET_Window.prototype.getContainer=function(){
	return ($('WINDOW_CONTAINER_'+this.name));
}

$Widget=new WIDGET_Widget();
