
//-------------------------------------------------------------------------------------- Cufon -------------//

Cufon.replace('.secondary h1',{textShadow: '2px 2px rgba(0,0,0,0.25)'})('h2', {color: '-linear-gradient(#01bbe1,#0181c8)'})('.home h2',{textShadow: '2px 2px rgba(0,0,0,0.25)'})('h3')('#masthead p');

//-------------------------------------------------------------------------------------- Masthead Functions -------------//

var $masthead, $slides, $nav, slidecurr=0, slidecount, autoslide=true, to;

function init_masthead()
{
	var index;
	
	// Cache elements and get number of slides
	$masthead = $('#masthead > div');
	$slides = $('section', $masthead);
	$nav = $(innerShiv('<nav class="masthead"></nav>', false));
	slidecount = $slides.length;
	
	// Build the navigation based on the slides
	build_masthead_nav();
	
	// Set the active state on the first slide, and navigation
	$slides.eq(slidecurr).addClass('active');	
	$('li', $nav).eq(slidecurr).addClass('current');
	
	// Set autoslide
	if(autoslide) to = setTimeout('next_panel()', 6000);
	
	// If a link is clicked, and the current slide is different,
	// go to the appropriate slide
	$('nav a', $masthead).live('click', function() {
		index = $(this).parent().index(); 
		go_to_panel(index);
		slidecurr = index;
		clearTimeout(to);
		autoslide=false;
	});
}

function build_masthead_nav()
{
	var title = "";
	
	// Add a ul to the nav	
	$nav.html('<ul />');
	
	// For each of the slides, add a li with a link, with the correct title
	for(var i=0; i<slidecount; i++)
	{
		title = $slides.eq(i).find('h2').text();
		$('ul', $nav).append($('<li><a class="tooltip" href="javascript:;" style="background-image: url(assets/img/homepage/masthead-nav-'+(i+1)+'.jpg)" title="'+title+'">'+title+'</a></li>'));
	}

	// Add to the masthead
	$masthead.append($nav);
}

function next_panel()
{
	// Work out which is the next slide, and go to it
	slidecurr = slidecurr == (slidecount-1) ? 0 : slidecurr + 1;
	go_to_panel(slidecurr);
	
	// Do it again if autoslide is still true
	if(autoslide) to = setTimeout('next_panel()', 6000);	
}

function go_to_panel(i)
{	
	if(!_isie7 && !_isie8) {
		$slides.eq(i).find('h2, p').css({'opacity': 0.01, 'position':'relative', 'left':-40});
		$slides.eq(i).find('a').css({'opacity': 0.01});
	}
	else if(_isie8) {
		$slides.eq(i).find('h2, p, a').css({'display': 'none'});
	}
		
	if(_isie) {
		$slides.filter('.active').removeClass('active').hide();
		$slides.eq(i).addClass('active').show();
	} else {
		$slides.filter('.active').removeClass('active').css('opacity', 1).hide();
		$slides.eq(i).addClass('active').stop().fadeIn(1000);
	}
	
	if(!_isie7 && !_isie8) {
		$slides.eq(i).find('h2').delay(400).animate({'left':0, 'opacity':1}, 300, 'easeInOutCubic');
		$slides.eq(i).find('a').delay(600).animate({'opacity':1}, 300, 'easeInOutCubic');
		$slides.eq(i).find('p').delay(600).animate({'left':0, 'opacity':1}, 300, 'easeInOutCubic');
	}
	else if(_isie8) {
		$slides.eq(i).find('h2').delay(400).fadeIn();
		$slides.eq(i).find('a').delay(600).fadeIn();	
		$slides.eq(i).find('p').delay(600).fadeIn();		
	}
	
	$('li', $nav).filter('.current').removeClass('current');
	$('li', $nav).eq(i).addClass('current');
}

//-------------------------------------------------------------------------------------- Tooltip -------------//

function init_tooltips() 
{
	var $tooltip, t, ypos;
	
	$('.tooltip').live('mouseover', function() {
		
		t = $(this).attr('title');		
		$(this).attr('title','');
		
		width = $(this).width() + 4;
		
		offset = $(this).offset();
		
		$tooltip = $('<p class="itooltip"><b class="ir">&darr;</b>'+t+'</p>');
		$('body').prepend($tooltip);
		
		$tooltip.css({'top':'-9999em','left':'-9999em'});
		ttwidth = $tooltip.width() + parseInt($tooltip.css('paddingLeft')) + parseInt($tooltip.css('paddingRight'));
		
		xpos = ((width-ttwidth)/2) + offset.left;
		ypos = offset.top - 56;
		
		$tooltip.css({'top':ypos,'left':xpos,'opacity':0}).animate({'top':ypos+16, 'opacity':1}, 200);		
				
	});
	
	$('.tooltip').live('mouseout', function() {
		$(this).attr('title', t);
		$tooltip.remove();
	});
}

//-------------------------------------------------------------------------------------- Logo Hover Effect -------------//

function logo_hover()
{	
	$('a.sitelogo').hover(function() {
		var $logohover = $('<span class="sitelogo-hover">&nbsp;</span>');
		
		if(_isie7 || _isie8) $(this).append($logohover);
		else {		
			$logohover.hide();					
			$(this).append($logohover);
			$logohover.fadeIn(200);
		}
	
	}, function() {
		if(_isie7 || _isie8) $('.sitelogo-hover', this).remove();
		else $('.sitelogo-hover', this).fadeOut(600, function() {$(this).remove();});
	});
}

//-------------------------------------------------------------------------------------- Contact Form -------------//

var is_wideform = 0, $content, $aside, $map, widths = {};

function init_contact()
{
	// Google map
	loadScript("http://maps.google.com/maps/api/js?sensor=false&callback=init_map");
	
	// Cache elements and widths
	$content = $('.content > div');
	$aside = $('aside');
	$map = $('#map');
	widths.content = $content.width() / $content.parent().width() * 100;
	widths.aside = $aside.width() / $aside.parent().width() * 100;
	widths.map = $map.width() / $map.parent().width() * 100;

	// Make form width larger on focus
	$('#contactForm input, #contactForm textarea').live('focus', function() {
		//wide_form();
		$('#content').addClass('wide-form');
	});
	
	// Remove error on focus
	$('.error input, .error textarea').live('focus', function() {
		$(this).parent().find('span.error').fadeOut(200, function() {
			$(this).parent()/*.animate({'paddingBottom':0}, 400)*/.removeClass('error');
			$(this).remove();
		});
	});
	
	//$('h3').click(function() {
	//	thin_form();
	//});
	
	// Submit form
	$('#contactForm').live('submit', function() { return submit_contact(this); });
	
}

function wide_form()
{
	/*if(is_wideform) return false;
	else is_wideform = 1;
	
	if(!_isie7 && !_isie8) {		
		// Slide out to larger style on focus
		$content.animate({width: '60%'}, {duration: 800, queue: false, easing: 'easeOutExpo'});
		$aside.animate({width: '40%'}, {duration: 800, queue: false, easing: 'easeOutExpo'}); 
		$map.animate({width: '60%'}, {duration: 800, queue: false, easing: 'easeOutExpo'}); 
	}	*/
}

function thin_form()
{
	/*if(!is_wideform) return false;
	else is_wideform = 0;
	
	if(!_isie7 && !_isie8) {
		$content.animate({width: widths.content+'%'}, {duration: 800, queue: false, easing: 'easeOutExpo'});
		$aside.animate({width: widths.aside+'%'}, {duration: 700, queue: false, easing: 'easeOutExpo'}); 
		$map.animate({width: widths.map+'%'}, {duration: 800, queue: false, easing: 'easeOutExpo'}); 
	}*/		
}

function submit_contact(form)
{
	$('#formsubmit').after('<span class="process">Sending form...</span>').attr('disabled','disabled');
	$('#content').addClass('wide-form');
	setTimeout(function() { validate(form); }, 1000);
	return false;
}

function validate(form)
{
	var data = $(form).serialize();
	
	$.post("assets/components/processes/contact.php", data,
	function(response){
		$response = $(response);
		is_success = $('input', $response).length > 0 ? false : true;

		// Animate errors nicely		
		$errors = $('span.error', $response);		
		$errors.css({'top':20,'opacity':0});		
    	$(form).after($response).remove();
		$errors.animate({'top':-6, 'opacity':1}, 200);
		
		// Shrink the form down again on success
		if(is_success) setTimeout(function() { $('#content').removeClass('wide-form'); }, 2000); 
   	});
	
	return false;
}

//-------------------------------------------------------------------------------------- Google Map -------------//

var map, ll;

function init_map() 
{	
  ll = new google.maps.LatLng(51.705771,-1.994812);
  var opt = {
    zoom: 12,
    center: ll,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
	mapTypeControlOptions: {         
		style: google.maps.MapTypeControlStyle.DROPDOWN_MENU         
	} 
  }
  map = new google.maps.Map(document.getElementById("map"), opt);
    
  var mk = new google.maps.Marker({
      position: ll, 
      map: map,
	  icon: 'assets/img/interface/pin.png'
  });  
}

//-------------------------------------------------------------------------------------- DOM is Loaded -------------//
$(document).ready(function() {
		
	// External Links
	$('a.external').live('click', function() { 
		var nw = window.open($(this).attr('href'), '_blank'); 
		nw.focus(); 
		return false; 
	});	
		
	if($('body').hasClass('home')) {
		
		// Set tooltips for masthead navigation
		init_tooltips();
		
		// Set the content sections to the same height
		equalize('.home .content', 'section', 3);
		
		// Initialize masthead animation
		init_masthead();		
	}
	
	else {
		
		// Logo hover glow effect
		logo_hover();
		
		if($('body').hasClass('contact-us')) {
			// Init contact form things
			init_contact();
		}
		
	}
	
	
});

