$(document).ready(function() {
	
	// Call the "easycruit" module to get the json data containing the tickerline data.
	var easycruit_fullview_url = $("#easycruit_fullview_url").val();
	var number_of_items = $("#easycruit_number_of_items").val();
	var url = window.location.protocol + "//" + window.location.hostname + "/easycruit/get/json/" + number_of_items;
	var show_brand_only = $("#easycruit_show_brand_only").val();
	var department_id = $("#easycruit_department_id").val();
	var departments = new Array();
	
// This is dissabled. Status: onhold, please don't remove	
//	$("#easycruit_show_all_switcher").click(function(){
//		$("#easycruit_list.full tr.external_vacancy").toggle(1);
//		$("#easycruit_show_all_switcher_all").toggle(1);
//		$("#easycruit_show_all_department_name").toggle(1);
//	});
	
	$.getJSON(url, function(data) {
		$.each(data.vacancy_list, function(i,v) {
		if($("#easycruit_list ul").length>0){
			var li_content = ""; 
			li_content += '<li class= n'+ (i+1) + '>';
			li_content += '<span class="meta">' + v.departments.Department.Name + '</span>';
			li_content += '<a id="' + v.vacancy_id +'" href="'+ easycruit_fullview_url + '?vacancy=' + v.vacancy_id + '">' + v.versions.Version.Title + '</a>';
			li_content += '</li>';
			$("#easycruit_list ul").append(li_content);
		}
		else if($("#easycruit_list table").length>0){
			
			// This is dissabled. Status: onhold, please don't remove
			//if (show_brand_only == 1) {
			//	$("#easycruit_show_all_department_name:empty").text( v.departments.Department.Name );
			//}
			
			tr_class = 'local_vacancy';
			if ( department_id != 1 && show_brand_only == 1  && department_id != v.departments.Department.DepartmentId) {
				tr_class = 'external_vacancy';
			}
			
			var table_content = ""; 
			table_content += '<tr class="' + tr_class + '">';
			table_content += '<td><a id="' + v.vacancy_id +'" href="'+ easycruit_fullview_url + '?vacancy=' + v.vacancy_id + '">' + v.versions.Version.Title + '</a></td>'
			table_content += '<td>' + v.departments.Department.Name + '</td>';
			table_content += '<td>' + v.versions.Version.Location + '</td>';
			table_content += '<td>' + v.versions.Version.DateEnd + '</td>';
			table_content += '</tr>';
			$("#easycruit_list table tbody").append(table_content);
			
		}
		
		});
	});
	
	// Get the detail of the chosen vacancy.
	var url_parameter_array = getUrlVars();
	if ("vacancy" in url_parameter_array) {
		var vacancy_id = url_parameter_array["vacancy"]; 
		get_vacancy_detail(vacancy_id);
	}		

});

// Performed when the user clicks one of the vacancy links eather from the block view or from the full view.
$('#easycruit_list').find('a').live('click', function() {
	var vacancy_id = $(this).attr('id');
	get_vacancy_detail(vacancy_id);
	
	// We do NOT need to follow the link if we already are on the detail page. If the url contains the "vacancy" parameter we return false to abort the link. 
	var url_parameter_array = getUrlVars();
	if ("vacancy" in url_parameter_array) {
		return false;
	}		
	
});

// Call the vacancy_detail view of the easycruit module with the specified vacancy_id, and fill the detail in the html-fields.
function get_vacancy_detail(vacancy_id) {
	var detail_url = window.location.protocol + "//" + window.location.hostname + "/easycruit/vacancy_detail/" + vacancy_id;
	$.getJSON(detail_url, function(data) {
		$(".easycruitvacancy_details").css('visibility','visible');
		$("#easycruit_vacancy_info").css('visibility','visible');

		$("#easycruit_vavancy_logo").attr("src", data.vacancy_detail.Departments.Department.LogoURL);
		
		$("#easycruit_vavancy_about").text("");
		$("#easycruit_vavancy_about").append(data.vacancy_detail.Departments.Department.Versions.Version.About);

		$("#easycruit_vacancy_title_heading").text("");
		$("#easycruit_vacancy_title_heading").append(data.vacancy_detail.Versions.Version.TitleHeading);
		//we need the same top margin for Apply button if title heading present or not  
		if ($.trim($("#easycruit_vacancy_title_heading").html())) {
			$("#easycruit_vacancy_info_apply").addClass("with_title_heading");
                    }
		
		$("#easycruit_vacancy_title").text("");
		$("#easycruit_vacancy_title").append(data.vacancy_detail.Versions.Version.Title);
		
		$("#easycruit_vacancy_main_description").text("");
		$("#easycruit_vacancy_main_description").append(data.vacancy_detail.Versions.Version.Description);

		$("#easycruit_vacancy_info_jobtype").next('p').remove();           
		$("#easycruit_vacancy_info_jobtype").after("<p>" + data.duration + "</p>");

		$("#easycruit_vacancy_info_fulltime_parttime").next('p').remove();
		$("#easycruit_vacancy_info_fulltime_parttime").after("<p>" + data.extent + "</p>");

		$("#easycruit_vacancy_info_working_hours").next('p').remove();
		$("#easycruit_vacancy_info_working_hours").after("<p>" + data.operating_time + "</p>");
		
		$("#easycruit_vacancy_info_workplace").next('p').remove();                		                  
                if ( data.location !="" )  {                          
			$("#easycruit_vacancy_info_workplace").after("<p>" + data.location + "</p>");                       
                }
                     
		$("#easycruit_vacancy_info_commence").next('p').remove();                  
		if ( data.engagement !="" ){                 
			$("#easycruit_vacancy_info_commence").after("<p>" + data.engagement + "</p>");
		}

		$("#easycruit_vacancy_info_deadline").next('p').remove();                
		if ( data.date_end !="") {                 
			 $("#easycruit_vacancy_info_deadline").after("<p>" + data.date_end + "</p>");                            
                        }		
		
		$("#easycruit_vacancy_info_contactpersons div").remove();
		if ( ! jQuery.isEmptyObject(data.vacancy_detail.Departments.Department.ContactPersons) ) {
			var numberOfContactPersons = data.vacancy_detail.Departments.Department.ContactPersons.ContactPerson.length;
			if (numberOfContactPersons === undefined) {
				var person = '<div class="person">';
				person += '<p class="name">' + data.vacancy_detail.Departments.Department.ContactPersons.ContactPerson.CommonName + '</p>';
				person += '<p class="telephone">' + data.vacancy_detail.Departments.Department.ContactPersons.ContactPerson.Telephone + '</p>';
				person += '</div>';
				$("#easycruit_vacancy_info_contactperson").after(person);
			} else {
				$.each(data.vacancy_detail.Departments.Department.ContactPersons.ContactPerson, function(i,v) {
					var person = '<div class="person">';
					person += '<p class="name">' + v.CommonName + '</p>';
					person += '<p class="telephone">' + v.Telephone + '</p>';
					person += '</div>';
					$("#easycruit_vacancy_info_contactperson").after(person);
				});
			}
		}

		$("#easycruit_vacancy_info_apply").attr('href',data.vacancy_detail.Departments.Department.ApplicationURL);
		//show the info on the page
		$(".easycruit_vacancy_about").show();
		$(".easycruit-position").show();
		window.scrollTo(0,0);
	});
}


//Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
