// "Macro" for document.getElementById
function $(e){
    return document.getElementById(e);
}

// used to open dhtml popup, must include "~/JS/common.js" and "~/JS/subModal.js" to work
// pass in "returnFunc" optionally if you want to fire off return function (must be function ref)
function showDHTMLPopup(url, width, height, mPopTitle, returnFunc) {
    if(returnFunc == undefined | returnFunc == null) {
        showPopWin(url, width, height, mPopTitle);
    }else {
        showPopWin(url, width, height, mPopTitle, returnFunc);
    }
}

// used to close dhtml popup, must include "~/JS/common.js" and "~/JS/subModal.js" to work
// pass in "callReturnFunc" optionally if you want to fire off return function (have to setup in "showDHTMLPopup()")
function hideDHTMLPopup(callReturnFunc) {
    if(callReturnFunc == undefined | callReturnFunc == null) {
        hidePopWin();
    }else {
        hidePopWin(true);
    }
}

//Shows the instructions popup
function showPrintInstructions(section) {
    var url = "../popups/instructions.aspx?sec=" + section;    
	window.open (url,'Instructions','width=700,height=500,location=0,menubar=0,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=0,screenX=100,left=100,screenY=30,top=60');
}

//license agreement
function licenseAgreement() {
    openModalWindow("popups/licenseAgreement.aspx", 850, 600);
}

//Opens a modal window if IE, regular window if not.  Call by multiple other functions.
function openModalWindow(url, width, height) {
    if (window.showModalDialog) {
        //adjust size, showModalDialog uses size for overall size of browser, not the interior size like window.open
        width += 6;
        height += 51;
		return window.showModalDialog(url, window, 'help:no;resizable:no;scrollbars:yes;status:no;dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;');
    } else {
        var left = window.screenX + (window.outerWidth-width)/2;
        var top = window.screenY + (window.outerHeight-height)/2;
        
        if (left<0) {
            left = 0;
        }
        if (top<0) {
            top = 0;
        }
        
        var win = window.open(url, 'cdppopup', "left=" + left + ",top=" + top + ",resizable=no,scrollbars=yes,width=" + width + ",height=" + height + ",toolbar=0");
        if (win) {
            win.focus();
        }
    }
}

//opens a normal window but without toolbar, status bar, etc (looks like a modal window). 
//This is needed if using postbacks in popup, postbacks in modal window causes another page to load
function openMinimalWindow(url, width, height, showScroll) {
    var left = 0;
    var top = 0;
   
    if(/msie/i.test(navigator.userAgent)) {    //if IE being used
        left = window.screenLeft + (document.body.clientWidth-width)/2;
        top = window.screenTop + (document.body.clientHeight-height)/2;
    }else {
        left = window.screenX + (window.outerWidth-width)/2;
        top = window.screenY + (window.outerHeight-height)/2;
    }
    
    if (left<0) {
        left = 0;
    }
    if (top<0) {
        top = 0;
    }
    
    var ss = "";
    
    if(showScroll == null || showScroll == undefined || showScroll == true) {   //this param is optional
        ss = "yes";
    }else {
        ss = "no";
    }
        
	var attr = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",menubar=no,status=no,scrollbars=" + ss + ",resizable=no";
	
	var nw = window.open(url, "PopUp", attr);
	
	if(nw) {
	    nw.focus();
	}
}

/* instructions popup */
function showSecHelp(section) {
    var url = "../popups/instructions.aspx?alt=1&sec=" + section; 
	window.open (url,'Instructions','width=700,height=500,location=0,menubar=0,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=0,screenX=100,left=100,screenY=30,top=60');
}

/* focus control that is passed in a param */
function setFocus(prmID) {
	var eleNode = document.getElementById(prmID);
	
	if(eleNode != null)
		document.getElementById(prmID).focus();
}

/* block key strokes */
function blockInput(e) {
    return false;
}

/**************************/
/*  html content editing  */
/**************************/
var _mouseX = null;
var _mouseY = null;
var _menu = null;
var _menuTimer = null;
var _divContent = null;

function mouse_OnMove(event) {
	//set mouse cordinates, compensate for any scrolling in window.
	_mouseX=(event.clientX+document.documentElement.scrollLeft);
	_mouseY=(event.clientY+document.documentElement.scrollTop);
	
  //hides the menu if mouse moves off it
	if (_menu != null) {
	  var x1 = parseInt(_menu.style.left);
	  var x2 = x1 + parseInt(_menu.offsetWidth);
	  var y1 = parseInt(_menu.style.top);
	  var y2 = y1 + parseInt(_menu.offsetHeight);
	  var offset = 3;
	  
	  if (_mouseX < x1-offset || _mouseX > x2+offset || _mouseY < y1-offset || _mouseY > y2+offset) {
	    hideMenu();
	  }
	}
	
	//hide the current div border if we move off it
	if (_divContent != null) {
	  var x1 = getElemX(_divContent);
	  var x2 = x1 + parseInt(_divContent.offsetWidth);
	  var y1 = getElemY(_divContent);
	  var y2 = y1 + parseInt(_divContent.offsetHeight);
	  var offset = 3;

	  if (_mouseX < x1-offset || _mouseX > x2+offset || _mouseY < y1-offset || _mouseY > y2+offset) {
	    removeHighlight();
	  }
	}
}


function getElemX(elem) {
    var x = 0;
    do {
        x += elem.offsetLeft;
        elem = elem.offsetParent;
    } while (elem);
    return x;
}


function getElemY(elem) {
    var y = 0;
    do {
        y += elem.offsetTop;
        elem = elem.offsetParent;
    } while (elem);
    return y;
}


function removeHighlight() {
  if (_divContent != null) {
    _divContent.className = "htmlContent";
    _divContent = null;
    
  }
}

function contentRollOver(div) {
  _divContent = div;
  _divContent.className = "htmlContent htmlContentHighlight";
}

//function contentRollOut(div) {
//  div.className = "htmlContent";
//}

function showMenu(div, contentID, mode, hasUnpublishedContent, editorURL, contentColumn) {
  hideMenu();
  var a;
  
  
  //cancel if we don't have a mouse position
  if (_mouseX ==null || _mouseY==null) {
    return;
  }
  
  //create outer menu div
  _menu = document.createElement("DIV");
  _menu.style.left = _mouseX - 25 + "px";
  _menu.style.top = _mouseY - 10 +  "px";
  _menu.className = "htmlContentMenu";
  
  //Edit
  a = document.createElement("A");
  a.innerHTML = "Edit";
  a.href="#";
  a.onclick = function() {showContentEditor(contentID, editorURL, contentColumn);}
  _menu.appendChild(a);

  //Show Published
  a = document.createElement("A");
  a.innerHTML = "Show Published Content";
  a.href="#";
  a.className = "brdT";
  if (mode == "ShowPublished") {
    a.className += " check";
  } else {
    a.onclick = function() {menuClick(contentID, "ShowPublished");}
  }
  _menu.appendChild(a);

  //Show Pending
  a = document.createElement("A");
  a.innerHTML = "Show Pending Content";
  a.href="#";
  a.className = "brdB";
  if (hasUnpublishedContent) {
    if (mode == "ShowPending") {
      a.className += " check";
    } else {
      a.onclick = function() {menuClick(contentID, "ShowPending");}
    }
  } else {
    a.className += " disabled";
  }
  _menu.appendChild(a);

  //Publish
  a = document.createElement("A");
  a.innerHTML = "Publish";
  a.href="#";
  if (mode == "ShowPending") {
    if (hasUnpublishedContent) {
        a.onclick = function() {menuClick(contentID, "Publish", contentColumn);}
    } else {
      a.className = "disabled";
    }
  } else {
    a.className = "disabled";
  }
  _menu.appendChild(a);

  //finally, add the menu
  document.body.appendChild(_menu);
}

function hideMenu() {
  if (_menu!=null) {
      document.body.removeChild(_menu);
      _menu = null;
  }
}


function addMenuItem(name) {
  var a = document.createElement("A");
  a.innerHTML = name;
  _menu.appendChild(a);
}

function menuClick(contentID, value, contentColumn) {
  hideMenu();
  if (value == "Publish") {
    if (!confirm("Publish this content to the live site?")) {
      return;
    }
  }
  $("hdnContentMode").value = contentID + "|" + value + "|" + contentColumn;
  document.forms[0].submit();
}

function showContentEditor(contentID, editorURL, contentColumn) {
  hideMenu();

  var width = 700;
  var height = 600;
  
  //var url = "popups/ContentEditor.aspx?id=" + contentID;
  var url = editorURL + "?id=" + contentID + "&col=" + contentColumn; 
    var left;
    var top;
    
    if (window.screenX) {
      //FF
      left = window.screenX + (window.outerWidth-width)/2;
      top = window.screenY + (window.outerHeight-height)/2;
    } else {
      //ID
      left = window.screenLeft + (document.body.offsetWidth - width)/2;
      top = window.screenTop + (document.body.offsetHeight - height)/2 - 100;
    }


  if (left<0) {
      left = 0;
  }
  if (top<0) {
      top = 0;
  }

  var win = window.open(url, 'ContentEditor', "left=" + left + ",top=" + top + ",resizable=no,scrollbars=yes,width=" + width + ",height=" + height + ",toolbar=0");
  if (win) {
      win.focus();
  }
}

