"use strict";
var REDIPS = {};
REDIPS.dialog = (function () {
	var	init,
		show,
		hide,
		position,
		visibility,
		fade,
		shade,
		dialog_box,
		dialog_width = 0,
		dialog_height = 0,
		op_high = 60,
		op_low = 0,
		fade_speed = 18,
		function_call;
	init = function () {
		dialog_box = document.createElement('div');
		dialog_box.setAttribute('id', 'dialog');
		shade = document.createElement('div');
		shade.setAttribute('id', 'shade');
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(shade);
		body.appendChild(dialog_box);
		if (window.addEventListener) {
			window.addEventListener('scroll', position, false);
			window.addEventListener('resize', position, false);
		}
		else if (window.attachEvent) {
			window.attachEvent('onscroll', position);
			window.attachEvent('onresize', position);
		}
		else {
			window.onscroll = position;
			window.onresize = position;
		}
	};
	show = function (width, height, text, button1, button2) {
		var input1 = '',
			input2 = '',
			img,
			img_text;
		if (button1 === undefined) {
			button1 = 'Close';
		}
		dialog_width  = width;
		dialog_height = height;
		position();
		img = /(\.jpg|\.jpeg|\.gif|\.png)$/i;
		if (img.test(text)) {
			img_text = text.split('|');
			if (img_text[1] === undefined) {
				text = '<IMG src="' + img_text[0] + '" height="' + (height - 40) + '"/>';
			}
			else {
				text = '<IMG src="' + img_text[1] + '" height="' + (height - 40) + '"/>';
				text += '<P>' + img_text[0] + '</P>';
			}
		}
		button1 = button1.split('|');
		input1 = '<INPUT type="button" onclick="REDIPS.dialog.hide(\'' + button1[1] + '\');" value="' + button1[0] + '">';
		if (button2 !== undefined) {
			button2 = button2.split('|');
			input2  = '<INPUT type="button" onclick="REDIPS.dialog.hide(\'' + button2[1] + '\');" value="' + button2[0] + '">';
		}
		dialog_box.innerHTML = '<TABLE><TR><TD valign="center" height="' + height + 
								 '" width="' + width + '"><P>' + text + '</P><P>' + input1 + input2 + '</P>' +
								 '</TD></TR></TABLE>';
		shade.style.display = dialog_box.style.display = 'block';
		visibility('hidden');
		fade(op_low, 10);
	};
	hide = function (fnc) {
		function_call = fnc;
		fade(op_high, -20);
		dialog_box.style.display = 'none';
		visibility('visible');
	};
	position = function () {
		var window_width, window_height, scrollX, scrollY;
		if (typeof(window.innerWidth) === 'number') {
			window_width  = window.innerWidth;
			window_height = window.innerHeight;
			scrollX = window.pageXOffset;
			scrollY = window.pageYOffset;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			window_width  = document.documentElement.clientWidth;
			window_height = document.documentElement.clientHeight;
			scrollX = document.documentElement.scrollLeft;
			scrollY = document.documentElement.scrollTop;
			shade.style.width   = window_width  + 'px';
			shade.style.height  = window_height + 'px';
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			window_width  = document.body.clientWidth;
			window_height = document.body.clientHeight;
			scrollX = document.body.scrollLeft;
			scrollY = document.body.scrollTop;
		}
		dialog_box.style.left = (scrollX + ((window_width  - dialog_width)  / 2)) + 'px';
		dialog_box.style.top  = (scrollY + ((window_height - dialog_height) / 2)) + 'px';
		shade.style.top  = scrollY + 'px';
		shade.style.left = scrollX + 'px';
	};
	visibility = function (p) {
		var obj = [],
			x, y;
		obj[0] = document.getElementsByTagName('select');
		obj[1] = document.getElementsByTagName('iframe');
		obj[2] = document.getElementsByTagName('object');
		for (x = 0; x < obj.length; x++) {
			for (y = 0; y < obj[x].length; y++) {
				obj[x][y].style.visibility = p;
			}
		}
	};
	fade = function (opacity, step) {
		shade.style.opacity = opacity / 100;
		shade.style.filter  = 'alpha(opacity=' + opacity + ')';
		opacity += step;
		if (op_low <= opacity && opacity <= op_high) {
			setTimeout(
				function () {
					fade(opacity, step);
				}, fade_speed);
		}
		else if (op_low > opacity) {
			shade.style.display = 'none';
			if (function_call !== 'undefined') {
				window[function_call]();
			}
		}
	};
	return {
		op_high			: op_high,
		op_low			: op_low,
		fade_speed		: fade_speed,
		init			: init,
		show			: show,
		hide			: hide
	};
}());
function button1() {
	alert('Hi from function button1 ...');
}
function button2() {
	alert('Hi from function button2 ...');
}
window.onload = function () {
	REDIPS.dialog.init();
	REDIPS.dialog.show(663, 391, '|moving-notice/moving-notice-front.jpg');
};