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



//shows and hides the scene detail information
function showHideSceneDetails(idx,moreDetails,lessDetails){
	
	var index = idx;
	var lastTargetContainer = null;
	var toggleTargetId = '#sceneDetailInfo-' + index;
	var toggleTargetIdOld; 
	var clipResultId = '#resultClipImages-' + index;
	var showDetailLinkId = '#showDetailLink-' + index;
	var toggleTarget = $(toggleTargetId);
	var toggleTargetDisplayState = toggleTarget.css('display');
	var translationsArray = [
		{key:"Nav.nav_fts_scene_search_results_more_detail", value:"... More Details"},
		{key:"Nav.nav_fts_scene_search_results_less_detail", value:"Close Details X"}	
	];	
	

	if (toggleTargetDisplayState != 'block') {
		$('.sceneDetailsTabRow').hide();
		$('.limitedStars').show();
		$('.resultRowViewDetails').text(moreDetails);
		$(toggleTargetId).show('fast', function(){
			$(showDetailLinkId).children('.resultRowViewDetails').text(lessDetails);
			$(showDetailLinkId).children('.limitedStars').hide();
			});
		$(clipResultId).data('indexOld',index);
	}
	else {
		toggleTargetIdOld = '#sceneDetailInfo-' + $(clipResultId).data('indexOld');
		
		$(toggleTargetIdOld).hide(function(){
			$('.resultRowViewDetails').text(moreDetails);
			$('.limitedStars').show();
			});
	}

}


// adjust alt attribute position if scene thumbnail image fails to load
function sceneThumbLoadFail(obj){
	obj.style.marginLeft = '2px';
}


/* 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);
		$("#popup").removeClass('channelsPopup');
		$("#popup").addClass('genericPopup');
		var myPopupWidth = 500;
		myPopup.style.width = myPopupWidth + 'px';
		myPopup.style.textAlign = "left";
		myPopup.style.padding = "4px";
		x = getX(eTarget) + 3;
		var popupRightEdgePosition = getX(eTarget) + myPopupWidth;
		var windowWidth = $(window).width() - 35;
		if(popupRightEdgePosition >= windowWidth){
			var movePopup = windowWidth - popupRightEdgePosition;
			x = x + movePopup;
		}
		y = getY(eTarget) + eTarget.offsetHeight - 2;
		menuPopup(menu.innerHTML,x,y);
		activeMenu = menuId;
	}
	eTarget.onmouseout = showAlertThing;
	killDefault(e)
}

function showChannelMenu(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);
		$("#popup").addClass('channelsPopup');
		$("#popup").removeClass('genericPopup');
		myPopup.style.width = "196px";
		myPopup.style.textAlign = "left";
		myPopup.style.padding = "4px";
		x = getX(eTarget) +  3;
		y = getY(eTarget) + eTarget.offsetHeight - 2;
		menuPopup(menu.innerHTML,x,y);
		activeMenu = menuId;
	}
	eTarget.onmouseout = showAlertThing;
	killDefault(e)
}

function showChannelFAQ(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);
		$("#popup").addClass('channelsPopup');
		$("#popup").removeClass('genericPopup');
		myPopup.style.width = "500px";
		myPopup.style.textAlign = "left";
		myPopup.style.padding = "4px";
		x = getX(eTarget) +  0;
		y = getY(eTarget) + eTarget.offsetHeight - 2;
		menuPopup(menu.innerHTML,x,y);
		activeMenu = menuId;
	}
	eTarget.onmouseout = showAlertThing;
	killDefault(e);
}

function showRecurMinuteFAQ(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);
		$("#popup").addClass('channelsPopup');
		$("#popup").removeClass('genericPopup');
		myPopup.style.width = "500px";
		myPopup.style.textAlign = "left";
		myPopup.style.padding = "4px";
		x = getX(eTarget) +  -350;
		y = getY(eTarget) + eTarget.offsetHeight - 200;
		menuPopup(menu.innerHTML,x,y);
		activeMenu = menuId;
	}
	eTarget.onmouseout = showAlertThing;
	killDefault(e);
}

function killDefault(e) {
	e.stopPropagation ? e.preventDefault() : e.returnValue = false;
}

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();
	$("#popup").removeClass('channelsPopup');
	$("#popup").addClass('genericPopup');
	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) {
	myPopup.style.width = "255px";
	myPopup.style.textAlign = "left";
	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 */



//removed from searchCriteriaForm.js
// page translations utilities for FTS

var theaterId;
var translations = [];

function setTranslations(data) {
	translations = eval(data);
}

function getMessageKeys() {
	var messageKeys = new Array();
	for (var i=0; i<translations.length; i++) {
		messageKeys[i] = translations[i].key;
	}
	return messageKeys;
}

function setTranslationMessage(key, value) {
	for (var i=0; i<translations.length; i++) {
		if (translations[i].key == key) {
			translations[i].value = value;
			return true;
		}
	}
	return false;
}

function getTranslationMessage(key) {
	for (var i=0; i<translations.length; i++) {
		if (translations[i].key == key) {
			return translations[i].value;
		}
	}
	return key;
}

//for aligning IE6 images in scene detail
function alignImage() {
	var imgHeight;
	var topPadding;

	$(window).load( function() {
		$('.sceneThumbnail').each(function(){
			imgHeight = $(this).height();
			topPadding = (90 - imgHeight) / 2;

			if (topPadding > 0) {
				$(this).css({verticalAlign: 'top', marginTop: topPadding});
			}		
		});
	});
}


//for aligning IE6 images in scene detail
$(function(){
	var sceneThumbLength = $('.sceneThumbnail').length;

	if(isIE6() && sceneThumbLength > 0){
		var timer = null;

	    $(window).load( function() {
	        if (timer) {
	           clearInterval(timer);
	           timer = null;
	        }
			alignImage();
	    });
	    timer = setInterval( function() {
	        if (checkAvailable()){
	           alignImage();
			   clearInterval(timer);
	        }
	    }, 1000 );

	 	function checkAvailable(){
			$('img').each( function() {
				try {
					if (this.height == 0) {
						return false;
					}
				}
				catch (e) {
					return false;
				}
			});
			return true;
		}
	}

});





