// Global Variables //
var TIMER = 10;
var SPEED = 20;
var defaultWidth = 425;
var defaultHeight = 160;
var documentObj;
var windowObj;
var NavigatorVar;
var UID;

// Gradient switching
var fade = false;

// Get the current top-level window, the container|
documentObj = document;
windowObj = window;
if (window.top.document.body) {
    if (window.top.document.body.tagName.toLowerCase() == "body") {
        documentObj = window.top.document;
        windowObj = window.top;
    } else if (window.parent.document.body.tagName.toLowerCase() == "body") {
        documentObj = window.parent.document;
        windowObj = window.parent;
    }
}

// Get the single largest number
function getUID() {
    var d = new Date();
    return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
}

// Get the single largest number
UID = getUID();

// Access to browser type
function getNavigator() {
    if (navigator.userAgent.indexOf("MSIE") > 0) return 100;
    if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) return 200;
    if (isSafari = navigator.userAgent.indexOf("Safari") > 0) return 300;
    if (isCamino = navigator.userAgent.indexOf("Camino") > 0) return 400;
    if (isMozilla = navigator.userAgent.indexOf("Gecko") > 0) return 500;
    return 0;
}

// Access to IE version
function getIEVer() {
    var ua = navigator.userAgent; 
    var b = ua.indexOf("MSIE ");
    if (b < 0) 
        return 0;
    return parseFloat(ua.substring(b + 5, ua.indexOf(";", b))); 
}

// Get the current number of browser
NavigatorVar = getNavigator();
if (NavigatorVar == 100)
    NavigatorVar += getIEVer();

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

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

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

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

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title, message, type, autohide, iframeScr, width, height) {
    // Set IE Timer & Speed
    if (NavigatorVar < 200) {
        TIMER = TIMER * 2;
        SPEED = SPEED * 2;
    }

    // Set UID List
    var uidList
    if (!documentObj.getElementById('top-dialog-box-uid-list')) {
        uidList = documentObj.createElement('input');
        uidList.id = 'top-dialog-box-uid-list';
        uidList.type = 'hidden';
        uidList.value = UID;
        documentObj.body.appendChild(uidList);
    } else {
        uidList = documentObj.getElementById('top-dialog-box-uid-list');
        if (uidList.value == "")
            uidList.value = UID;
        else
            uidList.value += "," + UID;
    }

    // Set the default type
  if(!type) {
    type = 'error';
  }  
  
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!documentObj.getElementById(UID + 'dialog')) {
    dialog = documentObj.createElement('div');
    dialog.id = UID + 'dialog';
    dialog.className = 'top-dialog-box';
    dialogheader = documentObj.createElement('div');
    dialogheader.id = UID + 'dialog-header';
    dialogheader.className = 'top-dialog-box-header';
    dialogtitle = documentObj.createElement('div');
    dialogtitle.id = UID + 'dialog-title';
    dialogtitle.className = 'top-dialog-box-title';
    dialogclose = documentObj.createElement('div');
    dialogclose.id = UID + 'dialog-close';
    dialogclose.className = 'top-dialog-box-close';

    dialogclose.title = UID;
    
    dialogcontent = documentObj.createElement('div');
    dialogcontent.id = UID + 'dialog-content';
    dialogcontent.className = 'top-dialog-box-content';
    dialogmask = documentObj.createElement('div');
    dialogmask.id = UID + 'dialog-mask';
    dialogmask.className = 'top-dialog-box-mask';
    
    documentObj.body.appendChild(dialogmask);
    documentObj.body.appendChild(dialog);
    
    dialog.appendChild(dialogheader);
    dialog.appendChild(dialogcontent);
    
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialogheader.style.cursor = 'default';
    dialogheader.setAttribute('style', '-moz-user-select:none;');
    dialogheader.setAttribute('onselectstart', 'return false');
    dialogheader.onselectstart = function() { return false; }
    
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = documentObj.getElementById(UID + 'dialog');
    dialogheader = documentObj.getElementById(UID + 'dialog-header');
    dialogtitle = documentObj.getElementById(UID + 'dialog-title');
    dialogclose = documentObj.getElementById(UID + 'dialog-close');
    dialogcontent = documentObj.getElementById(UID + 'dialog-content');
    dialogmask = documentObj.getElementById(UID + 'dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }

  // Set the height of the shadow
  dialogmask.style.height = documentObj.body.offsetHeight + 'px';

  // Setting level
  dialogmask.style.zIndex = 1000;
  dialog.style.zIndex = 1000;

  // Settings dialog box size
  if (width)
      dialog.style.width = width + 'px';
  else
      dialog.style.width = defaultWidth + 'px';
  if (height)
      dialogcontent.style.height = height + 'px';
  else
      dialogcontent.style.height = defaultHeight + 'px';

  // Set Dialog Location //
  setDialogLocation();

  // Set Header Position
  dialogheader.className = 'top-dialog-box-header top-dialog-box-' + type + "header";
  dialogtitle.innerHTML = '&nbsp;' + title;
  dialogcontent.className = 'top-dialog-box-content top-dialog-box-' + type;

  // Set iframe
  if (iframeScr) {
      dialogcontent.style.padding = '0';
      dialogcontent.innerHTML = '<iframe src="' + iframeScr + '" name="iframe_' + UID + '" width="100%" height="100%" scrolling="auto" frameborder="0" style="border-style:none; border-width:0px;"></iframe>';
  } else {
      dialogcontent.style.padding = '6px';
      dialogcontent.innerHTML = message;
  }

  if (fade) {
      dialog.alpha = 0;
      dialog.style.opacity = .00;
      dialog.style.filter = 'alpha(opacity=0)';
      dialog.timer = setInterval("fadeDialog(1)", TIMER);
  } else {
      dialog.style.visibility = "visible";
  }
  
  if (autohide) {
      windowObj.setTimeout("hideDialog(" + UID + ")", (autohide * 1000));
      dialogclose.style.visibility = "hidden";
  } else {
      dialogclose.style.visibility = "visible";
  }

  if (!documentObj.body.onresize) {
      documentObj.body.setAttribute('onresize', 'setDialogLocation();');
      documentObj.body.onresize = setDialogLocation;
  }

  if (!documentObj.onmousemove && !documentObj.onmouseup && !documentObj.onmousedown) {
      setDialogDrag();
  }
}

// Set Dialog Drag //
function setDialogDrag() {
    var uid = getCurrentUID();
    var dialogheader = documentObj.getElementById(uid + 'dialog-header');
    var dialogclose = documentObj.getElementById(uid + 'dialog-close');
    var dialog = documentObj.getElementById(uid + 'dialog');

    dialogclose.onmousedown = function(a) {
        dialogheader.Mobile = 1;
    }

    dialogheader.onmousedown = function(a) {
        if (!dialogheader.Mobile) {
            if (!a) a = windowObj.event;
            var x = a.layerX ? a.layerX : a.offsetX
            var y = a.layerY ? a.layerY : a.offsetY;

            if (dialogheader.setCapture)
                dialogheader.setCapture();
            else if (windowObj.captureEvents)
                windowObj.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);

            dialogheader.style.cursor = 'move';

            documentObj.onmousemove = function(a) {
                if (!a) a = windowObj.event;
                if (!a.pageX) a.pageX = a.clientX;
                if (!a.pageY) a.pageY = a.clientY;
                var tx = a.pageX - x - 10, ty = a.pageY - y - 10;
                dialog.style.left = tx + 'px';
                dialog.style.top = ty + 'px';
            }

            documentObj.onmouseup = function() {
                if (dialogheader.releaseCapture)
                    dialogheader.releaseCapture();
                else if (windowObj.captureEvents)
                    windowObj.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);

                documentObj.onmousemove = null;
                documentObj.onmouseup = null;

                dialogheader.style.cursor = 'default';
            }
        }
    }
}

// Set Dialog Location //
function setDialogLocation() {
    var uidList = documentObj.getElementById('top-dialog-box-uid-list');
    
    if (uidList.value != "") {
        var UL = uidList.value.split(',');
        var dialog;
        var pwidth = pageWidth();
        var pheight = pageHeight();
        var left = leftPosition();
        var top = topPosition();
        var dialogwidth;
        var dialogheight;
        var topposition;
        var leftposition;

        for (i = 0; i < UL.length; i++) {
            dialog = documentObj.getElementById(UL[i] + 'dialog');
            dialogwidth = dialog.offsetWidth;
            dialogheight = dialog.offsetHeight;

            if (pheight < dialogheight) {
                topposition = 0;
            } else {
                if (NavigatorVar > 106) {
                    topposition = (pheight / 2) - (dialogheight / 2);
                } else {
                    topposition = top + (pheight / 2) - (dialogheight / 2);
                }
            }

            if (pwidth < dialogwidth) {
                leftposition = 0;
            } else {
                if (NavigatorVar > 106) {
                    leftposition = (pwidth / 2) - (dialogwidth / 2);
                } else {
                    leftposition = left + (pwidth / 2) - (dialogwidth / 2);
                }
            }

            if (pwidth < dialogwidth || pheight < dialogheight) {
                dialog.style.position = 'absolute';
            } else {
                if (NavigatorVar > 106) {
                    dialog.style.position = 'fixed';
                } else {
                    dialog.style.position = 'absolute';
                }
            }

            dialog.style.top = Math.round(topposition) + 'px';
            dialog.style.left = Math.round(leftposition) + 'px';
        }
    }
}

// Get the current can be set UID
function getCurrentUID() {
    var uidList = documentObj.getElementById('top-dialog-box-uid-list');
    var UL = uidList.value.split(',');
    return UL[UL.length - 1];
}

// Get the UID list
function getUIDList() {
    var uidList = documentObj.getElementById('top-dialog-box-uid-list');
    if (uidList != null) {
        return uidList.value.split(',')
    } else {
        return null;
    }
}

// Reconstruction of UID List
function resetUIDList() {
    var uid = getCurrentUID();
    if (uid != '') {
        var dialog = documentObj.getElementById(uid + 'dialog');
        var dialogmask = documentObj.getElementById(uid + 'dialog-mask');

        var uidList = documentObj.getElementById('top-dialog-box-uid-list');
        var n = uidList.value.lastIndexOf(',');
        if (n > 0)
            uidList.value = uidList.value.substr(0, n);
        else
            uidList.value = "";

        windowObj.setTimeout("SetCloseDialog()", 250);

        documentObj.body.removeChild(dialogmask);
        documentObj.body.removeChild(dialog);        
    }
}

// Set The IE6,7 Close Dialog Bug //
function SetCloseDialog() {
    if (document.forms.length > 0) {
        for (i = 0; i < document.forms[0].elements.length; i++) {
            if (document.forms[0].elements[i].type != 'hidden') {
                try {
                    document.forms[0].elements[i].focus();
                    break;
                } catch (e) {
                    continue;
                }
            }
        }
    }
}

// Hide the dialog box //
function hideDialog(uid) {
    if (fade) {
        var uid = getCurrentUID();
        var dialog = documentObj.getElementById(uid + 'dialog');
        var dialogmask = documentObj.getElementById(uid + 'dialog-mask');
        clearInterval(dialog.timer);
        dialog.timer = setInterval("fadeDialog(0)", TIMER);
    } else {
        resetUIDList();
    }
}

// Gradient Dialog //
function fadeDialog(flag) {
    var uid = getCurrentUID();
    if (uid != '') {
        var dialog = documentObj.getElementById(uid + 'dialog');
        var dialogmask = documentObj.getElementById(uid + 'dialog-mask');
        if (flag == null) {
            flag = 1;
        }
        if (fade) {
            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 >= 100) {
                clearInterval(dialog.timer);
                dialog.alpha = 99;
                dialog.timer = null;
                dialog.style.opacity = '';
                dialog.style.filter = '';
            } else if (value <= 1) {
                clearInterval(dialog.timer);
                resetUIDList();
            }
        } else {
            if (flag == 1) {
                dialog.style.visibility = "visible";
                dialogmask.style.visibility = "visible";
            } else {
                resetUIDList();
            }
        }
    }
}
