//
// Javascript functions to provide progress bar
//
var ProgressBar = Class.create({
	
	initialize: function(request_json, oncomplete, barid, varname)
	{
		this._request_url = request_json.url;
		this._oncomplete = oncomplete;
		this._freq = request_json.delay;
		this._linkmode = request_json.linkmode;
		this._extralinktext = request_json.extralinktext;
		this._extralinkdate = request_json.extralinkdate;
		this._barid = barid;
		this._complete = false;
		this._varname = varname;
		this._nohideoncompletion = request_json.nohideoncompletion;
		window.setTimeout(this._varname + ".update_progress_info()", this._freq);
		if(document.getElementById('progress-bar_' + this._barid))
		{
	        document.getElementById('progress-bar_' + this._barid).style.marginLeft = 0;
		}
	},
	
    // Update progress bar
    update_progress_info: function() 
    {
        new Ajax.Request(this._request_url, {
					method: "get",
					onSuccess: this._OnLoad.bind(this)
					
				});
	},
	
	_OnLoad: function(data)
	{
		res = data.responseJSON;
        var progress = parseInt(res.progress);
        var progress_width = 0;
        if(document.getElementById('progress-surround_' + this._barid))
        {
        	document.getElementById('progress-surround_' + this._barid).style.display='';
		}
		if(document.getElementById('progress-bar_' + this._barid))
		{
	        document.getElementById('progress-bar_' + this._barid).style.width = progress + '%';
		}
		if(document.getElementById('progress-info_' + this._barid))
		{
			var strVal = "Unknown";
			if(res.message)
			{
				strVal = res.message;
			}
			if(res.link)
			{
				if(res.linktext)
				{
					strVal = res.linktext;
				}
				if(this._extralinktext && res.extratext)
				{
					strVal += " - " + res.extratext;
				}
				if(this._extralinkdate && res.time)
				{
					strVal += "<small> - " + res.time + "</small>";
				}
				if(this._linkmode == "button")
				{
					strVal = "<input class=\"imsbutton\" value=\"" + strVal + "\" name=\"htmlbutton\" id=\"\" onclick=\"window.location.href='" + res.link + "';\" type=\"button\">";
				}
				else
				{
					strVal = "<a href=\"" + res.link + "\">" + strVal + "</a>";
				}
				if(document.getElementById('progress-surround_' + this._barid) && !this._nohideoncompletion)
				{
					document.getElementById('progress-surround_' + this._barid).style.display='none';
				}
				if(document.getElementById('progress-button_' + this._barid))
				{
					document.getElementById('progress-button_' + this._barid).innerHTML = strVal;
					document.getElementById('progress-info_' + this._barid).innerHTML = res.message;
				}
				else
				{
					document.getElementById('progress-info_' + this._barid).innerHTML = strVal;
				}
			}
			else
			{
		        document.getElementById('progress-info_' + this._barid).innerHTML = strVal;
			}
		}
		var dismissEl = $("download-dismiss_"+this._barid);
		if (dismissEl) 
		{
		  if (res.allowdismiss) dismissEl.show();
		  else dismissEl.hide();
		}
		
        if(res.complete && !this._complete)
        {
           	this._complete = true;
           	eval(this._oncomplete);
		}
		if(res.nextrequest > 0)
		{
			window.setTimeout(this._varname + ".update_progress_info()", res.nextrequest);
		}
		else if(!this._complete)
		{
			window.setTimeout(this._varname + ".update_progress_info()", this._freq);
		}
    }
});
