
/*
 * Javascript to initialise the document
 */

// Custom text
Cufon.replace('h1, h2, h3, .breadcrumb');


$(document).ready(function() {
	
	/*
	 * quickquote form
	 */ 
	
	// initialise
	$('form input').css('color', '#aaa')
	$('form button').removeClass('active').addClass('inactive');
	$('#quickquote p.destination, form .inline-message, #quickquote p.message').css('display', 'none');
	if ($('#container-type').val() == 'container ramp') $('p.size').hide();
	
	// hide default text
	$('input[type="text"]').focus(function()
	{
		$(this).css('color', '#6d6d6d');
		
		if (this.value == this.defaultValue)
		{
			this.value = '';
		}
	}).blur(function() {
		if (this.value == '')
		{
			this.value = this.defaultValue;
			$(this).css('color', '#aaa');
		}
	}).mouseup(function(event) {
		event.preventDefault();
	});
	
	// expand comment
	$('#quickquote-form textarea#comments').focus(function() {
		$(this).css({height:'88px'});
	}).blur(function() {
		if (this.value == '') $(this).css({height:'23px'});
	});

	// update form depending on container type
	$('#container-type').change(function()
	{
		if ($(this).val() == 'self-pack removal' || $(this).val() == 'project cargo')
		{
			// show the destination
			$('p.destination').slideDown('slow').children('input, select').addClass('required');
		}
		else
		{
			$('p.destination').slideUp('slow').children('input, select').removeClass('required');
		}
		
		if ($(this).val() == 'container ramp' || $(this).val() == 'single phase chiller'|| $(this).val() == 'pallet cages')
		{
			$('p.size').slideUp('slow');
		}
		else
		{
			$('p.size').slideDown('slow');
		}
	}).change();
	
	
	
	// validate form
	$('#quickquote-form, #contact-form').validform({
		alert: false
	},function(){
		$('.form-elements').slideUp('slow');
		$('.message').html('sending&hellip;').slideDown('fast');
		
		// get variables
		var vars = {
			ajax: 'true',
			iscontact: $('#iscontact').val(),
			name: $('#name').val(), 
			phone_number: $('#phone').val(), 
			email_address: $('#email').val(), 
			container_type: $('#container-type').val(),
			location: $('#dropoff').val(),
			location_state: $('#dropoff-state').val(),
			destination: $('#destination').val(),
			destination_state: $('#destination-state').val(),
			hire_or_buy: $('input[name="hire or buy"]:checked').val(),
			size: $('input[name="size"]:checked').val(),
			comments: $('#comments').val()
		}
		
		// send via ajax
		$.post($('body').attr('id') + '/submit', vars, function(data, textStatus){
			if (textStatus == 'success')
			{
				$('form p.message').html(data);
				
				// track analytics
				_gaq.push(['_trackPageview', $('body').attr('id') + '/submit']);
				
				// track AdWords conversion
				$('form p.message').after(
					'<img height="1" width="1" style="border-style:none;" alt="" ' + 
					'src="http://www.googleadservices.com/pagead/' +
					'conversion/1030039032/' +
					'?label=cechCKzE3AEQ-MuU6wM&amp;' +
					'guid=ON&amp;' + 
					'script=0"/>'
				);
			}
			else
			{
				$('form p.message').text('There was an error sending your quote. Please try again.');
				$('form .form-elements', this).slideDown('slow');
			}
		});
		
		return false;
	});
	
	
	/*
	 * slideshow
	 */
	
	if ($('#slideshow').length)
	{
		// load the slideshow data
		$.getJSON($('body').attr('id') + '/slideshow', null, function(data, textStatus){
			// function to handle loaded slideshow data
			if (textStatus != 'success') return;

			slideData = data;

			// add class to image
			$('#slideshow img').attr('class', 'bottom');

			// add next image placeholder
			$('#slideshow').append('<img class="top" width="560" height="250" alt="" style="display:none"/>\n');

			setTimeout("showNextImage()", 2000);
		});
	}
	
	
	/*
	 * show map
	 */
	$('#locations a.map-link').click(function() {
		var source = $(this).attr('href');
		
		$(this).parent().append('<iframe style="display:none" width="380" height="180" frameborder="0" scrolling="no" '
			+ 'marginheight="0" marginwidth="0" src="'
			+ source + '"></iframe>');
		$(this).parent().children('iframe').slideDown('slow');
		$(this).slideUp('slow');
		
		return false;
	});
	
	
	/*
	 * faq
	 */
	$('#faq #content p:not(.intro)').css('display', 'none');
	$('#faq #content h3').click(function() {
		if ($(this).next('p').css('display') == 'none')
		{
			$(this).next('p').slideDown();
		}
		else
		{
			$(this).next('p').slideUp();
		}
	});
	
});


var slideData = {};
var slideCount = 0;


// function to show images
function showNextImage()
{
	// copy bottom image to top
	$('#slideshow img.top').attr('src', $('#slideshow img.bottom').attr('src') );
	$('#slideshow img.top').css('display', 'block');
	
	// load new image to bottom
	$('#slideshow img.bottom').attr({
		'src': 'images/' + slideData['image' + slideCount],
		'alt': slideData['alt' + slideCount]
	});
	$('#slideshow a').attr('href', slideData['link' + slideCount]);
	
	// fade out top image
	$('#slideshow img.top').fadeOut('slow');
	
	// increment counter
	slideCount++;
	if (slideCount > slideData.numberOfImages - 1) slideCount = 0;
	
	// preload next image
	if (document.images)
	{
		var slide = slideData[slideCount];
		imageCache = new Image(560, 250); 
		imageCache.src = 'images/' + slideData['image' + slideCount];
	}
	
	// set timeout to go to next image
	setTimeout("showNextImage()", 6000);
}
