window.addEvent('domready', function(){            
 new SmoothScroll();
});

window.addEvent('scroll', function(){
	var top=$('gotop');
	if (top) {
		if (window.getScrollTop()==0) $E('a',top).setStyle('visibility','hidden');
		else $E('a',top).setStyle('visibility','visible');		
	}
});

window.addEvent('domready', function(){
 $$('input.form').each(function(el){
		el.addEvent('focus',function(){
			this.addClass('formFocus');
		});
		el.addEvent('blur',function(){
			this.removeClass('formFocus');
		});
	});
});

window.addEvent('load', function(){            
 var myTips = new Tips($$('.tooltip-trigger'), {
	 maxTitleChars: 30,
		fixed: true,
		className: 'tooltip'
 });
});

Element.extend({
	resizingTextArea: function() {
		var rows = this.rows;
		var cols = this.cols;
		var defaultRows = Math.max(rows, 1);
		function resize(){
			var lines = this.value.split('\n'), newRows = lines.length;
			lines.each(function(line){
				if (line.length >= cols) newRows += Math.floor(line.length / cols);
			});
			newRows+=1;
			if (newRows > rows) this.rows = newRows;
			if (newRows < rows) this.rows = Math.max(defaultRows, newRows);
		};
		return this.addEvent('click', resize).addEvent('keyup', resize).addClass('resizing');
	},
	widthingTextArea: function() {
		var input=this;
		var size=this.getComputedSize();
		var colWidth=(size.width)/this.cols*0.966;
		var pad=size.totalWidth-size.width;
		function update(){
		 var containerWidth=input.getParent().getComputedSize().width;
		 if (containerWidth>0) input.cols=Math.floor((containerWidth-3*pad)/colWidth);	
 	};
		return window.addEvent('domready',update).addEvent('resize',update);
 }
});

var Countable = new Class({
  options: {
    maximum: 255,
    offset: 256
  },
  initialize: function(inputId, options) {
    // Merges the default options with the ones given as parameters
    this.setOptions(options);
 
    // Retrieves the textarea element based on its name
    input = $(inputId);
 
    if (input) {
      // Creates an element to serve as a handle:
      // 
      //   <span class="nota"></div>
      //
						
						//resizable
						input.cols=42;
						input.rows=3;
						//input.widthingTextArea();
						input.resizingTextArea();
						
      this.handle = new Element("div", {'class': "nota",'styles': {'padding-left': '0px'}});
 
      // Injects this element just after the textarea
      this.handle.injectAfter(input); //setHTML('&nbsp')
 
      // Fires action upon events
      input.addEvent('keydown', this.onKeyPress.bindWithEvent(this));
      input.addEvent('keyup', this.onKeyPress.bindWithEvent(this));		
 
      // Keeps a reference to the handle
      this.input = input;
						
						this.update();
    }
  },
  onKeyPress: function(event) {
    event = new Event(event);
 
    if(!event.shift && !event.control && !event.alt && !event.meta) {
      this.update();
    }
  },
  update: function() {
    // Removes the character entered as the maximum has been reached
    if (this.input.value.length > this.options.maximum) {
      this.input.value = this.input.value.substring(0, this.options.maximum);
    }
 
    // Handles the display of the number of remaining characters
    if (this.input.value.length > (this.options.maximum - this.options.offset)) {
      // Calculates the number of characters left
      var count = this.options.maximum - this.input.value.length;
 
      // Processes message
      if (count == 0) {
        var string = "<span class=\"error\">(caratteri esauriti)</span>";
      } else if (count == 1) {
        var string = "(ancora 1 carattere)";
      } else if (count == this.options.maximum) {
        var string = "(" + count + " caratteri)";
      } else {
        var string = "(ancora " + count + " caratteri)";
      }
 
      // Displays information message
      this.handle.setHTML(string);
						this.handle.setStyle('display', 'block');
						//this.input.setStyle('height', 255-count);
						
    } else {
					 this.handle.setHTML('&nbsp');
						this.handle.setStyle('display', 'none');
				}
  }
});
 
// Adds options management support
Countable.implement(new Options, new Events);

var LimitedSelect = new Class({
  options: {
    maximum: 3
  },
  initialize: function(inputId, options) {
    // Merges the default options with the ones given as parameters
    this.setOptions(options);
 
    // Retrieves the textarea element based on its name
    input = $(inputId);
 
    if (input) {
      // Creates an element to serve as a handle:
      // 
      //   <span class="nota"></div>
      // 				
						 // Keeps a reference to the handle
      this.input = input;
						
						//set input float
						input.setStyle('float', 'left');
						//input.setStyles('float: left');
						
      this.handle = new Element("div", {'class': "nota",'styles': {'padding-left': '8px'}});
					 this.handle.injectAfter(input);			
						
      // Fires action upon events
      input.addEvent('change', this.onChange.bindWithEvent(this));
						
						this.selectedOptions = [];
						
						window.addEvent('load', function(){
        this.handle.setStyle('margin-left', this.input.getSize().size.x+'px');
								this.listSelected();
      }.bind(this));
    }
  },
  onChange: function(event) {
    event = new Event(event);
      this.listSelected();
  },
  listSelected: function() {
    // Removes the character entered as the maximum has been reached
			var out='';
			var count=0;
			var select=this.input;
				
   if (select.options.length>0) {
     for(var i=0;i<select.length;i++) {
						
			 	 if(select.options[i].selected && !new RegExp(i,'g').test(this.selectedOptions.toString())){
        this.selectedOptions.push(i);
      }
						
     if(!select.options[i].selected && new RegExp(i,'g').test(this.selectedOptions.toString())){
      this.selectedOptions = this.selectedOptions.sort(function(a,b){return a-b});  
       for(var j=0; j<this.selectedOptions.length; j++){
         if(this.selectedOptions[j] == i){
            this.selectedOptions.splice(j,1);
         }
       }
     }

     if(this.selectedOptions.length > this.options.maximum){
      var throwAlert = true;
        select.options[i].selected = false;
        this.selectedOptions.pop();
     }
						
	 		  if (this.input.options[i].selected && this.input.options[i].value!='') {
							out +="&rsaquo; " + this.input.options[i].text + "<br>";
							count++;
						}
     }
					
      // Displays information message
						if (count>0) {
       this.handle.setHTML('<strong>Selezionati:</strong><br>' + out);
						} else {
							this.handle.setHTML('&nbsp;');
						}
						
						if(throwAlert == true){
        alert('Puoi slezionare solo '+ this.options.maximum +' elementi!');
        document.body.focus();
      }
				}
  }
});
 
// Adds options management support
LimitedSelect.implement(new Options, new Events);

function Toggle(menu) {
	$$('#'+menu +' li').each(function(el, i) {
				if (el.hasClass('level1'))	{														
     el.removeClass('level1');
			 	el.addClass('level0');
				} else if (el.hasClass('level0'))	{														
     el.removeClass('level0');
			 	el.addClass('level1');
				}
  });
	return false;
}

function checkUploading(fields) {
	//controllo se ci sono dei files da uploadare per cui visualizzo la progressbar
	var progress=false;
	var toUpload=fields;
	toUpload.each(function(field){
	if ($(field)) {
	 if ($(field).value!='') progress=true;
	}
	});
 if (progress) {
		new StickyWinModal({
				content: $('stickyWinContainer').innerHTML,	
				modalOptions:{
						modalStyle:{
								'background-color':'#ffffff',
								'opacity':.6
						},
						hideOnClick:false
				}
		});
	}
}

function equalHeights(divs) {
	var height = 0;
		
	divs.each( function(e){
		if (e.offsetHeight > height){
			height = e.offsetHeight;
		}
		//e.removeClass('(^minh)');
	});
		
	divs.each( function(e){
		e.setStyle('height', height + 'px');
		if (e.offsetHeight > height) e.setStyle( 'height', (height - (e.offsetHeight - height)) + 'px' );
	}); 
}

Element.extend({
	urlCharLimit: function() {
		function trim(){
			var car=this.getCaretPosition();
			var result='';
			for (j=0;j<this.value.length;j++) {
				var chr=this.value.charAt(j);
    var cod=chr.charCodeAt(0);
				if (48<=cod && cod<=57) {				
					result+=chr;
				} else if (97<=cod && cod<=122) {					
					result+=chr;					
				} else if (65<=cod && cod<=90) {
					result+=String.fromCharCode(cod+32);
				} else {
					if (j<car) car-=1;
				}
			}
			this.value=result;
			this.setCaretPosition(car);
		}
		return this.addEvent('click', trim).addEvent('keyup', trim);
	}
});


