/******************************************************************
* MediaCoder extension base scripts
* Distributed under GPL license
* Copyright (c) 2007 Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
******************************************************************/

var xmlhttp = CreateXMLHttp();
var debug;
if (GetToken(document.location.href, "debug")) {
	debug = true;
} else {
	debug = false;
}

function CreateXMLHttp()
{
	var newxmlhttp = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
	if (!newxmlhttp) alert("XMLHttpRequest object cannot be created.");
	return newxmlhttp;
}

function NewXML(rootkey)
{
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", rootkey, null);
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 	}
	else
	{
		return;
	}
	return xmlDoc;
}

function LoadXMLFile(xmlfile)
{
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		//xmlDoc.onload = callback;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			//if (xmlDoc.readyState == 4) callback()
		};
 	}
	else
	{
		return;
	}
	xmlDoc.async = false;
	try { 
		if (!xmlDoc.load(xmlfile)) return null;
	}
	catch (ex)
	{
		return null;
	}
	return xmlDoc;
}

function AddPrefNode(doc, path, value)
{
	var i;
	var tokens = path.split('.');
	var node = doc.firstChild;
	var newnode;
	for (i = 0; i < tokens.length; i++) {
		newnode = doc.createElement("node");
		newnode.setAttribute("key", tokens[i]);
		node = node.appendChild(newnode);
	}
	node.setAttribute("value", value);
}

function AddPrefElements(doc, arrayElements)
{
	var i;
	for (i = 0; i < arrayValues.length; i+=2) {
		AddPrefNode(doc, arrayValues[i], arrayValues[i+1]);
	}
}

function AddPrefValues(doc, arrayValues)
{
	var i;
	for (i = 0; i < arrayValues.length; i+=2) {
		AddPrefNode(doc, arrayValues[i], arrayValues[i+1]);
	}
}

function AlertConnError()
{
	if (debug) alert("Error connecting to MediaCoder.");
}

function PostData(url, data)
{
	try { 
		xmlhttp.open("POST", url, true);
		xmlhttp.send(data);
	}
	catch (ex) {
		AlertConnError();
	}
}

function PostPrefXML(data)
{
	PostData("/prefs/import", data);
}

function SendCommand(cmd)
{
	try { 
		xmlhttp.open("GET", "/ui?cmd=" + cmd, false);
		xmlhttp.send(null);
	}
	catch (ex) {
		AlertConnError();
	}
}

function PlayVideo(hwnd)
{
	try { 
		xmlhttp.open("GET", "/ui?cmd=play " + hwnd, false);
		xmlhttp.send(null);
	}
	catch (ex) {
		AlertConnError();
	}
}

function StopPlay()
{
	try { 
		xmlhttp.open("GET", "/ui?cmd=stop_play " + hwnd, false);
		xmlhttp.send(null);
	}
	catch (ex) {
		AlertConnError();
	}
}

function PlayConvertedVideo(hwnd)
{
	try { 
		xmlhttp.open("GET", "/ui?cmd=preview " + hwnd, false);
		xmlhttp.send(null);
	}
	catch (ex) {
		AlertConnError();
	}
}

function QueryString(url, async)
{
	try { 
		xmlhttp.open("GET", url, async);
		xmlhttp.send(null);
		return xmlhttp.responseText;
	}
	catch (ex) {
		AlertConnError();
		return null;
	}
}

function QueryInfo(info)
{
	return QueryString("/mc/get?query=" + info, false);
}

function QueryPref(path, opts)
{
	var node;
	var requrl = "/prefs/prefs.xml?type&";
	if (opts)
		requrl += opts + "&";
	if (path)
		requrl += "path=" + path;
	xmlhttp.open("GET", requrl, false);
	xmlhttp.send(null);
	node = xmlhttp.responseXML.firstChild;
	if (!node) {
		alert("Failed to retrieve preference data from MediaCoder.");
		return null;
	}
	return getChildNode(node);
}

function AddFile(filename)
{
	if (filename) {
		xmlhttp.open("GET", "/queue?action=addfile&file=" + filename, false);
		xmlhttp.send(null);
	}
}

function SetWindowSize(width, height)
{
	window.innerWidth = width;
	window.innerHeight = height;
}

function OpenURL(urlstring) {
	xmlhttp.open('GET', '/openurl?url=' + urlstring, false);
	xmlhttp.send(null);
}

function ShowItem(id)
{
	var e = document.getElementById(id);
	if (e) e.style.display = "block";
}

function HideItem(id)
{
	var e = document.getElementById(id);
	if (e) e.style.display = "none";
}

function ValueOf(id)
{
	return document.getElementById(id).value;
}

function GetToken(str, token)
{
	var idx = str.indexOf(token + '=');
	if (idx < 0) return null;
	var argstr = str.substring(idx + token.length + 1);
	idx = argstr.indexOf('&amp;');
	return idx > 0 ? argstr.substring(0, idx) : argstr;
}

function SetPrefValue(param, val)
{
	return callRPC("mc.SetPref", param, val);
}

function GetPrefValue(param)
{
	return callRPC("mc.GetPref", param);
}

function ShowCropper()
{
	return callRPC("ui.ShowTimeDialog");
}

function ShowTimeDialog()
{
	return callRPC("ui.ShowTimeDialog");
}

function ShowUI(shown)
{
	return callRPC("ui.Show", shown);
}

function GetItem(index)
{
	return callRPC("mc.GetItem", index);
}

function GetSelectedFile()
{
	var index = callRPC("ui.GetSelectIndex");
	if (index < 0) return null;
	var selitem = GetItem(index);
	return selitem["filename"];
}
