/**
 * jQuery.newshighlights
 * v0.2
 */

$(document).ready(function() {

	var defaults = {
		ms:			5000,
		items:			8,
		image_prefix:	"images/banner_02_0",
		image_postfix:	".jpg",
		cycle:			false
	};

	var list = {
		list_array: 		[],		// the array holding the <li> tags

		current_list_item:	0,		// temp container for the index of the current selection
		current_selection:	0,		// the list item selected (in sequence) by the loop function 
		user_selection:		0,		// the list item selected (out of sequence) by the user
		
		paused:				false,	// true if the user has selected an item or is hovering over the image
		selected:			false	// true if the user is hovering over the image, halts the loop at the current position (in sequence)
	};

	// initialisation stuff
	list.list_array = $.makeArray($(".highlight"));

	$i = 0;	// temporary for next loop
	$.each(list.list_array, function() {
		$(this).data("index",$i);
		if($i == 0) {
			setActive($(this));
			setImage($(this));
		}
		$i++;
	});

	function setActive($list_item) {
		if(list.current_selection != 0) {
			list.current_selection.removeClass("selected");
		}
		$list_item.addClass("selected");
		list.current_selection = $list_item;
		list.current_list_item = $list_item.data("index");
	}

	$(".banner").hover(function() {
		if (!list.paused) { list.paused = true; }
	}, function() {
		if (list.paused) { list.paused = false; }
	});

	$(".highlight").hover(function() {
		setActive($(this));
		setImage($(this));
	}, function() {

	});

	// Set the correct link and image
	function setImage($list_item) {
		$link = $list_item.children().children().attr("href");
		$(".newshighlights-link").children().attr("href", $link);
		$(".newshighlights-link").children().children().attr("src", defaults.image_prefix+$list_item.data("index")+defaults.image_postfix);
	}

	// cycle through each list item
	setInterval(function() {
		// if $paused is not true
		if(!list.paused) {
		
		}
	}, defaults.ms);

	// cycle through each list item
	setInterval(function() {
		// if $paused is not true
		if(!list.paused) {
			$.each(list.list_array, function() {
				// if the current item matches the index 
				if (list.current_list_item == $(this).data("index")) {
					setActive($(this));
					setImage($(this));
				}
			});
			// increment the 
			if(list.current_list_item+1 < defaults.items) { list.current_list_item++; } else { list.current_list_item = 0; }
		} else {

		}
	}, defaults.ms);
});
