//*****************************************************************************************
//
// JavaScript File
//
// Name:         NavigationTemplates.js
// Description:  List of javascript functions used by Navigation templates(Two-Column 
//               Services and Navigation Page with Tabs)
//
//*****************************************************************************************
//
// FUNCTIONS
//
// Name:         ResetActiveSection 
// Description:  Resets the active button by clearing the cookies
// Accepts:      None
// Returns:      None
//
// Name:         getCWSObj 
// Description:  Finds an HTML Control based on the control's id 
// Accepts:      id - control's id
// Returns:      HTML Control
//
// Name:         readNavCookie 
// Description:  Search for a cookie and returns its value
// Accepts:      name - the keyname of the cookie
// Returns:      CookieValue - the value of the cookie
//
// Name:         writeNavCookie 
// Description:  Creates a cookie and assigns value to it
// Accepts:      name - the keyname of the cookie
//               value - the value of the cookie
// Returns:      None
//
// Name:         SetActiveSection 
// Description:  Sets the active button and section
// Accepts:      obj
// Returns:      None
//
// Name:         setDisplay
// Description:  Sets the initial state of the page
// Accepts:      obj - the id of the section to set the visibiliy
// Returns:      style - can take the value ‘none’ or ‘’ to hide or show the 
//                       section respectively. 
//
// Name:         doBookmark
// Description:  toggles the anchor sections based from section parameter passed
// Accepts:      url - anchor name
// Returns:      N/A
//
// Name:         ResetDiv
// Description:  Reloads the div section of the page by reassigning the html content
//				 of the div
// Accepts:      id 
// Returns:      N/A
//
// Name:         DisplayDefaultSection
// Description:  Displays/hides a section (with description or w/o description) depending 
//				 on the value retrieved from the cookie.
// Accepts:      N/A 
// Returns:      N/A
//
// Name:         ShowDescription
// Description:  Shows/hides cells containing arrow and clear images when the user toggles.
//				 the Show/Hide description links.
// Accepts:      blnShowDesc 
// Returns:      N/A
//
// Name:         ShowHideRow
// Description:  Shows/hides the row containing the descriptions. It also contains the code that 
//				 delays resetting of the div section to fix the issue of overlapping text in firefox
// Accepts:      blnShowDesc 
// Returns:      N/A
//
// Name:         ResetDescriptionDiv
// Description:  Reloads the section containing the description by reassigning the innerHTML property
//               of the 'div' section to its own (ex code. div.innerHTML = div.innerHTML). This is the fix
//				 for the resizing issue in IE.
// Accepts:      N/A
// Returns:      N/A
//
// Name:         ReloadQuicktime
// Description:  Reloads the quicktime movie by adding parameters(src, controller, showstatusbar) 
//				 to the object that renders the quicktime player. This is a fix for 
//               the bug where the quicktime player is not reloaded after the section 
//				 has been reloaded using 'innerHTML' property.			  
// Accepts:      objDiv 
// Returns:      N/A
//
// Name:         ShowQTController
// Description:  Checks for the quicktime object and shows its control bar
// Accepts:      N/A
// Returns:      N/A
//
// Name:         MediaInsideDescriptionOrSection
// Description:  Checks if the Media object is embedded inside the description div of 2 column
//				 Service template or ContentSection div of the nav pages with tabs
// Accepts:      ID - media object's id
// Returns:      boolean
//
// Name:         GetMediaSectionId
// Description:  Search for the id of the section containing the media object
// Accepts:      ID - media object's id
// Returns:      ID - section's id
//
// Name:         StopMedia
// Description:  Checks for the embedded media object and stops it if found. 
// Accepts:      ID - media object's id
// Returns:      N/A
//
// Name:         PlayMedia
// Description:  Checks for the embedded media object and plays it if found. 
// Accepts:      ID - media object's id
// Returns:      N/A
//
//
//*****************************************************************************************
//
// REVISION HISTORY
//
// Date Created: 02/28/2007
// Author:       Eric V. Caraan
//
// Revision Date     Author     Description 
// -------------     ------     -----------         
// 
//*****************************************************************************************

 function ResetActiveSection()
{		

	var IE = document.all?true:false;
	if (IE)
        { 
		if (event.clientY < 0 && event.clientX > document.body.offsetWidth-30) 
		{		
			writeNavCookie(ActiveSectionButton, "");
		}
	}
	else 
	{
		writeNavCookie(ActiveSectionButton, "");
	}
}

function getCWSObj(id) 
{
	if (document.getElementById) {
		return document.getElementById(id);
		}
	else if (document.all) {
		getCWSObj = document.all[id];
		}
}

function setDisplay(obj,style)
{
	obj.style.display=style;
}

function setInitialPageState()
{
	var IE = document.all?true:false;
	try
	{
		SetActiveSection(getCWSObj(readNavCookie(ActiveSectionButton)));
		
		//This is a fix of overlapping sounds of quicktime movie in safari and IE
		if (navigator.appName == 'Netscape' || navigator.appName == 'Microsoft Internet Explorer'){		
			
			var sectionID = GetMediaSectionId('QuickTimePlayer');
			
			if (sectionID != 'Section1' && sectionID.indexOf('Section') != -1){
				StopMedia('QuickTimePlayer'); 
			}
		}
	}
	catch(Err)
	{
	}
	
	if(!IE) ResetActiveSection();
}

function readNavCookie(name)
{
  var cookieValue = '';
  var search = name + '=';
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(';', offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeNavCookie(name, value)
{
  try
  {
	document.cookie = name + '=' + escape(value);
  }
  catch(Err)
  {}
}

function SetActiveSection(obj)
{

	// Declare Section state value
	var Hide='none';
	var Show='';
	var maxNoOfButtons = 100;

	// Check if container for section buttons and content already exist in html body 
	if(getCWSObj('SectionButtons')==null || getCWSObj('SectionContent')==null) return;

	if (obj==null) obj = getCWSObj('SectionButton1');

	for (var i = 1;i<=maxNoOfButtons;i++)
	{
			//Pre-terminate Loop if SectionButton# is equal to null
			if (getCWSObj("SectionButton" + i)==null) return;

			if ('SectionButton' + i !=obj.id) 
			{
				//Stops media objects when hidden in IE 
				if (navigator.appName == 'Microsoft Internet Explorer'){
				
					var objMedia;
			 
					objMedia = GetCurrentMedia();
			 
					//Stops media objects when object is not embedded in section1 of Nav pages with tabs
					if (objMedia != null){	
						
						var strID = objMedia.id;
					
						//retrieve name attribute if id is empty. this is the case for media player.
						if (strID == '') strID = objMedia.name;				
				
						if  (obj.id != 'SectionButton1' || GetMediaSectionId(strID) != 'Section1'){			
							if (MediaInsideDescriptionOrSection('MediaPlayer')){
								StopMedia('MediaPlayer');
							}	
							else if (MediaInsideDescriptionOrSection('Rp1')){
								StopMedia('Rp1');
							} 		
							else if (MediaInsideDescriptionOrSection('QuickTimePlayer')){
								ReloadQuicktime();
							} 
						}
					}
				}	
			
				getCWSObj("SectionButton" + i).className = ''; 
				getCWSObj("Section" + i).style.display  = Hide;
			}
			else
			{
				obj.className = 'bodloadTab';
				getCWSObj("Section" + i).style.display  = Show;			
			}
	}	
}

function doBookmark(url)
{
	var currentURL = document.location.href;
	var sharpLocation = currentURL.lastIndexOf('#')
	
	if (sharpLocation != -1) currentURL = currentURL.substring(0,sharpLocation)
	
	location = currentURL + url;
	
}

function DisplayDefaultSection()
{
	//Declare Variables
	var activeSection = readNavCookie(currentPageName)
	var indexMediaPlayer = document.location.href.indexOf("?Player=");
	
	//If current page cookie value indicates active section is with
	//description and current page is playing media player, load
	//page with descriptions showing, else load without descriptions
	if(activeSection == "SectionWithDesc" && indexMediaPlayer != -1)
	{
		setDisplay(getCWSObj('ShowDesc'),'none');
		setDisplay(getCWSObj('HideDesc'),'');		
		
		//Delay call for the ShowDescription the function if browser is IE. This  
		//is a fix for the issue of quicktime player not reloading in IE.
		if (navigator.appName == 'Microsoft Internet Explorer'){		
			setTimeout("ShowDescription(true)",1000);
		}
		else{
			ShowDescription(true);
		}	
	}
	else
	{
		setDisplay(getCWSObj('ShowDesc'),'');
		setDisplay(getCWSObj('HideDesc'),'none');
		ShowDescription(false);				
	}
}

function SetActiveDescSection(activeSection)
{	
	//Set the value of the cookie to the active section
	writeNavCookie(currentPageName, activeSection);	
}

function ResetDiv(id)
{	
	if (navigator.appName == 'Microsoft Internet Explorer'){		
		var objDiv = getCWSObj(id);		
		
		if (objDiv != null)
		{			
			objDiv.innerHTML = objDiv.innerHTML;	
			
			//Plays Real Player media object when shown in IE 
			var objMedia;
			 
			objMedia = GetCurrentMedia();			 
			
			if (objMedia != null){	
				
				var strID = objMedia.id;
			
				//retrieve name attribute if id is empty. this is the case for media player.
				if (strID == '') strID = objMedia.name;				
		
				if  (id == GetMediaSectionId(strID)){									
					if (MediaInsideDescriptionOrSection('Rp1')){
						PlayMedia('Rp1');
					} 							
				}
			}
						
			 //If the section contains an embedded quicktime movie, call ReloadQuicktime function
			 //to reload the quicktime player 			 
			ReloadQuicktime();
			 
		}
	}
	else if (navigator.appName == 'Netscape'){
		if (MediaInsideDescriptionOrSection('QuickTimePlayer')){
			StopMedia('QuickTimePlayer'); //This is a fix of overlapping sounds of quicktime movie in safari
		}
	}
}

function ShowDescription(blnShowDesc)
{	
	var i;
	
	var style1 = '';
	var style2 = 'none'
	
	
	if (blnShowDesc){
		style1 = 'none';
		style2 = '';
	} 	
	
	//show/hide the cell containing the arrow image of the CategoryLink
	for(i = 0;i < cnt_arrowImage; ++i)
	{
		setDisplay(getCWSObj('arrowImage' + i),style1);
		
	}	
	
	//show/hide the cell containing the spacer image of the CategorySublink
	for(i = 0;i < cnt_clearImage; i++)
	{
		setDisplay(getCWSObj('clearImage' + i),style1);
	}
	
	//show/hide the row containing the CategoryLinks spacer
	for(i = 0;i < cnt_rowSpacer; i++)
	{
		setDisplay(getCWSObj('descRowSpacer' + i),style2);
	}	
	
	//show/hide the row containing the description				
	if (navigator.appName == 'Netscape'){		
		if (blnShowDesc){
			 setTimeout("ShowHideRow(true)",1);
		}		
		else{
			setTimeout("ShowHideRow(false)",1);
		}
		
	}
	else{
		ShowHideRow(blnShowDesc)
	}		
	
}


function ShowHideRow(blnShowDesc){

	var arrDescRow = document.getElementsByTagName('tr');	
	var i = 0;	
	var style = 'none';
	
	if (blnShowDesc){		
		style = '';
	} 	
	
	//Stops media objects when hidden in IE 
	if (!blnShowDesc && navigator.appName == 'Microsoft Internet Explorer'){			
		if (MediaInsideDescriptionOrSection('MediaPlayer')){
			StopMedia('MediaPlayer');
		}	
		else if (MediaInsideDescriptionOrSection('Rp1')){
			StopMedia('Rp1');
		} 		
	}	
	
	while(i < arrDescRow.length)
	{
		if (arrDescRow[i].id == 'descRow') arrDescRow[i].style.display = style;
		++i;
	}	
	
	//Delay call for the ResetDecriptionDiv if Browser is firefox. This is a fix for overlapping
	//description table which contains a long word.
	if (navigator.appName == 'Netscape'){
	
		if (MediaInsideDescriptionOrSection('QuickTimePlayer')){
			StopMedia('QuickTimePlayer'); //This is a fix of overlapping sounds of quicktime movie in safari
		}
			
		if (blnShowDesc){			
			setTimeout("ResetDescriptionDiv(true)",100);		
		}
		else{
			setTimeout("ResetDescriptionDiv(false)",100);		
		}		
	}
	else if(navigator.appName == 'Microsoft Internet Explorer'){			
		ResetDescriptionDiv(blnShowDesc);
	}	
	
}

function ResetDescriptionDiv(blnShowDesc)
{	
	//Get 'div' elements and assign it to an array variable
	var arrDescRowDiv = document.getElementsByTagName('div');	
	var i = 0;	
		
	
	while(i < arrDescRowDiv.length)
	{
		if (arrDescRowDiv[i].id == 'descRowDiv'){
			
			 //Reset the 'div' element containing the description field
			 arrDescRowDiv[i].innerHTML = arrDescRowDiv[i].innerHTML;
		 		 
			 //If the section contains an embedded quicktime movie and if the browser is IE, call ReloadQuicktime function
			 //to reload the quicktime player 
			 if (arrDescRowDiv[i].innerHTML.indexOf('QuickTimePlayer') != -1 && navigator.appName == 'Microsoft Internet Explorer' && blnShowDesc){												
				ReloadQuicktime();
			 }
		}
		++i;
	}		
		
	//Stops windows media player when hidden in IE.
	if (!blnShowDesc && navigator.appName == 'Microsoft Internet Explorer'){			
		if (MediaInsideDescriptionOrSection('MediaPlayer')){
			document.getElementById('MediaPlayer').autoStart = false;
		}	
	}
	
	//Plays media objects when shown in IE 
	if (blnShowDesc && navigator.appName == 'Microsoft Internet Explorer'){			
		if (MediaInsideDescriptionOrSection('MediaPlayer')){
			document.getElementById('MediaPlayer').autoStart = true;
			setTimeout('PlayMedia("MediaPlayer");', 1000);
		}	
		else if (MediaInsideDescriptionOrSection('Rp1')){
			PlayMedia('Rp1');
		} 		
	}	

}


function ReloadQuicktime()
{
	var strMovieURL, controlWidth, controlHeight, strDivId;		
	var strMediaObject;
	var curObj, objParentDiv;			
		
	curObj = document.getElementById('QuickTimePlayer');
	
	//Reinsert Quicktime if quicktime is found and is in the description field
	if (curObj != null && MediaInsideDescriptionOrSection(curObj.id)){					
			
		//stop quicktime player. this is the fix for overlapping sounds when the Controller is redisplayed.
		StopMedia(curObj.id);
					
		//search for the 'src' attribute in the 'EMBED' element.
		strMovieURL = curObj.innerHTML;
		strMovieURL = strMovieURL.substring(strMovieURL.indexOf('src="') + 5, strMovieURL.length - 1);
		strMovieURL = strMovieURL.substring(0, strMovieURL.indexOf('"'));							
		
		//search for the width attribute in the Quicktime object
		controlWidth = curObj.innerHTML;
		controlWidth = controlWidth.substring(controlWidth.indexOf('width="') + 7, controlWidth.length - 1);
		controlWidth = controlWidth.substring(0, controlWidth.indexOf('"'));
		
		//search for the height attribute in the Quicktime object
		controlHeight = curObj.innerHTML;
		controlHeight = controlHeight.substring(controlHeight.indexOf('height="') + 8, controlHeight.length - 1);
		controlHeight = controlHeight.substring(0, controlHeight.indexOf('"'));	
		
		//generate the media oject using the QT_GenerateOBJECTText found in LoadMediaControl.js
		strMediaObject = QT_GenerateOBJECTText(strMovieURL, controlWidth, controlHeight, '', 'CONTROLLER', 'true', 'ShowStatusBar', 'true', 'type', 'video/QuickTime', 'SRC' , strMovieURL, 'QTSRC', strMovieURL, 'id', 'QuickTimePlayer', 'AutoPlay', 'false');
					
		//Reload the media object
		curObj.outerHTML = strMediaObject;
		
		//Show Quicktime Controller. This is the fix for missing controller upon reloading the quicktime movie
		setTimeout('ShowQTController()', 1000);		
		
	}
}

function ShowQTController()
{
	var objQuicktime;
	
	objQuicktime = document.getElementById('QuickTimePlayer');
	
	if (objQuicktime != null){
		try{
			objQuicktime.SetControllerVisible(true);	
			objQuicktime.SetAutoPlay(true);
			objQuicktime.Play();
		}
		catch(ex){}
	}
	
}

function MediaInsideDescriptionOrSection(ID)
{
	var curObj = document.getElementById(ID);
	var objParent;
		
	if (curObj != null){
		try{
			objParent = curObj.parentElement;
			
			while(objParent){
				
				//return true if quicktime is found inside description or section content
				if (objParent.tagName == 'DIV' && (objParent.id == 'descRowDiv' || objParent.id.indexOf('SectionContent') != -1)) return true;
				
				try{
					objParent = objParent.parentElement;
				}
				catch(ex){
					break;
				}
			}		
			
			return false;
		}
		catch(ex){return false;}
	}
	else{
		return false;
	}
}

function GetMediaSectionId(ID)
{
	var curObj = document.getElementById(ID);
	var objParent;
	var strEmptyId = '';
	
	if (curObj != null){
		try{
			objParent = curObj.parentElement;
			
			while(objParent){
				
				//return section id if quicktime is found inside section content
				if (objParent.tagName == 'DIV' && objParent.id.indexOf('Section') != -1) return objParent.id;
				
				try{
					objParent = objParent.parentElement;
				}
				catch(ex){
					break;
				}
			}		
			
			return strEmptyId;
		}
		catch(ex){return strEmptyId;}
	}
	else{
		return strEmptyId;
	}
	
}

function StopMedia(ID)
{
	var objMedia;
	
	objMedia = document.getElementById(ID);
	
	if (objMedia != null){					
		if (ID == 'MediaPlayer'){
			try{
				objMedia.Stop();
				objMedia.autoStart = false;
			}
			catch(ex){} 
		}						
		else if (ID == 'Rp1'){
			try{
				objMedia.DoStop();
				objMedia.SetAutoStart(false);
			}
			catch(ex){}			
		}
		else if (ID == 'QuickTimePlayer'){
			try{
				objMedia.Stop();		
			}
			catch(ex){}
		}							
	}	
}

function PlayMedia(ID)
{
	var objMedia;
	
	objMedia = document.getElementById(ID);
	
	if (objMedia != null){					
		if (ID == 'MediaPlayer'){
			try{
				objMedia.Play();					
			}
			catch(ex){} 
		}						
		else if (ID == 'Rp1'){
			try{
				objMedia.DoPlay();
				objMedia.SetAutoStart(true);
			}
			catch(ex){}			
		}
		else if (ID == 'QuickTimePlayer'){
			try{
				objMedia.Play();		
			}
			catch(ex){}
		}							
	}	
}

function GetCurrentMedia()
{
	var objMedia;
	
	objMedia = document.getElementById('MediaPlayer');
	
	if (objMedia != null) return objMedia;
	
	objMedia = document.getElementById('Rp1');
	
	if (objMedia != null) return objMedia;
		
	return null;

}
