/** Change the detail view button from '-' to '+'
 * @param {string} div
 * @param {string} secondaryDiv
 */
var lastResultAreaDiv = 'none';
function replaceGridDetailViewButton(div,secondaryDiv){
	lastResultAreaDiv = div;
	var closedDetails = replaceSecondaryDetailViewButton(secondaryDiv);
	if (closedDetails) {
		hideShowLastDetailsGridRows();
		lastTopGridRow = 'none';
		lastBottomGridRow = 'none';
		lastDetailsGridCell = 'none';
	} else {
		//if the current bottomRowId is the same as the lastBottomGridRow, we do nothing
		var currentDetailsButton = document.getElementById(secondaryDiv);
		var currentDetailsButtonRow = currentDetailsButton.parentNode.parentNode;
		var	bottomRowId = currentDetailsButtonRow.id;
		if (currentDetailsButtonRow.className == 'sceneGridTopRow') {
			currentDetailsButtonRow.className = 'sceneGridTopRow activeSceneGridTopRow';
		}
		var activeSceneGridThumbCellId = div.replace('resultDetailsButton','sceneGridThumbCell') + '_1';

		if (bottomRowId == lastBottomGridRow) {
		} else {
			//otherwise, we hide the last lastTopGridRow, show lastBottomGridRow, show currentTopRow, hide currentDetailsButtonRow

			// hide the last lastTopGridRow, show lastBottomGridRow
			hideShowLastDetailsGridRows();
			
			//show currentTopRow, hide currentDetailsButtonRow
			var bottomRow = document.getElementById(bottomRowId);
			var detailsCellId = bottomRowId.replace('viewButtonTopRow','detailsGridCell');
			var detailsCell = document.getElementById(detailsCellId);
			detailsCell.className = 'activeDetailsCell';
			
			lastBottomGridRow = bottomRowId;
			lastDetailsGridCell = detailsCellId;
		}
	
	}
	
}
function hideShowLastDetailsGridRows() {
	if (lastBottomGridRow != 'none'){
		var RowToShow = document.getElementById(lastBottomGridRow);
		RowToShow.style.display = '';
		RowToShow.className = 'sceneGridTopRow';
		var CellToHide = document.getElementById(lastDetailsGridCell);
		CellToHide.className = '';
	}
}
function replaceDetailViewButton(div){
	var divElement = document.getElementById(div);
	if( divElement.innerHTML == closeDetailsText){
		divElement.innerHTML = viewDetailsText;
	}
	else {
		divElement.innerHTML = closeDetailsText;
	}
	if (lastResultAreaDiv != 'none' && lastResultAreaDiv != div){
		document.getElementById(lastResultAreaDiv).innerHTML = viewDetailsText;
	}
	lastResultAreaDiv = div;
}

var lastSecondaryResultAreaDiv = 'none';
var lastDetailsGridCell = 'none';
var lastTopGridRow = 'none';
var lastBottomGridRow = 'none';
var lastActiveSceneGridThumbCellId = 'none';
var closeDetailsText = '';
var viewDetailsText = '';

function initializeViewCloseText(viewDetailsTranslation, closeDetailsTranslation) {
	viewDetailsText = viewDetailsTranslation;
	closeDetailsText = closeDetailsTranslation;
}

function replaceSecondaryDetailViewButton(div){
    // this method requires a call to initializeViewCloseText first
	var activeSceneGridThumbCellId = div.replace('resultDetailsButton','sceneGridThumbCell');
	var closedDetails = false;
	var divElement = document.getElementById(div);
	if(divElement.innerHTML == closeDetailsText){
		divElement.innerHTML = viewDetailsText;
		divElement.className = 'resultRowViewDetails inactiveDetailsCell';
		document.getElementById(activeSceneGridThumbCellId).className = '';
		lastActiveSceneGridThumbCellId = 'none';
		closedDetails = true;
	}
	else {
		var divElement = document.getElementById(div);
		divElement.innerHTML = closeDetailsText;
		divElement.className = 'resultRowViewDetails activeDetailsCell';
		document.getElementById(activeSceneGridThumbCellId).className = 'activeDetailsCell';
		if (lastActiveSceneGridThumbCellId != 'none') {
			document.getElementById(lastActiveSceneGridThumbCellId).className = '';			
		}
		lastActiveSceneGridThumbCellId = activeSceneGridThumbCellId;
	}
	if (lastSecondaryResultAreaDiv != 'none' && lastSecondaryResultAreaDiv != div){
		var lastSecondaryResultAreaDivElement = document.getElementById(lastSecondaryResultAreaDiv);
		lastSecondaryResultAreaDivElement.innerHTML = viewDetailsText;
		lastSecondaryResultAreaDivElement.className = 'resultRowViewDetails inactiveDetailsCell';
	}
	lastSecondaryResultAreaDiv = div;
	return closedDetails;
}

/**
 * @author Netrimos 4-26-07
 * Open or close an element
 * This method requires two pieces in the HTML to work properly.<br><br>
 * One is this hidden element at the end of the page:<br><br>
 * A Div with this id "id="stagingDIV" style="visibility:hidden;"<br><br>
 * The other is an array to store all of the set Elements that will be using the tool. 
 * The Array stores the names and an item count.<br><br>
 * @param {array} arrayName Name of an array that holds the names of the Element ID's.
 * @param {string} elementID Number of the item in the array that is being opened.
 * @param {string} innerHTMLcontent "optional" Use if you need to add an HTML string to the Element.
 */

function openCloseDiv(arrayName,elementID,innerHTMLcontent){
	if (innerHTMLcontent){
		document.getElementById(arrayName[elementID]).innerHTML = innerHTMLcontent;
	}
	for (i=0;i<arrayName.length;i++){
		var arrayName_I_Element = document.getElementById(arrayName[i]);
		if (arrayName_I_Element != null) {
			if (elementID == i && arrayName_I_Element.style.display!='block'){
				arrayName_I_Element.style.display = 'block';
			}
			else if(arrayName_I_Element.style.display=='block'){
				arrayName_I_Element.style.display = 'none';
			}
		}
	}
}

/* sjh */
var needToInitializePopUp = true
var activeMenu = '';
function showDeliveryMenu(menuId, e) {
	var eTarget = e.target ? e.target : e.srcElement;
	if (needToInitializePopUp) initializePopUp();
	if (activeMenu == menuId) {
		popMenu.hide();
		activeMenu = '';
	} else {
		var menu = document.getElementById(menuId);
		myPopup.style.width = "140px";
		myPopup.style.padding = "0px";
		x = getX(eTarget) +  3;
		y = getY(eTarget) + eTarget.offsetHeight - 2;
		menuPopup(menu.innerHTML,x,y);
		activeMenu = menuId;
	}
	eTarget.onmouseout = showAlertThing;
}
function showAlertThing() {
	killPopUp();
}

function initializePopUp(){
	myPopup = document.getElementById('popup');
	needToInitializePopUp = false;
}

function loadPopUp(popupId){
	myPopup = document.getElementById(popupId);
	needToInitializePopUp = false;
}

function menuPopup(contentHTML,xPos,yPos){
	document.getElementById('stagingDIV').innerHTML = contentHTML;
	popupHeight = document.getElementById('stagingDIV').offsetHeight;
	document.getElementById('stagingDIV').innerHTML = ""
	myPopup.innerHTML = contentHTML;
	popMenu.show(xPos, yPos);
}

function getX(e) {
	var xPosition = 0;
	if (e.offsetParent) {
		while (e.offsetParent) {
			xPosition += e.offsetLeft;
			e = e.offsetParent;
		}
	}
	else if (obj.x) {
		xPosition += obj.x;
	}
	return xPosition;
}

function getY(e) {
	var yPosition = 0;
	if (e.offsetParent) {
		while (e.offsetParent) {
			yPosition += e.offsetTop;
			e = e.offsetParent;
		}
	}
	else if (obj.y) {
		yPosition += obj.y;
	}
	return yPosition;
}

var timeoutId;
function showTagDetailsPopUp(popUpContentUrl, tagId, event) {
	var menuId = 'tagPopUp'+tagId;
	var xPosition = event.clientX;
	var yPosition = event.clientY;
	var timeoutString = "createDetailsPopUp('" + popUpContentUrl + "', '" + menuId + "', " + xPosition + ", " + yPosition + ")";
	timeoutId = setTimeout(timeoutString, 1000);
}
function killPopUp() {
	clearTimeout(timeoutId);
	popMenu.hide();
}

function createDetailsPopUp(popUpContentUrl, popUpId, xPos, yPos) {
	if (needToInitializePopUp) initializePopUp();
	var xPosition = xPos;
	var yPosition = yPos;
	var tagInfoPopUpDiv = document.getElementById(popUpId);
	if(tagInfoPopUpDiv == null) {
		// the div doesn't exist so we create it
		// add the div to the page
		var infoPopUpDiv = document.createElement('div');
		infoPopUpDiv.id = popUpId;
		infoPopUpDiv.setAttribute("class", "tagInfoPopUp");
		infoPopUpDiv.setAttribute("className", "tagInfoPopUp");
		infoPopUpDiv.innerHTML = 'loading';
		infoPopUpDiv.style.left = xPosition + 'px';
		infoPopUpDiv.style.top = yPosition + 'px';
		document.getElementsByTagName("BODY")[0].appendChild(infoPopUpDiv);
		$.ajax({
            url: popUpContentUrl,
            type:"GET",
			success: function(data, textStatus){
                this.yPos = yPosition;
                this.xPos = xPosition;
                targetDiv = popUpId;
                loadTagPopUpContent(data, targetDiv, xPos, yPos);
			}   
		});
	} else {
		//the div exists
		if (tagInfoPopUpDiv.innerHTML == 'loading') {
			// the content is loading
			return;
		} else {
			// the content has been loaded
			// show the div with previously loaded content
			showTagPopUp(popUpId, xPosition, yPosition);
			return;
		}
	}
	showTagPopUp(popUpId, xPosition, yPosition);
}

function loadTagPopUpContent(content, menuId, xPosition, yPosition) {
	var menu = document.getElementById(menuId);
	menu.innerHTML = content;
	if (activeMenu == menuId) {
		showTagPopUp(menuId, xPosition, yPosition);
	}
}

function showTagPopUp(menuId, xPosition, yPosition) {
	var menu = document.getElementById(menuId);
	var alignment = alignPopup(xPosition, yPosition, menu.clientHeight, myPopup.style.width);
	activeMenu = menuId;
	menuPopup(menu.innerHTML,alignment[0],alignment[1]);
}

/**
* new popup menu
**/
var popMenu = {
  timer: null,
  activePopID: null,
  offX: 0,   // horizontal offset 
  offY: 0,   // vertical offset 
  show: function(xPos, yPos) {
  	clearTimeout(this.timer);
    var mnu = document.getElementById? document.getElementById("popup"): null;
    if (!mnu) return;
    this.activeMenuID = "popup";
    if ( mnu.onmouseout == null ) mnu.onmouseout = this.mouseoutCheck;
    if ( mnu.onmouseover == null ) mnu.onmouseover = this.clearTimer;
    this.position(mnu,xPos,yPos);
  },
  
  hide: function() {
    this.clearTimer();
    if (this.activeMenuID && document.getElementById) 
      this.timer = setTimeout("document.getElementById('popup').style.display = 'none';activeMenu = '';", 200);
  },

  position: function(mnu, xPos, yPos) {
	mnu.style.left = xPos + "px";
	mnu.style.top = yPos + "px";
    this.timer = setTimeout("document.getElementById('popup').style.display = 'block'", 20);
  },
  
  mouseoutCheck: function(e) {
    e = e? e: window.event;
    // is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
    var mnu = document.getElementById(popMenu.activeMenuID);
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( mnu != toEl && !popMenu.contained(toEl, mnu) ) popMenu.hide();
  },
  
  // returns true of oNode is contained by oCont (container)
  contained: function(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) 
      if ( oNode == oCont ) return true;
    return false;
  },

  clearTimer: function() {
    if (popMenu.timer) clearTimeout(popMenu.timer);
  } 
}
/**
* end new popup menu
**/
/* end sjh */
