$(document).ready(function(){
	//$('ul.categories').makeacolumnlists({cols: 2, colWidth: 120, equalHeight: 'ul', startN: 1});
	//$('#clients #sub-content ul').makeacolumnlists({cols: 3, colWidth: 0, equalHeight: 'ul', startN: 1});
	//$('#products .additional ul').makeacolumnlists({cols: 2, colWidth: 0, equalHeight: 'ul', startN: 1});
	//$('#services .additional.foot ul').makeacolumnlists({cols: 2, colWidth: 0, equalHeight: 'ul', startN: 1});
	
	$('ul.categories').columnize({columns: 2, width: 120});
	$('#clients #sub-content ul').columnize({columns: 3});
	$('#products .additional ul').columnize({columns: 2});
	$('#services .additional.foot ul').columnize({columns: 2});
	
	$('.slide-gallery').cycle({
		fx: 'fade',
		speed: 1750
	});
	$('ol#first-fresh-fast li').hover(function() {
		$(this).stop().animate({backgroundColor: '#282828'}, 'medium'); 
	}, function() { 
		$(this).stop().animate({backgroundColor: '#171717'}, 'medium');
	});
	
	$.each($('#home dl'), function(){
		$(this).addClass('enhance');
		var dlHeight = $(this).height();
		$(this).css('height', dlHeight - 5 +'px');
		$(this).find('dd').hide().css('height', dlHeight - 25 +'px');
		$(this).find('dt:first-child').addClass('current');
		$(this).find('.current').next('dd').show();
	});
		$('#home dt a').click(function(e){
			e.preventDefault();

			$(this).parents('dl')
				.find('.current').removeClass('current')
				.next('dd').hide();
			$(this).parent('dt').addClass('current').next('dd').show();
		});
	
	$.each($('#services dl'), function(){
		$(this).addClass('enhance');
		$(this).find('dt').append('<p><a href="#" class="expand">Expand</a></p>');
		$(this).find('dd').hide();
		$(this).find('dt:first-child').addClass('current');
		var ddHeight = $(this).find('.current').next('dd').show().height();
		
		var articleHeight = $(this).parent().height();
		
		var dlHeight = $(this).height();
		
		var newDlHeight = dlHeight + ddHeight;
		$(this)
			.css('height', newDlHeight + 50 + 'px');
	});
		$('#services dt').live('click', function(e){
			e.preventDefault();

			$(this).parents('dl')
				.find('.current').removeClass('current')
				.next('dd').hide();
			$(this).addClass('current').next('dd').show();
			var ddHeight = $(this).parent('dl').find('.current').next('dd').height();
			var dlHeight = $(this).parent('dl').removeAttr('style').height();
			var newDlHeight = dlHeight + ddHeight;
			$(this)
				.parent('dl').css('height', newDlHeight + 50 + 'px');
		});
	
	$('a[rel="video"]').click(function (e) {
		if (!is_touch_device()) {
			e.preventDefault();
			
			$('body').remove('#video-test').append('<div id="video-test">');
			
			var src = $(this).attr('href');
			
			$.ajax({
				url: src,
				cache: false,
				success: function(data) {

					var vid = $(data).find('#video-content');
					vid.appendTo('#video-test');
					VideoJS.setupAllWhenReady();
					
					//alert(vid.find('video').innerHeight());
					
					$('#video-test').modal({
						closeHTML:"",
						opacity: 80,
						containerCss:{
							backgroundColor: "#333",
							borderColor: "#FFF",
							//height: vid.find('video').attr('height'),
							padding: 10,
							width: vid.find('video').attr('width')
						},
						overlayClose:true,
						onClose: function(){
							vid.remove();
							$.modal.close();
							$('body').find('#video-test').remove();
						}
					});
					
				}
			});
			
			return false;
		}
	});
	
	$("#send-mail").submit(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var nameVal = $("#name").val();
		var emailFromVal = $("#email").val();
		var phoneVal = $("#phone").val();
		//var callbackVal = $("#callback").val();
		var infoVal = $("#message").val();
		
		if(nameVal == '') {
			$("#name").after('<span class="error">You forgot to enter your name.</span>');
			hasError = true;
		}
		
		if(emailFromVal == '') {
			$("#email").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$("#email").after('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}
		
		if(infoVal == '') {
			$("#message").after('<span class="error">You forgot to enter a message.</span>');
			hasError = true;
		}
		
		if(hasError == false) {
			//$(this).hide();
			//$("#send-mail").append('<img src="http://www.smallfry.com/wp-content/themes/smallfry/Images/spinner.gif" alt="Loading" id="loading" />');
			$(this).html('sending...');
			
			$.post("http://www.smallfry.com/wp-content/themes/smallfry/send-gmail.php",
   				{ emailName: nameVal, emailAddress: emailFromVal, emailPhone: phoneVal, emailInfo: infoVal },
   					function(data){	
						//$("#send-mail").slideUp("normal", function() {
							//$("#send-mail").before('<p class="success"><strong>Thanks. Your message has been sent.</strong></p>');	
							$("#send-mail").html('<p class="success"><strong>Thanks. Your message has been sent.</strong></p>');
						//});
   					}
				 );
		}
		
		return false;
	});
	
	$("#join-mail-list").submit(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailFromVal = $("#subscribe-email").val();
		
		if(emailFromVal == '') {
			$("#subscribe-email").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$("#subscribe-email").after('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}
		
		if(hasError == false) {
			$(this).html('sending...');
			
			$.post("http://www.smallfry.com/wp-content/themes/smallfry/join-mail-list-gmail.php",
   				{ emailAddress: emailFromVal },
   					function(data){	
						//$("#join-mail-list").slideUp("normal", function() {
							$("#join-mail-list").html('<p class="success"><strong>Thanks. Your email address has been added to our mailing list.</strong></p>');	
						//});
   					}
				 );
		}
		
		return false;
	});
	
	function is_touch_device() {  
		try {  
			document.createEvent("TouchEvent");  
			return true;  
		} catch (e) {  
			return false;  
		}  
	}
});
