function ModalWindow(owner){
  this.owner = owner;
}

ShowModalWindow = function (owner){
  return ModalWindow._show(owner,arguments);
}

ModalWindow._show = function(owner,args){
  if (Object.isIE){
  	  if (!bindReady.isReady){
		return;
	  }
  }
  var mwindow = owner.mwindow;
  if (typeof(mwindow)=='undefined'){
    var mwindow = owner.mwindow = new ModalWindow(owner);	
    if (((args.length-1) % 2) == 0){
      for (var i=0;i<((args.length-1)/2);i++){
	    property_name = args[i*2+1];
	    if (typeof(mwindow[property_name])!='undefined'){
		  mwindow[property_name] = args[i*2+2];
		}
		else {
		  alert('wrong property'+property_name);
		}
	  }
    }
	else {
	  alert('count of arguments should be odd!');
	}
	mwindow.init();		
  }
  mwindow.showWindow();
  return mwindow;
}

rei.extend(ModalWindow.prototype,{
  oncreate:null,
  onclose:null,
  owner_onclick:null,
  onshow:null,
  Title:'',  
  text:null,
  div:null,
  width:350,

  animation_timeout:50,
  animation_timer:null,
  shadow_size:8,
  glass:null,
  de:document.documentElement,
  d:document,  
  
  init:function(){
  	var this1 = this;
    this.createGlass();
  
	var hint = this.hint_div =  this.createEl("div");
  	hint.className = "tooltip";
  	this.d.body.appendChild(hint);
  
  	
    hint.style.width = this.width+'px';
  	  
	var hint_inner = this.createEl("div");
  	hint_inner.className = "border";
  	hint.appendChild(hint_inner);  	  
  	  
    var t = this.title_div = this.createEl('div');
    t.innerHTML = this.Title;
    t.className = "title";
    t.onmousedown = function(e) {this1.startDragging(e)};
    t.onmouseup = function(e) {this1.stopDragging(e)};
	hint_inner.appendChild(t);
  	  
  	if (this.text){
  	  var content = this.createEl('div');
  	  content.className = "content";
  	  content.innerHTML = this.text;
  	  hint_inner.appendChild(content);
  	}
  	if (this.div) {
  	  var d = this.d.getElementById(this.div);
  	  hint_inner.appendChild(d);
  	  d.className = '';
  	}
  
	if (this.oncreate!=null){
    	this.oncreate();
  	} 
	if (this.owner_onclick!=null){
		this.owner.onclick = this.owner_onclick;
	}	  
  
  	this.createCB();
  	this.createShadow();
  	//this.showWindow();
  },
  
  createCB:function(){
    var c_img  = this.createEl('IMG',{height:'16px',width:'16px',cursor:'pointer'});
    c_img.src = '/images/delete.png';
    c_img.align = 'right';
    c_img.vspace = '1';
    c_img.hspace = '1';
  
    var this1 = this;
    c_img.onclick = function(){
      this1.hideWindow();
    }
    this.title_div.insertBefore(c_img,this.title_div.firstChild);
  },  
  
  createGlass:function(){
    if (browser.msie){
        var g = this.glass = this.createEl('div',{filter:"alpha(opacity:30)",position:'absolute',left:'0px',top:'0px',width:this.getWinW()+'px',height:this.getWinH()+'px',background:'black',display:'none'});
    	g.innerHTML = '&nbsp';
    	this.d.body.appendChild(g); 
    }
    else {
    	var g = this.glass = this.createEl('div',{opacity:0.3,position:'absolute',left:'0px',top:'0px',width:this.getWinW()+'px',height:this.getWinH()+'px',background:'black',display:'none'});
    	g.innerHTML = '&nbsp';
    	this.d.body.appendChild(g);
    } 
  },
  
  createEl:function(name,style){
   var el = this.d.createElement(name);
   for(key in style){
     if (typeof(style[key])!='function'){
       el.style[key] = style[key]; 
     }
   }
   return el;
  },  

  createSImg:function(src,style){
	style.position="absolute";
	var sh1 = this.createEl('img',style); 
	this.hint_div.appendChild(sh1);
	sh1.alt=""; 
	sh1.src=src;
	return sh1;
  },

  createShadow:function(){
  	with(this){
		createSImg(ModalWindow.img_base+"sh_rt.png",{width:"8px",height:"8px",right:"0px",top:"0px"});
		createSImg(ModalWindow.img_base+"sh_rb.png",{width:"8px",height:"8px",right:"0px",bottom:"0px"});
		createSImg(ModalWindow.img_base+"sh_lb.png",{width:"8px",height:"8px",left:"0px",bottom:"0px"});
		createSImg(ModalWindow.img_base+"sh_b.png",{width:(hint_div.offsetWidth-16) + "px",height:"8px",left:"8px",style:"0px"});
		createSImg(ModalWindow.img_base+"sh_r.png",{width:"8px",height:(hint_div.offsetHeight-16) + "px",right:"0px",top:"8px"});
	}
  },
  
  startDragging:function(e){
	var this1 = this;
	with(this){
		this.old_onmousemove = d.onmousemove; 
		if (typeof(event)!='undefined') {
			var newx = (event.clientX+de.scrollLeft);
			var newy = (event.clientY+de.scrollTop);
		}
		else {
			var newx = (e.pageX);
			var newy = (e.pageY);	  
		}
		this.drag_left_offset = newx - getPOL(this.hint_div);
		this.drag_top_offset =  newy - getPOT(this.hint_div);
		d.onmousemove = function(e){this1.trackMM(e);return false;};
	}
  },
  
  stopDragging:function(e){
	this.d.onmousemove = this.old_onmousemove; 
	this.old_onmousemove = null;
  },
  
  trackMM:function(e){
	var newx;
	var newy;
	if (typeof(event)!='undefined') {
	  newx = (event.clientX+this.de.scrollLeft);
	  newy = (event.clientY+this.de.scrollTop);
	}
	else {
	  newx = (e.pageX);
	  newy = (e.pageY);	  
	}
	
	this.hint_div.style.left = newx - this.drag_left_offset + 'px';
	this.hint_div.style.top = newy - this.drag_top_offset + 'px';	
	
  },  
  
  getPOL:function(obj){ //page offset left
	var x;
    x = obj.offsetLeft;
    if (obj.offsetParent != null)
    x += this.getPOL(obj.offsetParent);
    return x;		
  },

  getPOT:function(obj){ //page offset top
	var y;
    y = obj.offsetTop;
    if (obj.offsetParent != null)
    y += this.getPOT(obj.offsetParent);
    return y;
  },
  
  showWindow:function(){
	  var top 	= this.top = (this.getWinH()-this.hint_div.offsetHeight)/2+this.de.scrollTop;
 	  var left 	= this.left = (this.getWinW()-this.hint_div.offsetWidth)/2+this.de.scrollLeft;
	  this.hint_div.style.left       = left + "px";
      this.hint_div.style.top        = top + "px"; 
      this.startBorderAnimation();
      this.visible = true;
      this.showGlass();
  },
  
  destroyWindowData:function(my_id){
//  	var cont=$(my_id);
//  	var contOwner=cont.parentNode;
//  	contOwner.removeChild(cont);
	this.d.body.removeChild(this.hint_div);
  },
    
  
  startBorderAnimation:function(){
  	var count_steps = this.count_steps = 3;
  	this.b_left	    = this.getPOL(this.owner);
  	this.b_top	 	= this.getPOT(this.owner);
  	this.b_height 	= this.owner.offsetHeight;
  	this.b_width 	= this.owner.offsetWidth;
    
  	this.b_left_step   = (this.left-this.b_left)/count_steps;
  	this.b_top_step	   = (this.top-this.b_top)/count_steps;
  	this.b_height_step = (this.hint_div.offsetHeight-this.b_height-this.shadow_size)/count_steps;
  	this.b_width_step  = (this.hint_div.offsetWidth-this.b_width-this.shadow_size)/count_steps;
  	if (typeof(this.brdr)=='undefined'){
    	this.brdr = this.createEl('div',{border:'1px solid gray',position:'absolute',overflow:'hidden'});
    	this.brdr.innerHTML = '&nbsp;';
		this.d.body.appendChild(this.brdr);
  	}
  	
  	this.setBorderSizes();  
  	this.brdr.style.display = 'block';
  	var this1 = this;
  	this.animation_timer = window.setTimeout(function(){this1.borderIteration()},this.animation_timeout);
  },

  setBorderSizes:function(){
    with(this){
  		brdr.style.left 	= b_left+'px';
  		brdr.style.top		= b_top+'px';
  		brdr.style.width	= b_width+'px';
  		brdr.style.height	= b_height+'px';
    }
  },
 
  borderIteration:function(){
  	with(this){
  		if (count_steps>0){
  			b_left	  = b_left   + b_left_step;
			b_top	  = b_top	 + b_top_step;
  			b_height  = b_height + b_height_step;
  			b_width   = b_width  + b_width_step;  
			setBorderSizes();
  			count_steps--;
			var this1 = this;
  			this.animation_timer = window.setTimeout(function(){this1.borderIteration()},animation_timeout);
  		}
  		else {
    		brdr.style.display = 'none'; 
    		hint_div.style.visibility = "visible";
			animation_timer = null;
			if (onshow) onshow();
  		}
  	}
  },
  
  showGlass:function(){
  	with(this){
		glass.style.display = 'block';
		glass.style.width = getWinW()+'px';
		glass.style.height = d.body.clientHeight+'px';
	}  
  },
  
  hideGlass:function(){
	this.glass.style.display = 'none';  
  },
  
  hideWindow:function(){  
    with(this){
  		hint_div.style.visibility = "hidden";
//		this.d.body.removeChild(hint_div);
  		visible = false;
  		hideGlass();
  		if (onclose){
  			onclose();
  		}
    }
  },  
  
  getWinW:function(){
  	return this.de.clientWidth || 0;
  },

  getWinH:function(){
  	return this.de.clientHeight || 0;
  },

  getScrX:function(){
  	return this.de.scrollLeft;// || window.scrollX || 0;
  },

  getScrY:function(){
    return this.de.scrollTop;// || window.scrollY || 0;
  }  
  
});


ModalWindow.images_preloaded = new Array();

ModalWindow.preloadImage = function(url){
    var v = new Image();
    v.src = url;
    ModalWindow.images_preloaded.push(v);
}

ModalWindow.preloadImages = function(){
  with(ModalWindow){    
	preloadImage(img_base+"sh_rt.png");
	preloadImage(img_base+"sh_rb.png");
	preloadImage(img_base+"sh_lb.png");
	preloadImage(img_base+"sh_b.png");
	preloadImage(img_base+"sh_r.png");
  }
}
ModalWindow.img_base = '/images/rcorners/shadow/';
ModalWindow.preloadImages();

if (browser.msie && browser.version<7){

ModalWindow.prototype.showGlass=function(){
  	with(this){
		glass.style.display = 'block';
		glass.style.width = getWinW()+'px';
		glass.style.height = d.body.clientHeight+'px';
		hideSelects();
	}  
};
  
ModalWindow.prototype.hideGlass=function(){
	this.glass.style.display = 'none'; 
	this.showSelects(); 
};
  
ModalWindow.prototype.hideSelects = function(){
	this.h_s = new Array();
	var selects = this.d.getElementsByTagName("select");
	var inner_selects = this.hint_div.getElementsByTagName("select");
	var rect = this.hint_div.getBoundingClientRect();
	var i;var j;var k;
	for(i=0;i<selects.length;i++){
		var sel = selects[i];
		var is_inner = false;
		for(j=0;j<inner_selects.length;j++){
			if (inner_selects[j]==sel){
			  is_inner = true;
			}
		}
	    if (!is_inner){ //should to hide
			sel.style.visibility = 'hidden'; 
		    this.h_s.push(sel);
		}
	}
};
  
ModalWindow.prototype.showSelects = function(){
	  while(this.h_s.length>0){
	    var sel = this.h_s.pop();
		sel.style.visibility = 'visible'; 
	  }
};
 
}
