(function($) {

    /*
     * Auto-growing textareas; technique ripped from Facebook
     */
    $.fn.autogrow = function(options) {
        
        this.filter('textarea').each(function() {
            
            var $this       = $(this),
                minHeight   = $this.height(),
                lineHeight  = $this.css('lineHeight');
            
            var shadow = $('<div></div>').css({
                position:   'absolute',
                top:        -10000,
                left:       -10000,
                width:      $(this).width(),
                fontSize:   $this.css('fontSize'),
                fontFamily: $this.css('fontFamily'),
                lineHeight: $this.css('lineHeight'),
                resize:     'none'
            }).appendTo(document.body);
            
            var update = function() {
                
                var val = this.value.replace(/</g, '&lt;')
                                    .replace(/>/g, '&gt;')
                                    .replace(/&/g, '&amp;')
                                    .replace(/\n/g, '<br/>');
                
                shadow.html(val);
                $(this).css('height', Math.max(shadow.height() + 20, minHeight));
            
            }
            
            $(this).change(update).keyup(update).keydown(update);
            
            update.apply(this);
            
        });
        
        return this;
        
    }
    
})(jQuery);


$(document).ready(function() {

    $("select.disabled").attr('disabled', true);

	$(function() {
    	var allOptions = $('#categoryItems option').clone();
   		$('#selectCategory').change(function() {
       	var val = $(this).val();
        $('#categoryItems').html(allOptions.filter('.option-' + val));
   		});

       	 
    $("#selectCategory").change(function() {
        if ($(this).attr("value") === "2") {
            $("select.disabled").attr('disabled', false);
        }
        else if ($(this).attr("value") === "3") {
            $("select.disabled").attr('disabled', false);
        } 
        else { 
        	$("select.disabled").attr('disabled', true);
        }		
    });

   		
	});

	//Auto Grow Textareas
	$('textarea').autogrow();

	//When page loads...
	
	//Hide all content
	$(".tabbed_content").hide();
	
	//Show first tab content
	$(".tabbed_content:first").show(); 
	
	//On Hover Event
	
	//First - Build the Div that will hold content
	$('#sub_navigation').append('<div class="link_description"></div>'); 
	
	//Find the alt tag content on hover and place into the appended div
	$('#sub_navigation li a[alt]').each(function() {
		//Add content when hovered
		$(this).hover(function(e) {
			$('.link_description').html($(this).attr('alt')).fadeIn('fast');
		}, function() {
		//Remove content when hover off
			$('.link_description').fadeOut('fast');
		});
	});
	
	//Find the alt tag content on hover and place into the appended div
	$('#sub_navigation_linkable li a[alt]').each(function() {
		//Add content when hovered
		$(this).hover(function(e) {
			$('.link_description').html($(this).attr('alt')).fadeIn('fast');
		}, function() {
		//Remove content when hover off
			$('.link_description').fadeOut('fast');
		});
	});
	
	//On Click Event
	$("#sub_navigation li a").click(function() {
		//Hide all tab content
		$(".tabbed_content").hide(); 
		
		//Find the href attribute value to identify the active tab + content
		var activeTab = $(this).attr("href"); 
		//Fade in the active ID content
		$(activeTab).fadeIn(); 
		return false;
	});

});