
HF.progress = function(el, conf) {
	this.init(el, conf);
};

HF.progress.IMAGE_LOADER = '/tpl/shared/handfisch/img/hf_progress_loader.gif';

HF.progress.TYPE_LOADER   = 1;
HF.progress.TYPE_PROGRESS = 2;




HF.progress.prototype = {
	
	element: null,
	elementConf: null,
	
	status: -1,
	
	imageLoader: null,
	
	type: HF.progress.TYPE_LOADER,
	listenUrl: null,
	listenTimer: null,
	configParams: ['type', 'listenUrl', 'listenTimer'],
		
	listenTimerPnt: null,

	
	init: function(el, conf) {
		this.elementConf = el;
		this.fixedcenter = true;
		this.context          = [];
	
		if (conf) for(var c in conf) this[c] = conf[c];

		this.dialog = new YAHOO.widget.SimpleDialog("simpledialog1",  { 
			width: "380px",
			effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
			visible: false,
			modal:true,
			zIndex:99,
			close:true,
			fixedcenter:true,
			constraintoviewport:true,
			mystate:null,
			dragOnly:true,
			icon: YAHOO.widget.SimpleDialog.ICON_WARN,
			buttons: [ { text:'OK', handler:function() { this.hide(); } , isDefault:true} ]
		} );
		
		this.dialog.onBeforeShow = function() {
			this.div_tl = document.createElement("DIV");
			this.div_tl.className = 'tl';
			this.appendToHeader(this.div_tl);
			this.div_tr = document.createElement("DIV");
			this.div_tr.className = 'tr';
			this.appendToHeader(this.div_tr);
		}

		this.dialog.subscribe("beforeShow", this.dialog.onBeforeShow);
		/*this.dialog.hideEvent.subscribe(this._after_dialog, this);*/
		
	},
	
	/*
	_after_dialog: function(e, a, o) {
		return;
		if (o.load_after_success && o.dialog.mystate) {
			var url = o.load_after_success;
			if (url == '_self') url = window.location.href;
			window.location.replace(url);
		}
	},	
	*/
	
	show: function() {
		
		this.dialog.cfg.queueProperty('fixedcenter', this.fixedcenter);
		this.dialog.cfg.queueProperty('context', this.context);
		this.dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
		this.dialog.mystate = null;
		
		if (!this.element) {
			this.element = $(this.elementConf);
			if (!this.element) { alert('element not found: '+this.elementConf); return false;}
		}
		
		if (this.type == HF.progress.TYPE_LOADER && !this.imageLoader) {
			this.imageLoader = document.createElement('img');
			this.imageLoader.src = HF.progress.IMAGE_LOADER;
			this.element.appendChild(this.imageLoader);
		}
		
		if (this.listenUrl) this.listenUrlCall();
	},
	
	hide: function() {
		if (this.imageLoader) {
			this.element.removeChild(this.imageLoader);
			this.imageLoader = null;
		}
		this.dialog.mystate = 1;
/*		this._after_dialog(null, null, this);*/
	},

	listenUrlCall: function() {
		YAHOO.util.Connect.setForm('frmcell', false);
		YAHOO.util.Connect.asyncRequest('POST', this.listenUrl, {success:this.listenUrlCallback, scope:this});
		if (this.listenTimer && this.listenTimer > 0) {
			var me = this;
			this.listenTimerPnt = window.setTimeout(function() {me.listenUrlCall()}, this.listenTimer);
		}
	},
	
	listenUrlCallback: function(o) 
	{
		if (!o.responseText) 
		{
			res = {'status':0, 'html':'Keine Antwort'};
		}
		else 
		{
			try 
			{
			    res = YAHOO.lang.JSON.parse(o.responseText);
			}
			catch (e) 
			{
				res = {'status':0, 'html':'Syntax-Error-JS:<pre style="white-space:pre;">'+o.responseText+'</pre>'};
			}
		}
		

		if(this.callbackOutside)
		{	
			eval(this.callbackOutside + "(" + res.status + ");");
		}

		if (res.write_id) {
			for(var d in res.write_id) {
				var elem;
				if (elem = $(d)) elem.innerHTML = res.write_id[d];
			}
		}
		
		if (res.html) 
		{
			var header = (res.header) ? res.header : 'Erfolg';
			this.dialog.setHeader(header);
			this.dialog.setBody(res.html);
			this.dialog.render(document.body);
			this.dialog.show();
			
			window.clearTimeout(this.listenTimerPnt);
			this.listenTimerPnt = null;
			this.hide();
			return;

		}
		
		if (this.type == HF.progress.TYPE_LOADER && res.status == 1) {
			if (this.listenTimerPnt) {
				window.clearTimeout(this.listenTimerPnt);
				this.listenTimerPnt = null;
			}
			this.hide();
		}
		
		

		
	}
	
};
