
// fetch job details for the specified job
function fetchJobDetails(jobID) {
	// set the script path on server side
	// alert(serverPath + '?jobID=' + jobID);
	$.post(serverPath, {jobID: jobID}, function(xml) {
		// handle Filter response
		handleFetchResponse(xml);
	});
}

// replace the job in link with a new one
function replaceLinkedJobID(ctrlID, id) {
	var ctrlLink = $(ctrlID)[0].href;
	ctrlLink = ctrlLink.substring(0, ctrlLink.lastIndexOf('/') + 1);
	ctrlLink += id;
	$(ctrlID)[0].href = ctrlLink;
}

//active or deactive job or professional
function activeListingItem(e, linkName) {
	e.preventDefault();

	// get id and parameter
	var params = linkName.split(':');
	var id = params[0];
	var status = params[1];
	
	// set the script path on server side
	$.post(activePath, {id: id, status: status}, function(xml) {
	 	// handle Filter response
	 	handleStatusResponse(xml);
	});	
}

//handle Filter response
function handleStatusResponse(xml) {
	var id = '#' + $("id", xml).text();
	var status = $("link", xml).text();
	if ('null' != status) {
		$(".cellStatus").html(status);
	}

	registerClickEvent();
}

function registerClickEvent() {
	$("#jobEdit .cellStatus a").click(function(e) {
		var linkName = $(this)[0].name;
		activeListingItem(e, linkName);
	});	
}

// handle Fetch response and populate the job details on Job board
function handleFetchResponse(xml) {
	// get jobs count
	var counter = $("counter", xml).text();
	if (counter > 0) {
		// populate job related information on career board
		$("#jobTitle").html($("job > title", xml).text());
		$("#employer").html($("job > employment", xml).text());
		
		var refId = $("job > refid", xml).text();
		$(".jobDetails #refID").html(refId);
		
		var title = $("job > title", xml).text();
		$(".jobDetails #title").html(title);
		
		var discipline = $("job > discipline", xml).text();
		$(".jobDetails #discipline").html(discipline);
		
		var subject = refId + ' - ' + title;
		var city = $("city", xml).text();	
		$(".jobDetails #city").html(city);
		if(city != ''){
			subject += ' - ' + city;
			
			$(".jobDetails #city").parent().show();
			$(".jobDetails #city").html(city);
		}else{
			$(".jobDetails #city").parent().hide();
			$(".jobDetails #city").html('N/A');
		}
		
		var location = $("job > location", xml).text();
		$(".jobDetails #location").html(location);
		if ('' != location) {
			subject += ' - ' + location;
		}
		
		var activities = $("activities", xml).text();
		if(activities != ''){
			$(".jobDetails #activities").parent().show();
			$(".jobDetails #activities").html(activities);
		}else{
			$(".jobDetails #activities").parent().hide();
			$(".jobDetails #activities").html('N/A');
		}
		
		var profile = $("profile", xml).text();
		if(profile != ''){
			$(".jobDetails #profile").parent().show();
			$(".jobDetails #profile").html(profile);
		}else{
			$(".jobDetails #profile").parent().hide();
			$(".jobDetails #profile").html('N/A');
		}
		
		var remark = $("remark", xml).text();
		if(remark != ''){
			$(".jobDetails #remark").parent().show();
			$(".jobDetails #remark").html(remark);
		}else{
			$(".jobDetails #remark").parent().hide();
			$(".jobDetails #remark").html('N/A');
		}
		
		// output consultant related information
		$(".jobDetails #consultantName").html($("consultant_name", xml).text());
		
		var consultantEmail = $("consultant_email", xml).text();
		var consultantEmailLink = '<a href="mailto:{email}?subject={subject}" class="job_link" style="font-weight: normal;">{email}</a>';
		consultantEmailLink = consultantEmailLink.replace(/{email}/gi, consultantEmail);
		consultantEmailLink = consultantEmailLink.replace(/{subject}/gi, subject);
		$(".jobDetails #consultantEmail").html(consultantEmailLink);
		
		// for frontend career board only
		if ('backend' != boardType) {
			var serviceEmailLink = '<a href="mailto:{email}?subject={subject}" class="job_link" style="font-weight: normal;">{email}</a>';
			serviceEmailLink = serviceEmailLink.replace(/{email}/gi, 'info@dinel.nl');
			serviceEmailLink = serviceEmailLink.replace(/{subject}/gi, subject);		
			$(".jobDetails #serviceEmailLink").html(serviceEmailLink);
		}
		
		// replace mail, print, pdf and save link
		// for frontend only
		var id = $("id", xml).text();
		if ('backend' != boardType) {
			// job detials layer
			replaceLinkedJobID(".jobDetails #applyLink", id);
			
			// #more layer
			replaceLinkedJobID("#more #mailLink", id);
			replaceLinkedJobID("#more #pdfLink", id);
			replaceLinkedJobID("#more #saveLink", id);

			// as well as Apply directly and More information link
			replaceLinkedJobID("#more #link_job_apply", id);
			replaceLinkedJobID("#more #link_more_info", id);
			
			// update save status to indicate this job was saved or not
			var status = $("save_status", xml).text();
			$("#more #saveLink").html(status);
		} else {
			// show employer name
			var employer = $("employer", xml).text();
			if(employer != ''){
				$(".jobDetails #employer").parent().show();
				$(".jobDetails #employer").html(employer);
			}else{
				$(".jobDetails #employer").parent().hide();
				$(".jobDetails #employer").html('N/A');
			}

			// show vacancy details
			var vacancy = $("vacancy_details", xml).text();
			if(vacancy != ''){
				$(".jobDetails #vacancy_details").parent().show();
				$(".jobDetails #vacancy_details").html(vacancy);
			}else{
				$(".jobDetails #vacancy_details").parent().hide();
				$(".jobDetails #vacancy_details").html('N/A');
			}
			
			replaceLinkedJobID("#jobEdit .pdfLinkIcon", id);
			replaceLinkedJobID("#jobEdit .editLinkIcon", id);
			replaceLinkedJobID("#jobEdit .deleteLinkIcon", id);
			
			// construct status link
			var status = $("active", xml).text();
			var statusLink = '';
			if ('1' == status) {
				statusLink = '<span class="cellStatus"><a class="active" name="' + id + ':deactive" href="#">&nbsp;&nbsp;</a></span>';
			} else {
				statusLink = '<span class="cellStatus"><a class="deactive" name="' + id + ':active" href="#">&nbsp;&nbsp;</a></span>';
			}
			
			$(".cellStatus").html(statusLink);
			registerClickEvent();
			
			// update view, save, forward and application count
			var viewCount = $("views", xml).text();
			$(".jobDetails #viewCount").html(viewCount);
			
			
			var saveCount = $("saves", xml).text();
			$(".jobDetails #saveCount").html(saveCount);
			
			var printCount = $("prints", xml).text();
			$(".jobDetails #printCount").html(printCount);
			
			var forwardCount = $("forwards", xml).text();
			$(".jobDetails #forwardCount").html(forwardCount);
		}
		
	} else {
		// job no found
		var errorMessage = $("error", xml).text();
		$("#right").html(errorMessage);
	}
}

$(document).ready(function() {
	var myHistory = new JobListing();
	myHistory.load();
	
	// get ID set
	var jobIDSet = myHistory.jobIDSet;
	//alert(jobIDSet);
	var currentJobID = myHistory.currentJobID;
	if ('backend' != boardType) {
		currentJobID = currentJobID.substring(currentJobID.lastIndexOf('-') + 1);
	} else {
		currentJobID = currentJobID.substring(currentJobID.lastIndexOf('/') + 1);
	}
	
    var jobListing = jobIDSet.split(',');
    var listingLength = jobListing.length;
    
    // get the index of the current job
    var currentJobIndex = 0;
    for (i=0; i < listingLength; i++) {
    	if (jobListing[i] == currentJobID) {
    		currentJobIndex = i;
    		break;
    	}
    }
	
	// check if we need to show Previous/Next or not
	if ('' == jobIDSet) {
		$(".previousJob a").addClass("hide");
		$(".nextJob a").addClass("hide");		
	}

	$(".previousJob a").click(function(e) {
		e.preventDefault();
		currentJobIndex--;
		if (-1 == currentJobIndex) {
			currentJobIndex = listingLength - 1;
		}
		jobID = jobListing[currentJobIndex];
			
		// fetch job details for the specified job
		fetchJobDetails(jobID);		
	})
	
	$(".nextJob a").click(function(e) {
		e.preventDefault();
		currentJobIndex++;
		if (listingLength == currentJobIndex) {
			currentJobIndex = 0;
		}
		jobID = jobListing[currentJobIndex];
		
		// fetch job details for the specified job
		fetchJobDetails(jobID);		
	})
	
	// only for backend professional board
	if ('backend' == boardType) {
		registerClickEvent();
	}
});

