// ----------------------------------------------------------------------
// CenterPoint Energy
// CNP Public Site - JavaScript functions for site-specific actions.
// ----------------------------------------------------------------------

var activeServiceAreaMenus = $H(); // Stores all active Service Area Menus to control their time to live

/*
 Invokes the 'UpdateServiceAreaServlet' to handle a visitor's 
 request to change their active Service Area. The servlet
 action will also redirect the visitor to the appropriate page.
 If a 'destinationURL' is not provided, the action will redirect
 to the active page.
*/	
function updateServiceArea(serviceAreaCode, destinationURL) {
	
	var displayURL = (destinationURL != '' && destinationURL != undefined)
		? destinationURL 
		: top.location.href;
	var queryString = (destinationURL != '' && destinationURL != undefined) 
		? destinationURL.substring(destinationURL.indexOf('?'), destinationURL.length) 
		: top.location.search;
		
	// If query string exists, ensure to maintain all parameters and remove existing 'sa' parameter...
    if(queryString != '' && queryString != undefined) {
    	if(queryString.search('sa=') != -1) {
    		var normalizedURL = displayURL.substring(0, displayURL.indexOf(queryString));
    		var startIndex = queryString.indexOf('sa=');
    		var firstPart = queryString.substring(0, startIndex-1);
    		var lastPart = queryString.substring(startIndex+5, queryString.length); // this assumes Service Area code is two chars
    		displayURL = normalizedURL + firstPart + lastPart;
    	}	
    }
    
    // Redirect browser...
	window.location = "/portal/updateSA/?sa="+serviceAreaCode+"&redirect="+escape(displayURL);
}

/*
 Invokes the requested Service Area Menu, rendering the menu just below 
 the specified element.
*/
function showServiceAreaMenu(elementObject, menuID) {
	var xy = Position.cumulativeOffset(elementObject);
	var offset = $(elementObject).getHeight();
	var serviceAreaMenu = $("service-area-menu-"+menuID);
	serviceAreaMenu.style.left = xy[0];
	serviceAreaMenu.style.top = xy[1] + offset;
	showElement(serviceAreaMenu);
	stallServiceAreaMenuTimeout(menuID);
}

/*
 Hides the specified Service Area Menu.
*/
function hideServiceAreaMenu(menuID) {
	if(!activeServiceAreaMenus[menuID]) {
		var serviceAreaMenu = $("service-area-menu-"+menuID);
		hideElement(serviceAreaMenu);
	}
}

/*
 Starts the Service Area Menu display timeout so it will close when 
 it is inactive.
*/
function startServiceAreaMenuTimeout(menuID) {
	activeServiceAreaMenus.remove(menuID);
	setTimeout("hideServiceAreaMenu('"+menuID+"');", 1500);
}

/*
 Stalls the specified Service Area Menu display timeout so it will stay open.
*/
function stallServiceAreaMenuTimeout(menuID) {
	var keyValueSet = new Object();
    keyValueSet[menuID] = 'true';
	activeServiceAreaMenus.merge(keyValueSet);
}

/*
 Controls the swapping of panels for the 'Tabbed Services Menu' as a visitor
 mouses over each tab.
*/
function swapTabbedServicesPanel(panelID) {
	var panels = new Array('naturalgas', 'electric', 'pipelines', 'fieldservices', 'more');
	
	for(i=0;i<panels.length;i++) {
		var tab = $('tab-'+panels[i]);
		if(panels[i] == panelID) {
			var imageOn = '/staticfiles/CNP/Common/SiteAssets/img/tab-' + panels[i] + '-on.gif';
			tab.setAttribute('src', imageOn);
			showElementByIDWithCSS('panel-'+panels[i]);
		} else {
			var imageOff = '/staticfiles/CNP/Common/SiteAssets/img/tab-' + panels[i] + '-off.gif';
			tab.setAttribute('src', imageOff);
			hideElementByIDWithCSS('panel-'+panels[i]);
		}
	} // end-for
}

/*
 Sets the specified element ID to "visible".
*/
function showElementByID(elementID) {
	var elementObject = $(elementID);
	showElement(elementObject);
}


/*
 Sets the specified element ID to "hidden". 
*/
function hideElementByID(elementID) {
	var elementObject = $(elementID);
	hideElement(elementObject);
}

/*
 Sets the specified element to use the "visible" style
 by passing the ID of the element.
*/
function showElementByIDWithCSS(elementID) {
	var elementObject = $(elementID);
	showElementWithCSS(elementObject);
}

/*
 Sets the specified element to use the "hidden" style
 by passing the ID of the element object. 
*/
function hideElementByIDWithCSS(elementID) {
	var elementObject = $(elementID);
	hideElementWithCSS(elementObject);
}

/*
 Sets the specified element to "visible".
*/
function showElement(elementObject) {
	if(elementObject != undefined) elementObject.style.visibility = 'visible';
}

/*
 Sets the specified element to "hidden". 
*/
function hideElement(elementObject) {
	if(elementObject != undefined) elementObject.style.visibility = 'hidden';
}

/*
 Sets the specified element to use the "visible" style
 by passing the actual element object.
*/
function showElementWithCSS(elementObject) {
	if(elementObject != undefined) elementObject.className = 'visible';
}

/*
 Sets the specified element to use the "hidden" style
 by passing the actual element object. 
*/
function hideElementWithCSS(elementObject) {
	if(elementObject != undefined) elementObject.className = 'hidden';
}

/*
 Invokes a DPM component cache refresh for the specified VCMID
 of an active content region.
*/
function clearDPMCache(componentID) {
	var activeURL = top.location.href;
	window.location = "/vgn-ext-templating/CNP/dpm-cache-refresh.jsp?id="+componentID+"&redirect="+escape(activeURL);
}

/*
 Toggles the DPM component cache controls (either "ON" of "OFF").
*/
function toggleDPMCacheControls(status) {
	var activeURL = top.location.href;
	window.location = "/vgn-ext-templating/CNP/dpm-cache-toggle-controls.jsp?toggle="+status+"&redirect="+escape(activeURL);
}

/*
 Toggles the DPM component in-context-edit (ICE) controls (either "ON" of "OFF").
*/
function toggleDPMEditControls(status) {
	var activeURL = top.location.href;
	window.location = "/vgn-ext-templating/CNP/dpm-edit-toggle-controls.jsp?toggle="+status+"&redirect="+escape(activeURL);
}
