function updateHomeNews() {
	$.getJSON("/ajax/index.php?q=latest_news_item", function(data) {
		$('#newsTitle').html(data.title);
		$('#newsContent').html(data.content);
		return false;
	});
}

// JW Player Setup and Listener Event handlers

function getPlayerNumber(stringNum) {
	var parsedNo = "";
	for(var n=0; n<stringNum.length; n++) {
		var i = stringNum.substring(n,n+1);
		if(i >= 0 && i <= 9) parsedNo += i;
	}
	return parseInt(parsedNo);
}

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	player.addModelListener("TIME", "jwTimeMonitor");
	player.addModelListener("STATE", "stateListener");
}

function stateListener(obj) {
	var id = getPlayerNumber(obj.id);
	var state = obj.newstate;
	if(state == "IDLE" || state == "PAUSED" || state == "COMPLETED") {
		$('#playbutton'+id).html('Play');
	} else {
		$('#playbutton'+id).html('Pause');
	}
}

function jwTimeMonitor(obj) {
	player = window.document[obj.id];
	var id = getPlayerNumber(obj.id);
	var position = obj.position;
	var duration = $(obj.id).getPlaylist()[0].duration;
	var percentage = Math.round((position/duration)*100) + '%';
	if(percentage == '100%') percentage = 'End';
	$('time'+id).innerHTML = percentage;
}

// Generic attribute-changing function
function CngAttr(obj,prop,assign){
	obj.style[prop]=assign;
}

// Keyword search validation
function checkSearchText(form) {
	if(form.input_q.value == "Enter a keyword" || form.input_q.value == "") {
		alert("Please enter a keyword to search for");
		form.input_q.focus();
		return false;
	} else {
		return true;
	}
}

// Text replacement of keyword search form
function setSearchText(input) {
	var placeholder = 'Search for a keyword';
	if (input.value == placeholder) {
		$(input).css('fontStyle','normal').css('color','#000').attr("value", "");
	} else if (input.value == "") {
		$(input).css('fontStyle','italic').css('color','#999').attr("value", placeholder);
	}
}

// SUS Evaluation form validation
function checkSusForm(form) {
	with(form) {
		for(var i = 1; i <= 10; i++) {
			var verified = false;
			var radios = document.getElementsByName("q" + i);
			for(var r = 0, radio; radio = radios[r]; r++) if(radio.checked) verified = true;
			
			if(verified != true) {
				alert("Please answer question "+i+".");
				return false;
			}
		}
		
		if(email.value == "") {
			var email_message = "You have chosen not to send us your email address. We are collecting this information so that we can contact you regarding Phase Two of the Evaluation.\n\nYou do not have to send us these details - it is up to you!\n\nShould we proceed without this information?";
			if(confirm(email_message) == false) {
				email.focus();
				return false;
			}
		} 
	}
	return true;
}

// Contact form validation
function checkContactForm(form) {
	with(form) {
		at = email.value.indexOf("@") + 1;
		dot =  email.value.length - email.value.lastIndexOf(".") - 1;
		
		if (to.value == "x") {
			alert("Please select a recipient from the drop-down list and try again");
			to.focus();
			return false;
		} else if (message.value == "" || message.value == "Enter your message here") {
			alert("Please enter a message");
			message.focus();
			return false;
		} else if (name.value == "") {
			alert("Please enter your name");
			name.focus();
			return false;
		}  else if (at < 2 || at >= (email.value.length - 5) || dot < 0 || dot > 4) {
			alert("Please enter a valid email address.");
			//alert("Failed!\n@ after sums: " + at + "\nForm length: " + form.email.value.length + "\nPosition of @: " + form.email.value.lastIndexOf("@"));
			email.focus();
			return false;
		} else {
			return confirm("Would you like to send this message?");
		}
	}
}

// Contact form text field handling
function handleMessageArea(textarea) {
	if (textarea.value == "Enter your message here") {
		textarea.value = "";
	} else if (textarea.value == "") {
		textarea.value = "Enter your message here";
	}
}

function getTopSearchOptions() {
	$.getJSON("/ajax/index.php?q=select_filter&difficulty="+$('#q_difficulty').attr("value"), function(tasks) {
		$("#q_task option").remove();
		$.each(tasks, function(index, task) {
			$('<option />', { 
				value: task.value,
				text: task.label
			}).appendTo('#q_task');
		});
		$('#q_task').focus();
	});
}

function getNextTip() {
	$.getJSON("/ajax/index.php?q=tip", function(data) {
		$('#tip_text').html(data.tip);
		$('#tip_link').attr("href", '/strategy/' + data.id);
		return false;
	});
}

function nextSlideshowImage(slideshow) {
	var currentImage = $(slideshow).children(".slideshow-images").children("img:visible");
	$(currentImage).fadeOut("fast", function() {
		if($(this).next("img").length > 0) {
			$(this).next("img").fadeIn();
		} else {
			$(this).siblings(":first").fadeIn();
		}
	});
}

function previousSlideshowImage(slideshow) {
	var currentImage = $(slideshow).children(".slideshow-images").children("img:visible");
	$(currentImage).fadeOut("fast", function() {
		if($(this).prev("img").length > 0) {
			$(this).prev("img").fadeIn();
		} else {
			$(this).siblings(":last").fadeIn();
		}
	});
}

// jQuery stuff
jQuery(document).ready(function() {
	$(".slideshow .slideshow-images img:not(:first-child)").hide();
	
	$(".slideshow-buttons a.btn_next").bind("click", function() {
		nextSlideshowImage($(this).parent(".slideshow-buttons").parent(".slideshow"));
		return false;
	});
	
	$(".slideshow-buttons a.btn_previous").bind("click", function() {
		previousSlideshowImage($(this).parent(".slideshow-buttons").parent(".slideshow"));
		return false;
	});
	
	$("#difficulty_description, #task_description").hide();
	
	$('#a_difficulty_description').bind("click", function() {
		$("#task_description").slideUp();
		$("#difficulty_description").slideToggle();
	});
	
	$('#a_task_description').bind("click", function() {
		$("#difficulty_description").slideUp();
		$("#task_description").slideToggle();
	});
});
