// Global Variables
//

document.write('<div id="popupcontainer"><span style="display:none">&nbsp;</span></div>');

var menuImages = new Array();
var workTextPopupList = new Object();

// Entry Point
//

window.onload = OnLoad;

function OnLoad()
{
   CacheMenuImages();

   var page = GetBasename(location.href);

   if ( page == 'music' )
   {
      EnableScriptedFeatures();
   }
   else
   {
      EnableWorkPopups(document);
   }

   DemangleMailAddresses();
}

// Image Caching
//

function CacheMenuImages()
{
   var tabs = document.getElementById('menutabs').getElementsByTagName('a');

   for ( var i = 0; i < tabs.length; i++ )
   {
      // Normal tab
      menuImages.push(new Image());
      menuImages[menuImages.length-1].src =
         'layout/tabs/' + tabs[i].id + '.gif';
      // Mouse over tab
      menuImages.push(new Image());
      menuImages[menuImages.length-1].src =
         'layout/tabs/' + tabs[i].id + '-over.gif';
      // Selected tab
      menuImages.push(new Image());
      menuImages[menuImages.length-1].src =
         'layout/tabs/' + tabs[i].id + '-sel.gif';
   }
}

// Links have 'corrupted' email address 
//

function DemangleMailAddresses()
{
	var links = document.getElementsByTagName('a');

	for ( var i = 0; i < links.length; i++ )
	{
		if ( links[i].className == 'mail' )
		{
			links[i].href = links[i].href.replace(/_ait_/, '@');
			links[i].href = links[i].href.replace(/_doit_/, '.');
			// links[i].onmouseover = 'window.alert("Please be aware that Rebekah will not be able to respond to your message until after June 18")';
		}
	}
}

// Text Show/Hide Functions
//

function EnableScriptedFeatures()
{
   var spans = document.getElementsByTagName('span');
   var i;

   for ( i = 0; i < spans.length; i++ )
   {
      if ( spans[i].className == 'detailscontrol' )
      {
         spans[i].style.display = 'inline';
      }
   }

   var divs = document.getElementsByTagName('div');

   for ( i = 0; i < divs.length; i++ )
   {
      if ( divs[i].className == 'workdetails' )
      {
         HideText( divs[i].parentNode.id );
         EnableWorkPopups( divs[i] );
      }
   }

   if ( document.location.hash.length )
   {
      ShowText( document.location.hash.slice(1) );
   }
}

function EnableWorkPopups( parentElement )
{
   var anchors = parentElement.getElementsByTagName('a');
   var i;

   for ( i = 0; i < anchors.length; i++ )
   {
      if ( anchors[i].className == 'popuplink' )
      {
         anchors[i].href = 'javaScript:ShowPopup("' + parentElement.parentNode.id + '-popup", "' + anchors[i].href + '")'
      }
   }
}

function UpdateText( id )
{
   if ( document.getElementById(id).getElementsByTagName('a')[0].innerHTML == 'less' )
   {
      HideText( id )
   }
   else
   {
      ShowText( id )
   }
}

function ShowText( id )
{
   document.getElementById(id).getElementsByTagName('a')[0].innerHTML = 'less';
   document.getElementById(id).getElementsByTagName('div')[0].style.display = 'block';
}

function HideText( id )
{
   document.getElementById(id).getElementsByTagName('a')[0].innerHTML = 'more';
   document.getElementById(id).getElementsByTagName('div')[0].style.display = 'none';
}

// Text Popup Functions
//

function WorkTextPopup( id, url )
{
   // Member variables

   this.widget_ = document.createElement('div');
   this.widget_.id = id;
   this.widget_.className = 'popup';

   this.left_ = 0;
   this.top_ = 0;
   this.width_ = 0;
   this.height_ = 0;
   this.windowTop_ = 0;
   this.windowWidth_  = 1024;
   this.windowHeight_ = 720;

   this.leftBorderWidth_ = 320;
   this.topBorderHeight_ = 180;


   // Member functions

   this.GetText = function( iframeUrl )
   {
      var html = '';

      html += '<iframe src="' + iframeUrl + '" frameborder=0 style="margin:4px; padding:0px; width:100%; height: 90%">';
      html +=    'Iframes not supported! <a href="' + iframeUrl + '">Click here to view</a>.';
      html += '</iframe>';
      html += '<div class="hr"></div>';
      html += '<div class="centre">';
      html +=    '<a class="close" href="javaScript:workTextPopupList[\'' + this.widget_.id + '\'].Hide()" accesskey="C">Close</a>';
      html += '</div>';

      return html;
   }

   this.GetViewPort = function()
   {
      if ( window.innerWidth )
      {
         this.windowTop_    = window.pageYOffset;
         this.windowWidth_  = window.innerWidth;
         this.windowHeight_ = window.innerHeight;
      }
      else if ( document.documentElement && document.documentElement.clientWidth )
      {
         this.windowTop_    = document.documentElement.scrollTop;
         this.windowWidth_  = document.documentElement.clientWidth;
         this.windowHeight_ = document.documentElement.clientHeight;
      }
      else if ( document.body )
      {
         this.windowTop_    = document.body.scrollTop;
         this.windowWidth_  = document.body.clientWidth;
         this.windowHeight_ = document.body.clientHeight;
      }
   }

   this.Position = function()
   {
      this.GetViewPort();

      this.left_ = ( this.windowWidth_ - this.width_ ) / 2;
      this.top_  = ( ( this.windowHeight_ - this.height_ ) / 2 ) + this.windowTop_;

      this.widget_.style.left = this.left_ + 'px';
      this.widget_.style.top  = this.top_ + 'px';
   }

   this.Show = function()
   {
      this.Position();
      this.widget_.style.display = 'block';
      this.widget_.style.visibility = 'visible';
   }

   this.Hide = function()
   {
      this.widget_.style.overflow = 'auto';
      this.widget_.style.visibility = 'hidden';
      this.widget_.style.display = 'none';
   }


   // Setup contents of popup

   var directions = new Array( 'o', 'i',  't', 'b',  'l', 'r' );
   var div = null;
   var div1 = this.widget_;
   for ( var d = 1; d < 9; d++ )
   {
      div = document.createElement('div');
      div.className = 'link' + directions[Math.ceil(d / 4) - 1] + directions[( ( Math.ceil(d / 2) - 1 ) % 2 ) + 2] + directions[((d - 1) % 2) + 4];
      div1.appendChild(div);
      div1 = div;
   }
   var popupContents = div;
   popupContents.innerHTML = this.GetText(url);

   document.getElementById('popupcontainer').appendChild(this.widget_);

   this.widget_.style.zIndex = this.widget_.style.zIndex ? this.widget_.style.zIndex + 1 : 100;

   this.Position();
   this.width_ = Math.max( 460, Math.round( ( this.windowWidth_  - this.leftBorderWidth_ ) * .55 ) );
   this.height_ = Math.max( 460, this.windowHeight_ - ( this.topBorderHeight_ * 2 ) );

   popupContents.style.width  = this.width_ + 'px';
   popupContents.style.height = this.height_ + 'px';

   return this;
}

function ShowPopup( id, url )
{
   if ( !workTextPopupList[id] )
   {
      workTextPopupList[id] = new WorkTextPopup(id, url);
   }

   workTextPopupList[id].Show();
}

// Helper Functions
//

function GetBasename( url )
{
   var page = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));

   if ( page.lastIndexOf('_') > -1 )
   {
      page = page.substring(0, page.lastIndexOf('_'));
   }

   return page;
}

function Alert()
{
   window.alert(this.className);
}
