if (!nu) {
	var nu = new Object();
}

nu.Panels = Class.create();
nu.Panels.prototype = {
	
	panels: [],
	toggles: [],
	current: 0,
	toggleSaveLinks: false,
	saveLinks: [],
	duration: 0.5,
	
	initialize: function(stretcherClass, panelClass) {
		
		this.toggles = document.getElementsByClassName(stretcherClass); //div that stretches
		this.panels = document.getElementsByClassName(panelClass); // where user clicks
		
		// checking if toggels and panels exist (since they're returning an array and if the array is > 0 then run onInit)
		if (this.toggles.length > 0 && this.panels.length > 0) {
			
			this.saveLinks = document.getElementsByClassName('saveToPage');
			this.toggleSaveLinks = (this.saveLinks.length > 0);
			
			this.toggles.each(function(toggle, i){
				var setCurrent = this.setCurrent.bind(this, i);
				toggle.onclick = function(){				
					setCurrent();
					return;
				}
				this.contract(i, 0);
			}.bind(this));	
			
			this.onInit();
		}		
	},
	
	onInit: function() {},

	setCurrent: function(i) {		
		var links = this.toggles[i].getElementsByTagName('a');
		links[links.length-1].blur();
		if (this.current != i) {			
			this.contract(this.current, this.duration);
			this.expand(i, this.duration);
		}		
	},
	
	expand: function(i, d) {
		var links = this.toggles[i].getElementsByTagName('a');
		links[links.length-1].className = 'activehead';	
		new Effect.BlindDown(this.panels[i], {duration: d});
		new Effect.Opacity(this.panels[i], {to: 0.99, from:0, duration: d});
		this.onExpand(i);		
		this.current = i;
	},
	
	onExpand: function(i) {},
		
	contract: function(i, d) {
		var links = this.toggles[i].getElementsByTagName('a');
		links[links.length-1].className = '';
		this.panels[i].style.height = 'auto';
		this.panels[i].style.height = this.panels[i].getHeight() + "px";
		new Effect.BlindUp(this.panels[i], {duration: d});
		new Effect.Opacity(this.panels[i], {to: 0, from:0.99, duration: d});
		this.onContract(i);
	},
	
	onContract: function(i) {}
}


nu.MainPanels = Class.create();
Object.extend(Object.extend(nu.MainPanels.prototype, nu.Panels.prototype), {
		
	onInit: function() {		
		if (!this.checkHash()) {
			this.expand(0,0);
		}
	},
		
	checkHash: function() { 
		var found = false;
		this.toggles.each(function(toggle, i){	
			if (window.location.href.indexOf('#section'+(i+1)) > 0) {	
				this.expand(i,0);
				found = true;
			}								
		}.bind(this));
		return found;
	},
	
	onExpand: function(i) {
		if (this.toggleSaveLinks) {
			this.saveLinks[i].style.display = "block";
		}
		// show bespoke banners
		if ($('bannerBespoke' + (i+1))) {
			$('bannerBespoke' + (i+1)).style.display = 'block';
		}		
	},
	
	onContract: function(i) {
		if (this.toggleSaveLinks) {
			this.saveLinks[i].style.display = "none";
		}
		// hide bespoke banners
		if ($('bannerBespoke' + (i+1))) {
			$('bannerBespoke' + (i+1)).style.display = 'none';
		}
	}
}); 


nu.SidePanels = Class.create();
Object.extend(Object.extend(nu.SidePanels.prototype, nu.Panels.prototype), {
	
	onInit: function() {
		if (!this.checkPage()) {
			this.expand(0,0);
		}
	},
		
	checkPage: function() {
		var foundUrl = false;
		this.toggles.each(function(toggle, i){
			var sectionNameBefore = toggle.getElementsByTagName('a')[0].href.split('#')[1].replace(/\?/g, '');
			var sectionName = sectionNameBefore.replace(/ /g, '-');
			if (window.location.href.toLowerCase().indexOf('/' + sectionName.toLowerCase() + '/') != -1) {
				this.expand(i,0);
				foundUrl = true;
			}
		}.bind(this));
		return foundUrl;
	}
}); 

function initMainPanels(){	
	new nu.MainPanels('panelTitle', 'panel');
}

function initMainPanelsBasic() {
	new nu.MainPanels('panelTitle', 'panel');
}
/*
function initSmallPanels(){	
	new nu.SidePanels('panelSmallTitle', 'panel');
}
*/
function initSubPanels(){
    if (document.URL.indexOf('/Guide-me/') == -1)
	    new nu.SidePanels('subNavTitle', 'subNavPanel');
}
function initNeedsPanels(){ 
	new nu.MainPanels('needsGoalContainer', 'needsPanel')	
}