// JavaScript Document



/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/

	document.observe('dom:loaded', function(){

		if ($('captcha'))
		{
			$('captcha').writeAttribute("autocomplete", "off" );
		}
		
		
		$$('div.featuresPanel').each(function(el) {
			el.innerHTML = '<div class="inner">'+el.innerHTML+'</div>';									  
		});	
		
		$$('div.featuredProject a.link').each(function(el) {
			if(el.href)
			{
				
				var desc = el.next();
				desc.insert({bottom: '<a href="'+el.href+'"><span>Read about this project »</span></a>'});
			}
		});
		
		// There is no way to centrally align a floated element, this js fixes that
		$$('.fCenter').each(function(el){
			el.setStyle({'cssFloat': 'left'});						 
			var diff = el.parentNode.getWidth() - el.getWidth();
			diff = Math.round(diff / 2);
			el.setStyle({'marginLeft': diff+'px'});
		});
		
	});
	
	

/*--------------------------------------------------------------------------------
	Features overview - Builds an anchored list of contents using the titles
	of all of the features.
--------------------------------------------------------------------------------*/

	document.observe('dom:loaded', function(){
		
		$$('div.featuresOverview').each(function()
		{
		
			var str = '<h3>Contents</h3>';
			str += '<div class="contents clear box1">';
			
			var feature_count = 0;
			var first_module = '';
			
			// Loop through all module headings
			$$('div.featuresOverview h3').each(function(element, key1)
			{
				if (key1 == '')
				{
					first_module = element;
				}
							
				var class_var = '';
				if (key1 % 2 != 0)
				{
			//		class_var = ' end';	
				}
				
				
				str += '<div class="item' + class_var + '">';
					str += '<h4>' + element.innerHTML + '</h4>';
					str += '<ul>';
					
					// Loop through all features headings
					var ul = element.next('ul');
					
					ul.addClassName('clear box1');
					
					ul.select('li').each(function(li, key2)
					{
						if (key2 % 2 != 0)
						{
							li.addClassName('end');
						}
						
						var heading = li.down();
						
						feature_count++;
						var title = heading.innerHTML;
						var html = '<a name="f' + feature_count + '"></a>';
						Element.insert(heading, {before: html});
						str += '<li><a href="#f' + feature_count + '">' + title + '</a></li>';
					});
					
					str += '</ul>';
				str += '</div>';
			});
			
			str += '</div>';
			
			Element.insert(first_module.up(), {before: str});
		});
	});
			
	
	
	
	
	
	

/*--------------------------------------------------------------------------------
	
	
	CMS CORE SCRIPTS - The following core scripts are used by features of the CMS
	Removing any of these scripts may prevent certain features from working.
	
	
--------------------------------------------------------------------------------*/

	document.observe('dom:loaded', function(){
	
	/*--------------------------------------------------------------------------------
		Sets <a> links that have a class name 'blank' to open in a new window.
	--------------------------------------------------------------------------------*/

		$$('a.blank').each(function(element){
			element.onclick = function()
			{
				window.open(this.href);
				return false;
			}
			element.onkeypress = function()
			{
				window.open(this.href);
				return false;
			}
		});
	
	/*--------------------------------------------------------------------------------
		Applies functionality to the HTML of the features tab
	--------------------------------------------------------------------------------*/

		if ($('features'))
		{
			
			// Set the title attribute of each feature link with it's innerHTML  
			$$('#features li a').each(function(element){
				element.title = element.innerHTML;
			});

			// Display the print icon and set it's onclick event to open the browser's print dialog window.
			var print_element = $('features').select('li.print')[0];
			if (print_element)
			{
				print_element.style.display = 'block';
				print_element.onclick = function() { print(); }
			}

			// Display the bookmark icon and sets it's onclick event to open the browser's bookmark dialog window.
			var bookmark_element = $('features').select('li.bookmark')[0];
			if (bookmark_element)
			{
				bookmark_element.style.display = 'block';
				bookmark_element.onclick = function() { 
				
					var url = window.location.href; 
					var title = window.document.title;
					
					// Firefox
					if (window.sidebar)
					{
						window.sidebar.addPanel(title, url,"");
					}
					
					// Opera
					else if (window.opera && window.print)
					{
					   var elem = document.createElement('a');
					   elem.setAttribute('href', url);
					   elem.setAttribute('title', title);
					   elem.setAttribute('rel', 'sidebar');
					   elem.click();
					} 
					
					// IE
					else if (document.all)
					{
					   window.external.AddFavorite(url, title);
					}
					
					return false;
				
				}
			}

			// Displays the decrease font size icon and sets its onclick event to decrease the document's font size
			var font_size_element = $('features').select('li.fontSizeIncrease')[0];
			if (font_size_element)
			{
				font_size_element.style.display = 'block';
				font_size_element.onclick = function() {
					if (document.body.style.fontSize == "") 
					{
						document.body.style.fontSize = "0.8em";  
					}
					document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (0.2) + "em";
					createCookie("fontSize", document.body.style.fontSize, 30);
					
					return false;
				}
				
				// Load any preset font size
				if (readCookie("fontSize"))
				{
					document.body.style.fontSize = readCookie("fontSize");
				}
			}

			// Displays the increase font size icon and sets its onclick event to increase the document's font size
			var font_size_element = $('features').select('li.fontSizeDecrease')[0];
			if (font_size_element)
			{
				font_size_element.style.display = 'block';
				font_size_element.onclick = function() {
					if (document.body.style.fontSize == "") 
					{
						document.body.style.fontSize = "0.8em";  
					}
					document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (-0.2) + "em";
					createCookie("fontSize", document.body.style.fontSize, 30);
					
					return false;
				}
			}
		}
		
		
		
	/*--------------------------------------------------------------------------------
	--------------------------------------------------------------------------------*/
	
		$$('div.document_tree').each(function(document_tree) {
											  
			var html = ''; // The generated HTML to be added to the document tree div
			var previous_heading = 0; // The heading type. H1 = 1, H2 = 2.
			
			$$('#column1').each(function(div) {
			
				var descendants = div.descendants();
				for (n in descendants)
				{
					var element = descendants[n];
					
					if (element == '[object HTMLHeadingElement]')
					{
						var heading = element.nodeName.replace('H', '');
						heading = parseInt(heading);
						
						if (previous_heading == 0)
						{
							previous_heading = heading;
							html += '<ul>';
						}
						else if (heading > previous_heading)
						{
							num_uls = heading - previous_heading;
							for (i = 0; i < num_uls; i++)
							{
								html += '<ul>';
							}
						}
						else if (heading < previous_heading)
						{
							num_uls = previous_heading - heading;
							for (i = 0; i < num_uls; i++)
							{
								html += '</ul>';
							}
						}
						
						previous_heading = heading;
						
						// Determine the heading element's ID, used for the anchor and its link.
						var anchor_name = element.identify();
						
						// Create the anchor and insert it above the heading
						var a_obj = document.createElement('a');
						a_obj.setAttribute('name', anchor_name);
						element.insert({before: a_obj});
						
						// Create the HTML for the anchor link
						var str = element.innerHTML.replace(/(<([^>]+)>)/ig, ''); 
						html += '<li><a href="#' + anchor_name + '">' + str + '</a></li>';
					}
				}
				
				// Close all open <ul> tags
				if (previous_heading > 0)
				{
					for (i = 0; i < previous_heading; i++)
					{
						html += '</ul>';
					}
				}
				
				document_tree.innerHTML = html;
			
			});
			
		});	
	
	/*--------------------------------------------------------------------------------
	--------------------------------------------------------------------------------*/
	
	});
	
	
	
	
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/

	function createCookie(name,value,days) 
	{
		if (days) 
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else 
		{	
			var expires = "";
		}
		
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		
		return null;
	}
	
	function eraseCookie(name) 
	{
		createCookie(name,"",-1);
	}



