/**
 * @author Nikolay Khokhlov, Arkhangelsk 2009
 */
var MooWindow = new Class({
	Implements: [Options],
	options: {
    	innerbg:'#fff',
    	innerHeight: "300px",
    	innerWeight: "300px",
    	outerbg:'#fff',
    	color:'#333',
    	opacity: 0.8,
    	border: '6px solid #ebccaf'
	},
	initialize: function(options){
		this.setOptions(options);
		_this=this;
		this.isIE6=this.isIE6();
		if(this.exist()!=true) this.create();
		this.moowindow=$("moowindow");
		this.mooshadow=$("mooshadow");
		this.mooshadow.addEvent("click", function(){
			_this.hide();
		});
		this.moowindow.setStyle("opacity","0");
		this.mooshadow.setStyle("opacity","0");
		this.targetCoords = this.moowindow.getCoordinates();
	},
	ieVersion:function(){
		v=/(\w+) (\w+)/.exec(navigator.appVersion);
		if(v!=null)return v[2];
		else return 0;
	},
	//IE6 не поддерживает position fixed
	isIE6:function(){
		if ((navigator.appName == "Microsoft Internet Explorer") && (this.ieVersion() <= 6)){return true;}
		else return false;
	},
	show: function(text, ajaxed){
		
		this.setPosition();
		this.mooshadow.fade(this.options.opacity);
		//ajaxed
		if(ajaxed!=null){
			var req = new Request.HTML({url:text, 
				onSuccess: function(html) {
					_this.moowindow.set('text', '');
					_this.moowindow.adopt(html);
				},
				onFailure: function() {
					this.moowindow.set('text', 'Error.');
				}
			}).send();
		}
		else this.moowindow.set("text", text);
		this.moowindow.fade(1);
		if(this.isIE6==true){window.addEvent("scroll", function(){_this.setPosition();});}
	},
	hide:function(){
		if(this.isIE6==true) window.removeEvent("scroll", this.setPosition);
		this.mooshadow.fade(0);
		this.moowindow.fade(0);
	},
	/* создание объектов */
	create:function(){
		// тень
		var s = document.createElement("div");
		s.id='mooshadow';
		s.style.display="block";
		s.style.position='absolute';
		s.style.visibility="hidden";
		s.style.zIndex=9999999;
		s.style.top="0px";
		s.style.left="0px";
		s.style.background=this.options.outerbg;
		s.style.width="100%";
		s.style.height="1000px";
		document.body.appendChild(s);
		//окно
		w = document.createElement("div");
		w.id="moowindow";
		w.style.display="block";
		w.style.position='absolute';
		w.style.visibility="hidden";
		w.style.zIndex=9999999;
		w.style.background=this.options.innerbg;
		w.style.width=this.options.innerWeight+"px";
		w.style.height=this.options.innerHeight+"px";
		w.style.padding="40px";
		w.style.border=this.options.border;
		w.style.overflow="auto";
		document.body.appendChild(w);
		if(this.isIE6==false){
			s.style.position="fixed";
			w.style.position="fixed";
		}
		else{
			s.style.display='none';
			w.style.display='none';	
		}
		
	},
	exist:function(){
		if($('modalWindow')!=null) return true;
		else return false;
	},
	clientWidth:function(){
		return document.documentElement.clientWidth;
	},
	clientHeight:function(){
		return document.documentElement.clientHeight;
	},
	scrollTop:function(){
		return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	},
	scrollLeft:function(){
		return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
	},
	setPosition: function(){
		w = _this.clientWidth();
		h = _this.clientHeight();
		t = _this.scrollTop();
		if(this.isIE6==true){
			this.mooshadow.style.display='block';
			this.moowindow.style.display='block';
			this.moowindow.style.left=(w-_this.options.innerWeight)/2-20;
			this.moowindow.style.top=t+(h-_this.options.innerHeight)/2-20;
			this.mooshadow.style.height=h;
			this.mooshadow.style.width=w;
			this.mooshadow.style.top=t;
		}
		else{
			this.moowindow.style.left=(w-_this.options.innerWeight)/2-40+"px";
			this.moowindow.style.top=(h-_this.options.innerHeight)/2-40+"px";
			
			this.mooshadow.style.height=h+"px";
			this.mooshadow.style.width=w+"px";
			this.mooshadow.style.top=0+"px";
		}
	}
});
