var C_scroller = Class.create({
	initialize: function(ext_options){
		this.pausescroll = false;
		this.current_element= 0;
		this.total_element= 0;
		
		this.options = {
			data: new Array(),
			next_btn: $$(".scroller-next A").first(),
			prev_btn: $$(".scroller-prev A").first(),
			content_container: $("scroller-content"),
			main_container: Object,	// Hover per pause scroll
			
//			current_element: 0,
//			total_element: 0,
			speed_animation: 0.3,
			base_company_path: '/ebook/company.php?id=',
			output_template: 
			'<div class="company-data"><a class="company-link" title="<%= title %>" ' +
			'href="<%= base_path %><%= id %>"><%= content %></a></div>',
			url_data: '/ajax_php/ebook_companies.php',
			
			autoscroll: true,
			scrolldelay: 3,
			random_start: true
			//,pausescroll: false
		};
		
		if(this.options.content_container){
			this.options.main_container = this.options.content_container.up(1);	// Hover per pause scroll
		}
		
		Object.extend(this.options, ext_options || { });
		
		if(this.options.content_container){
			this.options.next_btn.observe('click', this.scroll_next.bind(this));
			this.options.prev_btn.observe('click', this.scroll_prev.bind(this));
			this.load_data();
		//autoscroll
			if(this.options.autoscroll == true){
				this.autoscroll_it();
			}
		}
	},
	
	autoscroll_it: function(){
		var pe = new PeriodicalExecuter(this.autoscroll_next.bind(this), this.options.scrolldelay);
	//events
		Event.observe(this.options.main_container ,'mouseover', (function() {
			  this.pausescroll = true;
		}).bind(this));
		Event.observe(this.options.main_container ,'mouseout', (function(){
			  this.pausescroll = false;
		}).bind(this)); 
	},
	
	autoscroll_next: function(){
		if(this.pausescroll != true){
			this.scroll_next();
		}
	},
	
	render_hash: function(hash_element){
		if(hash_element){
			//hash_element
			var template_syntax= /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/;
			var data_template = new Template(this.options.output_template, template_syntax);
			
			hash_element.set('base_path', this.options.base_company_path);
			var HTML_data = data_template.evaluate(hash_element);
			
			this.options.content_container.fade({
				duration: this.options.speed_animation, 
	//			parent_options: this.options, 	// hack for binding this? --> binded funzione intera
				afterFinish: (function(){
					this.options.content_container.update(HTML_data);
					this.options.content_container.appear({
						duration: this.options.speed_animation,
						delay: this.options.speed_animation
					});
				}).bind(this)
			});
		}
	},
	
	load_data: function(){
		//	Ajax loader
		if(!this.options.url_data.blank()){
			var _load_result = new Array();
			var _url = this.options.url_data;
			
			new Ajax.Request(_url,{
				method: 'get',
				asynchronous: true,
				evalJS: false,
				onFailure: function(transport) {
					//alert("Impossibile recuperare i dati [1]");
					return false;
				},
				onSuccess: (function(transport) {
					var _to_eval_code = transport.responseText;
					if(!_to_eval_code.blank()){
						eval(_to_eval_code);
						this.options.data = _load_result;
						this.total_element = this.options.data.size()-1;

						var random_first_index = 0;
						if(this.options.random_start == true){
							random_first_index = Math.floor(Math.random() * ((this.options.data.size()-1) - 0 + 1) + 0);
						}
						this.render_hash(this.options.data[random_first_index]);
					}
				}).bind(this)
			});		
		}
	},
	
	scroll_next: function(event){
		if(event){
			Event.stop(event);
		}
		if(this.current_element == this.total_element){
			this.current_element = 0;
		}else{
			this.current_element++;
		}
		var next_hash = this.options.data[this.current_element];
		this.render_hash(next_hash);
	},
	
	scroll_prev: function(event){
		Event.stop(event);
		if(this.current_element == 0){
			this.current_element = this.total_element;
		}else{
			this.current_element--;
		}
		var next_hash = this.options.data[this.current_element];
		this.render_hash(next_hash);
	}
});