var yvw = {
	init: function() {
		this.searchform.init();
		this.worksform.init();
		this.faqs.init();
		this.expandable.init();
		if($('#intro_nav').length == 1) {
			this.intronav.init();
		}
		this.careers.init();
	}
}; 

yvw.searchform = {
	defval: 'Search',
	init: function() {
		$('#search_input').focus(function(){
			if(this.value == yvw.searchform.defval) {
				this.value = '';
			}
		});
		$('#search_input').blur(function(){
			if(this.value == '') {
				this.value = yvw.searchform.defval;
			}
		});	
		$('#search_submit').bind('click', function(){
			if ($("#search_input").val()==yvw.searchform.defval) $("#search_input").val("");
				return true;
		});		
	}
};

yvw.intronav = {
	paused: false,
	current: false,
	interval: 4000,
	intid: false,
	init: function(){
		$('#intro_nav li').each(function(i){
			$(this).attr('id', 'intro_nav_' + i);
			$(this).bind('mouseenter', function(){
				yvw.intronav.stopint();
				yvw.intronav.toggle($(this).attr('id').split('_').pop());
			});
			$(this).bind('mouseleave', function(){
				yvw.intronav.startint();
			});
			$(this).bind('click', function(){
				window.location = $(this).find('a').attr('href');
				return false;
			});
		});
		$('#intro .intro_item').each(function(i){
			$(this).attr('id', 'intro_item_' + i);
			$(this).addClass('inactive');
		});
		this.toggle(0);
		this.intid = window.setInterval('yvw.intronav.next()', this.interval);
	},
	startint: function() {
		this.intid = window.setInterval('yvw.intronav.next()', this.interval);
	},
	stopint: function() {
		window.clearInterval(this.intid);
	},
	toggle: function(n){
		$('#intro_nav_' + this.current).removeClass('active');
		$('#intro_nav_' + n).addClass('active');
		$('#intro_item_' + this.current).addClass('inactive');
		$('#intro_item_' + n).removeClass('inactive');
		this.current = parseInt(n);
	},
	next: function(){
		if(!this.paused) {
			if(this.current < 3) {
				this.toggle(this.current + 1);
			}
			else {
				this.toggle(0);
			}
		}
	}
};

yvw.worksform = {
	defval: 'Postcode',
	init: function(){
		$('#works').attr('class', 'postcode');
		$('#works .tab').each(function(i){
			$(this).bind('focus click', function(){
				var el = $(this).attr('rel');
				$('#works').attr('class', el);
				$('#' + el).focus();
				return false;
			});
		});
		$('#postcode').focus(function(){
			if(this.value == yvw.worksform.defval) {
				this.value = '';
			}
		});
		$('#postcode').blur(function(){
			if(this.value == '') {
				this.value = yvw.worksform.defval;
			}
		});	
	}
};

yvw.careers = {
	interval: 5000,
	total: 0,
	current: 0,
	initid: false,
	init: function() {
		if($('#careers_hero').length != 1) {
			return false;
		}
		this.total = $('#careers_hero .item').length;
		this.toggle(0);
		this.intid = window.setInterval('yvw.careers.next()', this.interval);	
	},
	next: function() {
		if((this.current+1) < this.total) {
			this.toggle(this.current + 1);
		}
		else {
			this.toggle(0);
		}
	},
	toggle: function(n) {		
		$('#careers_hero_' + this.current).removeClass('active');
		$('#careers_hero_' + n).addClass('active');
		this.current = parseInt(n);
	}
};

yvw.faqs = {
	init: function() {
		$('#faqs .item').each(function(i){
			$(this).find('.answer').css('height', 0);
			$(this).find('a.toggle').bind('click', function(){
				yvw.faqs.toggle($(this));
				return false;
			});
		});
	},
	toggle: function(el) {
		var a = el.parents('.item').find('.answer');
		if(!a.hasClass('open')) {
			a.animate({'height': a.find('div').height()});
			a.addClass('open');
		}
		else {
			a.animate({'height': 0});
			a.removeClass('open');
		}
	}
};

yvw.expandable = {
	init: function() {
		$('#expandable .item').each(function(i){
			$(this).find('.answer').css('height', 0);
			$(this).find('a.toggle').bind('click', function(){
				yvw.expandable.toggle($(this));
				return false;
			});
		});
	},
	toggle: function(el) {
		var a = el.parents('.item').find('.answer');
		if(!a.hasClass('open')) {
			a.animate({'height': a.find('div').height()});
			a.addClass('open');
		}
		else {
			a.animate({'height': 0});
			a.removeClass('open');
		}
	}
};


$(document).ready(function() {
	yvw.init();
});




