var isIE = (document.all) ? true : false;

var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
}

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}

var BindAsEventListener = function(object, fun) {
	return function(event) {
		return fun.call(object, (event || window.event));
	}
}

function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

var SimpleDrag = Class.create();
SimpleDrag.prototype = {
  initialize: function(drag) {
	this.Drag = $(drag);
	this._x = this._y = 0;
	this._fM = BindAsEventListener(this, this.Move);
	this._fS = Bind(this, this.Stop);
	this.Drag.style.position = "absolute";
	addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start));
  },
  Start: function(oEvent) {
	this._x = oEvent.clientX - this.Drag.offsetLeft;
	this._y = oEvent.clientY - this.Drag.offsetTop;
	addEventHandler(document, "mousemove", this._fM);
	addEventHandler(document, "mouseup", this._fS);
	if (isIE) {
		addEventHandler(this.Drag, "losecapture", this._fS);
		this.Drag.setCapture();
	} else {
		addEventHandler(window, "blur", this._fS);
		oEvent.preventDefault();
	};
  },
  Move: function(oEvent) {
	this.Drag.style.left = oEvent.clientX - this._x + "px";
	this.Drag.style.top = oEvent.clientY - this._y + "px";
  },
  Stop: function() {
	removeEventHandler(document, "mousemove", this._fM);
	removeEventHandler(document, "mouseup", this._fS);
	if (isIE) {
		removeEventHandler(this.Drag, "losecapture", this._fS);
		this.Drag.releaseCapture();
	} else {
		removeEventHandler(window, "blur", this._fS);
	};
  }
};

function getPopupDiv() {
	var popupDiv = document.getElementById('popupDiv');
	if (popupDiv == null) {
		popupDiv = document.createElement('div');
		popupDiv.innerHTML = "<table border=0 cellpadding=5 bgColor='#999999' style='font-family:\"Arial\";font-size:14px;font-weight:bold;color:#000000'><tr><td align=left id='popupDivTitle'></td><td><img id='popupDivCounter' width='1' height='1'></td><td align=right><span style='cursor:pointer' onClick='document.getElementById(\"popupDiv\").style.visibility=\"hidden\"' onMouseOver='this.style.color=\"#FFFFFF\"' onMouseOut='this.style.color=\"#000000\"'>Close</span></td></tr><tr><td colspan=3 bgColor='#F7F7F7'><iframe id='popupDivFrame' width='800' height='600' frameborder='0'></iframe></td>";
		popupDiv.setAttribute('id', 'popupDiv');
		popupDiv.style.position = "absolute";
		popupDiv.style.left = "0px";
		popupDiv.style.top = "0px";
		popupDiv.style.zIndex = "999";
		popupDiv.style.border = "1px outset #CCCCCC";
		popupDiv.style.visibility = "hidden";
		document.body.appendChild(popupDiv);
		new SimpleDrag("popupDiv");
	}
	return popupDiv;
}

function popupPage(src, title) {
	var popupDiv = getPopupDiv();
	document.getElementById('popupDivFrame').src = src;
	document.getElementById('popupDivTitle').innerHTML = title;
	document.getElementById('popupDivCounter').src = "http://www.easynth.com/counter.php?url=Popup: " + src;
	popupDiv.style.left = (document.body.clientWidth - popupDiv.clientWidth) / 2 + document.body.scrollLeft;
	popupDiv.style.top = (document.body.clientHeight - popupDiv.clientHeight) / 2 + document.body.scrollTop;
	popupDiv.style.visibility = "visible";
}