//************************************
//*	MILESPLIT.ACUDEO
//* By Jason Byrne
//* Tremor Acudeo Control
//* Include Requirements:
//* - swfobject.js
//* - milesplit.ajax.js

///////////////////////////////////////////
// VARIABLES

// ID Names
var Acudeo_CommentList = 'Video_Comments';
var Acudeo_Tags = 'Video_Tags';

// Settings
var Acudeo_VideoID = '';
var Acudeo_VideoURL = '';
var Acudeo_ImageURL = '';
var Acudeo_ContentPath = '';
var Acudeo_ContentType = 'progressive';
var Acudeo_AutoPlay = 'True';
var Acudeo_VideoTitle = '';
var Acudeo_EndSlateURL = '';
var Acudeo_ProgrammableAreaURL = '';
var Acudeo_CompanionBannerID = '';

// Temp variable
var lasttag = '';
					

///////////////////////////////////////////
// INITIALIZATION


function Acudeo_InitializePlayer(programid) {
	if (Acudeo_CompanionBannerID) {
		var event = new Object;   
		event['type'] = "compAd";
		event['callback'] = Acudeo_ShowBanners;
		tmObj.register(event);
	}
	if (Acudeo_VideoID > 0) {
		var vidurl = "http://videos.milesplit.com/" + Acudeo_VideoID + ".flv";
		var imageurl = "http://videos.milesplit.com/stills/" + Acudeo_VideoID + ".jpg";
		tmObj.set("VideoURL", vidurl);
		tmObj.set("ImageURL", imageurl);
		//alert(vidurl);
	} else {
		tmObj.set("VideoURL", Acudeo_VideoURL);
		tmObj.set("PreviewImageURL", Acudeo_ImageURL);
	}
	tmObj.set("ContentPath", Acudeo_ContentPath);
	tmObj.set("ContentType", Acudeo_ContentType);
	tmObj.set("VideoTitle", Acudeo_VideoTitle);
	tmObj.set("AutoPlay", Acudeo_AutoPlay);
	tmObj.set("EndSlateURL", Acudeo_EndSlateURL);
	tmObj.set("ProgrammableAreaURL", Acudeo_ProgrammableAreaURL);
	tmObj.start(programid);					
}

function Acudeo_ShowBanners(banners) {
	tmDisplayBanner(banners, Acudeo_CompanionBannerID, 300, 250);
}

///////////////////////////////////////////
// AJAX FUNCTIONS


function AddVideoTag(user, session) {
	var tag = prompt('New Tag:');
	if (tag) {
		lasttag = tag;
		Ajax_AddTag(Acudeo_VideoID, user, session, tag);
	}
	return false;
}

function AddComment(comment) {
	if (Acudeo_CommentList) {
		var parent = document.getElementById(Acudeo_CommentList);
		// Create elements
		var post = document.createElement('div');
		var msg = document.createElement('div');
		var meta = document.createElement('div');
		// set style elements
		post.className = 'post';
		meta.className = 'meta';
		msg.className = 'message';
		// set propreties
		meta.innerHTML = comment.Properties['UserName'] + ' wrote on ' + comment.Properties['DatePosted'];
		msg.innerHTML = comment.Properties['Message'];
		// insert 
		parent.appendChild(post);
		post.appendChild(meta);
		post.appendChild(msg);
	}
}

function onVideoCommentAdded(source) {
	var doc = new MileSplit_XmlDocument(source.responseText);
	var comment = doc.Items[0];	
	AddComment(comment);
}

function onVideoTagAdded(source) {
	var doc = new MileSplit_XmlDocument(source.responseText);
	if (doc.Status > 0) {
		if (Acudeo_Tags) {
			var tags = document.getElementById(Acudeo_Tags);
			tags.innerHTML += ' ' + lasttag;
		}
	} else {
		alert('Tag not added: ' + doc.Message + ' (' + doc.Status + ')');	
	}
}

function Ajax_AddComment(id, user, session, message) {
	var params = new Array();
	params[0] = new Array('method', 'videos.addComment');
	params[1] = new Array('id', id);
	params[2] = new Array('user_name', user);
	params[3] = new Array('session_key', session);
	params[4] = new Array('message', message);
	params[5] = new Array('format', 'xml');
   	MileSplit_Ajax_Request(MileSplit_ApiUrl, MileSplit_MakeQueryString(params), 'onVideoCommentAdded', MileSplit_Method)
}

function Ajax_AddTag(id, user, session, tag) {
	var params = new Array();
	params[0] = new Array('method', 'videos.addTag');
	params[1] = new Array('id', id);
	params[2] = new Array('user_name', user);
	params[3] = new Array('session_key', session);
	params[4] = new Array('tag', tag);
	params[5] = new Array('format', 'xml');
   	MileSplit_Ajax_Request(MileSplit_ApiUrl, MileSplit_MakeQueryString(params), 'onVideoTagAdded', MileSplit_Method)
}

