var multimedia; //matrix of all multimedia. columns are priority, src, blurb, link
var position; //index of furthest left, visible mmobject that is displayed. so position, position+1, and position+2 are seen
var xmlDoc; //used to load the external file we will read
var xmlLocation = '/now/multimedia-xml.xml'; //location of the xml file we will read
var limit = 3; //limit is 3 unless we have fewer than 3 mmobjects loaded
$().ready(function() {
	xmlDoc = $.ajax({url:xmlLocation, success: loadMultimedia});
});

function shiftElements(direction){
	//wrap around the array
	if (direction == 'left'){
		if (position == 0){
			position = multimedia.length-1;
		} else {
			position--;
		}
	} else {
		if (position == multimedia.length-1){
			position = 0;
		} else {
			position++;
		}
	}

	//set all three images and blurbs with elements from the array, starting from position
	for (i=1; i<1+limit; i++){ //start at 1 to match element name, -1 from position so we're in the right place of the array
		//modulus so we don't shoot out of bounds
		document.getElementById("thumbnail" + i).src = multimedia[(position+i-1)%(multimedia.length)][1];
		document.getElementById("title" + i).innerHTML = multimedia[(position+i-1)%(multimedia.length)][2];
		document.getElementById("link" + i).href = multimedia[(position+i-1)%(multimedia.length)][3];
	}
}

function loadMultimedia(data, status){
	multimedia = new Array();
	//loop over all mm objects and create array containing their properties
	$(data).find('mmobject').each(function(i) {
		multimedia[i] = new Array();
		multimedia[i][0] = $(this).find('mpriority').text();
		multimedia[i][1] = $(this).find('thumbnail').text();
		multimedia[i][2] = $(this).find('title').text();
		multimedia[i][3] = $(this).find('link').text();
	});

	//sort by priority, shuffled within each group (i.e., 4s always first, but order of 4s is random)
	multimedia.sort(sortByPriority);

	//immediately, we shift right once, so set position to the end so after the shift position = 0
	position = multimedia.length-1;

	//if fewer than 3 mmobjects, hide list items and adjust width accordingly
	if (multimedia.length < 3){
		limit = multimedia.length;
		listitems = document.getElementsByTagName("li");
		listitems[2].style.display = "none";
		if (multimedia.length == 2){
			document.getElementById("multimediacontent").style.width = "530px";
		} else if (multimedia.length == 1){
			document.getElementById("multimediacontent").style.width = "415px";
			listitems[1].style.display = "none";
		} else if (multimedia.length == 0){
			document.getElementById("multimediacontent").style.width = "300px";
			listitems[1].style.display = "none";
			listitems[0].style.display = "none";
		}
	}

	//use shift function from our random position and then we're set
	shiftElements('right');
}

function sortByPriority(a, b) {
	//if a's priority is greater, return -1 (put it first). 0 = greater, 1 = put it after
	if (a[0] > b[0]){
		return -1;
	}
	if (a[0] == b[0]){//if they are equal, randomly return 1 or 0. this gives us the random with each priority
		return Math.floor(Math.random()*2);
	}
	if (a[0] < b[0]){
		return 1;
	}
}

function activate(direction){
	//change the hovered over arrow image to an active version of it
	if (direction == 'left'){
		document.getElementById("multimedialeftarrow").src = "/now/design-files/images/leftarrow-active.gif";
	} else {
		document.getElementById("multimediarightarrow").src = "/now/design-files/images/rightarrow-active.gif";
	}
}

function deactivate(direction){
	//change the activated arrow image back to normal
	if (direction == 'left'){
		document.getElementById("multimedialeftarrow").src = "/now/design-files/images/leftarrow.gif";
	} else {
		document.getElementById("multimediarightarrow").src = "/now/design-files/images/rightarrow.gif";
	}
}

//this is for headlines - just trying to avoid giving it its own .js file
function randomizeHeadlines(classname) {
	var contents=randomizeContent.collectElementbyClass(classname);
	contents.text.sort(sortByPriority);
	for (var i=0; i<contents.ref.length; i++) {
		contents.ref[i].innerHTML=contents.text[i];
		contents.ref[i].style.visibility="visible";
	}
}