$(document).ready(function() {
	// Jquery loaded - all guns blazing!
	$('body').addClass('jquery');
	
	tate.init();
});

tate={
	autorotate:true,
	timedelay:4000,
	autointerval:null,
	init:function(){
		tate.featurebox();
		tate.inlinelabels();
		if($.browser.mozilla)$('body').addClass('moz');
		if($.browser.msie && $.browser.version<=7)tate.ie();
	},
	ie:function(){
		$('body').addClass('ie');
		$('#nav li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		$("hr").replaceWith('<img src="assets/images/layout/hr-fade.gif" alt="dividing line" class="hr" />');
		if($.browser.version==7){
			$('body').addClass('ie7');
		}else{
			$('body').addClass('ie6');
		}
	},
	featurebox:function(){
		$('#featurebox .titles li h2').bind('mouseover',function(){
			$$=$(this).parent();
			if(!$$.is('.active')){
				$$.stop().animate({height:'230px'}).addClass('active');
				$$.siblings().stop().animate({height:'30px'}).removeClass('active');
				$i=$('#featurebox .images li');
				$i.filter(':visible').fadeOut(function(){
					$i.eq($$.index()).fadeIn();	
				});
			}
		});
		if(tate.autorotate){
			tate.autointerval=setInterval('tate.rotate()',tate.timedelay);
			$('#featurebox').hover(function(){
				clearInterval(tate.autointerval);
			},function(){
				tate.autointerval=setInterval('tate.rotate()',tate.timedelay);
			});
		}
	},
	rotate:function(){
		var $active=$('#featurebox .titles li.active');
		var activeindex=$active.index();
		var targetindex=activeindex+1;
		if(targetindex>=$('#featurebox .titles li').length)targetindex=0;
		var $target=$('#featurebox .titles li').eq(targetindex)
		$target.stop().animate({height:'230px'}).addClass('active');
		$active.stop().animate({height:'30px'}).removeClass('active');
		$('#featurebox .images li:visible').fadeOut(function(){
			$('#featurebox .images li').eq(targetindex).fadeIn();
		});
	},
	inlinelabels:function(){
		$('label.overlay')
			.each(function(){
				$$=$(this);
				$span=$(this).children('span');
				$$.children('input').attr('title',$span.html()).val($span.html());
				$span.hide();
			})
			.children('input')
				.bind('focus',function(){
					if($(this).val()==$(this).attr('title'))
						$(this).val('');
				})
				.bind('blur',function(){
					if($(this).val()=='')
						$(this).val($(this).attr('title'));
				});
		$('#newsletter-signup').bind('submit',function(){
			var r=true;
			$(this).children('label.overlay').each(function(){
				$$=$(this).children('input');
				if($$.val()=='' || $$.val()==$$.attr('title')){
					r=false;
				}
			});
			
			if(r==true){
				var c=$('#company-name').val();
				var e=$('#email-address').val();
				$('#newsletter-signup button').html('Signing up...');
				$.get($('#newsletter-signup').attr('action') + '?ajax=true&company-name=' + c + '&email-address=' + e,function(data){
					if(data=="ok"){
						$('#newsletter-signup').replaceWith('<p>Thanks for signing up to the TATE Newsletter.</p><p>You should receive a confirmation email momentarily.</p>');
					}else{
						alert(data);
						$('#newsletter-signup button').html('Sign up');
					}
				})
			}
			return false;
		});
	}
}
