function createCookie(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(3*24*60*60*1000)); //3 days
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie() {
	var nameEQ = "menus=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

document.observe("dom:loaded", function() {
//draw arrows on the nav
var x = $$('#secondary-navigation li.open > ul').size();
if (x > 0) { $$('#secondary-navigation li.open').first().addClassName('arrow') }

//link to hide/show services menu
var x = $('services-nav-first');
if (x) {
	link = new Element('div');
	link.writeAttribute('id','hide');
	link.innerHTML = '<span>Hide menu</span>'; 
	x.insert({ 
		after : link
	});

	//find menu cookie
	if (readCookie() == 'h') {
		$$('#services-nav-first > div')[0].hide();
		$$('#hide span')[0].innerHTML = 'Show services menu';
		$('hide').addClassName('show');
	}
	
	$('hide').observe('click',function(e) {
		Effect.toggle($$('#services-nav-first > div')[0],'blind', {
			duration:0.3,
			afterFinish:function(){
				if ($('hide').hasClassName('show')) {
					$$('#hide span')[0].innerHTML = 'Hide menu';
					$('hide').removeClassName('show');
					createCookie('menus','v');
				} else {
					$$('#hide span')[0].innerHTML = 'Show services menu';
					$('hide').addClassName('show');
					createCookie('menus','h');
				}
			}
		})
	})
}

//clear the search box when clicked
$$('#search-box .text')[0].observe('focus',function() {
	this.value = '';
});

$$('#search-box .text')[0].observe('blur',function() {
	this.value = this.value == '' ? 'Search' : this.value;
});

//glossary bling
$$('dl.jargon-section a, #index a').each(function(x) {
	var link = x.readAttribute('href');
	var _link = link.substr(1);
	
	//only scroll to anchors that exist
	$(_link) &&	x.observe('click',function(e) {
		e.stop();
		Effect.ScrollTo(_link,{
				duration:0.4,
				afterFinish : function () {window.location.href = link}
		});
	});
});

$$('a.link-top').each(function(x) {
	var link = x.readAttribute('href');
	x.observe('click',function(e) {
		e.stop();
		Effect.ScrollTo('wrapper',{
			duration:0.4,
			afterFinish : function () {window.location.href = link}
		})
	})
});

});
