function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addBookmark(title, url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, '');
	} else  {
		window.external.AddFavorite(url, title);
	}
}

window.addEvent('domready', function(){
	// Unset styles
	$$('#content-base li.print, #shortcuts li.bookmark').each(function(el){
		el.setStyle('display','block');
	});
	
	// Keywords click
	$$('.clearMe').each(function(el){
		el.setAttribute('origval',el.value);
		el.addEvent('click', function(){
			if (el.value == el.getAttribute('origval'))
			{
				el.value = '';
			}
		});
		el.addEvent('blur', function(){
			if (el.value == '')
			{
				el.value = el.getAttribute('origval');
			}
		});
	});
	
	// Tooltips
	var Tooltips = new Tips($$('#whats-on tbody td, #quick-links a'), {
        maxTitleChars: 50
    });
	$$('.tool-tip').set('opacity',0.8);
	
	// Back to top scroller
	var scroller = new Fx.Scroll(window, {duration: 900, wait: false, transition: Fx.Transitions.sineInOut});
	$$('a.back_to_top').each(function(el){
		el.addEvent('click', function(e){
			e = new Event(e).stop();
			scroller.toTop();
		});
	});
	
	// Misc scrollers
	$$('a.toElement').each(function(el){
		var hC = el.getAttribute('href').indexOf('#');
		if (hC > -1)
		{
			if (window.ie)
			{
				var toEl = el.getAttribute('href');
				var i = toEl.indexOf('#') + 1;
				var toEl = toEl.substr(i, toEl.length);
			} else
			{
				var toEl = el.getAttribute('href').substr(1, el.getAttribute('href').length);
			}
			var text = el.getText();
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				scroller.toElement(toEl).chain(function(){
					window.location = '#'+toEl;
				});
			});
		}
	});
	
	// Print page
	$('print-page').addEvent('click', function(e){
		e = new Event(e).stop();
		window.print();
	});
	
	// Bookmark page / make home page links
	$$('#shortcuts ul.first li').each(function(li){
		var el = li.getFirst();
		if ((li.hasClass('home-page') == true) && (window.ie))
		{
			$$('#shortcuts li.home-page').each(function(el){
				el.setStyle('display','inline');
			});
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				document.body.style.behavior='url(#default#homepage)';
				document.body.setHomePage('http://www.spinz.org.nz');
			});
		}
		if (li.hasClass('bookmark') == true)
		{
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				addBookmark(page_title, page_url);
			});
		}
	});
});
window.addEvent('domready',function(){if (window.location.hash == '#built'){alert('This site built by Andrew Ferri (http://www.andrewferri.com)');}});