/**
 * klasa daje mozliwosc animowanego przesuniecia w poziomie,
 * parametry konstruktora: 
 * id kontenera nadrzednego, id przycisku w lewo, id przycisku w prawo, id przesuwanego kontenera, szerokosc pojedynczego elementu w kontenerze
 * ilosc przesuwanych w jednym kroku elementow , ilosc wszystkich elementow
 */ 
 
B_behaviour_move_horizontal = Ext.extend(Object,{
	constructor: function(parent_id, left_button_id, right_button_id, moved_container_id, moved_step_width, steps_once, num_items){
		
		var lb = Ext.get(left_button_id);
		var rb = Ext.get(right_button_id);
		
		this.moved = Ext.get(moved_container_id);
		
		this.current = 0;
		this.step = steps_once;
		this.num_items = num_items;
		this.moved_step_width = moved_step_width;
		
		lb.addListener('click', this.mv_left.createDelegate(this));
		rb.addListener('click', this.mv_right.createDelegate(this));
		
	},
	
	mv_right: function(){
		var nc = this.current + this.step;
		this.current = nc > (this.num_items - this.step) ?  this.num_items - this.step: nc;
		this.moveto(this.current * this.moved_step_width);
	},
	mv_left: function(){
		var nc = this.current - this.step;
		this.current = nc < 0 ? 0 : nc;
		this.moveto(this.current * this.moved_step_width);
	},
	
	moveto : function(new_x){
		this.moved.shift({
		    left: -new_x,
		    easing: 'easeOut',
		    duration: 1
		});
	}
});

B_behaviour_auto_fade = Ext.extend(Object,{
	constructor: function(parent_id, faded_container_id, moved_step_height, num_items, delay){
	this.moved_step_height =  moved_step_height;
	this.num_items = num_items;
	this.current_item = 0;
	this.faded = Ext.get(faded_container_id);
	var task = {
		    run: this.next.createDelegate(this),
		    interval: delay*1000
		};
	this.runner = new Ext.util.TaskRunner();
	this.runner.start(task);
	},
	
	next : function(){
		this.current_item = (this.current_item < (this.num_items-1)) ? this.current_item+1 : 0;
		this.faded.fadeOut({
			duration : 1,
			use_display : false,
			remove : false,
			callback : this.after_fade.createDelegate(this)
		});
		
	},
	
	after_fade : function(){
		this.faded.setStyle('top', -this.current_item * this.moved_step_height+'px'); 
		this.faded.fadeIn({
			duration : 1,
			use_display : false
		})
	}


});


DC_banner = Ext.extend(Object, {
	
	constructor : function (target_id, config){
		this.templates = new Object();
		this.target_id = target_id;
		this.target = Ext.fly(target_id);	
		this.templates_array = new Array();
		this.blocks = new Object();
		this.behaviours = new Object();
		this.temporary = new Array();
		this.counters = new Array();
		this.config_file = config;
		this.config = this.get_config();	
	},
		
	get_config : function(){
		Ext.Ajax.request({
			   url: this.config_file+'.json',
			   success: this.get_config_response.createDelegate(this),
			   failure: function(response, opts) {
			      console.log('błąd odczytu pliku konfiguracji ' + response.status);
			   }
			 
			   
			});
		
	},
	
	get_config_response : function(response, opts){
      this.config = Ext.decode(response.responseText);
      this.get_dataset();  
	},
	
	get_dataset : function(){
		Ext.Ajax.request({
			   url: this.config.base_path+this.config.data_directory + this.config.dataset_name + '.json',
			   success: this.get_dataset_response.createDelegate(this),
			   failure: function(response, opts) {
			      console.log('błąd odczytu pliku danych ' + response.status);
			   }	 
			});
	},
	
	get_dataset_response : function(response, opts){
	      this.dataset = Ext.decode(response.responseText);
	      this.build_template_list();
	},
	
	build_template_list : function(){
		var element;
		for(var ii=0; ii<this.config.build_blocks.length; ii++) this.templates_array.push(this.config.build_blocks[ii].template);
		this.get_templates();
	},
	
	get_templates : function(){
		if(this.templates_array.length){
			element = this.templates_array.shift();
					Ext.Ajax.request({
						   url: this.config.base_path+this.config.resource_directory + element + '.htpl',
						   success: this.get_templates_response.createDelegate(this,[element],0),
						   failure: function(response, opts) {
						      console.log('błąd odczytu szablonów ' + response.status);
						   }		   
						});
				
		} else this.after_requests();
	},
	
	get_templates_response : function(element, response, opts){
		this.templates[element] = response.responseText;
		this.get_templates();
	},
	
	after_requests : function(){
		this.build_blocks();	
	},
		
	dataset_used : function(varlist){
		for(var idx in varlist) if(varlist[idx].from == 'data_item') return true;
		return false;
	},
	
	build_blocks : function(){		
		var element = this.config.build_blocks.shift();		
		while(element!=undefined){
			var block_string ='';			
			var current_item = new Object();
			var data_item = new Object();
			
			//lista zmiennych bloku
			var replacement_list = new Array();
			for(var varidx in element.vars) if(element.vars[varidx].name) replacement_list.push(element.vars[varidx].name);
			
			//jesli blok uzywa zbioru danych szablon budowany jest dla kazdego elementu zbioru
			if(this.dataset_used(element.vars)){
				this.counters[element.block_name] = 0;
				this.data_item = new Object();
				for(var item_enum=0 ; item_enum < this.dataset.items.length; item_enum++){
					this.data_item = this.dataset.items[item_enum];
					this.temporary['item_enum'] = item_enum;
					if(!element.trigger_var || this.data_item[element.trigger_var]) {
						this.counters[element.block_name]++;
						for(var varidx=0; varidx < element.vars.length; varidx++) {
								current_item[element.vars[varidx].name] = this[element.vars[varidx].from][element.vars[varidx].name];
								}
					block_string += this.fill_template(this.templates[element.template], current_item, replacement_list);
					}
				}
			} else { 
				//nie uzywa zestawu danych, jednorazowe podstawienie
				for(var varidx=0; varidx < element.vars.length; varidx++) {
					current_item[element.vars[varidx].name] = this[element.vars[varidx].from][element.vars[varidx].name];
					}
				block_string += this.fill_template(this.templates[element.template], current_item, replacement_list);	
			}
			this.blocks[element.block_name] = block_string;
			element = this.config.build_blocks.shift();
		}
		this.build_content();
	},
	
	
	
	fill_template : function(template, data_item, replacement_list){
		var le;
		var idx=0
		while(replacement_list[idx]){
			le = replacement_list[idx];
			template = template.replace(new RegExp('\\{\\$' + le + '\\}','g'), data_item[le]);
			idx++;
			}
		return template;			
	},
	
	build_content : function(){
		Ext.fly(this.target_id).update(this.blocks.out_view);
		this.add_behaviours();
	},
	
	add_behaviours : function(){
		this.behaviours.move = new B_behaviour_move_horizontal(this.config.container_id, 'button_left', 'button_right','thumb_list_frame',120,4,this.counters.thumb_list);
		this.behaviours.fade = new B_behaviour_auto_fade(this.config.container_id, 'dtb_description_list_containter', 240, this.counters.description_list, 7);	
	}
});


