// Create an object to hold all functiions and variables (fp stands for flat page)
fp = {

/************************************* CSS Classes ********************************************************/
	// Using a dynamic class AND a static class allows us to use a 'descendant' selector and show 
	// 2 different versions of the element (static and dynamic)
	dynamicClass:'fp',
	// Used for the image popup
	hideClass:'hide', 
	//hiddenContentContainer:'hidden_content',
  	hiddenContainerId:'hidden_content',
	radioDisplayYesID:'professional_user_yes',
	radioDisplayNoID:'professional_user_no',

	// Set variables for link text
	closeLabel : 'Close window',
	openLabel : 'Open window',
	//global parameters
	closelink:null,
	
/************************************* Initialise functionality ********************************************************/
  	// Initilise the div that stores the hidden content
  	init:function()
  	{
  	
  	/****************** Checks to see if the DOM is available (the browser supports it) AND the element is available ******/
		
		// Check to see if W3C DOM is available - if not terminate script
		if(!document.getElementById || !document.createTextNode){return;}
		// Check to see if 'hidden_content' element is available. 
		// If its not the script does not need to be ran and we can terminate
		// The container var is used outside the function so we do not use the var keyword
		container = document.getElementById(fp.hiddenContainerId);
		// If the 'hidden_content' element isnt available - terminate script
		if(container)
		{
			// If the 'hidden_content' element is availabe (we've got this far , so it must be) - apply a dynamic style
			// to hide it (display:none)
			
			if(document.getElementById(fp.radioDisplayYesID).checked != true)
			{
				fp.cssjs('add', container, fp.hideClass);
			}
			
			// Initilise the link that toggles the didden content  (not needed here, radio toggels content instead)
			// fp.initDisplayLink();
			fp.initRadioButton();
		};
  	},
  	
  	/****************** Toggle Link ****************************/

  	// Build the link that toggles the divs display status
  	initRadioButton:function()
  	{
		fp.addEvent(document.getElementById(fp.radioDisplayYesID),'click',fp.applyLinkEvent,false);
		fp.addEvent(document.getElementById(fp.radioDisplayNoID),'click',fp.applyLinkEvent,false);
	},
	
	// Method used to determine wheather to hide or show content
	applyLinkEvent:function(e)
	{
		var id = fp.getTarget(e);
		if(id.getAttribute('value') == 'yes')
		{
			if(fp.cssjs('check', container, fp.hideClass))
			{
				fp.cssjs('remove', container, fp.hideClass);
			}
		}
		if(id.getAttribute('value') == 'no') // TODO finish up hiding form
		{
			if(fp.cssjs('check', container, fp.hideClass))
			{
				
			}else
			{
				fp.cssjs('add', container, fp.hideClass);
			}
		}
		//fp.cancelClick(e);
	},


/************************************* helper methods ********************************************************/
	addEvent: function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
	// cssjs mthod that adds, removes or swaps classes
	// see http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
	cssjs:function(a,o,c1,c2)
	{
		switch (a)
		{
			case 'swap':
				o.className=!fp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!fp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className)
			break;
		}
	},
	
	// Use cancelClick method to cancel 'event bubbling' and default actions associated to elements
	cancelClick:function(e)
	{
		if (window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	},
	fixSafari:function(node)
	{
//		node.onclick = function() { return false; }; // Safari
	},
	
	// New getTargetFunction (http://lists.evolt.org/archive/Week-of-Mon-20060717/183797.html)
	getTarget:function(e)
	{
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body')
		{
			target=target.parentNode;
		}
		return target;
	}
}
// addEvent is a method used to sort of stack functions up after the page has loaded.
// On this page this method is watching the window and waiting till its loaded.
// After which the fp.init object is initialized
fp.addEvent(window, 'load', fp.init, false);