/**
 * GetSchooled WP Lib
 * 
 * Custom JavaScript functions and objects
 * Written for the GetSchooled WordPress Theme.
 * 
 * @version 0.1
 */




/**
 * Support old URLs from the Flash site
 */
function anchor_to_page() {

	// Only operate on the directory "index" (no route):
	if (window.location.pathname != "/")
		return;
	
	var _tra = [
		["home", "/"],

		["about_us", "/about"],
		["privacy", "/privacy"],
		["terms_of_use", "/terms-of-use"],
		["copyright_policy", "/copyright-policy"],
		
		["students/find_an_awesome_career", "/get-a-career"],
		["students/get_back_in_school", "/category/finish-school"],
		["resources/watch_the_show", "/get-schooled-you-have-the-right"],

		["resources/write_your_governor", "/get-involved/write-your-governer"],
		["resources/students_bill_of_rights", "/get-involved/students-bill-of-rights"],
		["resources/finance_your_future", "/category/scholarships"],
		["resources/finance_your_future/scholarships", "/fafsa-scholarships-and-government-grants"],
		["resources/finance_your_future/loans", "/student-loans"],
		["resources/finance_your_future/work_study", "/work-study-internships-rotc-and-more"],
		["resources/finance_your_future/costs", "/check-out-the-costs"],

		["partners/support_the_cause", "/about/support-the-cause"],
		["partners/featured_partners_affiliates", "/about/meet-our-partners"],

		['jobs/police_officer', '/police-officer'],
		['jobs/automotive_engineer', '/automotive-engineer'],
		['jobs/emt_worker', '/emt-worker'],
		['jobs/mechanical_engineer', '/mechanical-engineer'],
		['jobs/auto_mechanic', '/auto-mechanic'],
		['jobs/ecologist', '/ecologist'],
		['jobs/architect', '/architect'],
		['jobs/industrial_designer', '/industrial-designer'],
		['jobs/agronomist', '/agronomist'],
		['jobs/environmental_engineer', '/environmental-engineer'],
		['jobs/urban_planner', '/urban-planner'],
		['jobs/biomedical_equipment_tech', '/biomedical-equipment-tech'],
		['jobs/heart_surgeon', '/heart-surgeon'],
		['jobs/dental_hygienist', '/dental-hygienist'],
		['jobs/occupational_therapist', '/occupational-therapist'],
		['jobs/animator', '/animator'],
		['jobs/video_game_designer', '/video-game-designer'],
		['jobs/graphic_designer', '/graphic-designer'],
		['jobs/software_developer', '/software-developer'],
		['jobs/sound_designer', '/sound-designer'],
		['jobs/event_planner', '/event-planner'],
		['jobs/chef', '/chef'],
		['jobs/photographer', '/photographer'],
		['jobs/cruise_director', '/cruise-director'],
		['jobs/lighting_designer', '/lighting-designer'],
		['jobs/groundskeeper', '/groundskeeper'],
		['jobs/nutritionist', '/nutritionist'],
		['jobs/physical_therapist', '/physical-therapist'],
		['jobs/sportscaster', '/sportscaster'],
		['jobs/athletic_trainer', '/athletic-trainer'],
		['jobs/marine_biologist', '/marine-biologist'],
		['jobs/paleontologist', '/paleontologist'],
		['jobs/park_ranger', '/park-ranger'],
		['jobs/veterinarian', '/veterinarian'],
		['jobs/zoologist', '/zoologist'],
		['jobs/aerospace_engineer', '/aerospace-engineer'],
		['jobs/astronomer', '/astronomer'],
		['jobs/aviation_mechanic', '/aviation-mechanic'],
		['jobs/meteorologist', '/meteorologist'],
		['jobs/physicist', '/physicist'],
		['jobs/attorney', '/attorney'],
		['jobs/corrections_officer', '/corrections-officer'],
		['jobs/opera_singer', '/opera-singer'],
		['jobs/physical_education_teacher', '/physical-education-teacher'],
		['jobs/advertising_account_executive', '/advertising-account-executive'],
		['jobs/financial_planner', '/financial-planner'],
		['jobs/general_contractor', '/general-contractor'],
		['jobs/interior_designer', '/interior-designer'],
		['jobs/realtor', '/realtor'],
		['jobs/sales_representative', '/sales-representative'],
		['jobs/social_worker', '/social-worker'],
		['jobs/stock_broker', '/stock-broker'],
		['jobs/interpreter', '/interpreter'],
		['jobs/advertising_creative', '/advertising-creative'],
		['jobs/broadcast_producer', '/broadcast-producer'],
		['jobs/cinematographer', '/cinematographer'],
		['jobs/film_director', '/film-director'],
		['jobs/screen_writer', '/screen-writer'],
		['jobs/set_designer', '/set-designer'],
		['jobs/baggage_handler', '/baggage-handler'],
		['jobs/canned_good_inspector', '/canned-good-inspector'],
		['jobs/cashier', '/cashier'],
		['jobs/cosmetologist', '/cosmetologist'],
		['jobs/machine_operator', '/machine-operator'],
		['jobs/publicist', '/publicist'],
		['jobs/retail_salesperson', '/retail-salesperson'],
		['jobs/registered_nurse', '/registered-nurse']
		
	];

	for (var i = 0; i < _tra.length; i++) {
		if (_tra[i][0] === window.location.hash || ("#/"+_tra[i][0]) === window.location.hash) {
			window.location = _tra[i][1];
			return;
		}
	}
	
}

/**
 * Set a default value / clear behavior for form fields
 */
$.fn.defaultBind = function(base) {
	var _base = base;
	$(this)
		.val(_base)
		.focus(function(){ if ($(this).val() == _base) { $(this).val(""); } })
		.blur(function(){ if ($(this).val() == "") { $(this).val(_base); } });
	return;
}

/**
 * Quick javascript form validation
 */
$.fn.formValidate = function(type) {
	
	switch(type) {

		// Not null
		default:
		case "notnull":
			return ($(this).val() !== "" && $(this).val() !== null);
			break;

		// Valid Email
		case "email":
			return $(this).emailValidate();
			break;

		case "MM/DD/YYYY":
			
			return $(this).val().match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/);
			break;
			
		// Valid Date, and > 13 yrs
		case "agegate_13":

			// blow up the date
			var date = $(this).val();
			var da = date.split("/");
			
			// We should have 3 tokens
			if (!da.length || da.length != 3)
				return false;

			// Check age:
			var then = new Date(da[2], da[0], da[1]);
			var now = new Date();
			var diff = ( now.valueOf() - then.valueOf() );
			var age = parseInt(diff / (1000 * 60 * 60 * 24 * 365)); // get years

			if (age < 13)
				return false;

			break;
	}

	return true;
}

/**
 * Validate an Email Address with simple regex
 * @param string
 * @return bool
 */
$.fn.emailValidate = function(str) {

	// Optionally load string to validate from current selector instead of param
	if (!str) {
		if ($(this).val()) {
			str = $(this).val();
		}
	}
	
	var reggie = new RegExp(/^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(\d{1,3}\.){3}\d{1,3}\]?)$/i);
	if (typeof(str) === "string" && reggie.test(str)) {
		return true;
	}
	return false;
}

