function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

window.addEvent('domready', function(){
	// IE navigation click fix
	if (window.ie)
	{
		$$('#navigation span.image img').each(function(el){
			el.addEvent('click', function(){
				var loc = el.getParent().getParent().href;
				window.location = loc;
			});
		});
	}
									 
	// Contact page stuff
	if ($('contact'))
	{
		map = $$('#nz_map img');
		
		h2s = $$('#contact_details h2');
		
		h2s.each(function(h2){
			h2.setStyle('display', 'none');
			h2.getNext().setStyle('display', 'none');
		});
		
		//h2s[0].setStyle('display', 'block');
		//h2s[0].getNext().setStyle('display', 'block');
		
		links = $$('#nz_map area');
		
		links.each(function(el){
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				
				h2s.each(function(h2){
					h2.setStyle('display', 'none');
					h2.getNext().setStyle('display', 'none');
				});
				
				var href = el.getAttribute('href').substr(el.getAttribute('href').indexOf('#')+1, el.getAttribute('href').length);

				h2s.each(function(h2){
					var test = h2.getFirst().getAttribute('name');
					if (test == href)
					{
						h2.setStyle('display', 'block');
						h2.getNext().setStyle('display', 'block');
					}
				});
			});
		});
	}
	
	// IE6 navigation hover fix
	
	if (window.ie6){
		var zindex = 0;
		$$('#navigation li').each(function(el){
			el.addEvent('mouseenter', function(){
				el.addClass('hover');
			});
			el.addEvent('mouseleave', function(){
				el.removeClass('hover');
				el.setStyle('display', 'none');
				el.setStyle('display', 'inline');
			});
			zindex++;
		});
		$$('#navigation li').each(function(el){
			el.setStyle('z-index', zindex);
			zindex--;
		});
		$$('#navigation li li').each(function(el){
			el.removeEvents();
		});
	}
	
	// search box input clearing
	$$('input.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 == null))
			{
				el.value = el.getAttribute('origval');
			}
		});
	});
	
	// back to top link
	var scroller = new Fx.Scroll(window, {duration: 900, wait: false, transition: Fx.Transitions.sineInOut});
	$('back_to_top').addEvent('click', function(e){
		e = new Event(e).stop();
		scroller.toTop();
	});
	
	// print link
	$('print_link').addEvent('click', function(e){
		e = new Event(e).stop();
		window.print();
	});
	
	// on-the-fly font size change
	$$('#font_size a').each(function(el){
		el.addEvent('click', function(e){
			e = new Event(e).stop();
			if (el.hasClass('small') == true){
				setCookie('font_size', 'regular');
				$$('body').removeClass('larger');
				$$('#font_size a.small').addClass('active');
				$$('#font_size a.large').removeClass('active');
				$$('#font_size img').each(function(el){
					var src = el.getAttribute('src');
					src = src.replace('large', 'small');
					el.setAttribute('src', src);
				});
			} else{
				setCookie('font_size', 'large');
				$$('body').addClass('larger');
				$$('#font_size a.small').removeClass('active');
				$$('#font_size a.large').addClass('active');
				$$('#font_size img').each(function(el){
					var src = el.getAttribute('src');
					src = src.replace('small', 'large');
					el.setAttribute('src', src);
				});
			}
		});
	});
	
	// image preload
	var preload = new Array();
	preload.push(site_root+'/component/graphics/navigation/1_hover_bg.gif');
	preload.push(site_root+'/component/graphics/navigation/1_hover_left.gif');
	preload.push(site_root+'/component/graphics/navigation/1_hover_right.gif');
	preload.push(site_root+'/component/graphics/navigation/2_hover_bg.png');
	new Asset.images(preload);
});

window.addEvent('load', function(){
/***************************************
/  PNG transparency fix for IE 6
/**************************************/
	if ((window.ie6) && (document.body.filters)) 
	{
		$$('img').each(function(img){
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px !important; height:" + img.height + "px !important;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='crop');\"></span>" 
				img.outerHTML = strNewHTML
			}
		});
	
		$$('#navigation li li a').each(function(el){
			el.getFirst().getNext().setStyle('opacity', 0.9);
		});
	}
});