// Include all objects that should run on window load
var initialize = {
	functionsToLoad:function() {
		latest.init();
		flash.fade();
	}
}

// Fade flash message after 3 seconds
var flash = {
	// Flash Timer
	seconds: 3.0,
	fade : function() {
		var message = $('flashMessage');
		var login_error = $('login_error');
		if (!message && !login_error) { return; }
		if (message) {
			new Effect.Fade(message, { duration: flash.seconds });
		}
		if (login_error) {
			new Effect.Fade(login_error, { duration: flash.seconds });
		}
	}	
}

// Toggle for recent articles and podcasts
var latest = {
	// css classes
	dynamicClass:'dyn',
	currentLinkClass:'current',
	showClass:'show',

	// IDs
	parentID:'latest_content',
	tocID:'latest_tabs',

	// global properties
	current:null,
	sections:[],
	sectionLinks:[],
	init:function(){
		var targetName,targetElement;
		if(!document.getElementById || !document.createTextNode){return;}
		var parent=document.getElementById(latest.parentID);
		var toc=document.getElementById(latest.tocID);
		if(!parent || !toc){return;}
		DOMhelp.cssjs('add',parent,latest.dynamicClass);

		var loc=window.location.hash.replace('#','');
		var toclinks=toc.getElementsByTagName('a');
		var valid_hash = false;
		for(var i=0;i<toclinks.length;i++){
			DOMhelp.addEvent(toclinks[i],'click',latest.getSection, false);
			targetName=toclinks[i].getAttribute('href').replace(/.*#/,'');
			// If this link matches the url hash, set valid hash to true
			if (targetName == loc) { valid_hash = true; }
			toclinks[i].targetName=targetName;
			if(i==0){var presetLink=targetName}
			targetElement=document.getElementById(targetName);
			if(targetElement){
				latest.sections[targetName]=targetElement;
				latest.sectionLinks[targetName]=toclinks[i];
			}
		}
		// If a relevant hash exists return it, otherwise return the preset hash
		loc = valid_hash ? loc : presetLink;
		latest.showSection(loc);
	},
	getSection:function(e){
		var t=DOMhelp.getTarget(e);
		DOMhelp.cancelClick(e);
		latest.showSection(t.targetName);
	},
	showSection:function(sectionName){
		if(latest.current!=null){
			DOMhelp.cssjs('remove',latest.sections[latest.current],latest.showClass);
			DOMhelp.cssjs('remove',latest.sectionLinks[latest.current],latest.currentLinkClass);
		}
		DOMhelp.cssjs('add',latest.sections[sectionName],latest.showClass);
		DOMhelp.cssjs('add',latest.sectionLinks[sectionName],latest.currentLinkClass);
		latest.current=sectionName;
	}
}

// Object Events that run on page load
DOMhelp.addEvent(window,'load',initialize.functionsToLoad,false);


