//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}


// QUESTA FUNZIONE SERVE PER NON FARE RICARICARE LE IMMAGINI IN CACHE A IE6
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}
//

var win=null;

function OpenNewWindow(mypage,myname,w,h,scroll,pos)
{
    if(pos=="random")
    {
        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
    }
    if(pos=="center")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    else if((pos!="center" && pos!="random") || pos==null)
    {
        LeftPosition=0;
        TopPosition=20
    }

    settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

    win=window.open(mypage,myname,settings);

    if(win.focus)
    {
        win.focus();
    }
}

function verifyInputDomain(){
	if($('domain_name').value!='' && $('extension').value!=''){
		$('domainForm').submit();
	}
}

Element.collectTextNodes = function(element) {  
  return $A($(element).childNodes).collect( function(node) {
    return (node.nodeType==3 ? node.nodeValue : 
      (node.hasChildNodes() ? Element.collectTextNodes(node) : ''));
  }).flatten().join('');
};

Element.collectTextNodesIgnoreClass = function(element, className) {  
  return $A($(element).childNodes).collect( function(node) {
    return (node.nodeType==3 ? node.nodeValue : 
      ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? 
        Element.collectTextNodesIgnoreClass(node, className) : ''));
  }).flatten().join('');
};

var FakeSelect = { }
FakeSelect = Class.create({
  initialize: function(element,input, menu) {
    this.element     = element; 
    this.input       = $(input); 
    this.menu        = $(menu);  
    this.hasFocus    = false; 
    this.active      = false; 
    this.index       = 0;
    this.elementV	 = element.value;
	
    this.options = { };
    this.options.frequency    = this.options.frequency || 0.2;
    this.options.onShow       = 
      function(element, menu){ 
        if(!menu.style.position || menu.style.position=='absolute') {
          menu.style.position = 'absolute';
          Position.clone(element, menu, {
            setWidth: false,
            setHeight: false, 
            offsetTop: element.offsetHeight
          });
        }
        //Effect.Appear(update,{duration:0.15});
        Element.setOpacity(menu, 1)
        Element.show(menu)
        
      };
    this.options.onHide = this.options.onHide || 
      function(element, menu){ 
      		Element.setOpacity(menu, 0) 
        	Element.hide(menu)
      };

    this.observer = null;

    Element.hide(this.menu);
    
	document.observe('click', this.onDocumentClick.bindAsEventListener(this));
		
    Event.observe(this.element, 'click', this.onElementClick.bindAsEventListener(this));
  },

  show: function() {
    this.active = true;
    Element.addClassName(this.element, 'Closer');
	if(Element.getStyle(this.menu, 'display')=='none') this.options.onShow(this.element, this.menu);
    if(!this.iefix && 
      (Prototype.Browser.IE) &&
      (Element.getStyle(this.menu, 'position')=='absolute')) {
      new Insertion.After(this.menu, 
       '<iframe id="' + this.menu.id + '_iefix" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
      this.iefix = $(this.menu.id+'_iefix');
    }
    if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
    
  },
  
  fixIEOverlapping: function() {
	alert('bidone')
    Position.clone(this.menu, this.iefix, {setTop:(!this.menu.style.height)});
    this.iefix.style.zIndex = 100;
    this.menu.style.zIndex = 200;
    Element.show(this.iefix);
  },

  hide: function() {

    Element.removeClassName(this.element, 'Closer');
    
    this.active = false;
    if(Element.getStyle(this.menu, 'display')!='none') this.options.onHide(this.element, this.menu);
    if(this.iefix) Element.hide(this.iefix);
  },

  onElementClick: function(event) {
    
    this.hasFocus = true;
    if(this.observer) clearTimeout(this.observer);
      this.observer = 
        setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
    this.makeThemClickable()
    event.stop()
  },
  
  onDocumentClick: function(event) {
    // needed to make click events working
    setTimeout(this.hide.bind(this), 250);
    this.hasFocus = false;    
  },

  onObserverEvent: function() {
    
    if(!this.active) {
      this.show();
    } else {
      this.hide();
    }
  },
  
  makeThemClickable: function() {
      Element.cleanWhitespace(this.menu);
      Element.cleanWhitespace(this.menu.down());

      
      if(this.menu.firstChild && this.menu.down().childNodes) {
        entryCount = this.menu.down().childNodes.length;
        for (var i = 0; i < entryCount; i++) {
          var entry = this.menu.down().childNodes.item(i);
          //alert(entry.tagName)
          Event.observe(entry, "mouseover", this.onItemHover.bindAsEventListener(this));
          Event.observe(entry, "mouseout", this.onItemOut.bindAsEventListener(this));
		  Event.observe(entry, "click", this.onItemClick.bindAsEventListener(this));
        }
      }
  },
  
  onItemHover: function(event) {
    var element = Event.findElement(event, 'LI');
    element.className = 'selected';
    Event.stop(event);
  },
  onItemOut: function(event) {
    var element = Event.findElement(event, 'LI');
    element.className = '';
    Event.stop(event);
  },
  
  onItemClick: function(event) {
    var element = Event.findElement(event, 'LI');
	this.input.value = element.id;
    this.hide();
    Event.stop(event);
  }
});
