// JavaScript Document
//Var definitions
var player, file, title, description, image, thumbnail, current_text, thumb_count, pages, current_page;

//Player functions
$(document).ready(function() {
	player = document.getElementById("mediaspace");
});

function initiateXml(path) {
	$.ajax({
		type: "GET",
		url: path,
		dataType: "xml",
		success: parseXml
	});	
}

function parseXml(xml) {	
	var count = 0;
	$(xml).find("video").each(function() {
		file = $(this).find("filename").text();
		title = $(this).find("title").text();
		description = $(this).find("description").text();
		image = $(this).find("preview").text();								   
		thumbnail = $(this).find("thumbnail").text();								   
		$("#mediathumbs_text").append("<p id=\"media_text"+count+"\"><strong>"+title+"</strong><br/>"+description);
		$("#mediathumbs_gallery_inner").append("<a href=\"javascript:void(0);\" onclick=\"changeMedia('"+file+"', '"+title+"', '"+image+"', 1);\" onmouseover=\"showText('media_text"+count+"');\"><img src=\""+thumbnail+"\" /></a>");
		count++;
  	});
	thumb_count = count;
	if (thumb_count>3) {
		pages = Math.round(thumb_count/3);
		current_page = 1;
	}	
	
		//Set up player with first video
	file = $(xml).find("filename:first").text();
	title = $(xml).find("title:first").text();
	image = $(xml).find("preview:first").text();
	setTimeout("changeMedia(file, title, image, 0)", 2000);
	//changeMedia(file, title, image, 0);
	
	//Display initial video title and description
	$("#media_text0").css({'display':'block'});
	current_text = $("#media_text0");
}

function changeMedia(new_file, new_title, new_image, play) {
	var obj = {file:""+new_file+"", title:""+new_title+"", image:""+new_image+""};
	player.sendEvent('LOAD', obj);
	if (play==1) {
		player.sendEvent("PLAY",true);
	}
}

function showText(id) {
	$(current_text).css({'display':'none'});
	$("#"+id).css({'display':'block'});
	current_text = $("#"+id);
}

function scrollThumbs(dir) {
	var gallery = $("#mediathumbs_gallery_inner");
	var distance;
	if (dir=='left') {
		if (current_page>1) {
			distance = 
			$(gallery).animate({left:"+=210"});
			current_page--;
		}		
	}
	else {
		if (current_page<=pages) {
			$(gallery).animate({left:"-=210"});
			current_page++;
		}
	}
}
