function ccGetOffsetLeft(obj) {
  if (obj.tagName && obj.tagName=="BODY") { return 0;}
  else return obj.offsetLeft + ccGetOffsetLeft(obj.offsetParent);
}

function ccGetOffsetTop(obj) {
  if(!obj){ return 0; }
  if (obj.tagName && obj.tagName=="BODY") { return 0;}
  else return obj.offsetTop + ccGetOffsetTop(obj.offsetParent);
}

var ccDialogArray = [];
var ccActiveDialogId;
var ccIsDetached = false;

function ccShowDialogById(src, popId, displayById, clickFromId) {
  var popObj = document.getElementById(popId);
  ccDialogArray[popId]=popObj;
  var dialog = new ccDialog();
  dialog.setSrc(src);
  dialog.setDialogId(popId);
  dialog.setDisplayedBy(displayById);
  dialog.setClickFrom(clickFromId);
  dialog.show();
}

function ccDialog() {};
ccDialog.prototype.setSrc = function(s){this["src"] = s;}
ccDialog.prototype.setDialogId = function(id){this["dialogId"] = id;}
ccDialog.prototype.setDisplayedBy = function(id){this["displayed"] = id;}
ccDialog.prototype.setClickFrom = function(id){this["clickFrom"] = id;}
ccDialog.prototype.detach = function(){this["detached"] = true; this.show();}
//ccDialog.prototype.detach = function(){this["detached"] = true;}
ccDialog.prototype.show = function() {
  
  function ccIECheckDialog(clickFrom) {
    var fooNode =  window.event.srcElement;
    if (fooNode!=clickFrom) {
      if (fooNode.parentNode!=clickFrom) {ccHideAllDialogs();}
    }
  }

  var src = this["src"];
  var dialogId = this["dialogId"];
  var displayedBy = this["displayed"];
  ccIsDetached = this["detached"];
  var dialogObj = document.getElementById(dialogId);
  var displayByObj = document.getElementById(displayedBy);
  var left = ccGetOffsetLeft(displayByObj);
  if (document.body.clientWidth < (left + dialogObj.clientWidth)) {
    left = document.body.clientWidth - dialogObj.clientWidth - 12;
  }
  var top = ccGetOffsetTop(displayByObj) + displayByObj.offsetHeight;
  var h = dialogObj.getAttribute("ccheight");
  var w = dialogObj.getAttribute("width");
  
  ccActiveDialogId = dialogId;
  if (ccUseAccessibleProfile() || ccIsDetached) {
    if (window.screenX) {
      left += window.screenX + (window.outerWidth - window.innerWidth);
      top += window.screenY + (window.outerHeight - window.innerHeight);
    } else if (window.screenLeft) { // IE
      left += window.screenLeft + 10;
      top += window.screenTop + 50;
    }
    var ft= "resizable=yes,dependent=1,width="+w+",height="+h +",top="+top+",left="+left;
    if (ccIsDetached) {
      dialogId  = dialogId + (new Date()).getTime();
    }
    var winObj = window.open("/invisible.html", dialogId, ft);
    winObj.location.href=src;
    winObj.focus();
    if (!ccIsDetached) {
      ccDialogList[dialogId] = winObj;
    }
    
  } else {
    if (ccActiveDialogId != dialogId) { ccHideAllDialogs(); } //avoid blinking
    dialogObj.setAttribute("src", src );
    dialogObj.style.left = left;
    dialogObj.style.top = top;
    if (h) {dialogObj.style.height = h + "px";}
    dialogObj.style.visibility="visible";
    dialogObj.style.zIndex=10 + ccDialogOpenCount;
    ccDialogOpenCount = ccDialogOpenCount + 1;
    var clickFromObj = this["clickFrom"];
    if (clickFromObj) {
      if (document.all) {
        document.onclick = function() {ccIECheckDialog(clickFromObj);}
      } else {
        document.addEventListener("click",function(event){ccHideAllDialogs();},true);
      }
    }
    ccDialogList[dialogId] = this;
  }
}

function ccIsDialogActive(popObj) { return (popObj.id ==  ccActiveDialogId);}
function ccCloseDialog(id) { ccHideDialog(id); self.focus();}
function ccHideAllDialogs() { for (var key in ccDialogArray) {ccHideDialog(key);} }
function ccCleanDialog(id) {
  if (!ccUseAccessibleProfile()) {
    document.getElementById(id).contentWindow.document.body.innerHTML="";
  }
}

function ccHideDialog(popId) {
  if (ccUseAccessibleProfile()) { // ADA
    //popId.close();
    //window.focus();
    ccDialogList[popId].close();
    window.focus();
  } else {
    var popObj = document.getElementById(popId);
    if(typeof(popObj)!="undefined" && popObj!=null){
      popObj.style.visibility="hidden";
      popObj.height = 0;
    }
  }
}


function ccDetachDialog(ccDialogId) {
  if (ccUseAccessibleProfile()) { // ADA
    var win = ccDialogList[ccDialogId];
    win.name = ccDialogId + (new Date()).getTime(); 
    delete ccDialogList[ccDialogId];
  } else {
    ccHideDialog(ccDialogId);
    var dialogObj = ccDialogList[ccDialogId];
    dialogObj.detach();
    //dialogObj.show();
  }
}

var ccDialogList = [];
var ccDialogOpenCount = 0;
function ccCloseDialogs() {
  for(var i in ccDialogList) {var d=ccDialogList[i];if (!d.closed && d.close) { d.close();} }
}

if (ccUseAccessibleProfile()) {
  if (window.attachEvent) {
    window.attachEvent('onunload', ccCloseDialogs);
  } else if (window.addEventListener) {
    window.addEventListener("unload", ccCloseDialogs, true);
  }
}