$(document).ready(function() {

	var imageJson = $('input[name="image-titles"]').val();
	if(imageJson != undefined && imageJson.length > 0) {
		var image = $.parseJSON(imageJson);
		// now populate each 
		if(image.main != undefined) {
			$('div.main-image img').attr('title', image.main).attr('alt', image.main);
		}
		if(image.one != undefined) {
			$('.thumbnails .image1').attr('title', image.one).next('img').attr('alt', image.one).attr('title', image.one);
		}
		if(image.two != undefined) {
			$('.thumbnails .image2').attr('title', image.two).next('img').attr('alt', image.two).attr('title', image.two);
		}
		if(image.three != undefined) {
			$('.thumbnails .image3').attr('title', image.three).next('img').attr('alt', image.three).attr('title', image.three);
		}
	}
	
	
	// environmental page
	$('.environmental-policy').tab();
	// contact page
	$('#contact-options').show();
	$('#contact-form').hide();
	// Contact page drop down
	$('select[name="options"]').change(function() {
		var page = $(this).val();
		if(page != '') {
			$('input[name="enquiry_type"]').val(page);
			// get some ajax of this page
			$.get('/contact/'+page+'.htm', function(m) {
				$('#contact-form').hide();
				var message = 'Yes, I have tried these links';
				/*$.get('/contact/tried-these.html', function(response) {
					message = response;				
				});*/
				$('.output').html(m).append('<p><a href="#" class="show-form button"><span>'+message+'</span></a></p><div class="clear"></div>').slideDown();
				// add help link
			}).error(function() { 
				// no options, so show form
				$('.output').slideUp(function() { $(this).html(''); });		
				$('#contact-form').fadeIn();
			});
		} else {
			$('.output').slideUp(function() { $(this).html(''); });
			$('#contact-form').fadeOut();
		}
	});


	$('a.show-form').live('click', function() {
		$('#contact-form').fadeIn();
		$('.output').slideUp(function() { $(this).html(''); });							
		return false;
	});
	
	// servicing form
	$("#servicing-form").validate({
		rules: {
			name: "required",
			telephone: "required",
			reason: "required",
			email: {
				required: true,
				email: true
			},
			reason: "required",
			purchase_date: {
				required: true,
				date: true
			},
			comment: "required"
		},
		messages: {
			name: "Please enter your name",		
			telephone: "Please enter your telephone",	
			reason: "Please enter a reason for enquiry",		
			reason: "Please enter your reason for enquiry",		
			purchase_date: "Please enter your purchase date",		
			comment: "Please enter your comments",			
			email: "Please enter a valid email address"
		}
	});
	// contact form
	$("#contact-form form").validate({
		rules: {
			full_name: "required",
			email: {
				required: true,
				email: true
			},
			enquiry: "required"
		},
		messages: {
			full_name: "Please enter your name",				
			enquiry: "Please enter your enquiry",			
			email: "Please enter a valid email address"
		}
	});
	// newsletter
	$("#newsletter-signup form").validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {					
			email: "Please enter a valid email address"
		}
	});		
	
	// search results
	$('.search-results').easyPaginate({
		step:20,
		delay:0,
		pause:0
	});
	
	//home page
		//home inner fade
		$('#hero ul').after('<div id="banner-nav"></div>').cycle({
			fx: 'fade',
			timeout: 10000,
			pager: '#banner-nav'
		});
		// home buttons
		$('.main-spotlights ul li div.contain').addClass('short');
		$('.main-spotlights ul li div.contain').hover(function() {
			$(this).stop().animate({height:'110px'});
		},
		function() {
			$(this).stop().animate({height:'24px'});
		});
	
	// site
	// news page, correct pager count
	$('#news .page_navigation .page_num').each(function() {
		var $this = $(this);
		var num = parseInt($this.html(), 10)+1;
		$this.html(num);
	});
	// open in new window
	$('a[rel="blank"],a[rel="external"]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	// make breadrumb
	$('.crumbit').crumbit();
	// input clears
	$('.no-label').labels();
	// all PDF's in new window
	$('a[href*=".pdf"]').attr('target','_blank');
	//product list
	$('#products ul > li:odd').addClass('odd');	
	// set error page to current page
	$('input[name="error_page"][type="hidden"]').val(window.location.pathname);
	// Hide the second level nav
	$('#secondary-nav ul').robinsonNav();
	$('#primary-nav ul').robinsonNavPrimary();
	// forms
	$('.datepicker').datepicker({
		dateFormat: 'dd/mm/yy',
		changeMonth: true,
		changeYear: true
	});
	$('select[name="options"], select[name="reason"]').selectBox();
	// accordions
	$('.accordion').accordion({
		autoHeight: false
	});
	// ul li accordion
	$('.robinson-accordion').robinsonAccordion();
	// gallery
	$('a[class*="enlarge"]').colorbox({ width: "60%", height: "75%"});
	// tooltips
	$('[title]').qtip({position:{adjust:{x:-100}}});
	
});


// Adding first-child and last-child classes to home page and footer elements
$(function() {
  $('.spotlights div:first-child').addClass('first-child');
  $('.spotlights div:last-child').addClass('last-child');
  $('#footer div:first-child').addClass('first-child');
  $('#footer div:last-child').addClass('last-child');
  $('#primary-nav ul li:last-child').addClass('last-child');
  
});


