var rolloverReveal = {
	start : function() {
		$$('.rolloverelement').each(function(rolloverElement, index) {
				rolloverElement.getElements('.revealelement').each(function(revealElement, index){
						rolloverElement.addEvent('mouseover', function(){
								revealElement.setStyle('display', '');
							}
						);
						rolloverElement.addEvent('mouseout', function(){
								revealElement.setStyle('display', 'none');
							}
						);
					}
				);
			}
		);
	}
};

var clickReveal = {
	start : function() {
		$$('.clickrevealwrapper').each(function(clickRevealWrapper, index) {
			clickRevealWrapper.getElements('.clicktarget').each(function(clickTarget, index){
				//Custom event used to get round event handler execution order problem with IE
				//http://mootools.lighthouseapp.com/projects/2706/tickets/35-ie-s-attachevent-ff-s-addeventlistener-and-event-fire-order
				clickTarget.addEvent('click', function(event){
					this.fireEvent('togglereveal');
					new Event(event).stop();
				});
				clickTarget.addEvent('togglereveal', function(){
					var currentlyOpen = clickRevealWrapper.hasClass('clickrevealwrapperopen');
					clickRevealWrapper.getElements('.revealelement').each(function(revealElement, index){
						if(currentlyOpen)
							revealElement.setStyle('display', 'none');
						else
							revealElement.setStyle('display', '');
					});
					if(currentlyOpen)
						clickRevealWrapper.removeClass('clickrevealwrapperopen');
					else
						clickRevealWrapper.addClass('clickrevealwrapperopen');
				});
			});
		});
	}
};

var blockLinks = {
    start: function()
    {
        var blocks = $$('.blocklink');
        blocks.each(function(block){
            block.addEvent('click', function()
            {
               var link = this.getElementsByTagName('a')[0];
               document.location = link.href;
            });
        });
    }
};


function setupSelectOnFocusTextBox(textboxId, defaultText)
{
  var textbox = $(textboxId);
  if(textbox.value == '')
	textbox.value = defaultText;
  textbox.addEvent('focus', function()
	  {
	    if(this.value == defaultText)
	      this.value = "";
	    else
	      this.select();
	  });
  textbox.addEvent('blur', function()
	  {
	  	if(this.value == "")
	  		this.value = defaultText;
	  });
}

var navigationDropDowns = {
	start:function()
		{
			$$('.navigationdropdown').each(function(navigationDropDown, index)
				{
					navigationDropDown.addEvent('change', function()
						{
							var url = this.options[this.selectedIndex].value;
							if(url!='')
								window.location = url;
						});
				});
		}
};

var relatedVideos = {
	start:function()
		{
			$$('.relatedvideos').each(function(relatedVideosInstance, relatedVideosIndex)
				{
					var videoListItems = relatedVideosInstance.getElements('.videolistitem');
					videoListItems.each(function(videoListItem, videoListItemIndex)
						{
							var videoLink = videoListItem.getElements('a.videolink')[0];
							videoLink.addEvent('click', function(event)
								{
									new Event(event).stop();
									relatedVideos.selectVideo(relatedVideosInstance, videoListItem);
								});
						});
				});
		},
	selectVideo:function(relatedVideosInstance, videoListItem)
		{
			//Remove the selected css class from the list item
			relatedVideosInstance.getElements('.videolistitem').each(function(item, index)
				{
					item.removeClass('selectedvideolink');
				});
			videoListItem.addClass('selectedvideolink');
			
			//Get the video url
			var url = videoListItem.getElements('.videofileurl')[0].value;
			//Get the preview image url
			var imageUrl = videoListItem.getElements('.imagefileurl')[0].value;
			
			//Load and play the video
			var player = relatedVideosInstance.getElements('.flashvideo')[0].firstChild;
			player.sendEvent("LOAD", {file:url, image:imageUrl});
			player.sendEvent("PLAY", "true");
		}
}

var tabBoxInititializer = {
	start:function()
	{
		var tabBoxElement = $('supportingcontent');
		//If there is a tab box on the page (only allows one tabbox per page)
		if(tabBoxElement)
		{
			//Initialize the tab box
			var tabBox = new Tabbox("supportingcontent", { header: "h3", select: 0,  autosize: true });
			
			//'Click reveal' functionality changes the hight of the content, so when that happens, the tab box needs to recalculate it's height
			tabBoxElement.getElements('.clicktarget').each(function(clickTarget, index)
			{
				clickTarget.addEvent('togglereveal', tabBox.adjustFootprint.bindAsEventListener(tabBox));
			});
		}
	}
}

window.addEvent('load', function(){
		rolloverReveal.start();
		clickReveal.start();
        blockLinks.start();
		navigationDropDowns.start();
		relatedVideos.start();
		tabBoxInititializer.start();
	}
);