var divDialogs = {
   create_dialog: function create_dialog(type,element,div_handle,data)  {
	   //alert(element)
	   if($(element))
	   {
          el = $(element);
          divDialogs.position_dialog(el);
          if(type == "draggable")
          {
        	  new Draggable(el, {handle:div_handle})
          }
	   }
   },
   position_dialog: function position_dialog(el,top){

	  if(top == undefined)
		  top = false;
	  
	  var divwidth = el.getWidth();
	  var divheight = el.getHeight();

	  var window_width = divDialogs.get_document_window_width();
	  var width = divDialogs.convert_width(divwidth,window_width);
	  var window_height = divDialogs.get_document_window_height();
	  var height = divDialogs.convert_height(divheight,window_height);
	  var mleft = divDialogs.calculate_left(width,window_width);
	  if(!top)
	  var mtop = divDialogs.calculate_top(height,window_height);
	  else var mtop = top;
		
	  el.style.top = mtop+"px";
	  el.style.left = mleft+"px";
   },
   display_dialog: function display_dialog(el){
      new Effect.Appear(el);
   },	
   openWindow: function openWindow(path,wname,ht,wt)
   {
       aswindow_width = divDialogs.get_document_window_width();
       aswidth = divDialogs.convert_width(wt,aswindow_width);
       aswindow_height = divDialogs.get_document_window_height();
       asheight = divDialogs.convert_height(ht,aswindow_height);
       asleft = divDialogs.calculate_left(aswidth,aswindow_width);
       astop = divDialogs.calculate_top(asheight,aswindow_height);
       if(newwindow) newwindow.close();
       newwindow = window.open (path, wname,"menubar=0,resizable=0,screenX="+asleft+",screenY="+astop+",left="+asleft+",top="+astop+",width="+wt+",height="+ht+"");
       newwindow.focus();
   },
   showDiv: function showDiv(div_name){
		el = $(div_name);

		var divwidth = el.getWidth();
		var divheight = el.getHeight();

		var window_width = divDialogs.get_document_window_width();
	   var width = divDialogs.convert_width(divwidth,window_width);
	   var window_height = divDialogs.get_document_window_height();
	   var height = divDialogs.convert_height(divheight,window_height);
	   var mleft = divDialogs.calculate_left(width,window_width);
	   var mtop = divDialogs.calculate_top(height,window_height);

		el.style.top = 75+"px";
		el.style.left = mleft+"px";
	   new Effect.Appear(el);
	},
	hideDiv: function hideDiv(div_name)
	{
		el = $(div_name);
	   new Effect.Fade(el);
	},
	get_document_window_height: function get_document_window_height()
	{
	   if (document.body && document.body.clientHeight &&
	       (document.body.clientHeight > 1)) return document.body.clientHeight;
	   else if (window && window.innerHeight) return window.innerHeight;
	   else if (document.documentElement && document.documentElement.offsetHeight)
	      return document.documentElement.offsetHeight;
	   else if (document.body && document.body.offsetHeight)
	      return document.body.offsetHeight;
	   return 0;
	},
	get_document_window_width: function get_document_window_width()
	{
	   if (document.body && document.body.clientWidth)
	      return document.body.clientWidth;
	   else if (window && window.innerWidth) return window.innerWidth;
	   else if (document.documentElement && document.documentElement.offsetWidth)
	      return document.documentElement.offsetWidth;
	   else if (document.body && document.body.offsetWidth)
	      return document.body.offsetWidth;
	   return 0;
	},
	convert_width:	function convert_width(width,window_width)
	{
	   if (width == null) width = page_width + 52;
	   else if (typeof(width) == 'string') {
	      var percent_pos = width.indexOf('%');
	      if (percent_pos != -1)
	         width = window_width * (width.substr(0,percent_pos) / 100);
	   }
	   if (! document.all) {
	      if (width > (window_width - 25)) width = window_width - 25;
	   }
	   else if (width > (window_width - 10)) width = window_width - 10;
	   return width;
	},
	convert_height: function convert_height(height,window_height)
	{
	   if (height == null) height = window_height;
	   else if (typeof(height) == 'string') {
	      var percent_pos = height.indexOf('%');
	      if (percent_pos != -1)
	         height = window_height * (height.substr(0,percent_pos) / 100);
	   }
	//   else height += header_offset;
	   if (height > (window_height - 40)) height = window_height - 40;
	   return height;
	},
	calculate_left: function calculate_left(width,window_width)
	{
	   var left = (window_width - width) / 2;
	   if (! document.all) left += 10;
	   if (left < 0) left = 0;
	   return left;
	},
	calculate_top: function calculate_top(height,window_height)
	{
	   var top = ((window_height - height) / 2) - 3;
	   if (top < 0) top = 0;
	   return top;
	}
}
