/*******************************************************************************************
 * specToggle
 * @author: Steve Kirtley
 * Show/hide divs based on coin selection. Accessible fallback to anchor links.
 *******************************************************************************************/
	
	var specToggle = new function() {

		//--------------------------------------------------
		// Old browsers
			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation

			this.init = function() {
				//--------------------------------------------------
				// Debug

					console.log('specToggle.js: Initialisation');
				
				//--------------------------------------------------
				// Get Children of UL (li's with links)
				
				var spec_divs = [];
				
				if (document.getElementsByTagName) {
					var anchors = document.getElementById('product_specification').getElementsByTagName('a');
					// loop through all links, and search for any which any which have the the relevant class attributes
					for (var i=0; i<anchors.length; i++) {
						if (cssjs('check', anchors[i], 'specLnk')) {
							anchors[i].onclick = function() { return specToggle.changeCoin(this.href); };
							spec_divs[i] = anchors[i].href;
						}
					}
				}
					
				var showDefault = 0; // Ensure on first page load a spec panel is showing. (currently first in list)			
				
				this.hideAll(spec_divs, showDefault);
								
			}
			
			this.switchFlash = function(coin_number) {		
				if (document.getElementsByTagName) {
					var anchors = document.getElementById('product_specification').getElementsByTagName('a');
					var offset = coin_number - 1;
					var coin_select = anchors[offset].href;
					
					this.changeCoin(coin_select, 1); 
					
				}
			}
			
			this.changeCoin = function(href, flashState) {
				// Unset current active link state
				var specLinks = document.getElementById('product_specification').getElementsByTagName('li');
				for (var i=0; i<specLinks.length; i++) {
					if(cssjs('check', specLinks[i], 'current')) {
						cssjs('remove', specLinks[i], 'current'); // Remove current active state class
					}
					if(specLinks[i].firstChild.href == href) {
						cssjs('add', specLinks[i], 'current'); // Add correct active state class
						var linkNumber = i + 1;
					}
				}
				
				
				var spec_divs = [];
				
				if (document.getElementsByTagName) {
					var anchors = document.getElementById('product_specification').getElementsByTagName('a');
					// loop through all links, and search for any which any which have the the relevant class attributes
					for (var i=0; i<anchors.length; i++) {
						if (cssjs('check', anchors[i], 'specLnk')) {
							anchors[i].onclick = function() { return specToggle.changeCoin(this.href); };
							spec_divs[i] = anchors[i].href;
						}
					}
				}
				
				this.hideAll(spec_divs); // Hide all spec tables
				
				var showDiv = href.split("#")[1]; // Show Chosen spec table
				document.getElementById(showDiv).style.position = 'relative';
				document.getElementById(showDiv).style.left = 'auto';
				
				if(flashState != 1) {
					// Pass a parameter to the flash movie
					var flashMovie = document.getElementById('store_prodViewer');
					flashMovie.coin_num(linkNumber); // Pass Coin Change param into flash movie
				}
							
				return false; // stop anchor link being followed after execution
			}
			
			this.hideAll = function(divs, showDefault) {
				// Position all content divs off screen
				for (var k = (divs.length -1); k>= 0; k--) {
					divs[k] = divs[k].split("#")[1];
					if(document.getElementById(divs[k])) {
						if(k != showDefault) {
						document.getElementById(divs[k]).style.position="absolute";
						document.getElementById(divs[k]).style.left="-1000px";
						}
					}
				}
				
 				
			 }
			
			
		//--------------------------------------------------
		// On page load

			addLoadEvent(function() {
				specToggle.init();
			});
	
	}
