$(document).ready(function(){
	var supportsTouch = 'createTouch' in document;
	log("supportsTouch", supportsTouch);
	if (document.body[supportsTouch]) IST.clickAction = "touchstart";
	
	$window = $(window);
	$window.resize(function(event) {
		if ($window.width() < 960) {
			$('body').addClass("narrow");
			//$('#product_image').css('background-position', '0 0');
		} else {
			$('body').removeClass("narrow");
			//$('#product_image').attr('style', '');
		}
	
	});
	
	$('nav li')
		.bind("mouseover", function(event){
			$(this).addClass("hover");
		})
		.bind("mouseout", function(event){
			$(this).removeClass("hover");
		});
		
	if ($('body').hasClass('home')) {
		$('section#product_list ul li a')
			.bind("mouseover", function(event){
				$(this).parent().addClass("hover");
			})
			.bind("mouseout", function(event){
				$(this).parent().removeClass("hover");
			});
	}
	
	$('body.contact #result').bind('click', function(event) {
		event.preventDefault();
		$(this).fadeOut();
	});
	
	$('body.contact form p button').bind('click', function(event) {
		event.preventDefault();
		if ($(this).hasClass('ghost') == false) {
			
			var name = $('#name').val();
			var email = $('#email').val();
			var subject = $('select').val();
			var message = $('#message').val();
			if (name == "" || email == "" || message == "") {
				$('#result p').html('Please fill out all fields.');
				$('#result').fadeIn();
				setTimeout(function() { $('#result').fadeOut(); }, 3000);
			} else {
				$('form button').addClass('ghost');
				//log(name + ", " + email + ", " + subject + ", " + message);
				//var params ='name=' + name + '&email=' + email.replace("+", "%2B") + '&subject=' + subject + '&message=' + message;
				var params ='name=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email) + '&subject=' + subject + '&message=' + encodeURIComponent(message);
				log(params);
				//params = params.replace("+", "%2B");
				//var encodedParams = encodeURI(params);
				//log(encodedParams);

				$.ajax({
					type: 'POST',
					url: '/email.php',
					data: params,
					cache: false,
					success: function(html) {
						log(html);
						$('form button').removeClass('ghost');
						
						if (html.indexOf("sent") == 0) {
							$('#result p').html('Message sent. Thank you very much.');
							$('#message').val("");
							$('#email').val("");
							$('#name').val("");
						} else {
							$('#result p').html('There was a problem sending your message. Please check your email address is correct and try again.');
						}
						$('#result').fadeIn();
						setTimeout(function() { $('#result').fadeOut(); }, 3000);
						//$('#response').fadeIn('slow');
						//$('#response').html(html);
						//setTimeout("$('#response').fadeOut('slow')", 2000);
					},
					error: function(xhr, type) {
						$('#result p').html('There was a error on the server. Please try again.');
						$('#result').fadeIn();
						setTimeout(function() { $('#result').fadeOut(); }, 4000);
					}
				});
			}
		}

	});
});

IST = {
	clickAction: "click"
};

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
	//log.history = log.history || [];   // store logs to an array for reference
	//log.history.push(arguments);
	if(this.console){
	  console.log( Array.prototype.slice.call(arguments) );
	}
};
