// global variables //
var TIMER = 2;
var SPEED = 50;
var WRAPPER = 'dialogbox';

// 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;
}

var DialogBox=new Object();

// hide the dialog box //
DialogBox.hide = function() {
  var dialog = document.getElementById('dialog');
  if(!dialog.timer) {
    dialog.timer = setInterval("DialogBox.fade(0)", TIMER);
  }  
}


// fade-in the dialog box //
DialogBox.fade = function(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  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);
	if(typeof(dialog.on_dialogClosed)!='undefined' && flag!=1 ){
        dialog.on_dialogClosed(dialog);		
    }
  }
  
}

// build/show the dialog box, populate the data and call the fadeDialog function //
DialogBox.show = function(title, message, type, autohide,dimmension,onclose)
{
    if (!type)
    {
        type = 'error';
    }
    var dialog;
    var dialogheader;
    var dialogclose;
    var dialogtitle;
    var dialogcontent;
    var dialogmask;
    if (!document.getElementById('dialog'))
    {
        dialog = document.createElement('div');
        dialog.id = 'dialog';
        dialogheader = document.createElement('div');
        dialogheader.id = 'dialog-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';
        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','DialogBox.hideDialog()');
        dialogclose.onclick = DialogBox.hide;
        if(typeof(dimmension)!='undefined'){
            dialog.style.width=dimmension.width;
            dialog.style.height=dimmension.height;
        }else{
            dialogcontent.style.height='156px'
        }
    }
    else
    {
        dialog = document.getElementById('dialog');
        dialogheader = document.getElementById('dialog-header');
        dialogtitle = document.getElementById('dialog-title');
        dialogclose = document.getElementById('dialog-close');
        dialogcontent = document.getElementById('dialog-content');
        dialogmask = document.getElementById('dialog-mask');
        dialogmask.style.visibility = "visible";
        dialog.style.visibility = "visible";
		 if(typeof(dimmension)!='undefined'){
            dialog.style.width=dimmension.width;
            dialog.style.height=dimmension.height;
        }else{
            dialogcontent.style.height='156px'
        }
    }
    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 topposition = top + (height / 2) - (dialogheight / 2);
	if(typeof(dimmension.top)!='undefined'){
		topposition = top + dimmension.top;
	}	
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialog.style.top = topposition + "px";
    dialog.style.left = leftposition + "px";
	if(typeof(dimmension)!='undefined' && typeof(dimmension.top)!='undefined'){
		dialog.style.top = dimmension.top;		
	}else{
		//alert('dim:{0},top:{1}'.format(typeof(dimmension)=='undefined',typeof(dimmension)!='undefined' && typeof(dimmension.top)=='undefined'))
		
	}
	
    dialogheader.className = type + "header";
    dialogtitle.innerHTML = title;
    dialogcontent.className = type;
    dialogcontent.innerHTML = message;
    if(typeof(onclose)!='undefined'){
        dialog.on_dialogClosed=onclose;        
    }
        
    var content = document.getElementById(WRAPPER);
    //dialogmask.style.height = dialogcontent.offsetHeight + 'px';
    dialog.timer = setInterval("DialogBox.fade(1)", TIMER);
    if (autohide)
    {
        dialogclose.style.visibility = "hidden";
        window.setTimeout("DialogBox.hide()", (autohide * 1000));
    } else
    {
        dialogclose.style.visibility = "visible";
    }
}
DialogBox.clearContent = function(){
    $get('dialog-content').html('');    
}

var LoadProcess = new Object();

LoadProcess.show = function(position)
{
    var load_process;

    if (!document.getElementById('load_process'))
    {
        load_process = document.createElement('div');
        load_process.id = 'load_process';
        document.body.appendChild(load_process);
    }
    else
    {
        load_process = document.getElementById('load_process');
    }
    if(!position){
        var width = pageWidth();
        var height = pageHeight();
        var left = leftPosition();
        var top = topPosition();
        var load_process_width = load_process.offsetWidth;
        var load_process_height = load_process.offsetHeight;
        var topposition = top + (height / 2) - (load_process_width / 2);
        var leftposition = left + (width / 2) - (load_process_height / 2);

        load_process.style.top = topposition + "px";
        load_process.style.left = leftposition + "px";
    }else{
        load_process.style.top = position[1];
        load_process.style.left = position[0];    
    }
    load_process.style.visibility = "visible";
}

// hide the load_process  //
LoadProcess.hide = function()
{
    
    var load_process = document.getElementById('load_process');
    load_process.style.visibility = "hidden";
     
}