$(document).ready(function() {
	// Drop Down Links
	
	$("#linkbar").ptMenu();
	
	// Twitter Plugin
	
	$("#twitter_content").tweet({
		join_text: "auto",
		username: "dwoolfdesigns",
		loading_text: "Loading latest tweet...",
		count: 1,
		auto_join_text_default: ""
	});
	
	// Input Remove / Replace
	
	$("input").not("#submit, #search_submit, #client_submit, #comment-submit").each(function() {
	   var default_value = this.value;
	   $(this).focus(function() {
	       if(this.value == default_value) {
	           this.value = '';
	       }
	   });
	   $(this).blur(function() {
	       if(this.value == '') {
	           this.value = default_value;
	       }
	   });
	});
	
	// Client Login Toggle
	
	$("#client_login_button").toggle(function() {
		$("#client_login").slideDown(300);
	}, function() {
		$("#client_login").slideUp(300);
	});
	
	// Home Page Showcase
	
	$("#showcase li").not("#description").hover(function() {
		$("#showcase li").not(this).not("#description").animate({
			"opacity" : ".4"
		}, {queue:false, duration: 400});
		
		$(this).css({
			"cursor" : "pointer"
		});
		
	}, function() {
		$("#showcase li").not(this).animate({
			"opacity" : "1"
		}, {queue:false, duration: 400});
		
		$(this).css({
			"cursor" : "auto"
		});
	});
	
	// Optional Fancybox Integration
	
	$("a.fancybox").fancybox({ // This has to be in this function due to elements being appended
		'overlayOpacity' : .7,
		'overlayColor' : '#000'
	});
	
	// Column Fader
	
	$("#data_nav li").live('click', function() {
		var column_switch = $(this).attr('class');
		
		if($(this).hasClass('active_tab') == true) {
		}
		
		else {
		
			$("#data_nav li").removeClass("active_tab");
			
			$(this).addClass("active_tab");
			
			$("#home_data_container ul, #contact_data_container ul, #portfolio_data_container ul").not("#home_data_container ul li ul").hide();
			
			$("ul#" + column_switch).fadeIn(500);
		}
		
		return false;
	});
	
	// Testimonial and Portfolio Image Fader
	
	$("ul#testimonial_holder").innerfade({
		speed: 1500,
		timeout: 8000
	});
	
	$("#portfolio_item_images ul").innerfade({
		speed: 650,
		timeout: 3500
	});
	
	// Portfolio
	
	$("#portfolio_data_container ul li").not(".portfolio_section_title").hover(function() {
		$("#portfolio_data_container ul li").not(".portfolio_section_title").not(this).animate({
			"opacity" : ".3"
		}, { queue:false, duration: 400});
	}, function() {
		$("#portfolio_data_container ul li").not(".portfolio_section_title").not(this).animate({
			"opacity" : "1"
		}, { queue:false, duration: 400});
	});
	
	// Contact Form
	
	$("#submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var senderNameVal = $(this).parent().children("#name").val();
		if(senderNameVal == '' || senderNameVal == ' ' || senderNameVal == 'Name...') {
			$(this).parent().children("#name").css({ "border" : "1px solid #faa" });
			hasError = true;
		}
		
		var emailFromVal = $(this).parent().children("#emailFrom").val();
		if(emailFromVal == '') {
			$(this).parent().children("#emailFrom").css({ "border" : "1px solid #faa" });
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$(this).parent().children("#emailFrom").css({ "border" : "1px solid #faa" });
			hasError = true;
		}
		
		var subjectVal = $(this).parent().children("#subject").val();
		if(subjectVal == '' || subjectVal == ' ' || subjectVal == 'Subject...' || subjectVal == 'Location...') {
			$(this).parent().children("#subject").css({ "border" : "1px solid #faa" });
			hasError = true;
		}
		
		var messageVal = $(this).parent().children("#message").val();
		if(messageVal == '' || messageVal == ' ') {
			$(this).parent().children("#message").css({ "border" : "1px solid #faa" });
			hasError = true;
		}
		
		
		if(hasError == false) {	
			// Next four lines of code reverse the "red outline" error styling if there was any
			$(this).parent().children("#name, #emailFrom, #subject, #message").css({ "border" : "1px solid #e4e4e4", "border-top" : "1px solid #eee", "border-left" : "1px solid #eee" });
			
			$(this).css({ 'opacity' : '.5', 'width' : 'auto' });
   						
			$(this).val('Message Sent!');
			
			$.post("emailForm.php",
   				{ subject: subjectVal, message: messageVal, mailFrom: emailFromVal, from: senderNameVal + "<request@yoursite.com>", headers: "From: " + from + "\r\nReply-To: " + mailFrom },
   					function(data) {
   					});
		}
		
		return false;
	});
});
