var active_ul;

function blurMenuItem()
{
	if(typeof(active_ul) != 'undefined')
	{
		active_ul.setStyle('display', 'none');
	}
}

window.addEvent('domready', function() {
	var menu_lis = $$('header div#header-content div.menu ul li');
	for(var i = 0; i < menu_lis.length; i++)
	{
		menu_lis[i].addEvent('mouseover', function() {
			blurMenuItem();
			active_ul = this.getChildren('ul.sub-menu')[0];
			if(typeof(active_ul) != 'undefined')
			{
				active_ul.setStyle('display', 'block');
				active_ul.setStyle('left', this.offsetLeft);
				active_ul.addEvent('mouseout', blurMenuItem);
			}
		});
	}
	
	if($('s').value == '')
		$('s').removeClass('active');
	else
		$('s').addClass('active');
	
	$('s').addEvent('blur', function() { if($('s').value == '') $('s').removeClass('active'); });
	$('s').addEvent('focus', function() { $('s').addClass('active'); });
	
	if(window.location.hash == "#submitted")
	{
		$('contact_form').empty();
		var submit_message = document.createElement('P');
		submit_message.innerHTML = 'Thanks for your message!';
		submit_message.addClass('submit-message');
		$('contact_form').appendChild(submit_message);
	}
	
	var imgz = $$('img.polaroid');
	for(var i = 0; i < imgz.length; i++)
	{
		var the_original_img = imgz[i];
		var the_container = the_original_img.getParent();
		
		the_container.removeChild(the_original_img);
		
		if(the_container.get('tag') == 'a')
		{
			the_container.setStyle('text-decoration', 'none'); 
		}
		
		var image_width = (the_original_img.width + 30) + 'px';
		
		var div_w = new Element('div', {'class': 'w_polaroid ' + the_original_img.className, styles: {width: image_width}});
		var div_t = new Element('div', {'class': 't'});
		var div_b = new Element('div', {'class': 'b'});
		var div_l = new Element('div', {'class': 'l'});
		var div_r = new Element('div', {'class': 'r'});
		var div_tl = new Element('div', {'class': 'tl'});
		var div_tr = new Element('div', {'class': 'tr'});
		var div_bl = new Element('div', {'class': 'bl'});
		var div_br = new Element('div', {'class': 'br'});
		var the_img = new Element('img', {
			src: the_original_img.src,
			styles: { width: the_original_img.width }
		});
		
		the_container.appendChild(div_w);
		div_w.appendChild(div_t);
		div_t.appendChild(div_b);
		div_b.appendChild(div_l);
		div_l.appendChild(div_r);
		div_r.appendChild(div_tl);
		div_tl.appendChild(div_tr);
		div_tr.appendChild(div_bl);
		div_bl.appendChild(div_br);
		div_br.appendChild(the_img);
		
		var caption_text = the_original_img.getProperty('_caption');
		
		if(caption_text == null)
		{
			caption_text = "&nbsp;";
		}
		
		var p_caption = new Element('p', {html: caption_text});
		div_br.appendChild(p_caption);

		var div_padding = new Element('div', {'class': 'padding_10'});
		div_br.appendChild(div_padding);
	}
});

// Make HTML5 work in IE - urgh.
document.createElement("header");
document.createElement("hgroup");
document.createElement("nav");
document.createElement("article");
document.createElement("footer");
