// preload the logo and tagline

function externalLinks()
{
	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
			anchor.target = '_blank';
	}
}

/* And all that Malarkey // Trimming form elements

Please feel free to use this JavaScript file in any way that you like, although please
keep the comments intact and a link back to http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html
would be appreciated.

If you come up with a stunning design based on this technique, it would be really nice
if you would post a comment containing a URL on 
http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html#Comments 

Thanks to Brothercake (http://www.brothercake.com/) for his help with the Javascript.
His fantastic UDM 4 fully-featured and accessible website menu system is a must!
(http://www.udm4.com/) */

window.onload = function()
{
	toggleLinkContainer();
	toggleContentSearchForm();
	toggleCoreFieldsLinkContainer();
	externalLinks();
	striper('table', false, 'tr', 'oddRow, evenRow');
}

function toggleLinkContainer()
{
	if(document.getElementById)
	{
		var linkContainer = document.getElementById('toggleFormFields');
		
		if(!linkContainer)
		{
			return false;
		}
		
		var toggle = linkContainer.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode('Remove optional fields?'));
		toggle.onclick = function()
		{
			var linkText = this.firstChild.nodeValue;
			this.firstChild.nodeValue = (linkText == 'Remove optional fields?') ? 'Display optional fields?' : 'Remove optional fields?';
			
			var tmp = document.getElementsByTagName('div');
			for (var i=0;i<tmp.length;i++)
			{
				if(tmp[i].className.indexOf('optional') != -1)
				{
					tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
				}
			}
			
			var tmp = document.getElementsByTagName('fieldset');
			for (var i=0;i<tmp.length;i++)
			{
				if(tmp[i].className.indexOf('optional') != -1)
				{
					tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
				}
			}
			
			return false;
		}
	}
}

// crude hack of above function for page admin
function toggleCoreFieldsLinkContainer()
{
	if(document.getElementById)
	{
		var coreLinkContainer = document.getElementById('togglecoreformfields');
		
		if(!coreLinkContainer)
		{
			return false;
		}
		
		var toggle = coreLinkContainer.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode('Show only commonly used fields?'));
		
		toggle.onclick = function()
		{			
			var linkText = this.firstChild.nodeValue;
		
			for(var j=0; j<2; j++)
			{
				if(j == 0)
				{
					var htmlArray = document.getElementsByTagName('div');
				}
				else
				{
					var htmlArray = document.getElementsByTagName('fieldset');
				}
				
				switch(linkText)
				{
					case 'Show only commonly used fields?':
					
						this.firstChild.nodeValue = 'Show only complusory fields?';
						
						for (var i=0;i<htmlArray.length;i++)
						{
							// alert(tmp[i].className.indexOf('corefield'));
							if(htmlArray[i].className.indexOf('optional') != -1 && htmlArray[i].className.indexOf('corefield') < 0)
							{
								htmlArray[i].style.display = 'none';
							}
						}
						
						break;
						
					case 'Show only complusory fields?':
					
						this.firstChild.nodeValue = 'Show all fields?';
					
						for (var i=0;i<htmlArray.length;i++)
						{
							// alert(tmp[i].className.indexOf('corefield'));
							if(htmlArray[i].className.indexOf('optional') != -1)
							{
								htmlArray[i].style.display = 'none';
							}
						}
						
						break;
						
					case 'Show all fields?':
					
						this.firstChild.nodeValue= 'Show only commonly used fields?';
					
						for (var i=0;i<htmlArray.length;i++)
						{
							htmlArray[i].style.display = 'block';
						}
						
						break;
				}
			}

			return false;
		}
	}
}

function toggleContentSearchForm()
{
	if(document.getElementById)
	{
		var searchForm = document.getElementById('contentSearchFormText');
		
		if(!searchForm)
		{
			return false;
		}
		
		var toggle = searchForm.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode('Display search form?'));
		toggle.onclick = function()
		{
			var linkText = this.firstChild.nodeValue;
			this.firstChild.nodeValue = (linkText == 'Hide search form?') ? 'Display search form?' : 'Hide search form?';
			
			var tmp = document.getElementsByTagName('form');
			for (var i=0;i<tmp.length;i++)
			{
				if(tmp[i].className.indexOf('hideableForm') != -1)
				{
					tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
				}
			}
			
			return false;
		}
		
		// the above function is only done on click, we want to hide form
		var tmp = document.getElementsByTagName('form');
		for (var i=0;i<tmp.length;i++)
		{
			if(tmp[i].className.indexOf('hideFormInitially') != -1)
			{
				tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
			}
		}
		
	}
}

// Splintered striper 1.3
// reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
// but as a general DOM means of assigning any number of classes to children of a parent element.
// Patrick H. Lauke aka redux / www.splintered.co.uk
// Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/
// http://24ways.org/2005/splintered-striper

/*
 * Summary:      Core experiment function that applies any number of classes to all child elements
 *               contained in all occurences of a parent element (either with or without a specific class)
 * Parameters:   parentElementTag - parent tag name
 *               parentElementClass - class assigned to the parent; if null, all parentElementTag elements will be affected
 *               childElementTag -  tag name of the child elements to apply the styles to
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 */
function striper(parentElementTag, parentElementClass, childElementTag, styleClasses)
{
	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className == parentElementClass)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					if(currentChild.className.indexOf('Row') == -1)
					{
						currentChild.className = currentChild.className+" "+styles[k];
					}
				}
			}
		}
	}
}

function makeInvisible(id)
{
	document.getElementById(id).style.visibility='hidden';
	document.getElementById(id).style.display='none';
}