function defaultValues() {
	// default text
	if (document.getElementById('search-form')) {
		var searchform = document.getElementById('search-form');
		// set the text in the field to our default text
		searchform.keyword.value=searchtext;
		// if field is default text clear it out, otherwise leave it
		$('input#keyword').focus( function(){
			if(searchform.keyword.value==searchtext){
				searchform.keyword.value='';
			}
		});
		// if it is empty put the default text
		$('input#keyword').blur( function(){
			if(searchform.keyword.value==''){
				searchform.keyword.value=searchtext;
			}
		});
		searchform.location.value=locationtext;
		// if field is default text clear it out, otherwise leave it
		$('input#location').focus( function(){
			if(searchform.location.value==locationtext){
				searchform.location.value='';
			}
		});
		// if it is empty put the default text
		$('input#location').blur( function(){
			if(searchform.location.value==''){
				searchform.location.value=locationtext;
			}
		});
		// remove default text on submit
		$('form#search-form').submit( function(){
			if(searchform.keyword.value==searchtext){
				searchform.keyword.value='';
			}
			if(searchform.location.value==locationtext){
				searchform.location.value='';
			}
		});
	}
	
	if (document.getElementById('school-one')) {
		var profileform = document.getElementById('profile-info');
		var schooltext = "Start typing your school's name";
		if ($('#school-one').val()) {
		} else {
			$('#school-one').val("Start typing your school's name");
			$('#school-one').focus( function(){
				if($('#school-one').val()=="Start typing your school's name"){
					$('#school-one').val("");
				}
			});
			$('#school-one').blur( function(){
				if($('#school-one').val()==""){
					$('#school-one').val("Start typing your school's name");
				}
			});
		}
		if ($('#school-two').val()) {
		} else {
			$('#school-two').val("Start typing your school's name");
			$('#school-two').focus( function(){
				if($('#school-two').val()=="Start typing your school's name"){
					$('#school-two').val("");
				}
			});
			$('#school-two').blur( function(){
				if($('#school-two').val()==""){
					$('#school-two').val("Start typing your school's name");
				}
			});
		}
	}
	
}

function createModals() {
	$('.job-contact-info').jqm({modal: true, trigger: 'span.job-company'});
	$(".jqmWindow h4").prepend('<a class="close" href="#">Close</a>');
	$('.jqmWindow').jqmAddClose('.close');
}

function additionalFields() {
	// languages
	$('<a id="languages-expand" class="form-expand" href="#additional-languages">Do you speak other languages?</a>').insertBefore('#additional-languages');
  $("#additional-languages").hide();
  // address line two
  $('<a id="address-expand" class="form-expand" href="#additional-address">Need line two?</a>').insertBefore('#additional-address');
  $("#additional-address").hide();
  // outside the united states
  $('<a id="countries-expand" class="form-expand" href="#country">Outside the United States?</a>').insertBefore('#show-countries');
  $("#show-countries").hide();
  // more schools
  $('<a id="schools-expand" class="form-expand" href="#additional-school">Need to add another school?</a>').insertBefore('#additional-school');
  $("#additional-school").hide();
  // expand them
  $("#languages-expand").click(function() {
    $(this).next("#additional-languages").slideToggle(600);
    $("#languages-expand").hide();
  });
  $("#address-expand").click(function() {
    $(this).next("#additional-address").slideToggle(600);
    $("#address-expand").hide();
  });
  $("#countries-expand").click(function() {
    $(this).next("#show-countries").slideToggle(600);
    $("#countries-expand").hide();
  });
  $("#schools-expand").click(function() {
    $(this).next("#additional-school").slideToggle(600);
    $("#schools-expand").hide();
  });
}

function suggestValues() {
	$("#school-one").autocomplete("/autosuggest/schools/", {
		width: 260,
		selectFirst: false
	});
	$("#school-two").autocomplete("/autosuggest/schools/", {
		width: 260,
		selectFirst: false
	});
}

function editUserInfo() {
	$('h3#basic-info').append('<a id="expand-info" class="info-expand" href="#">Show</a>');
	$("#expand-info").click(function() {
    $(".vcard").slideToggle(600);
    $("#expand-info").text($("#expand-info").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $(".vcard").hide();
  $('h3#desired-job').append('<a id="expand-job" class="info-expand" href="#">Show</a>');
	$("#expand-job").click(function() {
    $("#job-info").slideToggle(600);
    $("#expand-job").text($("#expand-job").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $("#job-info").hide();
  $('h3#education-work').append('<a id="expand-work" class="info-expand" href="#">Show</a>');
	$("#expand-work").click(function() {
    $("#work-info").slideToggle(600);
    $("#expand-work").text($("#expand-work").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $("#work-info").hide();
  $('h3#user-resume').append('<a id="expand-resume" class="info-expand" href="#">Show</a>');
	$("#expand-resume").click(function() {
    $("#resume-info").slideToggle(600);
    $("#expand-resume").text($("#expand-resume").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $("#resume-info").hide();
  $('h3#user-applications').append('<a id="expand-applications" class="info-expand" href="#">Show</a>');
	$("#expand-applications").click(function() {
    $("#applications-info").slideToggle(600);
    $("#expand-applications").text($("#expand-applications").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $("#applications-info").hide();
  $('h3#user-alerts').append('<a id="expand-alerts" class="info-expand" href="#">Show</a>');
	$("#expand-alerts").click(function() {
    $("#alerts-info").slideToggle(600);
    $("#expand-alerts").text($("#expand-alerts").text() == 'Show' ? 'Hide' : 'Show');
    return false;
  });
  $("#alerts-info").hide();
}

function validateForms() {
	if (document.getElementById('profile-info')) {
		$("#profile-info").validate();
	}
	if (document.getElementById('profile-info')) {
		$("#profile-info").validate();
	}
	if (document.getElementById('update-profile-info')) {
		$("#update-profile-info").validate({
    	rules:{
    		email:{
					required:true,
					email:true
				},
				password:{
					minlength:6,
					maxlength:15
				},
				confirm_password:{
					equalTo:"#password"
				}
			}
		});
	}
	if (document.getElementById('update-profile-info')) {
		$("#update-profile-info").validate();
	}
}

function refineSearch() {
	if (document.getElementById('refine-form')) {
		$('<a id="refine-link" href="#">Edit Job Search</a>').insertBefore("#refine-form");
		$("#refine-form").hide();
		$("#refine-link").click(function() {
	  	$(this).next("#refine-form").slideToggle(600);
	  	$("#refine-link").hide();
	  	return false;
	  });
	}
	if (document.getElementById('recs-refine-form')) {
		$('<a id="recs-refine-link" href="#">Not seeing the positions you’re looking for?  Click here and control your search.</a>').insertBefore("#recs-refine-form");
		$("#recs-refine-form").hide();
		$("#recs-refine-link").click(function() {
	  	$(this).next("#recs-refine-form").slideToggle(600);
	  	$("#recs-refine-link").hide();
	  	return false;
	  });
	}
}

function dateSelector() {
	$('.date-pick').datePicker({clickInput:true,startDate:'01/01/1950'})
}

$(document).ready(function(){
	$("body").addClass("jquery"); // define as javascript ready
	$("dl.job-details dt:first-child").css({ borderTop:"0"});
	$("dl.job-details dd:nth-child(2)").css({ borderTop:"0"});
	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
	defaultValues();
	additionalFields();
	createModals();
	dateSelector();
	suggestValues();
	refineSearch();
	validateForms();
	editUserInfo();
});