function AddEventListener(obj, event, func)
{
	if (typeof(obj.addEventListener) != "undefined")
		obj.addEventListener(event, func, true);
	else
		obj.attachEvent("on" + event, func);
	return func;
}
function RemoveEventListener(obj, event, func)
{
	if (typeof(obj.removeEventListener) != "undefined")
		obj.removeEventListener(event, func, true);
	else
		obj.detachEvent("on" + event, func);
}
function getElementsByClassName(obj, strTagName, cl)
{
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = obj.getElementsByTagName(strTagName);
	for (var i = 0; i < elem.length; i++)
	{
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};
function isIE() { // Private method
  return navigator.userAgent.indexOf("MSIE") > -1;
}

function $__$(id)
{
	return $("#" + id).get(0);
}
function mdUtil(dialogId)
{
	this.dialogId = dialogId;
	this.createOverlays = function()
	{
		if (typeof(this.overlayContainer) != "undefined")
			return;
		var overlay = document.createElement("div");
		overlay.className = "mdOverlay";
		overlay.style.display = "none";
		document.body.appendChild(overlay);
		this.overlayContainer = overlay;
		this.overlayIframe = null;
		if (isIE())
		{
			//create iframe helper
			var overlayIframe = document.createElement("iframe");
			overlayIframe.className = "mdOverlayIframe";
			overlayIframe.frameBorder = "0";
			overlayIframe.src = "#";
			overlayIframe.style.display = "none";
			overlayIframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			document.body.insertBefore(overlayIframe);
			this.overlayIframe = overlayIframe;
		}
	}
	this.createDialog = function()
	{
		this.dialogObj = $__$(this.dialogId);
		try
		{
			var btnClose = getElementsByClassName(this.dialogObj, "*", "mdBtnClose")[0];
			btnClose.onclick = function(){mdUtilManager.closeDialog(); return false;};
		}
		catch(e){}
		try
		{
			var btnOk = getElementsByClassName(this.dialogObj, "*", "mdBtnOk")[0];
			btnOk.onclick = function()
			{	
				if (mdUtilManager.callerObj != null && typeof(mdUtilManager.callerObj.OkCallback) == "function")
				{
					mdUtilManager.callerObj.OkCallback();
				}
				//mdUtilManager.closeDialog();
				return false;
			};
		}
		catch(e){}
		try
		{
			var btnCancel = getElementsByClassName(this.dialogObj, "*", "mdBtnCancel")[0];
			btnCancel.onclick = function(){mdUtilManager.closeDialog(); return false;};
			this.btnClose = btnCancel;
		}
		catch(e){}
		this.eventListeners = false;
		//new DraggableObject(null, "modalDialog", null, Drag, null, "move");
	}
	this.autoSize = function()
	{
		this.dialogObj.style.height = ($(this.dialogObj).find(">div:eq(1)").height() + 50) + "px";
	}
	this.showModalDialog = function(title, url, width, height, callerObj, posX, posY)
	{
		mdUtilManager.callerObj = callerObj;
		this.dialogObj.style.display = "block";
		this.dialogObj.style.width = width + "px";
		this.dialogObj.style.height = height + "px";
		this.overlayContainer.style.display = "block";
		if (isIE())
			this.overlayIframe.style.display = "block";
		this.centerDialog();
		this.moveTo(posX, posY);
		this.fixOverlay();
		//set md title
		var spanTitle = getElementsByClassName(this.dialogObj, "*", "mdTitleText")[0];
		spanTitle.innerHTML = title;
		if (!this.eventListeners)
		{
			AddEventListener(window, "resize", this.fixOverlayCommon);
			AddEventListener(window, "scroll", this.fixOverlayCommon);
			AddEventListener(document.body, "keyup", this.handleKey);
		}
		//clear status message
		var spanMsg = $__$("mdResultMessage");
		if (spanMsg)
			spanMsg.innerHTML = "";
		/*if (this.btnClose)
			this.btnClose.style.visibility = "hidden";*/
	}
	this.fixOverlay = function()
	{
		if (isIE())
		{
			/*this.overlayIframe.style.width = this.overlayContainer.style.width = document.body.clientWidth + "px";
			this.overlayIframe.style.height = this.overlayContainer.style.height = document.body.clientHeight + "px";
			this.overlayIframe.style.left = this.overlayContainer.style.left = document.body.scrollLeft + "px";
			this.overlayIframe.style.top = this.overlayContainer.style.top = document.body.scrollTop + "px";
			this.overlayIframe.style.left = this.overlayContainer.style.left = document.body.scrollLeft + "px";
			this.overlayIframe.style.top = this.overlayContainer.style.top = document.body.scrollTop + "px";*/
			this.overlayIframe.style.width = this.overlayContainer.style.width = document.body.scrollWidth + "px";
			this.overlayIframe.style.height = this.overlayContainer.style.height = document.body.scrollHeight + "px";
			this.overlayIframe.style.left = this.overlayContainer.style.left = "0";
			this.overlayIframe.style.top = this.overlayContainer.style.top = "0";
			this.overlayIframe.style.left = this.overlayContainer.style.left = "0";
			this.overlayIframe.style.top = this.overlayContainer.style.top = "0";
		}else
		{
			/*this.overlayContainer.style.width = document.documentElement.clientWidth + "px";
			this.overlayContainer.style.height = document.documentElement.clientHeight + "px";
			this.overlayContainer.style.left = document.documentElement.scrollLeft + "px";
			this.overlayContainer.style.top = document.documentElement.scrollTop + "px";
			this.overlayContainer.style.left = document.documentElement.scrollLeft + "px";
			this.overlayContainer.style.top = document.documentElement.scrollTop + "px";*/
			this.overlayContainer.style.width = document.documentElement.scrollWidth + "px";
			this.overlayContainer.style.height = document.documentElement.scrollHeight + "px";
			this.overlayContainer.style.left = "0";
			this.overlayContainer.style.top = "0";
		}
	}
	
	//this is bad
	this.fixOverlayCommon = function()
	{
		mdUtilManager.fixOverlay();
	}
	//--bad
	
	this.closeDialog = function()
	{
		this.dialogObj.style.display = "none";
		this.overlayContainer.style.display = "none";
		this.overlayContainer.style.width = "0";
		if (this.overlayIframe)
		{
			this.overlayIframe.style.display = "none";
			this.overlayIframe.style.width = "0";
		}
		RemoveEventListener(window, "resize", this.fixOverlayCommon);
		RemoveEventListener(window, "scroll", this.fixOverlayCommon);
		RemoveEventListener(window, "keyup", this.handleKey);
	}
	this.centerDialog = function()
	{
		return;
		var posX = Math.round((document.body.clientWidth - this.dialogObj.offsetWidth) / 2);
		var posY = Math.round((document.body.offsetHeight - this.dialogObj.offsetHeight) / 2);
		if (posX < 0)
			posX = 0;
		if (posY < 0)
			posY = 0;
		this.dialogObj.style.left = posX + "px";
		this.dialogObj.style.top = posY + "px";
	}
	this.moveTo = function(posX, posY)
	{
		this.dialogObj.style.marginLeft = posX + "px";
		this.dialogObj.style.marginTop = posY + "px";
	}
	this.showMessage = function(msg)
	{
		var obj = $__$("mdResultMessage");
		if (obj)
		{
			obj.innerHTML = msg;
		}
		if (this.btnClose)
		{
			this.btnClose.style.visibility = "visible";
		}
	}
	this.handleKey = function(e)
	{
		e = e ? e : window.event;
		if (e.keyCode == 27)
		{
			mdUtilManager.closeDialog();
		}else if (e.keyCode == 13)
		{
			mdUtilManager.callerObj.OkCallback();
			mdUtilManager.closeDialog();
		}
	}
	if (!$__$(this.dialogId))
		return;
	this.createOverlays();
	this.createDialog();
}
function mdShow(title, src, width, height, callerObj, posX, posY)
{
	if (typeof(mdUtilManager) != "undefined")
	{
		mdUtilManager.showModalDialog(title, src, width, height, callerObj, posX, posY);
		return mdUtilManager;
	}
}
function AssignOpenerHandler(id, title, src, width, height)
{
	var obj = $__$(id);
	if (obj)
		obj.onclick = function(){ mdShow(title, src, width, height);return false; };
}
$(document).ready(function()
	{
		window.mdUtilManager = new mdUtil("modalDialog");
		window.hasSubscribed = false;
		window.hasVoted = false;
		$("#subscribeLink").click(function()
		{
			$("#modalDialog .mdBtnCancel").attr("value", "Закрити");
			$("#modalDialog .mdBtnOk").hide();
			$("#subscribeDialog").show();
			$("#pollDialog").hide();
			mdShow('Що таке RSS?', '', 400, 130, this, 190, 0).autoSize();
			return false;
		});
		$("#voteLink").click(function()
		{
			if (!window.hasVoted)
			{
				$("#modalDialog .mdBtnOk").show();
				$("#modalDialog .mdBtnOk").attr("value", "Голосувати");
				$("#modalDialog .mdBtnCancel").attr("value", "Вiдмiна");
			}else
			{
				$("#modalDialog .mdBtnCancel").attr("value", "Закрити");
				$("#modalDialog .mdBtnOk").hide();
			}
			$("#subscribeDialog").hide();
			$("#pollDialog").show();
			mdShow('Опитування', '', '250', '250', this, 200, 290).autoSize();
			return false;
		});
		if ($("#subscribeLink").get(0))
		{
		$("#subscribeLink").get(0).OkCallback = function()
		{
			var postData = {};
			postData.txtName = $("#subscribeDialog input[name=txtName]").get(0).value;
			postData.txtEmail = $("#subscribeDialog input[name=txtEmail]").get(0).value;
			if (postData.txtName.length == 0 || postData.txtEmail.length == 0)
			{
				alert("Ви повиннi заповнити поля Iм'я та Email.");
				return false;
			}
			var email_check = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
			if (!email_check.test(postData.txtEmail))
			{
				return false;
			}
			$.post("/subscribe.php", postData, function(data, textStatus)
			{
				$("#subscribeDialog p").hide();
				$("#subscribeDialog .response").show();
				$("#modalDialog .mdBtnOk").hide();
				$("#modalDialog .mdBtnCancel").attr("value", "Закрити");
			});
			window.hasSubscribed = true;
		}};
		if ($("#voteLink").get(0))
		{
		$("#voteLink").get(0).OkCallback = function()
		{
			if ($("#modalDialog input[checked]").length == 0)
			{
				alert("Любий друже, вибери хоть що-небудь, будь ласка!");
				return false;
			}
			var postData = {};
			postData.vote = $("#modalDialog input[checked]").attr("value");
			postData.poll = $("#modalDialog input[name='poll']").attr("value");
			$.post("/vote.php", postData, function(data, textStatus)
			{
				$("#pollDialog p").hide();
				$("#pollDialog .response").show();
				$("#modalDialog .mdBtnOk").hide();
				$("#modalDialog .mdBtnCancel").attr("value", "Закрити");
			});
			window.hasVoted = true;
		}}
	}
);

