function addTB_iframe(a, w, h) {
    a.href = a.href + "&TB_iframe=true&width=" + w + "&height=" + h;
}

$(function() {
	$('#confidential').click(function() {
		if ($(this).is(':checked')) {
			$('#company_name').attr('disabled','disabled').val('');
		} else {
			$('#company_name').removeAttr('disabled');
		}
		return;
	})

	if($('#country').val() == '') {
		$("#item-stateprov").val('').hide();
		$("#item-city").val('').hide();
		$("#item-postalcode").val('').hide();
	}
	else if($('#country').val() == 'US') {
		$("#item-stateprov").hide();
		$("#item-city").hide();
		$("#item-postalcode").show();
	}
	else {
		$("#item-stateprov").show();
		$("#item-city").show();
		$("#item-postalcode").val('').hide();
	}
	
	$('#country').change(function() {
		$('#zip-message').remove();
		$('#state').val('');
		$('#city').val('');

		if($(this).val() == '') {
			$("#item-stateprov").hide();
			$("#item-city").hide();
			$("#item-postalcode").hide();
			return;
		}
		else if($(this).val() == 'US') {
			$("#item-stateprov").hide();
			$("#item-city").hide();
			$("#item-postalcode").show();
			$('#postal_code').addClass("micro").attr('maxlength','5').val('');
			return;
		}
		
		$("#item-stateprov").show();
		$("#item-city").show();
		$("#item-postalcode").val('').hide();
		$('#postal_code').removeClass("micro").addClass("medium").attr('maxlength','15').val('');
	});
	
	$('#zip').keyup(function() {
		var zip = $(this).val();
		if(($('#country').val() != 'US') || (zip.length != 5)) return;
		$.get('/services/locations/' + zip, function(data) {
			if($('locations location', data).size() == 0) {
				var message = $('<p id="zip-message" class="form-item error">Please enter a valid ZIP Code</p>');
				$('#zip-message').remove();
				$('#state').val('');
				$('#city').val('');
				$('#zip').after(message);
				return;
			}
			
			$('#zip-message').remove();
			var message = '<p id="zip-message" class="form-item success">';
			$('locations location', data).each(function() {
				var location = $(this);
				var state = location.find('state').text();
				var city = location.find('city').text();
				message += city + ', ' + state;
				$('#state').val(state);
				$('#city').val(city);
			});
			message += '</p>';
			$('#zip').after(message);
		});
	});
/*
	if ($("#show_profile_false").is(':checked')) {
		$('#show-profile-example').hide();
	}
	*/
	$('#apply_online_1').click(function() {
		if ($("#apply_online_1").is(':checked')) {
			$('#apply_email').removeAttr('disabled').focus();
			$('#apply_fax').attr('disabled','disabled');
			$('#apply_web').attr('disabled','disabled');
		}
		return;
	})
	$('#apply_online_0').click(function() {
		if ($("#apply_online_0").is(':checked')) {
			$('#apply_email').attr('disabled','disabled');
			$('#apply_fax').removeAttr('disabled').focus();
			$('#apply_web').removeAttr('disabled');
		}
		return;
	})

	if ($("#show_profile_false").is(':checked')) {
		$('#show-profile-example').hide();
	}
	$('#show-profile').click(function() {
		if ($("#show_profile_true").is(':checked')) {
			$('#show-profile-example').show();
		} else {
			$('#show-profile-example').hide();
		}
		return;
	})
	
	$("#country").bind('change', function(){
		if($("#country").val()=='US'){
			$("#item-zip").show();
		}else{
			$("#item-zip").hide();
		}
	});
	
	$(".discard-job").bind('click', function(){
		return confirm('Are you sure you would like to discard this job draft? This action cannot be undone.');
	});
	$(".close-job").bind('click', function(){
		return confirm('Are you sure you would like to remove this job from the Job Board? This action cannot be undone.');
	});

	$('#more-options').click(function() {
		$('#more-search-options').show();
		$('#item-postalcode').show();
		$('#country').val('US');
		$('#item-zip').show();
		$(this).hide();
		return false;
	});
	
	$("div#loading").hide();
	
	// This is for the paging links
	var paging_page = 1;
	
	function jobs_to_page(type, page) {
		var id = page - 1;
		
		if (paging_page == page) return false;
		
		// Let's enable the other paging links
		for (j=0; j<$("div#paging a").size(); j++) {
			if (id != j) $("div#paging a").eq(j).attr("class","enabled");
		}
		
		// Let's set the data variables
		var infoPieces = $("div#paging a").eq(id).attr("name").split(",");
		var currentPage = infoPieces[0];
		var queryLimit = infoPieces[1];
		var queryTotal = infoPieces[2];
		var pagesTotal = infoPieces[3];
		var currentBox = infoPieces[4];
		
		// Let's display/hide the previous/links
		if (id >= 1) $("span.previous").show(); else $("span.previous").hide();
		if (id != pagesTotal-1) $("span.next").show(); else $("span.next").hide();
		
		// Let's do the behind-the-scenes-call
		window.scroll(0, 0);
		$("div#loading").show(); 
		$(".manage-jobs-table").hide();
		
		if (type == "manage-jobs-table") {
			$.get("/services/myjobs/" + currentBox + "/" + queryLimit + "/" + currentPage, function(data) {
				if ($("applications application", data).size() > 0) {
					var table_content = "<tr><th>Job Title</th><th>Company</th><th>Date Applied</th><th>Resume Attachment</th></tr>";
					
					$("applications application", data).each(function(i) {
						var application = $(this);
						
						table_content += '<tr><td><a href="/jobs/' + application.find("application_id").text() + '"><strong>' + application.find("application_title").text() + '</strong></a></td><td><strong>' + application.find("application_company").text() + '</strong></td><td>' + application.find("date").text() + '</td><td>' + application.find("application_attachment").text() + '</td></tr>';
						
						if (i == $("applications application", data).size()-1) {
							$("div#loading").hide();
							$(".manage-jobs-table").empty().append(table_content).show();
						}
					});
				} else {
					$("div#loading").hide();
					$(".manage-jobs-table").show();
				}
			});
		} else {
			if (currentBox == "applied") {
				$.get("/services/myjobs/" + currentBox + "/" + queryLimit + "/" + currentPage, function(data) {
					if ($("applications application", data).size() > 0) {
						var table_content = "<tr><th>Job Title</th><th>Company</th><th>Date Applied</th><th>Resume Attachment</th></tr>";
						
						$("applications application", data).each(function(i) {
							var application = $(this);
							
							table_content += '<tr><td><a href="/jobs/' + application.find("application_id").text() + '"><strong>' + (application.find("application_title").text()? application.find("application_title").text() : '[No title]') + '</strong></a></td><td><strong> ' + application.find("application_company").text() + '</strong></td><td>' + application.find("date").text() + '</td><td>' + application.find("application_attachment").text() + '</td></tr>';
							
							if (i == $("applications application", data).size()-1) {
								$("div#loading").hide();
								$(".manage-jobs-table").empty().append(table_content).show();
							}
						});
					} else {
						$("div#loading").hide();
						$(".manage-jobs-table").show();
					}
				});
			} else {
				$.get("/services/myjobs/" + currentBox + "/" + queryLimit + "/" + currentPage, function(data) {
					if ($("jobs job", data).size() > 0) {
						var table_content = "<tr><th>Job Code</th><th>Job Title</th><th>" + (currentBox == "draft" ? "Modified" : "Posted") + "</th>";
	
						if (currentBox != "draft") {
							table_content += "<th>Expire";
							table_content += (currentBox == "published" ? "s" : "d");
							table_content += "</th>";
						}
						
						table_content += "<th>&nbsp;</th></tr>";
						
						$("jobs job", data).each(function(i) {
							var job = $(this);
							
							table_content += '<tr><td>' + job.find("job_code").text() + '</td><td>';
							
							if (job.find("is_draft").text() == 1)
								table_content += '<a href="/jobs/manage/edit/' + job.find("job_id").text() + '">';
							else 
								table_content += '<a href="/jobs/' + job.find("job_id").text() + '">';
							
							table_content += job.find("job_title").text() + '</a></td><td>' + job.find("job_date").text() + '</td>' + (currentBox != "draft" ? "<td>" + job.find("job_expiration_date").text() + "</td>" : "") + '<td>';
							
							if (job.find("is_draft").text() == 1)
								table_content += '<a class="discard-job" href="/jobs/manage/discard/' + job.find("job_id").text() + '">Discard</a>';
							else if (job.find("is_published").text() == 1)
								table_content += '<a class="close-job" href="/jobs/manage/close/' + job.find("job_id").text() + '">Close</a>';
							else
								table_content += '&nbsp;';
							
							table_content += '</td></tr>';
							
							if (i == $("jobs job", data).size()-1) {
								$("div#loading").hide();
								$(".manage-jobs-table").empty().append(table_content).show();
							}
						});
					}  else {
						$("div#loading").hide();
						$(".manage-jobs-table").show();
					}
				});
			}
		}
		
		$("div#paging a").eq(id).attr("class","disabled");
		paging_page = page;
	}
	
	$("span.next").click(function() {
		jobs_to_page($("div#paging a").eq(paging_page).attr("id"), paging_page+1);
	});
	
	$("span.previous").click(function() {
		jobs_to_page($("div#paging a").eq(paging_page-2).attr("id"), paging_page-1);
	});
	
	$("div#paging a").each(function(i) {
		$("div#paging a").eq(i).click(function() {			
			jobs_to_page($("div#paging a").eq(i).attr("id"), i+1);
		});
	});
});
