Zadig.Overlay = {}
Zadig.Overlay.topMost = 2;
Zadig.Overlay.bringToFront = function(e)
{
	if(e.style.zIndex == Zadig.Overlay.topMost) return;
	e.style.zIndex = Zadig.Overlay.topMost = Zadig.Overlay.topMost+1; 
}	

Zadig.Modal= function(args)
{
	this.div = document.createElement("DIV");
	this.div.style.backgroundColor = (args.color!= undefined) ? args.color : "black";
	Zadig.setOpacity(this.div, 0);

	this.element = args.el;
	this.element.modal = this;
	
	this.time = args.time;
	this.callback = args.callback;
	this.adjust();
	
	Zadig.Overlay.bringToFront(this.div);
	Zadig.Overlay.bringToFront(this.element);
	this.element.style.display = 'block';
	this.refAnimate = Zadig.bind(this.fade, this);
	document.body.appendChild(this.div);

	Zadig.posAt(this.element);
	Zadig.setOpacity(this.element, 0);
	if(args.callback)args.callback({'method':'init'});
	this.animate = new Zadig.Animate({'duration':1000, 'callback':Zadig.bind(this.go, this)});
}

Zadig.Modal.prototype = {
'go' : function(current)
{
	Zadig.setOpacity(this.div, Math.round(current*50));
	Zadig.setOpacity(this.element, Math.round(current*100));
},
'goBack' : function(current)
{
	if(current > 1) return this.dispose();
	Zadig.setOpacity(this.div, Math.round((1-current)*50));
	Zadig.setOpacity(this.element, Math.round((1-current)*100));
},
'end': function()
{
	new Zadig.Animate({'duration':1000, 'callback':Zadig.bind(this.goBack, this)});
},
'dispose': function()
{
	document.body.removeChild(this.div);
	if(this.callback)this.callback({'method':'end'});	
},
'adjust':function()
{
	var rect = Zadig.pageRect();
	this.div.style.position = 'absolute';
	this.div.style.display='block';
	this.div.style.left = 0 + "px";
	this.div.style.top = 0 + "px";
	this.div.style.width = rect.x+'px'
	this.div.style.height = rect.y +'px';
}
};


Zadig.DragDrop = function(element, handle)
{
	this.element = element;
  
	this.handle = handle;
	this.oldX;
	this.oldY;
	this.mDown = Zadig.addListener('mousedown', element, Zadig.bind(this.startDrag, this));
	this.mMove = Zadig.bind(this.dragging, this);
	this.mUp = Zadig.bind(this.endDrag, this);
	this.mSec = Zadig.bind(this.sec, this);
}
Zadig.DragDrop.prototype=
{
'startDrag' : function(e)
{
	Zadig.stopEvent(e);
	this.oldX = e.screenX;
	this.oldY = e.screenY;
	Zadig.addListener('mousemove', document, this.mMove);
	Zadig.addListener('mouseup', document, this.mUp);
	Zadig.addListener('selectstart', document, this.mSec);
	this.handle.dragStart(e);
},
'dragging' : function(e)
{
	var x = this.oldX - e.screenX;
	var y = this.oldY - e.screenY;
	this.oldX = e.screenX;
	this.oldY = e.screenY;
	this.handle.dragMove(e, x, y);
},
'endDrag' : function(e)
{
	Zadig.removeListener('mousemove', document, this.mMove);
	Zadig.removeListener('mouseup', document, this.mUp);
	Zadig.removeListener('selectstart', document, this.mSec);
	this.handle.dragEnd(e);
},
'sec':function(e)
{
	Zadig.stopEvent(e);
	return false;
}
}

Zadig.Dialog = function(args)
{
	this.shadow = new Zadig.RoundShadow(null,{'width':300, 'height':400, 'level':8, 'color':args.shadow});
	var div = document.createElement("DIV");
	div.jsClass = this;
	document.body.appendChild(div);
	div.innerHTML= "<div class='topBar'><span class='icon'></span><a class='close'></a><div class='title'>"+args.title+"</div></div><div class='content'>"+args.content+"</div><div class='bottom'></div>";
	div.className = args.className;
	this.element = div;
	
	this.topBar = div.firstChild;
	Zadig.addListener('click',this.topBar,this.clickRef = Zadig.bind(this.click, this))
	this.content = this.topBar.nextSibling;
	this.watchers = new Array();
	this.watchers[0]=Zadig.Watcher.watch({'obj':this.element.style, 'prop':'zIndex','fn':Zadig.bind(this.test, this)});
	this.watchers[1]=Zadig.Watcher.watch({'obj':this.element.style, 'prop':'left','fn':Zadig.bind(this.test, this)});
	this.watchers[2]=Zadig.Watcher.watch({'obj':this.element.style, 'prop':'top','fn':Zadig.bind(this.test, this)});
	this.watchers[3]=Zadig.Watcher.watch({'obj':this.content.style, 'prop':'height','fn':Zadig.bind(this.test, this)});
	this.watchers[4]=Zadig.Watcher.watch({'obj':this.element.style, 'prop':'width','fn':Zadig.bind(this.test, this)});
	this.bottom = this.content.nextSibling;
	if(args.statusBar){ this.bottom.className ='statusBar'; this.statusBar = new Zadig.StatusBar(this);}
	new Zadig.DragDrop(this.topBar, this);
	Zadig.posAt(div);
	this.urls = new Array();
	if(args.load != undefined) this.navigate(args.load);
	if(Zadig.ie6)
		this.topBar.parentNode.style.width = Zadig.getRect(this.topBar.parentNode).width +'px';
	this.element.style.width = Zadig.getRect(this.element).width+ "px";
	Zadig.Overlay.bringToFront(this.element);
}
Zadig.Dialog.prototype = 
{
'test': function(i)
{
	if(i.prop=='left')
		this.shadow.container.style.left = parseInt(i.obj.left)-8 +"px";
	else if(i.prop=='top')
		this.shadow.container.style.top = parseInt(i.obj.top)-7 +"px";
	else if(i.prop=='width')
		this.shadow.setSize(parseInt(i.obj.width)+16, Zadig.getRect(this.element).height+16);
	else if(i.prop=='height')
		this.shadow.setSize(Zadig.getRect(this.element).width+16, Zadig.getRect(this.element).height+16);
	else if(i.prop=='zIndex')this.shadow.container.style.zIndex = i.obj.zIndex;
},	
'load': function(response)
{
	this.content.innerHTML = response;
	Zadig.bindComponents(null,this.content.getElementsByTagName("*")); 
},
'navigate': function(url)
{
	this.urls.push(url);
	new Zadig.Request(url, true, null, Zadig.bind(this.load, this));
},
'reload': function()
{
	this.navigate(this.urls.pop());
}	,
'click':function(e)
{
	if(Zadig.getTarget(e).className == 'close') {
		document.body.removeChild(this.element);
		this.clean();
		return;}
	Zadig.Overlay.bringToFront(this.element);
},
'clean':function()
{
	this.shadow.remove();
	for(var i in this.watchers)
	Zadig.Watcher.unwatch(this.watchers[i]);
},
'close':function()
{
	this.element.style.display = 'none';
},
'dragStart': function(e)
{
	
},
'dragMove': function(e, x, y)
{
	this.element.style.left = parseInt(this.element.style.left) - x + "px";
	this.element.style.top = parseInt(this.element.style.top) - y + "px";
	Zadig.Watcher.check();
},
'dragEnd': function(e)
{
	
}
	
}

Zadig.StatusBar =  function(dialog)
{
	this.dialog = dialog;
	
	this.element = dialog.bottom;
	this.text = document.createElement('div');
	this.element.appendChild(this.text);
	this.corner = document.createElement('div');
	this.element.appendChild(this.corner);
	this.corner.className = 'corner';
	new Zadig.DragDrop(this.corner, this);
}
Zadig.StatusBar.prototype = 
{
	
'dragStart': function(e)
{
	
	this.yBegin = Zadig.getRect(this.dialog.element).height - Zadig.getRect(this.dialog.topBar).height - Zadig.getRect(this.element).height;
},
'dragMove': function(e, x, y)
{
	x = Zadig.getRect(this.dialog.element).width - x;
	this.yBegin -= y;
	this.dialog.element.style.width =  x+ "px";
	this.dialog.content.style.height = this.yBegin + "px";
	Zadig.Watcher.check();
},
'dragEnd': function(e)
{
	
}
	
};

Zadig.showAlert = function (message)
{
	var n = new Zadig.Dialog({'className':'Frame Doc alert','title':'PROCURÔACHÔ','content':message,'shadow':"#0084d1"});
	return n;
}


