var line = 0;
var media;
var play = 1;
function getMedia(url, id){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			media = ajaxRequest.responseText.split('|^|');
			mediascroll();
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
}
function initmediascroll() {
	var url = '/media.php?line='+line;
	getMedia(url, 'media');
}
function previousmedia() {
	var oldPlay = play;
	clearTimeout(t);
	play = 1;
	line--;
	line--;
	if (line < -1) {
		line = media.length - 2;
	} else if (line < 0) {
		line = media.length - 1;
	}
	mediascroll();
	play = oldPlay;
}
function nextmedia() {
	var oldPlay = play;
	clearTimeout(t);
	play = 1;
	mediascroll();
	play = oldPlay;
}
function pausemedia() {
	clearTimeout(t);
	play = 0;
	document.getElementById('play').style.display = 'inline';
	document.getElementById('pause').style.display = 'none';
}
function playmedia() {
	clearTimeout(t);
	play = 1;
	mediascroll();
	document.getElementById('play').style.display = 'none';
	document.getElementById('pause').style.display = 'inline';
}
function mediascroll() {
	if (play == 1) {
		var temp = media[line].split('|*|');
		var ajaxDisplay = document.getElementById('media');
		if (temp[1] == 12345) {
			ajaxDisplay.innerHTML = "<img src=\"/library/images/media/"+temp[0]+"\" alt=\"\" style=\"height: 178px\"/>";
		} else {
			ajaxDisplay.innerHTML = "<a href=\""+temp[1]+"\"><img src=\"/library/images/media/"+temp[0]+"\" alt=\"\" style=\"height: 178px\"/></a><br/>";
		}
		temp = '';
	}
	line++;
	if (line >= media.length) {
		line = 0;
	}
	if (play == 1) {
		t = setTimeout("mediascroll()", 8000);
	}
}