// JavaScript Document

$j(function(){
	
	// slide-down images on homepage
	$j('#more_work li').hover(function(){
		$j(this).find('p').animate({top:'-130px'},{queue:false,duration:400});
	}, function(){
		$j(this).find('p').animate({top:'0px'},{queue:false,duration:400});
	});

	$j('#gallery').gallerify();
	
	// contact page
	$j('#body_contact').rfpForm();
	
	// easter egg :)
	$j('<div id="worm"></div>').appendTo('#footer_sleeve').toggle(
		function(){
			$j(this).parents('#footer_sleeve').animate({height:'835px'},{queue:false,duration:2000});
			$j('html,body').animate({scrollTop:$j('#footer_sleeve').offset().top + 865},{queue:false,duration:2000});
		}, function(){
			$j(this).parents('#footer_sleeve').animate({height:'70px'},{queue:false,duration:2000});
	});
	
	// cope with iphone layout
	if(navigator.platform == "iPhone") {
		
		setOrientation();
		addEventListener('orientationchange',setOrientation);
		
		function setOrientation() {
			window.orientation === 0 ? $j('body').attr({orient: 'portrait'}) : $j('body').attr({orient: 'landscape'});
		}
	}
});

jQuery.fn.gallerify = function() {
	return this.each(function(){
		var images = $j('img.thumb',this);
		images.hide().css({opacity:0});
		$j(images[0]).show().css({opacity:1});
		
		if (images.length > 1) {
			$j(this).after('<ul id="gallery_changer"></ul>');
			var changer = $j('+ul',this);
			images.each(function(){
				var numberLink = (images.index(this)+1).toString();
				if (numberLink.length == 1) numberLink = '0' + numberLink;
				$j('<li><a href="#">'+numberLink+'</a></li>').click(showImage).appendTo(changer);
			});
			$j('li:first',changer).addClass('first current');
		}

		function showImage() {
			$j(this).addClass('current').siblings().removeClass('current');
			var clicked = this;
			images.each(function(){
				if ($j(this).is(':visible')) {
					$j(this).animate({opacity:0},200,function(){
						$j(this).hide();
						$j(images[$j(clicked).parent().children('li').index(clicked)]).css({display:'block'}).animate({opacity:1},200);
					});
				}
			});
			return false;
		}
	});
}

jQuery.fn.rfpForm = function() {
	return this.each(function(){
		$j('#rfp_form,#step_2,#step_3').hide();
		if($j('#contact_flash').length<1){
			$j('<div class="radios" id="nature"><p class="label">What is the nature of your enquiry?</p><label for="type_1"><input type="radio" class="radio" id="type_1" name="contact[form_type]" value="general" checked="checked" /> General enquiry</label><label for="type_2"><input type="radio" class="radio" id="type_2" name="contact[form_type]" value="proposal" /> Proposed project</label></div>').insertBefore('#message_form');
			$j('#type_1').click(function(){
				$j('#message_form').show();
				$j('#rfp_form').hide();
			});
			$j('#type_2').click(function(){
				$j('#message_form').hide();
				$j('#rfp_form').show();
			});
			$j('<img class="submit back" src="/images/button_back.gif" alt="Back" />').click(function(){
				$j(this).parents('.step').hide().prev().show();
				return false;
			}).appendTo('#step_2,#step_3');
			$j('#step_3 input.submit').appendTo('#step_3');
			$j('<img class="submit next" src="/images/button_next.gif" alt="Next" />').click(function(){
				$j(this).parents('.step').hide().next().show();
			}).appendTo('#step_1,#step_2');
		}
	});
}