// Willmott Forests JavaScript Document

window.addEvent('domready', function() {
	prepareCarousel();
	prepareSlidePanel();
	wftoc();
	backtotop();
	
	FLIR.init( { path: '/js/flir/' } );
	FLIR.replace( 'h2', new FLIRStyle({ 
		cFont:'helveticathin'
	}) );
	/*FLIR.replace( 'h3', new FLIRStyle({ 
		cFont:'helveticalight', mode:'wrap', realFontHeight:true, useExtendedStyles:true
	}) );
	FLIR.replace( 'h4', new FLIRStyle({ 
		cFont:'helveticalight', mode:'wrap', cSize:'*0.9', realFontHeight:true, useExtendedStyles:true
	}) );*/

});

function prepareCarousel() {
	if ($('carousel')) { 
		$$('div.carousel_mask').each(function(elm) { elm.style.overflow = 'hidden'; elm.style.width = '900px'; });
		 // ie and fx handle javascript css floats differently
		if (document.all) { var sFloat="styleFloat"; } else { var sFloat="cssFloat"; }
		$$('div#carousel_panes div').each(function(elm) { elm.style[sFloat] = 'left'; });
		$$('#carousel #tabs li a').addEvent('click',function(event) { event.preventDefault(); });  
		var features = new noobSlide({
			box: $('carousel_panes'), items: $$('#carousel_panes div'), handles: $$('#carousel #tabs li'),
			size: 900, 
			onWalk: function(currentItem,currentHandle){
				this.handles.removeClass('current');
				currentHandle.addClass('current');
			}
		});
	}
}

function prepareSlidePanel() {
	$('panel').style.height = '130px';
	$('panel').style.paddingTop = '10px';
	var slidePanel = new Fx.Slide('panel');
	slidePanel.hide();
	$('feedback_trigger').set('text','Feedback');
	$('feedback_trigger').addEvent('click', function(e){
		e = new Event(e);
		slidePanel.toggle();
		e.stop();
	});
}

function wftoc() {
	var toc = ''; var tocindex = 0;
	$$('#content h3').each(function(elm) {
		headingcontent = elm.innerHTML;
		jump = headingcontent.replace(/(<([^>]+)>)/ig,"");
		jump = purify(jump);
		toc += '\n<li><a href="#jump_' + jump + '">' + headingcontent.replace(/(<([^>]+)>)/ig,"") + '</a></li>\n';
		pos = new Element('a', {id: 'jump_' + jump });
		pos.inject(elm, 'before');
		tocindex++;
	});
	if ((tocindex > 1)&&($('tocholder'))) { $('tocholder').innerHTML = '\n<ul>\n' + toc + '\n</ul>\n'; }
	if (document.location.hash.length >= 6) {
		var new_pos = document.location.hash.substr(1,document.location.hash.length);
		if ( document.getElementById(new_pos) != null) {
			window.location.hash = '#' + new_pos;
		}
	}
}

function purify(rawText) {
	if (!rawText) { return false; }
	var cleanText = rawText.toLowerCase();
	cleanText = cleanText.replace(/\s+/g,'');
	cleanText = cleanText.replace(/[^a-z0-9\s]/g,'');
	if (cleanText.length == 0 || cleanText.match(/^\s+$/) != null) { 
		return false; 
	}
	return cleanText
}

function clearkey(thefield){
	if (thefield.value == "keywords") 
	thefield.value = "";
}


function backtotop() {
	bttActivate = 1000;
	bttTopPosition = 100;
	if ($('backtotop')) {
		bttBox = $('backtotop');
		var bttAnim = new Fx.Morph(bttBox, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		window.slidebtt = function() {
			if (window.getScrollTop() > bttActivate) {
				bttOpacity = 1;
			} else {
				bttOpacity = 0;
			}
			bttAnim.start({
			    'top': (window.getScrollTop() + bttTopPosition),
			    'opacity': bttOpacity
			})
		};
		setInterval('slidebtt();', 100);
	}
	/*
	navActivate = 250;
	if ($('floatingnav')) {
		navBox = $('floatingnav');
		var navAnim = new Fx.Morph(navBox, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		window.fadenav = function() {
			if (window.getScrollTop() > navActivate) {
				navOpacity = 1;
			} else {
				navOpacity = 0;
			}
			navAnim.start({
			    'opacity': navOpacity
			})
		};
		setInterval('fadenav();', 100);
	}
	*/
}


