//*****************************************************************************************
//
// JavaScript File
//
// Name:         ContentArticle.js
//
//*****************************************************************************************
//*****************************************************************************************
//
// REVISION HISTORY
// Revision Date     Author     Description 
// 03.06.2008        AMV        Updated for RMT 3250.Added the ff. js functions
//                              ReloadQuicktime,ShowQTController, MediaInsideDescriptionOrSection
//                              GetMediaSectionId,StopMedia,PlayMedia,GetCurrentMedia
//                              and ResetDiv. Updated SetActiveSection to fix the issue on EA
//
// 
//*****************************************************************************************

   function ResetActiveSection()
{		

	var IE = document.all?true:false;
	if (IE)
        { 
		if (event.clientY < 0 && event.clientX > document.body.offsetWidth-30) 
		{		
			writeCWSCookie(ActiveSectionButton, "");
		}
	}
	else 
	{
		writeCWSCookie(ActiveSectionButton, "");
	}
}
   
   function MetaInfo()
	{
        /* Declare a variable for the 3 types of metainfo and get the corresponding container for each type */
  		var objMetaInfoIndustry = getCWSObj('MetaInfoIndustry'); 
		var objMetaInfoSubject =  getCWSObj('MetaInfoSubject');
		var objMetaInfoGeography = getCWSObj('MetaInfoGeography');
	
	    /* objMetaInfoIndustry must be not equal to null */
	    if (objMetaInfoIndustry!=null) 
		{
   			/* Call function DisplayMetaInfo */
			DisplayMetaInfo(objMetaInfoIndustry,stringIndustry,stringIndustryLabel);
		}

		/* objMetaInfoSubject must be not equal to null */
		if (objMetaInfoSubject!=null) 
		{
			/* Call function DisplayMetaInfo */
			DisplayMetaInfo(objMetaInfoSubject,stringSubject,stringSubjectLabel);
		}

		/* objMetaInfoGeography must be not equal to null */
		if (objMetaInfoGeography!=null) 
		{
			/* Call function DisplayMetaInfo */
			DisplayMetaInfo(objMetaInfoGeography,stringGeography,stringGeographyLabel);
		}

}

/* Funtion for finding HTML Control */
function getCWSObj(id) 
{
	if (document.getElementById) {
		return document.getElementById(id);
		}
	else if (document.all) {
		getCWSObj = document.all[id];
		}
}


/* Funtion for Setting the initial state of the page */
function setInitialPageState()
{
	var IE = document.all?true:false;
	try
	{
		SetActiveSection(getCWSObj(readCWSCookie(ActiveSectionButton)));
		
		//RMT 3250 
		//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'); 
		    }
		}
		//end of RMT 3250
	}
	catch(Err)
	{
	}
	MetaInfo();
	
	if(!IE) ResetActiveSection();
}

/* Funtion for processing the raw data of metainfo */
function DisplayMetaInfo(objMetaInfo,MetaInfoDelimitedText,MetaInfoDisplayText)
{
/* Create Temporary variable */
var strTemp = ''

/* Create metaInfoTemplate variable */
var metaInfoTemplate = '';

/* Split the raw data */
var arrayText = MetaInfoDelimitedText.split('|');


/* Iterate to array content */
for (var i=0; i <= (arrayText.length - 1); i++)
{
 
    /* Test if current iteration position mod 2 is equal to 1 */
    if ((i+1) % 2==1)
	 {
	    /* Code for Anchor Text */
	 	metaInfoTemplate = '<a href="@URL">@Text</a>';
	 	metaInfoTemplate = metaInfoTemplate.replace('@Text',CWSTrim(arrayText[i]));
	 }
	 else
	 {
	   /* Code for URL */
	   if (CWSTrim(arrayText[i]).length != 0) {
	   /* With URL */
   	   metaInfoTemplate = metaInfoTemplate.replace('@URL',CWSTrim(arrayText[i]));
   	   }
   	   else
   	   {
   		/* Without URL */
   		metaInfoTemplate = metaInfoTemplate.replace('<a href="@URL">','');
   		metaInfoTemplate = metaInfoTemplate.replace('</a>','');
   	   }
		
	 }
	 
	 /* Code for String concatenation of metainfo multi value */
	 if (arrayText.length >2 && (i+1) % 2==0)
	 {
		 if (strTemp!='')
		 {
		 strTemp=strTemp + '<span class="eleven">;</span>&nbsp;' + metaInfoTemplate;  
		 }
		 else
		 {
		 strTemp=metaInfoTemplate;  
		 }
	 }
}
     /* Display Multi value MetaInfo*/
     if (arrayText.length > 2)
     {
		objMetaInfo.innerHTML = '<strong>' + MetaInfoDisplayText + ':&nbsp;' +  '</strong>' + strTemp;
	 }
	 else
	 {	
		/* Single MetaInfo Value */
		objMetaInfo.innerHTML = '<strong>' + MetaInfoDisplayText + ':&nbsp;' +  '</strong>' + metaInfoTemplate;
	 }
}


/* function for setting the active buttons */
function SetActiveSection(obj)
{


/* Declare Section state value */
var Hide='none';
var Show='block'; 
var maxNoOfButtons = 100;

/* Check if container for section buttons and content are already exist on 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) 
			
			{
			
			//RMT 3250 starts here
			
			//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 for reading cookies */
function readCWSCookie(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 for creating cookies */
function writeCWSCookie(name, value)
{
  try
  {
	document.cookie = name + '=' + escape(value);
  }
  catch(Err)
  {}
}

function CWSTrim(inputString) 
{
    var removeChar = ' ';
	var returnString = inputString;
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

// START of RMT 3250 Enable Text Resizing on Old CMS Templates 
// Added javascript functions for embedded attachment and Content with sections

// Reloads the div section of the page by reassigning the html content
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
		}
	}
}

// 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.
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);		
		
	}
}

//Checks for the quicktime object and shows its control bar
function ShowQTController()
{
	var objQuicktime;
	
	objQuicktime = document.getElementById('QuickTimePlayer');
	
	if (objQuicktime != null){
		try{
			objQuicktime.SetControllerVisible(true);	
			objQuicktime.SetAutoPlay(true);
			objQuicktime.Play();
		}
		catch(ex){}
	}
	
}

//Checks if the Media object is embedded inside the description div 
//Content with sections 
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;
	}
}

//Search for the id of the section containing the media object
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;
	}
	
}

//Checks for the embedded media object and stops it if found. 
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){}
		}							
	}	
}

//Checks for the embedded media object and plays it if found. 
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;

}

// END of RMT 3250 Enable Text Resizing on Old CMS Templates 
