/* Accordion Menu
_________________________*/

$(document).ready(function(){
	$("dd").hide();
	$("dt a").click(function(){
		if( $(this).parent().next().css('display') == "block" ) {
		  
		  $("dd:visible").slideUp("slow", function(){$.scrollTo( $('body'), 1000)});
		  $("dd:visible").prev().find("a span:first-child").html("{ <u>Read</u> }");
        
		} else {
			
		  $("dd:visible").slideUp("slow");
		  $("dd:visible").prev().find("a span:first-child").html("{ <u>Read</u> }");
		  $(this).find("span:first-child").html("{ <u>Close</u> }");
		  $(this).parent().next().slideDown("slow", function(){$.scrollTo( $(this).prev(), 500, {offset:-10})});
		
		}
		return false;
	});
});



/* Form Text Replacement
___________________________________*/

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);

$(document).ready(function(){
    
   $(function(){ 
        // find all the input elements with title attributes
        $('input[title!=""], textarea[title!=""]').hint();
    }); 
});


/* Initialize TypeKit
___________________________________*/
try{Typekit.load();}catch(e){}



/* Change <ul> <li> styles for News & Events pages
_________________________________________________________*/

$(document).ready(function(){
    
    if( $(".blog-post").parent().parent().is("ul") )
    {
        $(".news-events").find(".blog-post").parent().css("margin", 0);
        $(".news-events").find(".blog-post").parent().parent().css("margin",0).addClass("no-bullet");
    }
});


/* Newsletters & Publications
   Sort through file list
_______________________________________________________*/

var month = new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

$(document).ready(function(){

    $("#sort-file-list").submit(function(e){
        
        e.preventDefault();
        
        var m = $("#month").val();
        for( x=0; x<12; x++ ){
            if( m == month[x] ){
                m = x;
			}
		}
        // month now correlates with correct array index
        
        var y  = parseInt($("#year").val());
        
        $(".file-list li").each(function (){
            
		    var d = $(this).attr("class");
            d = d.split(" ");
            d = d.splice(1);
            d[1] = parseInt(d[1]); // document year
            
            for( i=0; i<12; i++ ){
                if( d[0] == month[i] ){
                    d[0] = i;
				}
			}
            
            // show documents
            if( d[1] >= y ){
                $(this).css("display", "block");
			}
            
            // hide documents
            if( d[1] < y ){
                $(this).css("display", "none");
			
            }
            
            if( d[1] == y && d[0] < m ){
                $(this).css("display", "none");
			}
        });
        
    });
});


/* Form Validation
______________________________*/

$(document).ready(function(){

    $("input[type=tel]").mask("(999) 999-9999");
    $("input.zip").mask("99999");
    
    // a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
    jQuery.validator.addMethod("defaultInvalid", function(value, element) {
    return value != element.defaultValue;
    }, ""); 
    
    $("#newsletter-signup-form").validate({
    //set the rules for the field names
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            address: {
                required: true,
                minlength: 2
            },
            city: {
                required: true,
                minlength: 2
			},
            state: {
                required: true
			},
            zip: {
                required: true,
                minlength: 5,
                number: true
			}
        },
        //set messages to appear inline
        messages: {
            name: "*Please enter your name",
            email: "*Please enter a valid email address",
            address: "*Please enter your address",
            city: "*Please enter your city",
            state: "*Please select your state",
            zip: "*Please enter your zip code"
        }
    });
    
    
    // Contact Us
    $("#contact-us-form").validate({
       
        rules: {
            name: {
                required: true,
                minlength: 2
			},
            email: {
                required: true,
                email: true
			},
            subject: {
                required: true,
                minlength: 2
			},
            message: {
                required: true,
                minlength: 2
			}
		},
        
        messages: {
            name: "*Please enter your name",
            email: "*Please enter a valid email address",
            subject: "*Please title your message",
            message: "*Please enter a message"
		}
	});
});
