$(document).ready(function(){
	
	if ($('#tabs').length) {
   	   /*setup tabs with a fade transition*/
	   var $tabs = $("#tabs").tabs({ fx: [{opacity:'toggle', duration:'normal'},   // hide option
	                        {opacity:'toggle', duration:'fast'}] });
	   $tabs.tabs( "rotate" , 3000);
	   /*stops tab rotation when clicking on tab panel*/
	   $('.ui-tabs-panel').click(function() {
	     $tabs.tabs("rotate" , 0);
	   });

	   $("#tabs").show();
	}
	
	// if there is more than one featured product, rotate through them
	var totalProducts = $('.featuredProduct').length;
	
	var curProduct = 1; // start on second product in list
	
	if (totalProducts > 1) {
		// set height of featured product container to height of tallest child image
		
		var newHeight = 0;
		$('#products').children('.featuredProduct').each(function(){
			if ($(this).height() > newHeight) {
				newHeight = $(this).height();
			}
		});
		
		$('#products').height(newHeight);

		window.setInterval(rotateProducts,5000)
	}
	
	function rotateProducts() {
		$('.featuredProduct:visible').not('#featuredProduct' + curProduct).fadeOut('slow', function(){
			$('#featuredProduct' + curProduct).fadeIn('slow');
			
			curProduct++;

			if (curProduct == totalProducts) {
				curProduct = 0;
			}
		});
	}

	$('.mailingListFormHolder').dialog({
		autoOpen: false,
		title: 'Join Our Mailing List'
	});
	
	$('.testimonialFormHolder').dialog({
		autoOpen: false,
		title: 'Add a Testimonial'
	})
	
	// Join our Mailing List overlay
	if ($('.joinMailingList').length) {
		$('.joinMailingList').click(function(){
			$('.mailingListFormHolder').dialog('open');
			
			return false;
		});
		
		$('#mailingListForm').validate({
			submitHandler: function(form) {
		   		$(form).ajaxSubmit({
					beforeSubmit: function() {
						$('#mailingListForm input.submit').val('Submitting...');
					},
					success: function(){
						$('#mailingListForm .successMessage').fadeIn();
						$('input#mlname').val('');
						$('input#mlemail').val('');
						$('#mailingListForm input.submit').val('Submit');
					}
				});
			}
		});
		
	}
	
	if ($('.addTestimonial').length) {
		if(getUrlVars()["rel"] =='addTestimonial') {
			$('html,body').scrollTop(1200);
			$('.testimonialFormHolder').dialog('open');
		}
		
		
		$('.addTestimonial').click(function(){
			$('.testimonialFormHolder').dialog('open');
			return false;
		});
		
		$('#testimonialForm').validate({
			submitHandler: function(form) {
		   		$(form).ajaxSubmit({
					beforeSubmit: function() {
						$('#testimonialForm input.submit').val('Submitting...');
					},
					success: function(){
						$('#testimonialForm .successMessage').fadeIn();
						$('input#tname').val('');
						$('input#temail').val('');
						$('textarea#testimonialContent').val('');
						$('#testimonialForm input.submit').val('Submit');
					}
				});
			}
		});
	}
});

function getUrlVars() { // http://papermashup.com/read-url-get-variables-withjavascript/
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}
