function FilesSplashUI(listContainerId, confObj) {
	this.name = "FilesSplashUI";
	this.elm = document.getElementById(listContainerId);
	if (typeof confObj != "object") confObj = {};
	if (typeof confObj.numeric == "undefined") confObj.numeric = false;
	if (typeof confObj.serverMark == "undefined") confObj.serverMark = false;
	if (typeof confObj.linkPrefix == "undefined") confObj.linkPrefix = "http://files.mail.ru/";
	if (typeof confObj.clearContainer == "undefined") confObj.clearContainer = false;
	if (typeof confObj.ShowSuperFile == "undefined") confObj.ShowSuperFile = true;
	if (typeof confObj.WhiteFade == "undefined") confObj.WhiteFade = false;
	this.conf = confObj;
	if (confObj.clearContainer) {
		this.elm.innerHTML="";
		if (window.navigator.appName.indexOf('icros') !=-1) {
			var olddiv = this.elm;
			var newidv = document.createElement("div");
			newidv.id = listContainerId;
			olddiv.parentNode.insertBefore(newidv,olddiv);
			olddiv.parentNode.removeChild(olddiv);
			this.elm = newidv;
		}
	}
	function _addHandler(object, event, handler, useCapture) {
		if (object.addEventListener) {
			object.addEventListener(event, handler, useCapture ? useCapture : false);
		} else if (object.attachEvent) {
			object.attachEvent('on' + event, handler);
		}
	}
	_addHandler(window, 'resize', this.CheckUpBlockWidth);
	this.$ = function (id) {return document.getElementById(id)};
	this.deleteElement = function (elem) {elem.parentNode.removeChild(elem)};
};

FilesSplashUI.prototype.onAdd = function (id, fileName, fileSize, UpServer, itemsArr, optFileid) {
	var nextListId = "";
	if (itemsArr) nextListId = itemsArr.length + 1;
	var newDiv = document.createElement('div');
	newDiv.id = 'fstr' + id;
	newDiv.className = "strHidde";
	var yellowPanel = true;
	if (window.gIsMyDialog || this.conf.WhiteFade) yellowPanel = false;

	newHtml =
	'<div class="line">'+
	'<div class="str">'+
		'<table cellspacing="0">'+
			'<tr><td>'+
				'<span id="uFileBox'+id+'">'+
					'<span id="uFileIcon'+id+'" title="'+t('TtlSttsWaiting')+'">'+
						'<img src="http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif" width=16 height=16>'+
					'</span>'+
					'<span id="uFileProgressLabel' + id + '" class="percents"></span>' +
					'<span id="uFileName'+id+'" title="' + fileName;
					if (fileSize > -1)	newHtml +=' ('+fileSize+' '+t('UnitsByte')+')';
					newHtml +='">';
					if (this.conf.numeric) newHtml +='<span id="uFileNum'+ id + '">' +nextListId +'</span>.&nbsp;';
					if (UpServer && this.conf.serverMark) newHtml  += UpServer.ServerMark;
					newHtml += fileName+
					'</span>'+'\n<span id="uFileSize'+id+'">';
					if (fileSize > -1)	newHtml +='('+formatFsize(fileSize)+')';
					newHtml +='</span>' +
				'</span>'+
			'</td>'+
			'<td id="uFileControlBoxOne'+id+'" class="pl5 '+(yellowPanel?'boxY':'boxW')+'">'+
				'<span id="uFileControl'+id+'">'+
					'<a id="uFileDelIcon'+id+'" title="'+t('TtlIcCancelUpload')+'" href="#">'+
						'<img src="http://img.mail.ru/mail/ru/images/delete_alb_f.gif" width="16" height="16" />'+
					'</a>';
					if (this.conf.ShowSuperFile) {
						newHtml +='<a id="uFileSuper'+id+'" style="display:none" href="#" onclick="return ShowSuperFileOffer(this,event,'+id+ (optFileid ? "," + optFileid : "")  +');">'+
							'<img alt="['+t('FL_notSuperFileTitle')+']" title="'+t('FL_notSuperFileTitle')+'" src="http://img.mail.ru/mail/ru/images/files/superF.gif" width=16 height=16>'+
						'</a>';
					}	
					//'<a id="uFileRetryIcon'+id+'" style="display:none" title="'+t('TtlIcRetryUpload')+'" href="#">'+
					//	'<img src="http://img.mail.ru/mail/ru/images/btn_turn_left.gif" width="16" height="16" />'+
					//'</a>'+
				newHtml +='</span>'+
			'</td></tr>'+
		'</table>'+
	'</div>'+
	'<div class="fade" id="uFileFade'+id+'">'+
		'<table cellspacing="0"><tr>'+
			'<td>'+
				'<img width="50" height="16" alt="" src="http://img.mail.ru/mail/ru/images/files/'+
					(yellowPanel?'fade-yellow.png':'fade.png')+
				'" />'+
			'</td>'+
			'<td id="uFileControlBoxTwo'+id+'" class="'+(yellowPanel?'boxY':'boxW')+'"></td>'+
		'</tr>'+
		'</table>'+
	'</div>'+
	'<input name="uFilesIDs" type="hidden" id="'+ id +'"/></div>';

	newDiv.innerHTML = newHtml;
	this.elm.appendChild(newDiv);
	var _this = this;
	this.$('uFileDelIcon' + id).onclick = function() { _this.cbTerminate(id); return false; };
	//this.$('uFileRetryIcon' + id).onclick = function() { _this.cbRetry(id); return false; };

	this.CheckUpBlockWidth(id);
	return true;
};

FilesSplashUI.prototype.onReady = function (id) {
	if (this.$('uFileIcon' +  id)) {
		this.$('uFileIcon' +  id).innerHTML = "<img style='vertical-align: middle;' src='http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif' width=16 height=16>";
	}
	//this.CheckUpBlockWidth(id);
};

FilesSplashUI.prototype.onStart = function (id) {
	this.$('uFileProgressLabel' + id).innerHTML = '';
	this.$('uFileProgressLabel' + id).style.display = 'inline';
	this.$('uFileIcon' +  id).innerHTML = "<img style='vertical-align: middle;' src='http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif' width=16 height=16>";
	this.CheckUpBlockWidth(id);
};

FilesSplashUI.prototype.onStop = function (id) {
	//this.$('uFileRetryIcon' +  id).style.display = 'inline';
	this.$('uFileProgressLabel' + id).style.display = 'inline';
	this.$('uFileIcon' +  id).innerHTML = "<img style='vertical-align: middle;' src='http://img.mail.ru/mail/ru/images/files/delete_alb_fd' width=16 height=16>";
	this.CheckUpBlockWidth(id);
};

FilesSplashUI.prototype.onApproveUppload = function (id) {
	return true;
};

FilesSplashUI.prototype.onRetry = function (id) {
	//this.$('uFileRetryIcon' +  id).style.display = 'none';
	this.$('uFileIcon' +  id).innerHTML = "<img style='vertical-align: middle;' src='http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif' width=16 height=16>";
	this.CheckUpBlockWidth(id);
	return true;
};

FilesSplashUI.prototype.onDone = function (id, dataObj, extraData) {
	this.$('uFileIcon' +  id).style.display = 'none';
	this.$('uFileProgressLabel' + id).style.display = 'none';
	this.$('uFileSize' +  id).style.display = 'inline';
	this.$('uFileSuper' +  id).style.display = '';
	this.$('uFileSuper' +  id).setAttribute('x-fileid', dataObj.fileid);
	this.$('uFileSize' +  id).innerHTML = '(' + formatFsize(dataObj.filesize) + ')';
	this.CheckUpBlockWidth(id);
	return true;
};

FilesSplashUI.prototype.onProgress = function (id, bytesLoaded, bytesTotal) {
	var newProgress = Math.floor(100*bytesLoaded/bytesTotal);
	try {
		this.$('uFileIcon' +  id).style.display = 'none';
		this.$('uFileProgressLabel' + id).style.display = 'inline';
		this.$('uFileProgressLabel' + id).innerHTML = "[" + newProgress + "%]&nbsp;";
		this.$('uFileProgressLabel' + id).title = ts('TtlUploadedBytes', bytesLoaded);
	
		this.$('uFileProgressLabel'+id).setAttribute('X-LastProgress', newProgress);
	} catch (e) {}
	this.CheckUpBlockWidth(id);
	return true;
};

FilesSplashUI.prototype.onUploadError = function (id, errorCode, isCustom) {
	var labelObj = this.$('uFileIcon' + id);
	labelObj.innerHTML = '<img style="vertical-align: middle;" src="http://img.mail.ru/mail/ru/images/files/attent1.gif" width=16 height=16>&nbsp;';
	labelObj.style.display = 'inline';
	this.$('uFileProgressLabel' + id).style.display = 'none';

	var errObj = this.cbGetFileServer(id).GetErrorDetails(errorCode);

	var objArray = new Array();
	objArray.push(labelObj);

	var _this = this;
	for (var i in objArray) {
		var curObj = objArray[i];
		curObj.title = errObj.Title;
		if (errObj.IsConfirm) {
			curObj.onclick = function() {
				var answer = confirm(errObj.Text)
			 	if (answer) {
					_this.cbRetry(id); return false;
					return false;
				}
			};
		} else {
			curObj.onclick = function() { alert(errObj.Text); return false; };
		}
	}
	labelObj.setAttribute('X-ErrorCode', errorCode );
	labelObj.className = 'cursorHand';
	this.CheckUpBlockWidth(id);
	return true;
};


FilesSplashUI.prototype.onAfterUploadTerminate = function (id, isItDone, backupData) {
	//warning ! no this in common case for this method
	if (backupData.id) id = backupData.id;
	backupData.obj.firstChild.src = backupData.imgsrc;
	if (isItDone) {
		this.deleteElement(this.$("fstr" + id));
	} else {
		backupData.obj.onclick = backupData.onclick;
		backupData.obj.title = backupData.title
		alert (t('DeleteFailed'));
	}
	if (MRUUploader.queue.GetPackedList().length == 1) {
		gMsgCode = "";
		$('curLinkCodeContainer').style.display = "none";
	}
};

FilesSplashUI.prototype.onBeforeUploadTerminate = function (id, dataObj) {
	if (dataObj.fileid) {
		var fileId = dataObj.fileid;

		var objId;
		objId = 'uFileDelIcon' + id;
		var backupData = new Object();
		//backupData.name="backupData";
		backupData.title = this.$(objId).title;
		backupData.onclick = this.$(objId).onclick;
		backupData.imgsrc = this.$(objId).firstChild.src;
		backupData.fileid = fileId;
		backupData.id = id;
		backupData.obj = this.$(objId);
		var _this = this;
		backupData.cbFunction = function (id, isItDone, backupData) {_this.onAfterUploadTerminate(id, isItDone, backupData)};

		this.$(objId).title = t('TtlFileIsDeleting');
		this.$(objId).onclick = function () {};
		this.$(objId).firstChild.src = "http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif";
		this.CheckUpBlockWidth(id);
		return backupData;
	} else {
		this.deleteElement(this.$("fstr" + id));
		return false;
	}
};

FilesSplashUI.prototype.onLinkUpdate = function (linkcode) {
	if (linkcode) {
		var lkCont = $('curLinkCodeContainer');
		this.$('curLinkCodeContainer').style.display = "";
		this.$('curLinkCode').value  = this.conf.linkPrefix + linkcode;
	} else {
		this.$('curLinkCodeContainer').style.display = "none";
	}
};

FilesSplashUI.prototype.onRenumber = function (idArr) {
	if (!this.conf.numeric) return;
	for (var i = 0; i < idArr.length; i++) {
		if (this.$('uFileNum' + idArr[i])) { this.$('uFileNum' + idArr[i]).innerHTML= i + 1; }
	}
	try {
		this.$('lblTotalCount').innerHTML = idArr.length;
	} catch(e) {}
};

FilesSplashUI.prototype.CheckUpBlockWidth = function (id) {
	function gebi(e) {return document.getElementById(e)};
	function _processItem (id) {
		var wt=gebi('uFileBox'+id);
		var wo=gebi('fstr'+id);
		var b1=gebi('uFileControlBoxOne'+id);
		var b2=gebi('uFileControlBoxTwo'+id);

		var ct=gebi('uFileControl'+id);
		var fd=gebi('uFileFade'+id);

		try {
			var cw=(b1.offsetWidth-0) + (b2.offsetWidth -0);
			if ((wo.offsetWidth-cw)>wt.offsetWidth) {
				fd.style.display='none';
				b1.appendChild(ct);
			} else {
				fd.style.display='block';
				b2.appendChild(ct);
			}
		} catch (e) {/*race condtition when item is deleted during check up*/}
	}
	if (typeof id == "number") {
		_processItem(id);
	} else {
		var x=document.getElementsByName('uFilesIDs');
		for (var i=0;i<x.length;i++) {
			_processItem(x[i].id);
		}
	}
};

	
FilesSplashUI.prototype.UpdateUploadStatistics = function (serverObj) {
	var val;
	if (val = serverObj.SrvSpaceLeft) {
		try {
			this.$(serverObj.uid + 'lblAvailSizeSrv').innerHTML = formatFsize(val, 2);
			this.$(serverObj.uid + 'lblAvailSizeSrv').title = ts('TtlFree', formatFsize(val, 2, 2));
		} catch(e) {}
	}
	if (val = serverObj.SrvSpaceLeftCalc) {
		try {
			this.$(serverObj.uid + 'lblAvailSize').innerHTML = formatFsize(val, 2);
			this.$(serverObj.uid + 'lblAvailSize').title = formatFsize(val, 2, 2);
		} catch(e) {}
	}
};

//campatibility stub-function
function UpdateUploadStatistics(serverObj) {}


function cbAjaxIndicatorSuperFile(OnOff, vfileid) {
	var id = this.saved_obj.xuid;
	if (id) {
		if (OnOff) {
			$('uFileSuper' +  id).innerHTML = '<img src="http://img.mail.ru/mail/ru/images/files/indicator_arrows.gif" width=16 height=16>';
		} else {
			$('uFileSuper' +  id).innerHTML = '<img style="width: 16px; heigth: 16px;" src="http://img.mail.ru/mail/ru/images/files/superF.gif" title="' + t('FL_notSuperFileTitle') + '" alt="[' + t('FL_notSuperFileTitle') + ']"/>';
		}
	}
}

function ShowSuperFileOffer(obj, e, id, vfileid)
{
	if (!e) var e = window.event;
	obj.xuid = id;
	if (!vfileid) vfileid = this.$('uFileSuper' +  id).getAttribute('x-fileid') - 0;
	ShowSMSContextOffer(11, vfileid, e, obj, cbAjaxIndicatorSuperFile);
	if (e) e.returnValue=false;
	return false;
}

try {
	gJsReady({'name': 'UI'});
} catch (e) {};
