$(function() {

	if($('#mycarousel').length > 0) {
		function mycarousel_initCallback(carousel) {
			// Disable autoscrolling if the user clicks the prev or next button.
			carousel.buttonNext.bind('click', function() {
				carousel.startAuto(0);
			});

			carousel.buttonPrev.bind('click', function() {
				carousel.startAuto(0);
			});

			// Pause autoscrolling if the user moves with the cursor over the clip.
			carousel.clip.hover(function() {
				carousel.stopAuto();
			}, function() {
				carousel.startAuto();
			});
		};
		
		$('#mycarousel').jcarousel({
			scroll: 1,
			auto: 3,
			wrap: 'last',
			initCallback: mycarousel_initCallback
		});
	}
	
	if($('#contact-form').length > 0) {
		//contact form
		$('#error').dialog({
			autoOpen: false,
			width: 460,
			modal: true,
			buttons: {
				"Ok": function() { 
					$(this).dialog("close"); 
				}
			}
		});
		
		var form = $('#contact-form');
		var error = $('#error');
		$('#submit').click(function() {
		
			$(error).empty().append('<h3>There were problems with the following field(s):</h3><ul></ul>');
			$(error).find('ul').empty();
		
			//var name, email, company, phone, message;
			var iname, iemail, icompany, iphone, imessage;
		
			var name = $("input[name=name]").val();
			if (name.length==0) {
				var iname = 0;
				$(error).find('ul').append('<li>First name can\'t be blank</li>');
			}else{
				var iname = 1;
			}
				
			var email = $("input[name=email]").val();
			if (email.indexOf("@")==-1 || email.indexOf("@")==0 || email.length==0){
				var iemail = 0;
				$(error).find('ul').append('<li>Email does not appear to be a valid e-mail address</li>');
			}else{
				var iemail = 1;
			}
			
			var company = $("input[name=company]").val();
			if (company.length==0) {
				var icompany = 0;
				$(error).find('ul').append('<li>Company can\'t be blank</li>');
			}else{
				var icompany = 1;
			}
			
			var phone = $("input[name=phone]").val();
			if (phone.length==0) {
				var iphone = 0;
				$(error).find('ul').append('<li>Phone can\'t be blank</li>');
			}else{
				var iphone = 1;
			}

			var message = $("textarea[name=message]").val();
			if (message.length==0) {
				var imessage = 0;
				$(error).find('ul').append('<li>Message can\'t be blank</li>');
			}else{
				var imessage = 1;
			}
			
			if (iname==0 || iemail==0 || icompany==0 || iphone==0 || imessage==0) {
				$('#error').dialog('open');
				return false;
			}else{
				var dataString = 'name=' + name + 
				'&email=' + email +
				'&company=' + company +
				'&phone=' + phone +
				'&message=' + message;	
			
				$.ajax({
					type: "POST",
					url: "../bin/contact.php",
					data: dataString,
					success: function(res) {
						if (res == "yes") {
							$(error).empty().append('<h3>Message sent! We will get back to you soon!</h3>');
							$(form).find('input[type=text]').val(' ');
							$(form).find('textarea').val(' ');
							$('#error').dialog('open');
						}else{
							return false;
						}
					}

				});
				return false;
			}

		});
	}
	
});
