// ---------------------- 预览图片 -------------------------

var SupportFolder = new String();
var NeedPreview = false;
var BarID = null;
var FrmID = null;
var ProgressID = null;
var PageBarID = null;

function SetWebUploaderBar() 
{
	var div = document.getElementById(BarID);
 
	if (div == '[object]')
	{
	    var left = document.body.scrollLeft + (document.body.clientWidth - div.clientWidth) / 2;
	    var top = document.body.scrollTop + (document.body.clientHeight - div.clientHeight) / 2 ;
    	
	    div.style.display = 'inline';	
	    div.style.posLeft = left;
	    div.style.posTop = top;
	    div.style.position = "absolute";
	}
}

function DoInit(strSupportFolder)
{
	SupportFolder = strSupportFolder;

	var frm = document.getElementById(FrmID);
	if (frm == '[object]')
	{   
    	frm.src = SupportFolder + 'Uploader/UploaderBar.aspx?ProgressID=';	
	}
}

function DoPreviewImage(img)    
{
	var ext = img.src.substring(img.src.lastIndexOf('.')).toLowerCase();
	var pics = '.jpg, .jpeg, .gif, .bmp, .tif';

	if (pics.indexOf(ext) == -1)
	{
		img.style.display = 'none';
	}
	else
	{
		img.style.display = 'inline';
	}
}

function DoUpload()
{
//	SetWebUploaderBar(); 
//	SetWebUploaderBar(); // 要执行两次的，ｔｎｎｄ位置才能正确移动。
	
	var _theUniqueID = (new Date()).getTime() % 1000000000;
	var _href = window.location.href;
	var _search = window.location.search;
	var _action;
	var _form = document.forms[0];
	
	if (_search == '')	// 地址栏未携带参数使用? 否则使用&
	{
		_action = _href + '?UploadID=' + _theUniqueID;
	}
	else
	{
		if (_search.indexOf('UploadID=') == -1)
		{
			_action = _href + '&UploadID=' + _theUniqueID;			
		}
		else
		{
			var _start = _search.indexOf('UploadID=');
			var _end = _start + 18;			 
			var _arg = _search.substring(_start, _end);
			
			_action = _href.replace(_arg, 'UploadID=' + _theUniqueID);
		}
	}
	_form.action = _action;
	_form.submit();	
	
	ProgressID = _theUniqueID

	var frm = document.getElementById(FrmID);
	if (frm == '[object]')
	{
		frm.src = SupportFolder + 'Uploader/UploaderBar.aspx?ProgressID=' + _theUniqueID + '&BarID=' + BarID;
	}

    ShowDivBar()
}

function ShowProgressBar(strSupportFolder, strBarID, strFrmID, strPageBarID) 
{
	if (typeof(Page_ClientValidate) == 'function') 
	{		
		if(!Page_ClientValidate())
		{
			return false;
		}
	}

	BarID = strBarID;
	FrmID = strFrmID;
	PageBarID = strPageBarID
	
	var oPageBar = document.getElementById(strPageBarID);
 
	if (oPageBar == '[object]')
	{
	     oPageBar.style.display = 'inline';	   
	}
	
	DoInit(strSupportFolder);
	DoUpload();
	
	return true;
}

function ShowDivBar() 
{
	var timeoutid = null;        //这个变量是作定时器的ID	
	var hasFileName = false;			
	var oXMLDoc = new ActiveXObject('MSXML');

	oXMLDoc.url = SupportFolder + 'Uploader/UploaderProgress.aspx?ProgressID=' + ProgressID;
		
	var oRoot=oXMLDoc.root;     //获取返回xml数据的根节点

	if(oRoot.children != null)
	{
		var Kbps = oRoot.children.item("Kbps").text;

		tbPercent.style.width	= oRoot.children.item("Percent").text + '%';	// 已完成进度	
		tdPercent.innerHTML	= oRoot.children.item("Percent").text + '%&nbsp;';
		
//		if(Kbps > 0)
//		{
//			tdKbps.innerHTML	= Kbps + 'Kb/秒 (' +
//								+ oRoot.children.item("KbDone").text + ' Kb / ' 
//								+ oRoot.children.item("KbTotal").text + ' Kb)';
//		}
//							
//		tdTimeLeft.innerHTML= oRoot.children.item("min").text + ' 分 '
//							+ oRoot.children.item("secs").text + ' 秒' ;		// 剩余时间、进度等详细信息
//		
//		tdInformation.innerHTML = oRoot.children.item("Information").text;

//		if(!hasFileName)
//		{				
//			var strOldFileName = oRoot.children.item("FileName").text;
//			var strNewFileName = strOldFileName.substring(strOldFileName.lastIndexOf('\\') + 1);
//								
//			if (strOldFileName.length > 20)
//			{
//				if (strNewFileName.length > 20) 
//				{
//					tdFileName.innerText = strNewFileName.substr(0, 16) + '...';
//				}
//				else
//				{						
//					tdFileName.innerText = strNewFileName;
//				}
//			}
//			else
//			{
//				tdFileName.innerText = strOldFileName;
//			}
//			
//			tdFileName.title = strOldFileName;
//			hasFileName = true;					 
//		}				
	
//		document.title = tdInformation.innerText + '... 已完成 {' + tdPercent.innerText + '}';
	
		if(oRoot.children.item("Finished").text == 'True')
		{
		    tdPercent.innerHTML	= oRoot.children.item("Percent").text;
			clearTimeout(timeoutid);
			//alert('上传完毕，点确定关闭此窗口！');
			//window.close();
		}
		else
		{					
			timeoutid = setTimeout("ShowDivBar()", 1000);						// 每 0.5 秒重载
		}
	}
}
