var filesInList = [];
var sPlayList = [];
var myIntervalId = null;
var isFileParsing = false;
var queryFunction = null;
var myPlugin;
function WindowInit(args)
{
	queryFunction = args[0]["queryFun"];
	myPlugin = $e("extention_plugin2");
	myPlugin.init();
    setTimeout(OpenListFile,100);
}

function WindowUnInit()
{
    myPlugin.uninit();
}

function CloseWindow()
{
    if(myIntervalId)
    {
        clearInterval(myIntervalId);
        myIntervalId = null;
    }
	//
	var stats = myPlugin.getTransmitStatus();
	if(stats == 0x2020)
	{
		myPlugin.deviceTransmitCancel();
		setTimeout(DelayClose, 50);
	}
	else
		window.close();
}

function DelayClose()
{
	var stats = myPlugin.getTransmitStatus();
	if(stats != 0x2020)
	{
		alert(getLocalString("upload.stop.transmit"));
		setTimeout(function(){window.close();}, 50);		
	}
	else
	{
		setTimeout(DelayClose, 50);
	}
}

function OpenListFile()
{
    var file = Io.loadFile(getLocalString("upload.select.title"), ["List File(*.list)|*.list"], false);
    if(!file[0] || !file[0].path)
    {
        CloseWindow();
    }
    else
    {
        //parse list
        var doc = DomHelper.LoadLocalXMLFile(file[0].path);
		var root = DomHelper.getChildNode(doc);
        filesInList = [];
		sPlayList = [];
        var node = DomHelper.findChildNode(root, "file");
        while(node)
        {
            filesInList.push({'path':DomHelper.getNodeValue(node),'time':node.getAttribute("duration"), 'size':node.getAttribute("size")});
	    sPlayList.push(DomHelper.getNodeValue(node));
            node = DomHelper.getNextNode(node);
            node = DomHelper.findNode(node,"file");
        }
        isFileParsing = false;
		$e("parsed_file").value="0";
		$e("total_file").value=filesInList.length;
		$e("wnd_stats").value=getLocalString("upload.label.parsing");
        myIntervalId = setInterval(doFlieParsing, 100);
    }
}

function doFlieParsing()
{
    if(isFileParsing)
        return;
	//
	if(filesInList.length > 0)
	{
		var obj = filesInList.shift();
		var path = obj.path;
		var time=obj.time;
		var size=normalizeFileSize(obj.size);
		/*if(queryFunction)
		{
			var doc = queryFunction("type=50&path="+path);
			if(doc)
			{
				var root = DomHelper.getChildNode(doc);
				var stream = DomHelper.findChildNode(root,"stream");
				time = DomHelper.getNodeValue(DomHelper.findChildNode(stream,"duration"));
				time = Int2timeStr(time);
				size = DomHelper.getNodeValue(DomHelper.findChildNode(stream,"filesize"));
				size = normalizeFileSize(size);
				
			}
		}*/
		//make item
		var list = $e("upload_list");
		var item = list.appendChild( HtmHelper.make("richlistitem",{"value":path}) );
		var cell = item.appendChild(HtmHelper.make("listcell",{"flex":2,"label":getFileTitle(path)+"."+getFileExt(path)}));
		cell = item.appendChild(HtmHelper.make("listcell",{"flex":1,"label":size}));
		cell = item.appendChild( HtmHelper.make( "listcell", { "flex":1, "label":time } ) );
		cell = item.appendChild( HtmHelper.make( "listcell", {} ) );
		cell.appendChild(HtmHelper.make( "checkbox",{"checked":true}));
		
		$e("parsed_file").value = parseInt($e("parsed_file").value) +1;
		$e("parse_proc").value = parseInt($e("parsed_file").value) / parseInt($e("total_file").value) * 100;
	}
	else
	{
		if(myIntervalId)
		{
			clearInterval(myIntervalId);
			myIntervalId = null;
		}
		$e("wnd_stats").value=getLocalString("upload.label.done");
		var btns = $e("command_btns").getElementsByTagName("button");
		var n = 0;
		while(btns[n])
		{
			btns[n].removeAttribute("disabled");
			n++;
		}
	}
    isFileParsing = false;
}

function OpenDir(id)
{
    var dirAtrr = Io.openDir('Select');
    if(dirAtrr == null)
    return;
     document.getElementById(id).value = dirAtrr.path;
}

function UploadFiles()
{
	var destPath = $e("upload_path").value;
	if(!destPath)
	{
		alert("Please set a URL path!");
		return;
	}
	filesInList = [];
	var list = $e("upload_list");
	var items = list.getElementsByTagName("richlistitem");
	for(var i=0;i<items.length;++i)
	{
		var check = items[i].getElementsByTagName("checkbox");
		if(check[0].checked)filesInList.push(items[i].value);
	}
	isFileParsing = false;
		
	$e("parse_proc").value=0;
	$e("parsed_file").value="0";
	$e("total_file").value=filesInList.length;
	$e("wnd_stats").value=getLocalString("upload.label.transmit");
	$e("proc_row").removeAttribute("hidden");
	$e("upload_btn").setAttribute("disabled",true);
	myIntervalId = setInterval(UploadFile,100,destPath);
}

function UploadFile(destPath)
{
	if(isFileParsing)
	{
		var stats = myPlugin.getTransmitStatus();
		if(stats == 0x3030)
		{
			$e("parsed_file").value = parseInt($e("parsed_file").value) +1;
			$e("parse_proc").value = parseInt($e("parsed_file").value) / parseInt($e("total_file").value) * 100;
			isFileParsing = false;
		}
		else
		{
			var num = myPlugin.getTransmitPercent();
			$e("parse_proc_1").value = num;
			$e("parsed_file_precent").value = num;
		}
		return;
	}
	//
	if(filesInList.length > 0)
	{
		var path = filesInList.shift();
		$e("parse_proc_1").value = 0;
		$e("parsed_file_precent").value = 0;
		isFileParsing = true;
		myPlugin.deviceLocalTransmit(path, destPath);		
	}
	else
	{
		if(myIntervalId)
		{
			clearInterval(myIntervalId);
			myIntervalId = null;
		}
		$e("upload_btn").removeAttribute("disabled");
		$e("wnd_stats").value=getLocalString("upload.label.done");
		isFileParsing = false;
	}	
}

var pathOfPlayer =  "C:\\Program Files\\Windows Media Player\\wmplayer.exe";
function TestAndPlay()
{
	/*var sPlayer = Io.getSpecialDir("bin", "AChrom");
	if(sPlayer) {
		sPlayer.append("splayer");
		sPlayer.append("splayer.exe");
	}*/
	var sPlayer = Io.getFileFrom(pathOfPlayer);
	if(sPlayer && sPlayer.exists()) {
		var args = sPlayList;
		//alert(sPlayList[0] + ":" + sPlayList[1]);
		sPlayerExe = launchExe(sPlayer, args);
	}
	else
	{
		pathOfPlayer = prompt(getLocalString("upload.play.select"),"Confirm",pathOfPlayer);
		sPlayer = Io.getFileFrom( pathOfPlayer);
		var args = sPlayList;
		sPlayerExe = launchExe(sPlayer, args);
	}
}
