// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'dialog-content';

// calculate the current window width //
function pageWidth() {
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
	return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data //
function showDialog(title, message, type, autohide) {
	if (!type) 
		type = 'error';
  
	var dialog;
	var dialogheader;
	var dialogclose;
 	var dialogtitle;
	var dialogcontent;
	var dialogmask;
  	if ($('#dialog')) 
		$('#dialog').remove();
		
	if ($('#dialog-mask')) 
		$('#dialog-mask').remove();

	dialog = document.createElement('div');
	dialog.id = 'dialog';
	dialogheader = document.createElement('div');
	dialogheader.id = 'dialog-header';
	dialogheader.className = type + "header";
	dialogtitle = document.createElement('div');
	dialogtitle.id = 'dialog-title';
	dialogclose = document.createElement('div');
	dialogclose.id = 'dialog-close'
	dialogcontent = document.createElement('div');
	dialogcontent.id = 'dialog-content';
	dialogcontent.className = type;
	
	dialogmask = document.createElement('div');
	dialogmask.id = 'dialog-mask';
	document.body.appendChild(dialogmask);
	document.body.appendChild(dialog);
	dialog.appendChild(dialogheader);
	dialogheader.appendChild(dialogtitle);
	dialogheader.appendChild(dialogclose);
	dialog.appendChild(dialogcontent);;
	dialogclose.setAttribute('onclick','hideDialog()');
	dialogclose.onclick = hideDialog;
	
	dialogtitle.innerHTML = title;
	dialogcontent.innerHTML = message;
	
	var width = pageWidth();
	var height = pageHeight();
	var left = leftPosition();
	var top = topPosition();
	var dialogwidth = type == 'fullscreen' ? width : dialog.offsetWidth;
	var dialogheight = type == 'fullscreen' ? height : dialog.offsetHeight;
	var topposition = type == 'fullscreen' ? 0 : (top + (height / 3) - (dialogheight / 2));
	var leftposition = type == 'fullscreen' ? 0 : (left + (width / 2) - (dialogwidth / 2));
	
	dialog.style.top = topposition + "px";
	dialog.style.left = leftposition + "px";
	if (type == 'fullscreen') {
		dialog.style.height = height + 'px';
		dialog.style.width = (width - 20) + 'px';
		dialogheader.style.width = (width - 38) + 'px';
		dialogcontent.style.height = (height - 58) + 'px';
	}
	var content = document.getElementById(WRAPPER);
	dialogmask.style.height = $(document).height() + 'px';
	
	if (autohide) {
		dialogclose.style.visibility = "hidden";
		window.setTimeout("hideDialog()", (autohide * 1000));
	} else {
		dialogclose.style.visibility = autohide === false ? 'hidden' : 'visible';
	}
	$('#dialog-content .close').each(function() {
		$(this).click(hideDialog);
	});
}

function dialogBoxResize() {
	var dialog = $('#dialog')[0];
	if (typeof dialog == 'undefined') return false;
	
	var dialogheader = $('#dialog-header')[0];
	var dialogtitle = $('#dialog-title')[0];
	var dialogcontent = $('#dialog-content')[0];
	var dialogmask = $('#dialog-mask')[0];
	
	var type = dialogcontent.className;
	var width = pageWidth();
	var height = pageHeight();
	var left = leftPosition();
	var top = topPosition();
	var dialogwidth = type == 'fullscreen' ? width : dialog.offsetWidth;
	var dialogheight = type == 'fullscreen' ? height : dialog.offsetHeight;
	var topposition = type == 'fullscreen' ? 0 : (top + (height / 3) - (dialogheight / 2));
	var leftposition = type == 'fullscreen' ? 0 : (left + (width / 2) - (dialogwidth / 2));
	
	dialog.style.top = topposition + "px";
	dialog.style.left = leftposition + "px";
	if (type == 'fullscreen') {
		dialog.style.height = height + 'px';
		dialog.style.width = (width - 20) + 'px';
		dialogheader.style.width = (width - 38) + 'px';
		dialogcontent.style.height = (height - 58) + 'px';
	}

	var content = document.getElementById(WRAPPER);
	dialogmask.style.height = $(document).height() + 'px';
}

// hide the dialog box //
function hideDialog() {
	$('#dialog').hide();
	$('#dialog-mask').hide();
}


/**
 * custom confirm dialog function
 */
function confirmDialog(options) {
	title = options.title ? options.title : 'Please confirm!';
	message = options.message;
	okLabel = options.okLabel ? options.okLabel : 'OK';
	cancelLabel = options.cancelLabel ? options.cancelLabel : 'Cancel';
	okUrl = options.okUrl;
	cancelUrl = options.cancelUrl ? options.cancelUrl : '#';
	
	if (typeof okUrl == 'undefined') 
		alert('OK-Url is not defined!');
	
	message += '<span class="button ok"><a href="#" onclick="dialogBoxSubmit(); return false;"><span>' + okLabel + '</span></a></span> ';
	message += '<span class="button cancel"><a href="' + cancelUrl + '" class="close" onclick="return false"><span>' + cancelLabel + '</span></a></span></div>';
	
	message = '<form id="dialogBoxForm" method="post" action="' + okUrl + '">' + message + '</form>';
	
	showDialog(title, message, 'prompt', 100000);
	$('#dialogBoxForm .error').hide();
}

/**
 * custom confirm dialog function
 */
function promptDialog(options) {
	confirmDialog(options);
}

/**
 * custom confirm dialog function
 */
function successDialog(title, message, closeLabel) {
	message += '<span class="button ok"><a href="#" class="close"><span>' + closeLabel + '</span></a></span>';
	showDialog(title, message, 'success');
}

/**
 * custom confirm dialog function
 */
function warningDialog(title, message, closeLabel) {
	message += '<div class="button"><span class="button"><a href="#" class="accept close" onclick="return false">' + closeLabel + '</a></span></div>';
	showDialog(title, message, 'warning');
}

/**
 * custom confirm dialog function
 */
function errorDialog(title, message, closeLabel) {
	message += '<span class="button ok"><a href="#" class="close" onclick="return false"><span>' + closeLabel + '</span></a></span>';
	showDialog(title, message, 'error');
}

function dialogBoxSubmit() {
	var valid = true;
	$('#dialogBoxForm .required').each(function() {
		value = this.value.replace(/[^a-z0-9]+/i, '');
		type = this.type;
		$(this).removeClass('error');
		
		switch (type) {
			case 'text':
			case 'textarea':
				if (value == '') {
					$(this).addClass('error');
					valid = false;
				}
				break;
			case 'radio':
				alert('not implemented');
				break;
			case 'checkbox':
				if (!this.checked)
					valid = false;
				break;
			case 'hidden':
				if (this.name == 'check_radio') {
					var name = this.value;
					var checked = false;
					$('#dialogBoxForm input[name=' + name + ']').each(function() {
						if (this.checked)
							checked = true;
					});
					if (!checked)
						valid = false;
				}
				break;
		}
	});
	
	if (valid == true) {
		$('#dialogBoxForm').submit();
	} else {
		$('#dialogBoxForm p.error').fadeOut().fadeIn();
		$('#dialogBoxForm div.error').fadeOut().fadeIn();
		return false;
	}
}
