/*
pop version 2.5
Copyright 2010
Dual licensed under the MIT or GPL Version 2 licenses.
author mxm145
Email mxm145@163.com

example:
var p = new pop({
	width    : 300,
	height   : 300,
	title    : 'exampleTitle',
	content  : 'exampleContent',
	hasbg    : true/false,
	pos      : 'rb',
	bgColor  : 'ff0000',
	borColor : 'ffff00',
	drag      : true/false
	});
p['onbi'] = function(){
	alert('close');
}
*/
pop = function(){
	var ie = (function(){
		var undef,
		v = 3,
		div = document.createElement('div'),
		all = div.getElementsByTagName('i');
		while (
			div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
			all[0]
		);
		return v > 4 ? v : undef;
	}());
	function $(o){
		return document.getElementById(o);
	}
	function _(tag){
		return new _.prototype.init(tag);
	}
	function addEvent(elm,evType,fn,useCapture){
		if(elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		}else if(elm.attachEvent){
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		}else{
			elm['on' + evtype] = fn;
		}
	}
	_.prototype = {
		init : function(tag){
			this[0] = document.createElement(tag);
			return this;		
		},
		css : function(str){
			this[0].style.cssText = str;
			return this;
		},
		html : function(str){
			this[0].innerHTML = str;
			return this;
		}
	};
	_.prototype.init.prototype = _.prototype;
	function pop(p){
		this.width   = p.width || 300;
		this.height  = p.height || 300;
		this.title   = p.title || 'test';
		this.content = p.content || 'test';
		this.hasbg   = p.hasbg || false;
		this.pos     = p.pos || 'mid';
		this.bgColor = p.bgColor || 'E6E6E6';
		this.borColor= p.borColor || 'fff';
		this.drag    = p.drag || false;
		if($('H-pop')) return;
		if(this.hasbg) this.createBackGround();
		this.createPop();
		if(this.drag) this.drag();
	}
	pop.prototype = {
		createPop : function(){
			var _this = this;
			if(ie == 6){
				if(this.pos == 'rb'){
					this.div2 = _("div").css('position:absolute;bottom:0;right:0;background:url(http://www.fczz.com/images/login.jpg);z-index:82;width:'+this.width+'px;height:'+this.height+'px;')[0];
				}else{
					this.div2 = _("div").css('position:absolute;top:'+this.popHeight()+'px;left:'+this.popWidth()+'px;background:url(http://www.fczz.com/images/login.jpg);z-index:82;width:'+this.width+'px;height:'+this.height+'px;')[0];
				}
			}else{
				if(this.pos == 'rb'){
					this.div2 = _("div").css('position:fixed;bottom:0;right:0;background:url(http://www.fczz.com/images/login.jpg);z-index:82;width:'+this.width+'px;height:'+this.height+'px;')[0];
				}else{
					this.div2 = _("div").css('position:fixed;top:'+this.popHeight()+'px;left:'+this.popWidth()+'px;background:url(http://www.fczz.com/images/login.jpg);z-index:82;width:'+this.width+'px;height:'+this.height+'px;')[0];
				}
			}
			this.div3 = _("div").css('width:'+this.width+'px;height:42px;')[0];
			this.div5 = _("div").css('width:15px;margin-top:14px;margin-right:11px;display:inline;float:right;cursor:pointer').html('<img src="http://www.fczz.com/images/close.jpg" alt="关闭" />')[0];
			this.on(this.div5, 'click', function(){
				_this.onclose();
			});
			this.div6 = _("div").css('width:'+(this.width - 20)+'px;margin:0px auto;padding-top:6px;font-size:12px').html(this.content)[0];
			this.div2.id = 'H-pop';
			this.div3.id = 'H-title';
			this.div5.id = 'H-close';
			this.div6.id = 'H-content';
			this.div3.appendChild(this.div5);
			this.div2.appendChild(this.div3);
			this.div2.appendChild(this.div6);
			document.body.appendChild(this.div2);
			if(ie == 6){
				window.onscroll = function(){
					_this.stayWhere();
				}
			}
		},
		pageWidth : function(){
			return Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
		},
		pageHeight : function(){
			return Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
		},
		popWidth : function(){
			return Math.floor((this.pageWidth() - this.width)/2);
		},
		popHeight : function(){
			if(ie == 6){
				if(document.documentElement.scrollTop > 0 || document.body.scrollTop > 0){
					return Math.floor((document.documentElement.clientHeight - this.height)/2) + document.documentElement.scrollTop + document.body.scrollTop;
				}else{
					return Math.floor((document.documentElement.clientHeight - this.height)/2);
				}
			}else{
				return  Math.floor((document.documentElement.clientHeight - this.height)/2);
			}
		},
		popHeight2 : function(){
			if(document.documentElement.scrollTop > 0 || document.body.scrollTop > 0){
				var h1 = parseInt($('H-pop').offsetHeight, 10);
				var h2 = document.documentElement.clientHeight;
				var h3 = parseInt(document.documentElement.scrollTop, 10);
				return h2 - h1 + h3;
			}else{
				return Math.floor(document.documentElement.clientHeight - this.height);
			}
		},
		on : function(el, type, fn){
			el.addEventListener ? 
				el.addEventListener(type, fn, false) :
			el.attachEvent ?
				el.attachEvent("on" + type, fn) :
			el['on'+type] = fn;
		},
		createBackGround : function(){
			if($("H-bg") == null && $("H-bg2") == null){
				var div1 = _("div").css('position:absolute;top:0px;left:0px;background:#000;filter:Alpha(Opacity=60);opacity:0.6;z-index:81;width:'+this.pageWidth()+'px;height:'+this.pageHeight()+'px')[0];
				var ifr  = _("iframe").css('position:absolute;top:0px;left:0px;background:#000;filter:Alpha(Opacity=1);opacity:0.01;z-index:80;border:none;width:'+(this.pageWidth() - 4)+'px;height:'+this.pageHeight()+'px')[0];
				div1.id = 'H-bg';
				ifr.id = 'H-bg2';
				document.body.appendChild(div1);
				document.body.appendChild(ifr);
			}
		},
		drag : function(){
			var isDrag = false,dragId = $('H-pop'),_x,_y;
			dragId.onmousedown = function(e){
				isDrag = true;
				dragId.style.cursor = 'move';
				var e = e || window.event;
				_x = (e.x || e.clientX) - this.offsetLeft;
				_y = (e.y || e.clientY) - this.offsetTop;
			};
			document.onmousemove = function(e){
				var e = e || window.event;
				if(!isDrag) return;
				dragId.style.left = (e.x || e.clientX) - _x + "px";
				dragId.style.top = (e.y || e.clientY) - _y + "px";
			};
			document.onmouseup = function(){
				isDrag = false;
				dragId.style.cursor = 'default';
			}
		},
		onclose : function(){
			var bg = $("H-bg"), bg2 = $("H-bg2"),pop = $("H-pop"),title = $("H-title"),close = $("H-close"),content = $("H-content");
			if(content != null && close != null && title != null && pop != null){
				content.parentNode.removeChild(content);
				close.parentNode.removeChild(close);
				title.parentNode.removeChild(title);
				pop.parentNode.removeChild(pop);
			}
			if(bg != null && bg2 != null){
				bg.parentNode.removeChild(bg2);
				bg.parentNode.removeChild(bg);
			}
			content = null;
			close = null;
			title = null;
			pop = null;
			bg2 = null;
			bg = null;
			if(this['onbi']){
				this.onbi();
			}
		},
		stayWhere : function(){
			var p = $("H-pop");
			if(p){
				if(this.pos == 'rb'){
					p.style.top = this.popHeight2() + 'px';
				}else{
					p.style.top = this.popHeight() + 'px';
					p.style.left = this.popWidth() + 'px';
				}
			}else{
				return;
			}
		}
	};
	return pop;
}();
