
/*
Written by Steve Tucker, 2006
http://www.stevetucker.co.uk

I have heavily commented the prepareHiddenInformation function to help you understand its mechanics.
If you are new to Javascript and particularly programming languages in general then you may still
find it all somewhat confusing - but don't give up! These things take time to learn, and once you
have done so you will be an all round stronger programmer for it :)

For best legibility set your tab size to 8 spaces when reading the contents of this document.


function prepareHiddenInformation() {
	if (!document.getElementsByTagName) return false;	// Checks to make sure this function exists. Halts execution of script if not
	if (!document.getElementById) return false;	// Checks to make sure this function exists. Halts execution of script if not
	if (!document.getElementsByClassName('hidden_content')) return false;	// Checks to make sure this function exists. Halts execution of script if not
	
	var $hidden_content_boxes = document.getElementsByClassName('hidden_content');	// Gets list of all elements with class of 'hidden_content'
	for (var $i=0; $i<$hidden_content_boxes.length; $i++) {	// loops through the whole list...
		var $hidden_content_box = $hidden_content_boxes[$i];	// allocates each element to a variable (for easier reading)
		
		var $link = document.createElement('a');	// creates the toggle action link
		$link.onclick = function() {	// give the link an action when it is clicked. This action is as follows:
			if (this.getAttribute('status') == 'open') {	// if the link has a 'status' (internal variable) of 'open' the continue to close it, otherwise skip next 9 lines
				$hidden_content_box_id = this.getAttribute('rel');	// get the id of the box to be hidden from the link which has been clicked (each link is associated to a box)
				$hidden_content_box = document.getElementById($hidden_content_box_id);	// get the actual element to be hidden by its ID
				$hidden_content_box.style.display = 'none';	// give this element a CSS declaration of display:none; to hide it
				
				this.setAttribute('status','closed');	// update the link's status variable to 'closed'
				var $old_text = this.childNodes[0];	// get the old text (within the link element)
				this.removeChild($old_text);	// delete this old link text
				var $text = document.createTextNode('Products  >>>');	// create new text saying 'More Information >>>'
				this.appendChild($text);	// Insert the new text within the link element
			}
			else {	// if the link does not have a 'status' (internal variable) of 'open' then it must be closed already. Continue to open it, otherwise skip next 9 lines
				$hidden_content_box_id = this.getAttribute('rel');	// get the id of the box which is no longer to be hidden from the link which has been clicked (each link is associated to a box)
				$hidden_content_box = document.getElementById($hidden_content_box_id);	// get the actual element no longer to be hidden by its ID
				$hidden_content_box.style.display = 'block';	// give this element a CSS declaration of display:block; to show it
				
				this.setAttribute('status','open');	// update the link's status variable to 'open'
				var $old_text = this.childNodes[0];	// get the old text (within the link element)
				this.removeChild($old_text);	// delete this old link text
				var $text = document.createTextNode('Hide products');	// create new text saying 'Less Information <<<'
				this.appendChild($text);	// Insert the next text within the link element
			}
		}

		$link.setAttribute('title','');	// Give the link a non-specific title attribute
		$link.className = 'information_link';	// Give the link a class (for CSS styling)
		var $hidden_content_box_id = 'hidden_content'+$i;	// Create unique string 'hidden_content#' where '#' represents a unique integer and assign it to a variable
		$hidden_content_box.setAttribute('id',$hidden_content_box_id);	// Set the unique ID of the content as the unique value (above).
		$link.setAttribute('rel',$hidden_content_box_id);	// Set the 'rel' attribute with the same unique ID as above. This creates our associative connection between the link and the box it controls.
		var $text = document.createTextNode('Products  >>>');	// Create a text string 'More Information >>>'. This will act as a default link text when inserted into the link (below)
		$link.appendChild($text);	// Set the link text from the string (above)
		var $parent = $hidden_content_box.parentNode;	// Get the content box's parent element and assign it to a variable
		$parent.insertBefore($link,$hidden_content_boxes[$i]);	// Insert the link into the parent element, directly before the content box which will be hidden
	}
}



// Function to return a list of elements with a specific class attribute
document.getElementsByClassName = function($class) {
	var $results = Array();
	var $elements = document.getElementsByTagName("*");
	for (var $i=0; $i<$elements.length; $i++) {
		var $classes = $elements[$i].className.split(" ");
		for (var $j=0; $j<$classes.length; $j++) {
			if ($classes[$j] == $class) {
				$results[$results.length] = $elements[$i];
			}
		}
	}
	return $results;
}


// In order to pass the safety checks this section must appear AFTER the getElementsByClassName function (above)
if (document.getElementsByTagName && document.getElementsByClassName) {
	// Create a link to the Javascript-only stylesheet, which will primarily hide the hidden text
	var $link_element = document.createElement('link');
	$link_element.setAttribute("rel","stylesheet");
	$link_element.setAttribute("href","javascript_only.css");
	$link_element.setAttribute("media","screen");
	
	// Append this stylesheet link to the document
	var $head_element = document.getElementsByTagName('head')[0];
	$head_element.appendChild($link_element);
}

// Executes the prepareHiddenInformation function when page loads
window.onload = function() {
	prepareHiddenInformation();
}
*/

		/********************/

/* Tiny Scrolling - a smooth navigation between internal links and their destinations
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/tiny-scrolling
based on the works by Travis Beckham and Brian McAllister.
                v0.3 - March 27, 2006
*/

// Executes the prepareHiddenInformation function when page loads
window.onload = function() {
	tinyScrolling.init();
}


var tinyScrolling = {
	speed : 100,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 150,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedY:0,
	init: function() {
			var lnks = document.getElementsByTagName('a');   
			for(var i = 0, lnk; lnk = lnks[i]; i++) {   
				if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
				('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
				lnk.onclick = tinyScrolling.initScroll;   		
				}   
			}
	},
	getElementYpos: function(el){
			var y = 0;
			while(el.offsetParent){  
				y += el.offsetTop    
				el = el.offsetParent;
			}	return y;
	},		
	getScrollTop: function(){
			if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			else return window.pageYOffset;   
	},	
	getWindowHeight: function(){
			if (window.innerHeight)	return window.innerHeight;
			if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	},
	getDocumentHeight: function(){
			if (document.height) return document.height;
			if(document.body.offsetHeight) return document.body.offsetHeight;
	},
	initScroll: function(e){
			var targ;  
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;   
			tinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
			tinyScrolling.currentBlock = document.getElementById(tinyScrolling.hash);   
			if(!tinyScrolling.currentBlock) return;
			tinyScrolling.requestedY = tinyScrolling.getElementYpos(tinyScrolling.currentBlock); 
			tinyScrolling.scroll();  
			return false;
	},
	scroll: function(){
			var top  = tinyScrolling.getScrollTop();
			if(tinyScrolling.requestedY > top) {  
				var endDistance = Math.round((tinyScrolling.getDocumentHeight() - (top + tinyScrolling.getWindowHeight())) / tinyScrolling.brakeK);
				endDistance = Math.min(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK), endDistance);
				var offset = Math.max(2, Math.min(endDistance, tinyScrolling.maxStep));
			} else { var offset = - Math.min(Math.abs(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK)), tinyScrolling.maxStep);
			} window.scrollTo(0, top + offset);  
			if(Math.abs(top-tinyScrolling.requestedY) <= 1 || tinyScrolling.getScrollTop() == top) {
				window.scrollTo(0, tinyScrolling.requestedY);
				if(!document.all || window.opera) location.hash = tinyScrolling.hash;
				tinyScrolling.hash = null;
			} else 	setTimeout(tinyScrolling.scroll,tinyScrolling.speed);
	}		
}
