var popupWin = {
	isIE		: typeof event != "undefined",
	blockDiv	: null,
	isHot		: false,
	topDog		: null,
	whichDog	: null,
	hotDog		: null,
	nowX		: null,
	nowY		: null,
	offsetx		: null,
	offsety		: null,
	ddEnabled	: false,
	badElements : [],

	create : function(params) {

		var me = this;
		var body = document.getElementById("body");
		
		//body.style.overflow = "hidden";
		
		if(me.whichDog == null) {

			me.blockDiv = document.createElement("div");
			me.blockDiv.id = "popupWin__block";
			me.blockDiv.style.width = body.scrollWidth;
			me.blockDiv.style.height = body.scrollHeight;
			body.appendChild(me.blockDiv);

			me.whichDog = document.createElement("div");
			me.whichDog.id = "popupWin__";
			
			// if(me.isIE) me.whichDog.onselectstart = new Function("return false;");

			var div = document.createElement("div");
			div.id = "popupWin__header";
			div.innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'><tr><td width='100%' nowrap><div id='popupWin__header_content'></div></td><td><div id='popupWin__header_close' onclick='popupWin.close()'></div></td></tr></table>";
			me.whichDog.appendChild(div);

			div = document.createElement("div");
			div.id = "popupWin__content";
			div.style.whiteSpace = "nowrap";
			me.whichDog.appendChild(div);

			body.appendChild(me.whichDog);
			
			//document.getElementById("popupWin__header_content").onmousedown = ddInit;
			//document.getElementById("popupWin__header_content").onmouseup = stopDrag;
		}

		me.whichDog.style.width = "1px";

		if(document.getElementById("mainLayer")) {
			document.getElementById("mainLayer").style.opacity =".35";
			document.getElementById("mainLayer").style.filter =".65";
		}

		this.hideBadElementsForIE();

		document.getElementById("popupWin__header_content").innerHTML = params.header + "&nbsp;&nbsp;";
		document.getElementById("popupWin__content").innerHTML = params.content;

		me.blockDiv.style.display = "block";
		me.whichDog.style.display = "block";
		
		function setSize() {
			document.getElementById("popupWin__content").style.position = "";
			document.getElementById("popupWin__content").style.overflow = "";
			document.getElementById("popupWin__content").style.height = "";
			
			me.whichDog.style.width = me.whichDog.scrollWidth + (me.isIE ? 2 : 20);
			if(parseInt(me.whichDog.style.width) > body.clientWidth) {
				me.whichDog.style.width = body.clientWidth - 40;
			}
		}

		function moveLayer() {
			setSize();
			me.whichDog.style.left = body.clientWidth/2 - me.whichDog.offsetWidth/2;
			me.whichDog.style.top = body.clientHeight/2 - me.whichDog.offsetHeight/2 + body.scrollTop;
			
			if(me.whichDog.offsetHeight > body.clientHeight) {
				me.whichDog.style.top = 5 + body.scrollTop;
				
				document.getElementById("popupWin__content").style.position = "relative";
				document.getElementById("popupWin__content").style.overflow = "auto";
				document.getElementById("popupWin__content").style.height = body.clientHeight - (me.isIE ? 40 : 80);
				
				me.whichDog.style.width = me.whichDog.scrollWidth + me.whichDog.scrollLeft + 20;

			}

		}

		moveLayer();
		me.whichDog.style.visibility = "visible";

		function ddInit(e) {
			var b=me.getBounds(me.whichDog);
			me.offsetx=me.isIE ? event.clientX : e.clientX;
			me.offsety=me.isIE ? event.clientY : e.clientY;
			me.nowX=b.left;
			me.nowY=b.top;
			me.ddEnabled=true;
			document.onmousemove = dd;
		}

		function dd(e) {
			if(!me.ddEnabled) return;
			var left = me.isIE ? me.nowX+event.clientX-me.offsetx : me.nowX+e.clientX-me.offsetx;
			var top  = me.isIE ? me.nowY+event.clientY-me.offsety : me.nowY+e.clientY-me.offsety;
			if(left<1) left = 0;
			else if(left+me.whichDog.offsetWidth>body.clientWidth) left = body.clientWidth - me.whichDog.offsetWidth;
			if(top<1) top = 0 + body.scrollTop;
			else if(top+me.whichDog.offsetHeight>body.clientHeight) top = body.clientHeight - me.whichDog.offsetHeight;
			me.whichDog.style.left = left;
			me.whichDog.style.top  = top;
			return false;
		}

		function stopDrag(){me.ddEnabled=false;}
		window.onresize	= moveLayer;
		window.onscroll	= moveLayer;
	},

	close : function() {
		
		//body.style.overflow = "auto";
		
		if(document.getElementById("mainLayer")) {
			document.getElementById("mainLayer").style.opacity ="1";
		}

		popupWin.hideBadElementsForIE(false);
		popupWin.blockDiv.style.display = "none";
		popupWin.whichDog.style.display = "none";
		popupWin.whichDog.style.visibility = "hidden";

		document.onmousemove = null;
		window.onresize	 = null;
		
		popupWin.whichDog.className = "";
		document.getElementById("popupWin__header").className = "";
		document.getElementById("popupWin__header_content").className = "";
		
		if(typeof document.getElementById("popupWin__header_close").onCustomClose == "function") {
			var f = document.getElementById("popupWin__header_close").onCustomClose;
			document.getElementById("popupWin__header_close").onCustomClose = null;
			f();
		}
	},

	hideBadElementsForIE : function(_hide, me) {
		if(!(self.navigator.userAgent.indexOf("MSIE") >= 0)) return;
		if(parseFloat(navigator.appVersion.split('MSIE')[1]) >= 7) return;
		
		if(_hide !== false) _hide = true;
			
		var layer = this.whichDog, idS, i;
		var sels  = document.getElementsByTagName('select');
		if(_hide) this.badElements = [];

		for(i=0;i<sels.length;i++) {
			idS = sels[i].id;
			if(!idS) idS = (sels[i].id = 'UID_'+Math.round(Math.random()*100000));
			if(_hide && sels[i].style.visibility!='hidden') {
				sels[i].style.visibility='hidden';
				if(!inArray(this.badElements,idS)) this.badElements.push(idS);
			}
			else if(!_hide && inArray(this.badElements,idS)) {
				sels[i].style.visibility='inherit';
			}
		}
			
		if(!_hide) this.badElements = [];
			
		function inArray(_arr,_v) {
			for(var i=0;i<_arr.length;i++) if(_arr[i]==_v) return true;
			return false;
		}
	},

	getBounds : function(element) {
		var o = element, x=0, y=0, w=0, h=0;
		if(o) {
			w=o.offsetWidth;
			h=o.offsetHeight;
			while(o && o.style && (o.style.position.toLowerCase()!='absolute' || o.style.position.toLowerCase()!='relative')) {
				x += o.offsetLeft;
	        		y += o.offsetTop;
	        		o = o.offsetParent;
	        	}
        }
    	   return {left:x, right:x+w, top:y, bottom:y+h, width:w, height:h}
	},
	
	createError : function(mess, onClose) {
		this.create({header:"Error", content:mess});
		this.whichDog.className = "popupWin__error";
		document.getElementById("popupWin__header").className = "popupWin__header_error";
		document.getElementById("popupWin__header_content").className = "popupWin__header_content_error";
		
		if(typeof onClose == "function") {
			document.getElementById("popupWin__header_close").onCustomClose = onClose;
		}
	},
	
	createMessage : function(mess, onClose) {
		this.create({header:"Message", content:mess});

		if(typeof onClose == "function") {
			document.getElementById("popupWin__header_close").onCustomClose = onClose;
		}
	},
	
	createConfirm : function(mess, ok, cancel) {
		mess += "<div align='right' style='margin-top:20px;width:100%'><button id='popupWin__but_ok' style='width:60px'>OK</button>&nbsp;<button id='popupWin__but_cancel' style='width:60px'>Cancel</button></div>";
		this.create({header:"Confirm", content:mess});
		this.whichDog.className = "popupWin__confirm";
		document.getElementById("popupWin__header").className = "popupWin__header_confirm";
		document.getElementById("popupWin__header_content").className = "popupWin__header_content_confirm";
		document.getElementById("popupWin__but_ok").onclick = typeof ok == "function" ? ok : popupWin.close;
		document.getElementById("popupWin__but_cancel").onclick = typeof cancel == "function" ? cancel : popupWin.close;
		
		if(typeof cancel == "function") {
			document.getElementById("popupWin__header_close").onCustomClose = onClose;
		}
	}
}