// JavaScript Document

// Global vars
var fadeList = new Array();
var fadeInterval = 50;  // mS
var fadeIntervalDefault = 0.2;
var idList = new Array();
var idCnt = 0;


/************************************************************************
* Page functions
************************************************************************/

function SetPage(id, newSrc) {
  var el = _getElement(id);
  if (el != null)
    el.src = newSrc;
}

function FadePage(id, dir, newSrc, interval, setVar, setVal)
{
  var el = _getElement(id);
	var fo;
	var xmlHttp;

  if (typeof(interval) === 'undefined')
    interval = fadeIntervalDefault;
  if ((typeof(setVar) === 'undefined') || (typeof(setVal) === 'undefined'))
	{
    setVar = null;
    setVal = null;
  }

	if (el != null)
	{
  	if (newSrc != null)
		{
      // Initiate loading of new page.
			if (window.XMLHttpRequest)  // Firefox, Safari & IE7+
			{
				xmlHttp = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)  //IE 5&6
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}

      try {
				xmlHttp.open("GET", newSrc, false);
			} catch (e) {
				alert('XMLHttpRequest error: ' + e);
			}
			xmlHttp.send(null);

      fo = new _fadeObject(id, el, dir, xmlHttp.responseText, interval, setVar, setVal);
  	}
		else
		{
      fo = new _fadeObject(id, el, dir, null, interval, setVar, setVal);
		}
	
    fadeList.push(fo);
	  setTimeout("_fade()", fadeInterval);
	}
	else
	{
		alert("FadePage: error - " + id + ", " + dir + ", " + newSrc);
	}
}

function FadeText(id, dir, text, interval)
{
  var el = _getElement(id);
	var fo;

  if (typeof(interval) === 'undefined')
    interval = fadeIntervalDefault;

	if (el != null)
	{
    fo = new _fadeObject(id, el, dir, text, interval);
	  fadeList.push(fo);
	  setTimeout("_fade()", fadeInterval);
	}
	else
	{
		alert("FadePage: error - " + id + ", " + dir + ", " + newSrc);
	}
}

function OpenFloatPage(src, title)
{
	if (typeof(title) === 'undefined')
    title = '';

	FadePage('PaperContainer', 'in', null, 0.5);
	FadeText('PaperTitle', 'in', title, 0.5);
	FadePage('PaperText', 'in', src, 0.5);
}

function CloseFloatPage()
{
  FadePage('PaperContainer', 'out', null, 0.5);
}

function OpenListPage(src)
{
	FadePage('TLListContainer', 'in', src, 0.5);
}

function CloseListPage()
{
  var el = _getElement('TLListContainer');
	if (el == null)
	  return;

	if (el.style.display == 'none')
	  return;
	
	FadePage('TLListContainer', 'out', null, 0.5);
}

function PreloadImages()
{
  var d = document;
	if (d.images)
	{
		if (!d.p)
		  d.p = new Array();
		
    var i, j = d.p.length, a = PreloadImages.arguments;
		for (i = 0; i < a.length; i++)
      if (a[i].indexOf("#") != 0)
			{
				d.p[j] = new Image;
				d.p[j++].src = a[i];
			}
	}
}

/************************************************************************
* Dynamic HTML generation functions
************************************************************************/

function SetLeaderViewer(id, ministry, name) {
	var el = _getElement(id);
	if (el == null)
	  return;

  var html = '<link href="CSS/Main.css" rel="stylesheet" type="text/css" />' +
             '<div>' +
             '  <table cols="2" cellpadding="5px">' +
             '    <tr>' +
             '      <td width="40%" valign="top">' +
             '        <div class="LeaderInfoMinistry">' + ministry + '</div>' +
             '        <div class="LeaderInfoName">' + name + '</div>' +
             '      </td>' +
             '      <td width="60%" valign="top">' +
             '         <img class="LeaderInfoImage" src="Assets/Images/Leaders/' + name + ' 200x300.jpg" width="200px">' +
             '      </td>' +
             '    </tr>' +
             '  </table>' +
             '</div>';

	var fo = new _fadeObject(id, el, 'cross', html, fadeIntervalDefault);
	fadeList.push(fo);
	setTimeout("_fade()", fadeInterval);
}

function SetMessageViewer(date, speaker, title, file) {
	var el = _getElement('TRWindow');
	if (el == null)
	  return;

  var html = '<link href="CSS/Main.css" rel="stylesheet" type="text/css" />' +
             '<div>' +
						 '  <table cols="3" cellpadding="5px">' +
						 '    <tr valign="top">' +
						 '      <td colspan="3" width="100%">' +
						 '        <div class="MsgInfoTitle">' + title + '</div>' +
						 '      </td>' +
						 '    </tr>' +
						 '    <tr valign="top">' +
						 '      <td width="60%">' +
						 '        <div class="MsgInfoSpeaker">' + speaker + '</div>' +
						 '        <div class="MsgInfoDate">' + date + '</div>' +
						 '        <embed src="Media/Messages/' + file + '" autostart=false loop=false width="100%" height="16px" vspace="20px">' +
						 '        <div align="center"><a href="Media/Messages/' + file + '" class="MsgInfoFile">Right-click here to download.</a></div>' +
						 '      </td>' +
						 '      <td width="10%"></td>' +
						 '      <td width="40%">' +
						 '         <img class="MsgInfoImage" src="Assets/Images/SinglePhotos/' + speaker + '.jpg" width="100%">' +
						 '      </td>' +
						 '    </tr>' +
						 '  </table>' +
             '</div>';

	var fo = new _fadeObject('', el, 'cross', html, fadeIntervalDefault);
	fadeList.push(fo);
	setTimeout("_fade()", fadeInterval);
}

function SetResourceViewer(title, dir, thumb, file, file_type) {
	var el = _getElement('TRWindow');
	if (el == null)
	  return;

  var type_info = "";
	if (file_type == 'PDF')
		type_info = '<a href="http://www.adobe.com/products/acrobat/readstep2.html" onclick="window.open(this.href, \'popupwindow\', \'width=900,height=1000,scrollbars,resizable\'); return false;">Requires Adobe Reader<br /><img src=Assets/Images/get_adobe_reader.gif></a>';

  var html = '<link href="CSS/Main.css" rel="stylesheet" type="text/css" />' +
             '<div>' +
						 '  <table cols="2" cellpadding="5px">' +
						 '    <tr valign="top">' +
						 '      <td width="70%" align="center">' +
						 '        <span class="ResourceInfoTitle">' + title + '</span><br />' +
						 '        <br />' +
						 '        <a style="text-decoration:none" href="Media/' + dir + '/' + file + '" onclick="window.open(this.href, \'popupwindow\', \'width=900,height=1000,scrollbars,resizable\'); return false;"><img class="ResourceInfoImage" src="Media/' + dir + '/' + thumb + '"></a>' +
						 '      </td>' +
						 '      <td width="30%" valign="bottom" align="center">' +
						 '        <span class="ResourceType">' + type_info + '</span>' +
						 '      </td>' +
						 '    </tr>' +
						 '  </table>' +
             '</div>';

	var fo = new _fadeObject('', el, 'cross', html, fadeIntervalDefault);
	fadeList.push(fo);
	setTimeout("_fade()", fadeInterval);
}

function SetVideoViewer(section, title, file, width, height) {
	var el = _getElement('TRWindow');
	if (el == null)
	  return;

  var html = '<link href="CSS/Main.css" rel="stylesheet" type="text/css" />' +
             '<div>' +
             '  <h2 style="text-align:center">' + section + '</h2>' +
             '  <div class="MsgInfoTitle" style="text-align:center">' + title + '</div>' +
             '    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height + '" style="margin-left:20px" id="FLVPlayer"> \
										<param name="movie" value="FLVPlayer_Progressive.swf" /> \
										<param name="quality" value="high" /> \
										<param name="wmode" value="opaque" /> \
										<param name="scale" value="noscale" /> \
										<param name="salign" value="lt" /> \
										<param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Corona_Skin_3&amp;streamName=Media/' + file + '&amp;autoPlay=true&amp;autoRewind=false" /> \
										<param name="swfversion" value="8,0,0,0" /> \
										<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. --> \
										<param name="expressinstall" value="Scripts/expressInstall.swf" /> \
										<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. --> \
										<!--[if !IE]>--> \
										<object type="application/x-shockwave-flash" data="FLVPlayer_Progressive.swf" width="' + width + '" height="' + height + '"> \
											<!--<![endif]--> \
											<param name="quality" value="high" /> \
											<param name="wmode" value="opaque" /> \
											<param name="scale" value="noscale" /> \
											<param name="salign" value="lt" /> \
											<param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Corona_Skin_3&amp;streamName=Media/' + file + '&amp;autoPlay=true&amp;autoRewind=false" /> \
											<param name="swfversion" value="8,0,0,0" /> \
											<param name="expressinstall" value="Scripts/expressInstall.swf" /> \
											<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. --> \
											<div> \
												<h4>Content on this page requires a newer version of Adobe Flash Player.</h4> \
												<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p> \
											</div> \
											<!--[if !IE]>--> \
										</object> \
										<!--<![endif]--> \
									</object>' +
             '</div>';

	var fo = new _fadeObject('', el, 'cross', html, fadeIntervalDefault);
	fadeList.push(fo);
	setTimeout("_fade()", fadeInterval);
/*  doc.writeln('<script type="text/javascript"><!--swfobject.registerObject("FLVPlayer");//--></script>');*/
}

function ShowVideo(container, file, width, height) {
	var el = _getElement(container);
	if (el == null)
	  return;

  var html = '<link href="CSS/Main.css" rel="stylesheet" type="text/css" />' +
             '<div>' +
             '    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height + '" style="margin-left:20px" id="FLVPlayer"> \
										<param name="movie" value="FLVPlayer_Progressive.swf" /> \
										<param name="quality" value="high" /> \
										<param name="wmode" value="opaque" /> \
										<param name="scale" value="noscale" /> \
										<param name="salign" value="lt" /> \
										<param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Corona_Skin_3&amp;streamName=Media/' + file + '&amp;autoPlay=true&amp;autoRewind=false" /> \
										<param name="swfversion" value="8,0,0,0" /> \
										<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. --> \
										<param name="expressinstall" value="Scripts/expressInstall.swf" /> \
										<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. --> \
										<!--[if !IE]>--> \
										<object type="application/x-shockwave-flash" data="FLVPlayer_Progressive.swf" width="' + width + '" height="' + height + '"> \
											<!--<![endif]--> \
											<param name="quality" value="high" /> \
											<param name="wmode" value="opaque" /> \
											<param name="scale" value="noscale" /> \
											<param name="salign" value="lt" /> \
											<param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Corona_Skin_3&amp;streamName=Media/' + file + '&amp;autoPlay=true&amp;autoRewind=false" /> \
											<param name="swfversion" value="8,0,0,0" /> \
											<param name="expressinstall" value="Scripts/expressInstall.swf" /> \
											<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. --> \
											<div> \
												<h4>Content on this page requires a newer version of Adobe Flash Player.</h4> \
												<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p> \
											</div> \
											<!--[if !IE]>--> \
										</object> \
										<!--<![endif]--> \
									</object>' +
             '</div>';

	var fo = new _fadeObject('', el, 'cross', html, fadeIntervalDefault);
	fadeList.push(fo);
	setTimeout("_fade()", fadeInterval);
}


/************************************************************************
* Internal functions
************************************************************************/

function _idObject(id)
{
	// Store id and element together.
	this.num = idCnt++;
	this.id = id;
	this.el = document.getElementById(id);
	if (this.el == null)
		this.el = top.document.getElementById(id);
}

function _getElement(id)
{
	var i;
	
  // Check if id in list.
	for (i = 0; i < idList.length; i++)
	{
		// If so, return element.
		if (idList[i].id == id) {
			return idList[i].el;
		}
	}
	
	// Otherwise, add to list.
	var idObj = new _idObject(id);
	if (idObj.el != null)
		idList.push(idObj);
	
	return idObj.el;
}

function _fadeObject(id, el, dir, html, interval, setVar, setVal)
{
  this.id = id;
  this.el = el;
	this.dir = dir;
	this.html = html;
	this.setVar = setVar;
	this.setVal = setVal;
	this.ready = 0;
	var inc = fadeInterval / (interval * 1000);
	
	if (dir == 'in')
	{
		this.opacity = 0.0;
		this.inc = inc;
	}
	else {
		this.opacity = 1.0;
		this.inc = -inc;
	}
}

function _fade() {
	var fo;
	var i;

  // Loop throuhg all active objects.
  for (i = 0; i < fadeList.length; i++)
	{
		// Get current object.
		fo = fadeList[i];
		
		// If opacity is zero, then change source now.
		if (fo.opacity <= 0.0)
		{
			fo.opacity = 0.0;
			if (fo.html != null)
  			fo.el.innerHTML = fo.html;
			  if (fo.setVar != null)
	  	    Select(fo.setVar, fo.setVal);
  }

    // Update opacity.
    fo.opacity += fo.inc;
		fo.el.style.opacity = fo.opacity;
		fo.el.style.display = "block";
		
		// Check end bounds, removing element if completed.
		if (fo.inc > 0.0 && fo.opacity >= 1.0)
		{
			fadeList.splice(i,1);
		}

    if (fo.inc < 1.0 && fo.opacity <= 0.0)
		{
			if (fo.dir == 'cross')
			{
				fo.inc = -fo.inc;
			}
  		else
			{
			  fadeList.splice(i,1);
	  	  if (fo.dir == 'out' && fo.html == null)
				  fo.el.style.display = "none";
	  	}
		}
	}

	// Reset timeout if still elements to process.
	if (fadeList.length > 0) {
	  setTimeout("_fade()", fadeInterval);
	}
}

