/*
	Portable Analytical Systems
	---------------------------
	jQuery Calls & Methods
	Scott Luxford - August 2010
	monkeySac.com
*/

$(document).ready(function() {
	
	// Home + Application Page - Tagline Hover
	if($('#nav').length) {
		var tagline = $('#tagline'), original = $('#tagline').html();
		$('#nav a').hover(function() {
			$(this).data('title', $(this).attr('title')).attr('title', '');
			tagline.html($(this).data('title'));
		}, function() {
			$(this).attr('title', $(this).data('title'));
			tagline.html(original);
		});
	}
	
	// Banner slideshow
	$('#banner').scottSlider({
		transType: 'fade',
		slideTimer: 8000,
		slideDur: 1000
	});
	
	// News Archive
	if($('.news-archive').length) {
		$('.news-archive a.year').click(function(evt) {
			evt.preventDefault();
			$(evt.target).parent('.news-year').toggleClass('open');
		});
	}
	
	// Events Archive
	if($('.event-archive').length) {
		$('.event-archive a.year').click(function(evt) {
			evt.preventDefault();
			$(evt.target).parent('.event-year').toggleClass('open');
		});
	}
	
	// Contact Form
	$('#contactForm').submit(function(e) {
		e.preventDefault();
		if (checkContactForm(this)) {
			$('#contactFormSubmit').val('Sending your enquiry...');
			$.ajax({
				type: 'POST',
				url: '/site/contact_mailer.php', 
				data: $(this).serialize(),
				success: function(data) {
					window.location = 'http://portableas.com/thankyou';
				}
			});
		}
		return false;
	});
	
	// Table styling
	// Matrix: $('tr:odd').add('th:odd, td:odd', 'tr').addClass('alt');
	$('th:odd, td:odd', 'tr').addClass('alt');
	
	// Shadowbox
	Shadowbox.init({
	    // skip the automatic setup again, we do this later manually
	    skipSetup: true
	});
	
	Shadowbox.setup(".product-images a.enlarge");
	
	// Jump-down	
	$('.product-list h3').each(function(i, p) {	
		var product = $(p);
		var text = product.text(), value = product.attr('id');
	    $('#product-jump').append('<option value="#'+value+'">'+text+'</option>');
	});
	
	$('#product-jump').change(function() {
		var o = $($(this).val()).offset();
		$(window).scrollTop(o.top);
	});
	
});


function checkContactForm(formEl) {
	var errors = false;
	var required = $('.required', formEl);
	required.each(function(i, reqEl) {
		reqEl = $(reqEl);
		if (reqEl.val() == '') {
			reqEl.addClass('error');
			errors = true;
		} else {
			reqEl.removeClass('error');
		}
	});
	if(errors) {
		$('.errorMsg', formEl).show();
		return false;
	} else {
		$('.errorMsg', formEl).hide();
		return true;
	}
}
