$(document).ready(function() {
	// ADJUSTING PAGE FOR USERS WITH JAVASCRIPT ENABLED
	$(".container_12")
		.removeClass("noJS")
		.addClass("JS")
		;
	
	// PRELOADING AJAX LOADER IMAGE
	$.preLoadImages("images/loaders/black-news.gif");
	
	// SETTING VALUE OF CURRENT PHOTO HIDDEN INPUT
	$("#currentPhoto").attr('value', 0);
	
	// ANIMATED HOVERING ON LINKS
	$("a.linkFade").hover(function() {
		// Get colour for link from [section]Hover class
		var colorClass = new String($(this).attr('class').split(' ').slice(-1));
		var colorString = colorClass.substr(0, colorClass.length - 5);
		
		// Set up animated background and text colours
		var animations = new Object();
		animations.backgroundColor = '#f8f8f8';
		animations.color = stringToColor(colorString);
		
		// Animate the background and text colours for 50ms without queuing
		$(this).animate(animations, {duration: 50, queue: false});
	}, function() {
		// Set up animated background and text colours
		var animations = new Object();
		animations.backgroundColor = '#fff';
		animations.color = '#000';
		
		// Animate the background and text colours for 50ms without queuing
		$(this).animate(animations, {duration: 500, queue: false});
	});
	
	// NEXT PHOTO IN ARTICLE PHOTO GALLERY
	$(".nextPhoto").click(function() {
		var total = $("#total").val();
		var params = new Object();
		params.currentPhoto = $("#currentPhoto").val();
		if (params.currentPhoto < parseInt(total) - 1) {
			params.article = $("#articleID").val();
			
			// Position ajax loader
			var currentHeight = $(".image").height();
			$(".image").css('height', currentHeight);
			$("#photo").css('padding-top', ((currentHeight - 32) / 2) + 'px');
			
			// Display ajax loader
			$("#photo").attr('src', 'images/loaders/black-news.gif');
			
			// Get next photo
			$.post('functions/photo-next.php', params, function(text) {
				// Drop ajax loader positioning
				$(".image").css('height', '');
				$("#photo").css('padding-top', '');
				
				// Get data from returning script
				var data = JSON.parse(text);
				
				// Change width of photo container
				$(".photo")
					.removeClass('full')
					.removeClass('half')
					.addClass((data.imageWidth == 0) ? 'full' : 'half')
					;
				
				// Update the photo id displayed and kept in hidden input
				var parsedCurrent = parseInt(params.currentPhoto);
				$("#currentDisplay").html(parsedCurrent + 2);
				$("#currentPhoto").attr('value', parsedCurrent + 1);
				
				// Insert photo location and caption into the page
				$("#photo").attr('src', data.url);
				$("#caption").html(data.caption);
			});
		}
		return false;
	});
	
	// PREVIOUS PHOTO IN ARTICLE PHOTO GALLERY
	$(".lastPhoto").click(function() {
		var params = new Object();
		params.currentPhoto = $("#currentPhoto").val();
		if (params.currentPhoto > 0) {
			params.article = $("#articleID").val();
			
			// Position ajax loader
			var currentHeight = $(".image").height();
			$(".image").css('height', currentHeight);
			$("#photo").css('padding-top', ((currentHeight - 32) / 2) + 'px');
			
			// Display ajax loader
			$("#photo").attr('src', 'images/loaders/black-news.gif');
			
			// Get previous photo
			$.post('functions/photo-last.php', params, function(text) {
				// Drop ajax loader positioning
				$(".image").css('height', '');
				$("#photo").css('padding-top', '');
				
				// Get data from returning script
				var data = JSON.parse(text);
				
				// Change width of photo container
				$(".photo")
					.removeClass('full')
					.removeClass('half')
					.addClass((data.imageWidth == 0) ? 'full' : 'half')
					;
				
				// Update the photo id displayed and kept in hidden input
				var parsedCurrent = parseInt(params.currentPhoto);
				$("#currentDisplay").html(parsedCurrent);
				$("#currentPhoto").attr('value', parsedCurrent - 1);
				
				// Insert photo location and caption into the page
				$("#photo").attr('src', data.url);
				$("#caption").html(data.caption);
			});
		}
		return false;
	});
	
	// APPLICATION FORM
	$form = $(".applicationForm");
	if ($form.length > 0) {
		$(".inputHint").focus(function() {
			var $this = $(this);
			if ($this.hasClass('hintOn')) {
				$this
				.data('hintText', $this.val())
				.removeClass('hintOn')
				.val('');
			}
		}).blur(function() {
			var $this = $(this);
			if ($this.val() == '') {
				$this
				.addClass('hintOn')
				.val($this.data('hintText'));
			}
		});
		
		$(".required", $form).live('focus', function() {
			$(this).removeClass('required');
		});
		
		$("form", $form).submit(function() {
			var error = false;
			var msg = '';
			
			$("input[type='text'], textarea", $form).each(function(i) {
				if ($(this).hasClass('hintOn') || $(this).val() == '') {
					error = true;
					msg = 'Please ensure the required fields are filled in before submitting the form.';
					$(this).addClass('required');
				}
			});
			
			if (!error && !$("input.position").is(':checked')) {
				error = true;
				msg = 'Please select at least one position or section that you are interested in before submitting the form.';
			}
			
			if (!error && $("textarea[name='statement']").val().length > 1000) {
				error = true;
				msg = 'Please ensure your supporting statement is a maximum of 1,000 characters long.';
				$("textarea[name='statement']").addClass('required');
			}
			
			if (error) {
				alert(msg);
				return false;
			}
			
			return true;
		});
	}
});

// FUNCTION: STRING TO COLOR
// Takes a section's css title (news, features, etc.) and returns the hex value of that section's colour as a string
function stringToColor(str) {
	if (str == 'news') {
		return '#a11970';
	} else if (str == 'features') {
		return '#a98e1e';
	} else if (str == 'culture') {
		return '#00abc4';
	} else if (str == 'opinion') {
		return '#7397ae';
	} else if (str == 'sport') {
		return '#768b1c';
	} else if (str == 'out') {
		return '#ed7703';
	} else {
		return '#000';
	}
}

// FUNCTION: PRE LOAD IMAGES
// Function added to jQuery to allow images to be pre-loaded
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)