// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'main';

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

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

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

function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function isDef(arg, argtype){
	try {
		if (argtype === null || typeof(argtype) == 'undefined') return (arg !== null && typeof(arg) != 'undefined');
		return (arg !== null && typeof(arg) == argtype);
	}
	catch (e) {}
	return false;
}

function button(name, close){
	this.name = name;
	this.close = close;
}


function showConfirm(title,message,buttons,callback){
	if (!isDef(buttons)){ buttons = new Array(new button('No', 'no'), new button('Yes', 'yes')); }
	createDialog(title, message, 'confirm', buttons, null, callback);
}

function showDialog(title,message,type,buttons,autohide) {
	if (!isDef(buttons)){ buttons = new Array(new button('Ok', 'ok')); }
	createDialog(title, message, type, buttons, autohide, null);
}

function hideDialog() {
	var dialog = document.getElementById('dialog');
	clearInterval(dialog.timer);
	dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

function encodeUtf8(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function decode(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}


function createDialog(title,message,type,buttonList,autohide,callback) {
	//title = decode(title);
	//message = decode(message);
	
	if (!isDef(buttonList)) {buttonList = new Array(new button('OK', 'ok'));}
	if(!type) {
	    type = 'error';
	}
	var dialog = document.getElementById('dialog');
	if(dialog!=null) return;
	
	var dialog = document.createElement('div');
	dialog.id = 'dialog';
	var dialogheader = document.createElement('div');
	dialogheader.id = 'dialog-header';
	var dialogtitle = document.createElement('div');
	dialogtitle.id = 'dialog-title';
	var dialogcontent = document.createElement('div');
	dialogcontent.id = 'dialog-content';
	var dialogmask = document.createElement('div');
	dialogmask.id = 'dialog-mask';
	document.body.appendChild(dialogmask);
	document.body.appendChild(dialog);
	dialog.appendChild(dialogheader);
	dialogheader.appendChild(dialogtitle);
	dialog.appendChild(dialogcontent);
	    
    if(type!=='splash') {
  	  	var dialogfooter = document.createElement('div');
	    dialogfooter.id = 'dialog-footer';
	    dialogfooter.className = type + "footer";
    	dialog.appendChild(dialogfooter);
    	
		for (var id=0; id < buttonList.length; id++) {
		    var button = document.createElement('input');
		    button.id = 'dialog-' + id;
		    button.type = "button";
		    button.className = "dialog-button";
		    button.text = buttonList[id].close;
		    button.value = buttonList[id].name;
	        dialogfooter.appendChild(button);
	        
	        button.onclick = function(){
	        	hideDialog();
	        	callback(this.text);
	        };
		}
    }

	dialog.style.opacity = .00;
	dialog.style.filter = 'alpha(opacity=0)';
	dialog.alpha = 0;
	var width = pageWidth();
	var height = pageHeight();
	var left = leftPosition();
	var top = topPosition();
	var dialogwidth = dialog.offsetWidth;
	var dialogheight = dialog.offsetHeight;
	var topposition = top + (height / 3) - (dialogheight / 2);
	var leftposition = left + (width / 2) - (dialogwidth / 2);
	dialog.style.top = topposition + "px";
	dialog.style.left = leftposition + "px";
	dialogheader.className = type + "header";
	dialogtitle.innerHTML = title;
	dialogcontent.className = type;
	dialogcontent.innerHTML = message;
	 
	var content = document.getElementById(WRAPPER);
	dialogmask.style.height = content.offsetHeight + 'px';
	dialog.timer = setInterval("fadeDialog(1)", TIMER);
	if(title==null){
		dialogtitle.style.visibility = 'hidden';
	}
	if(type==='splash' && autohide==null){
		autohide = 1;
	}
	if(autohide) {
	    window.setTimeout("hideDialog()", (autohide * 1000));
	}
}



// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var dialogmask = document.getElementById('dialog-mask');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    //dialog.style.visibility = "hidden";
	//document.getElementById('dialog-mask').style.visibility = "hidden";
	
	clearInterval(dialog.timer);
	
	dialog.parentNode.removeChild(dialog);
	dialogmask.parentNode.removeChild(dialogmask);    
  }
}
