// fix for IE hover issue on CSS rollovers
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
	
// home page slideshow
var home_current_image;
var home_timer;

function home_slide_show(id) {
	if (id) {
		next = $('project_image' + id);
		clearInterval(home_timer);
	} else next = ($(home_current_image).next('img')  || $('home_top').down('img'));
	
	old_id = $(home_current_image).readAttribute('id').sub('project_image', '');
	new_id = next.readAttribute('id').sub('project_image', '');
	
	// project image
	new Effect.Fade(home_current_image, { duration: .8 });
	new Effect.Appear(next, {
		duration: .6,
		afterFinish: function() {
			home_current_image = next;
		}
	});
	
	// project caption
	new Effect.Fade('project_caption' + old_id, { duration: .8 });
	new Effect.Appear('project_caption' + new_id, { duration: .6 });
	
	// project fill block
	new Effect.Morph('project_block' + old_id, {
		style: 'background: #000;',
		duration: 0.5
	});
	new Effect.Morph('project_block' + new_id, {
		style: 'background: #FFF;',
		duration: 0.3
	});
	
}

// project photo slider
var slide_width = 600;
var slide_speed	= .7;
var slide_pos	= 1;
var slide_pages	= 0;

function photo_slide(direction) {
	slide_speed = 0.5;
	if (direction == "back") {
		if (slide_pos > 1) {
			change = slide_width;
			slide_pos--;
		} else {
			change = -((slide_pages - 1) * slide_width);
			slide_pos = slide_pages;
			slide_speed = 0.7;
		}
	} else if (direction == "next") {
		if (slide_pos < slide_pages) {
			change = -(slide_width);
			slide_pos++;
		} else {
			change = (slide_pages - 1) * slide_width;
			slide_pos = 1;
			slide_speed = 0.7;
		}
	}
	
	new Effect.MoveBy('slider', 0, change, { duration: slide_speed, transition: Effect.Transitions.sinoidal });
}

Event.observe(document, "dom:loaded", function() {
	// navigation functionality
	$$('#home_top ul li, #header ul li').each(function(x) {
		if (x.getElementsByTagName('ul').length) {
			x.onmouseover = function() { this.addClassName('hovered'); };
			x.onmouseout = function() { this.removeClassName('hovered'); };
		}
	});
	
	// text field selection styles
	if ($$('#page_body_main .text_field').length > 0) {
		$$('#page_body_main .text_field').each(function(x) {
			x.onfocus = function() { 
				this.className = 'text_field_selected';
				if (this.up('#email_signup') && this.value == 'your email address here') this.value = '';
			}
			x.onblur = function() { 
				this.className = 'text_field';
				if (this.up('#email_signup') && this.value == '') this.value = 'your email address here';
			}
		});
		if ($$('#page_body_main .text_field')[0].up('#page_body_left')) $$('#page_body_main .text_field')[0].activate();
	}
	
	// photo slider events
	if ($('slider')) {
		$('arrow_left').onclick = function(event) { 
			photo_slide('back');
		};
		$('arrow_right').onclick = function(event) { 
			photo_slide('next');
		};
		slide_pages = Math.ceil($$('#slider img').length / 8);
	}
	
	// home page slideshow
	if ($('home_top')) {
		if ($$('#home_top img').length > 1) {
			home_current_image = $$('#home_top img')[0];
			home_timer = setInterval("home_slide_show()", 5000);;
			
			$$('#home_caption #blocks div').each(function(x) {
				x.onclick = function() {
					id = x.readAttribute('id').sub('project_block', '');
					if (home_current_image.readAttribute('id').sub('project_image', '') != id) home_slide_show(id);
				};	
			});
		}
	}
});