var FPeriodical;

window.addEvent('domready', function() 
{
	// Enable click on logo
    if($('logo'))
    {
    	$('logo').addEvent('click', function() {
				window.location = 'index.php';
				return false;
			});
    }
		
	// External Links to new window
	$$('a[href^="http://"]').each(function(a) 
		{
			var href = a.get('href');
			if(!href.contains(window.location.host)) 
			{
				a.setProperty('target', '_blank');
			}
		});
		
	// Get sub menu working in IE6
	if ($$('#mainmenu li').length > 0)
	{
		$$('#mainmenu li').addEvent('mouseover', function (){
			this.set('class', this.get('class') + ' hover');
		});
		
		$$('#mainmenu li').addEvent('mouseout', function (){
			this.set('class', this.get('class').replace(' hover', ''));
		});
	}

	// Process .Tab divs in content into menu to display them individually
	if ($$('div.tab').length > 0)
	{
		var aNewUL = new Element('ul', {'class' : 'tabcontrol'});		
		
		$$('div.tab').each(function (aDiv, aIndex){
			if(aIndex != 0) aDiv.setStyle('display','none');
			
			var aNewLi = new Element('li', {'id' : 'tabitem_' + aIndex});
			
			if(aIndex == 0) aNewLi.set('class', 'selected');
			
			var aNewLink = new Element('a', {'html' : aDiv.getAttribute('title')});
			aNewLink.addEvent('click', ShowTab);
			
			aNewLink.inject(aNewLi);
			aNewLi.inject(aNewUL);
		});
		aNewUL.inject($$('div.tab')[0], 'before');
	}
	
	// Show the popup box if one is present in the page
	if($('popupboxclose') && $('popupboxenable'))
	{
		// Only display the popup box after a certain period of time
		//ShowPopUpBox.delay(10000);
		
		// Display pop up box if image is clicked on
		$('popupboxenable').addEvent('click', function(){
			ShowPopUpBox();
		});
		
		// Display pop up box if the form has been submitted and there are warnings
		 $$('#popupboxcontainer p.warning').each(function(){
			 ShowPopUpBox();
		 });
		 
		
		// Close the pop up box and remove the overlay
		$('popupboxclose').addEvent('click', function(){
			$('pageoverlay').destroy();
			$('popupboxcontainer').setStyle('display', 'none');
		});
	}

});

// Function to add background transparent overlay and display popup box
function ShowPopUpBox()
{	
	aBody = $$('body').getLast();
	aCurrentBodySize = aBody.getScrollSize();
	
	// Create the overlay
	var aOverLayDiv = new Element('div', {
		id: 'pageoverlay',
		styles: {
			'background-color': 'rgb(0, 0, 0)',
			opacity: '0.65',
			width: aCurrentBodySize.x + 'px',
			height: aCurrentBodySize.y + 'px',
			visibility: 'visible',
			position:'absolute',
			top:0,
			left:0,
			'z-index': '10000'
		},
		events: {
			click: function(){
				$('popupboxcontainer').setStyle('display', 'none');
				$('pageoverlay').destroy();
			}
		}
	});
	
	// Show the popup box div
	$('popupboxcontainer').setStyle('display', 'block');
	
	// Inject before the popup box in order to solve IE7 z-index issues!
	aOverLayDiv.inject($('popupboxcontainer'), 'before');
}

function ShowTab()
{
	aTabToShow = this.innerHTML;

	$$('div.tab').each(function (aCurrentDiv, aIndex){
		if(aCurrentDiv.getAttribute('title') != aTabToShow) 
		{
			aCurrentDiv.setStyle('display','none');
			$$('.tabcontrol li')[aIndex].set('class', '');
		}
		else 
		{	
			aCurrentDiv.setStyle('display','block');
			$$('.tabcontrol li')[aIndex].set('class', 'selected');
		}
	});
}
