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 outputFlash(src, width, height, img, version)
{
	if(flashinstalled == 2)
	{
		AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width',width,'height',height,'src',src,'quality','high','wmode','transparent','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie',src ); //end AC code
	} else
	{
		document.write('<img src="'+img+'" alt=""/>');
	}
}*/

function addBookmark(title, url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, '');
	} else  {
		window.external.AddFavorite(url, title);
	}
}

popup_id = 1;
function popup(url, width, height)
{
	var left = (window.getWidth()-width)/2;
	var top = 100;
	window.open(url, 'p_'+popup_id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizeable=0,width='+width+',height='+height+',left='+left+',top='+top);
	popup_id++;
}

window.addEvent('domready', function(){
	// Remove "Login" link from top nav
	//$('menu').getElement('ul').getLast().destroy();
	//$('menu').setStyle('left',90);
	
	// Unset styles
	$$('#content-base li.print, #shortcuts li.bookmark').each(function(el){
		el.setStyle('display','block');
	});

	// Select box fix for IE
	if (window.ie){
		$$('select.fixed-width').each(function(el){
			//el.setAttribute('disabled','disabled');
			var selectOptionsContainer = new Element('ul', {
				'class': 'selectOptions hidden'
			});
			var li = new Element('li');
			var counter = 0;
			el.getChildren().each(function(c){
				var nLi = li.clone();
				nLi.setAttribute('index',counter);
				nLi.setText(c.getText());
				nLi.injectInside(selectOptionsContainer);
				counter++;
				nLi.addEvent('click', function(){
					el.getChildren().removeClass('active');
					nLi.addClass('active');
					el.selectedIndex = nLi.getAttribute('index');
					selectOptionsContainer.addClass('hidden');
					window.location = el.value;
				});
				nLi.addEvent('mouseenter', function(){
					nLi.addClass('hover');
				});
				nLi.addEvent('mouseleave', function(){
					nLi.removeClass('hover');
				});
				selectOptionsContainer.addEvent('blur', function(){
					selectOptionsContainer.addClass('hidden');
				});
			});
			selectOptionsContainer.injectAfter(el);
			el.addEvent('click', function(e){
				el.setAttribute('disabled','disabled');
				el.setAttribute('disabled','');
				if (selectOptionsContainer.hasClass('hidden') == true)
				{
					el.blur();
					selectOptionsContainer.removeClass('hidden');
					selectOptionsContainer.focus();
				} else
				{
					selectOptionsContainer.addClass('hidden');
				}
			});
		});
	}

	// Text size switcher
	/*$('font-size-tool').addEvent('click', function(e){
	e = new Event(e).stop();
		if (readCookie('font-size') != 'large')
		{
			createCookie('font-size','large',365);
			$('body').addClass('large');
			$('font-size-tool').setText('Decrease text');
		} else
		{
			eraseCookie('font-size');
			$('body').removeClass('large');
			$('font-size-tool').setText('Increase text');
		}
	});*/
	
	// Keywords click
	$$('.clearMe').each(function(el){
		el.setAttribute('origval',el.value);
		el.addEvent('focus', function(){
			if (el.value == el.getAttribute('origval'))
			{
				el.value = '';
			}
		});
		el.addEvent('blur', function(){
			if (el.value == '')
			{
				el.value = el.getAttribute('origval');
			}
		});
	});
	
	// What's on calendar / Gallery tooltips
	var whatsOnTips = new Tips($$('#whats-on tbody td, #gallery li a, ul.titles li a'), {
        maxTitleChars: 50
    });
	$$('.tool-tip').setOpacity(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
	var print_page = $('print-page');
	if(print_page !== null)
	{
		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.mentalhealth.org.nz');
			});
		}
		if (li.hasClass('bookmark') == true)
		{
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				addBookmark(page_title, page_url);
			});
		}
	});
	
	// Popups
	$$('a.popup').each(function(el){
		el.addEvent('click', function(e){
			e = new Event(e).stop();
			var size = el.rel.split('x');
			popup(el.href, size[0], size[1]);
		});
	});
	
	// Poll validation
	if ($('poll-box'))
	{
		pollOption = false;
		$$('#POLL_FORM input.radioCheck').each(function(el){
			el.addEvent('click', function(){
				pollOption = true;
			});
		});
		$('POLL_FORM').addEvent('submit', function(e){
			if (pollOption == false)
			{
				e = new Event(e).stop();
			}
		});
	}
	
	if ($('poll-results'))
	{
		$$('#poll-results div.result-bar img').each(function(el){
			var w = el.width;
			el.width = 0;
			var fx = new Fx.Morph(el,{duration: 1600, transition: Fx.Transitions.Quad.easeOut});
			fx.start({'width':w});
			//var fx = new Fx.Tween(el,'width',{duration: 1600, transition: Fx.Transitions.Quad.easeOut}).start(w);
		});
	}
});
