// JavaScript Document

if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	
 $.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };	

$(document).ready( function() {
	
	
	
	
	var hpmessage = $('#hpmessage')
	if( $(hpmessage).length > 0 ) {
		$(hpmessage).hide();
		var content = $.trim( $(hpmessage).find('.content').html() );
		if(content != "") {
			$(hpmessage).wait().slideDown('slow');				
		}
	}
	
	var invalidValues = [];
	
	jQuery.validator.addMethod("notdefault", function(value, element) {
		var idx = invalidValues.indexOf(value);
		if(idx == -1) {
			return true;
		}
		else {
			return false;
		}
	}, "This field is required!");  
	
	
	
	$('#contactForm label').each( function() {
		$(this).hide();
		var text = $(this).html();
		if(text.match(/\*/)) {
			invalidValues.push(text);
		}
		
		$(this).parent().find('input').each( function() {
			$(this).val(text);
			
			$(this).blur( function() {
				if($(this).val() == "") {
					$(this).val(text);	
				}
			});
			
			$(this).focus( function() {
				var val = $(this).val()
				if(val == text) {
					$(this).val('');	
				}
			});
		});
		
	    $(this).parent().find('textarea').each( function() {
			$(this).html(text);
			
			$(this).blur( function() {
				var currenthtml = $(this).val() || $(this).html();
				if(currenthtml == "") {
					$(this).html(text);	
				}
			});
			
			$(this).focus( function() {
				var val =  $(this).val() || $(this).html();
				if(val == text) {
					$(this).html('');	
				}
			});
		});		
		
	});
	
	$("#contactForm").validate({submitHandler: function(form) {
			if($('#contactForm').valid()) {
				$("#contactForm #valid").val('SubmitOK');
				$.ajax({
				  type: 'POST',
				  url: 'formhandler.cfm',
				  data: $("#contactForm").serialize(),
				  success: function(message) {
					  _gaq.push(['_trackPageview',"/contact/"+window.location.pathname]);
					  $('#contactFormWrapper').slideUp("slow", function() {
						 	$("#contactForm").html('<div>Thanks for contacting us!</div>'); 
							 $('#contactFormWrapper').slideDown("slow");
					  });
				  }
				});	
			}
			return false;
	 }});
});
