FG_navi = Ext.extend(Object,{
	constructor: function(delay, default_doc, element_id){
	this.current_location = '';
	this.doc_content = '';
	this.default_doc = default_doc;
	this.element = element_id;
	this.faded = Ext.get(element_id);
	this.aktywny_widok = '';
	var task = {
		    run: this.check_location.createDelegate(this),
		    interval: delay*1000
		};
	this.runner = new Ext.util.TaskRunner();
	this.runner.start(task);
	},
	
	check_location : function(){
		var state = location.hash.substring(1);
		if (state == '') state = this.default_doc;
		if (state!=this.current_location){
			this.current_location = state;
			this.change(this.current_location);
		}
	},
	
	change : function(ca){
		Ext.Ajax.request({
			   url: 'http://drzewaformowane.fingero.eu?alias='+ca,
			   success: this.get_response.createDelegate(this,[this.element],0),
			   failure: function(response, opts) {
			      console.log('błąd odczytu dokumentu ' + response.status);
			   }		   
			});
		},
	

	get_response : function(element, response, opts){
		this.doc_content = response.responseText;
		this.faded.fadeOut({
			duration : 1,
			use_display : false,
			remove : false,
			callback : this.after_fade.createDelegate(this)
		});
	},
	
	after_fade : function(){
		Ext.fly(this.element).update(this.doc_content);
		this.faded.fadeIn({
				duration : 1,
				use_display : false
			})
	}
});

FG_bgc = Ext.extend(Object,{
	constructor: function(delay, faded_element, num_items){
	this.current_item=0;
	this.delay = delay;
	this.num_items = num_items;
	this.faded = Ext.get(faded_element);
	this.task = {
		    run: this.change_background.createDelegate(this),
		    interval: this.delay*1000
		};
	this.stopped = false;
	this.runner = new Ext.util.TaskRunner();
	this.runner.start(this.task);
	},
	
	stop_bgc : function(){
		console.log('stop bg');
		this.stopped=true;
		this.runner.stop(this.task);
		this.faded.stopFx();
		
	},
	
	start_bgc : function(){
		if(this.stopped){
			this.runner.start(this.task);
			this.stopped=false;
		}
	},
	
	change_background : function(){
		this.current_item = (this.current_item < (this.num_items)) ? this.current_item+1 : 0;
		
		this.faded.fadeOut({
			duration : 2,
			use_display : false,
			remove : false,
			callback : this.after_fade.createDelegate(this)
		});
		
	},
	
	after_fade : function(){
		if(this.stopped)this.faded.setVisible(false,true);
		this.faded.setStyle('background-image', 'url(css/img/tree_'+this.current_item+'.png)'); 
		this.faded.fadeIn({
			duration : 2,
			use_display : false,
			callback: this.hide_when_stopped.createDelegate(this)
		})
	
	 },
	
	hide_when_stopped : function(){
		console.log('stopped: '+this.stopped);
		if(this.stopped)this.faded.setVisible(false,true);
	}
	

});

