function DESemail(emailname,emailserver) {
	document.write("<a href='mailto:" + emailname + "@" + emailserver +"'>");
	document.write(emailname + "@" + emailserver); 
	document.write("</a>"); 
}

// Accessible Pop Ups
function DESopen(url,width,height,toolbar,scroll) {
	window.open(url, "", "scrollbars="+scroll+",toolbar="+toolbar+",height="+ height +",width="+width);
	return false;
}


// sets the select box values to 0 (called onload to prevent mismatch with shown jobs)
function setFilterDefaults() {
	if (document.getElementById("filter_contract")) document.getElementById("filter_contract").selectedIndex=0;
	if (document.getElementById("filter_terms")) document.getElementById("filter_terms").selectedIndex=0;
	if (document.getElementById("filter_location")) document.getElementById("filter_location").selectedIndex=0;
	
}

function init_job_list() {
	if (document.getElementById("job_list")) {
		
		var cookieDomain = 'roylerecruitment.localhost';
		//var cookieDomain = 'des02.co.uk';
		
		// hide all 'more details' for initial state
		$('.list .more_details').hide();
		
		// when more link clicked show/hide
		$('a.mlink').click( function() {
			var detailsID = $(this).attr("rel");
			if ($("#"+detailsID).css("display") == 'block') { // check if shown already
				$(this).text("Show Full Details");
				$("#"+detailsID).slideUp("fast");
			}
			else {
				$(this).text("Hide Full Details");
				$("#"+detailsID).slideDown("fast");
			}
			return false;
		});
		
		// Expanded and List Views
		$("#expanded_view").click(function() {
			$("#job_view_types a").removeClass("active");
			$('.more_details').show();
			$(this).addClass("active");
			$('.mlink').text("Hide Full Details");
			$.cookie('royle_view', 'expanded', { expires: 7, path: '/', domain: cookieDomain, secure: false }); // set cookie
			return false;
		});
		$("#list_view").click(function() {
			$("#job_view_types a").removeClass("active");
			$('.more_details').hide();
			$(this).addClass("active");
			$('.mlink').text("Show Full Details");
			$.cookie('royle_view', 'list', { expires: 7, path: '/', domain: cookieDomain, secure: false }); // set cookie
			return false;
		});
		
		
		// Make filters from select boxes based on classes in <li>
		$("select.select_filter").change( function() {
			
			// remove 'active' if set to default
			if ($(this).val() == 0) { $(this).removeClass("active"); }
			
			// hide all jobs to start
			$("#job_list li.easy_clear").hide();
			
			// get 3 values (are 3 possible select boxes)
			var contactFilter = $("#filter_contract").val();
			var locationFilter = $("#filter_location").val();
			var termsFilter = $("#filter_terms").val();
			
			// build 'match' string
			var matchString ='';
			if (document.getElementById("filter_contract") && contactFilter!=0) {
				$("#filter_contract").addClass("active");
				matchString = matchString+'.'+contactFilter;
			}
			if (document.getElementById("filter_location") && locationFilter!=0) {
				$("#filter_location").addClass("active");
				matchString = matchString+'.'+locationFilter;
			}
			if (document.getElementById("filter_terms") && termsFilter!=0) {
				$("#filter_terms").addClass("active");
				matchString = matchString+'.'+termsFilter;
			}
			
			//alert(matchString);
			
			// if we have something to match
			if (matchString != '') {
				// loop li
				var count = 0;
				$("#job_list li.easy_clear").each(
					function(intIndex) {
						if ($(this).is(matchString)) {
							$(this).show();
							count++;
						}
					}
				);
				// if no jobs matched our string, display a message
				if (count>0) {
					$("#job_list_no_message").html("");
					$("#job_list_no_message").hide();
				} else {
					$("#job_list_no_message").html("<p>No jobs matched your filters, please refine.</p>");
					$("#job_list_no_message").show();
				}
			}
			
			// else just show all
			else { $("#job_list li.easy_clear").show(); }
				
		});
		
	}
}

$(document).ready(function(){
	
	init_job_list();
	
	if (document.getElementById("app_safety_exp")) { $("#app_safety_exp").datepicker(); }
	
	$("input.btn").click( function() { $(this).val("Please Wait"); });
	
	if (document.getElementById("banners")) {
		$('#banners').cycle({
			fx: 'fade', 
	    speed: 500,
	    timeout: 5000
		});
	}
	
	

}); // End jQuery
