var DDSPEED = 10;
var DDTIMER = 15;

function init(){
	document.getElementById('one-ddheader').style.backgroundImage='url(button_01sa.png)';
	document.getElementById('nomenu-ddheader').style.backgroundImage='url(button_02news.png)';
	document.getElementById('two-ddheader').style.backgroundImage='url(button_03sortino.png)';
	document.getElementById('three-ddheader').style.backgroundImage='url(button_04territorio.png)';
}

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
	//h.style.backgroundImage='url(button_01saX.png)';
	c.style.width='auto';
	if(h.style.backgroundImage.indexOf('X.png')==-1)
	    h.style.backgroundImage=h.style.backgroundImage.replace('.png','X.png');
	//str.replace('.png','X.png');
	//alert(h.style.backgroundImage);
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return;}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    //h.style.backgroundImage='url(button_01sa.png)';
    if(h.style.backgroundImage.indexOf('X.png')!=-1)
	h.style.backgroundImage=h.style.backgroundImage.replace('X.png','.png');
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  //h.style.backgroundImage='../button_01saX.png';
  //h.style.backgroundImage='url(button_01saX.png)';
  if(h.style.backgroundImage.indexOf('X.png')==-1)
    h.style.backgroundImage=h.style.backgroundImage.replace('.png','X.png');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

 function clickIE() {if (document.all) {(message);return false;}}
 function clickNS(e) {if (document.layers||(document.getElementById && !document.all)) {if (e.which==2||e.which==3) {(message);return false;}}}
 
  if (document.layers) {
  	document.captureEvents(Event.MOUSEDOWN);
  	document.onmousedown=clickNS;
  	}
  	else{
  		document.onmouseup=clickNS;
  		document.oncontextmenu=clickIE;
  	}
  	document.oncontextmenu=new Function('return false');
  	
  	function externalLinks() {

if (!document.getElementsByTagName) return;

var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
var relvalue = anchor.getAttribute("rel");

if (anchor.getAttribute("href")) {
var external = /external/;
var relvalue = anchor.getAttribute("rel");
if (external.test(relvalue)) { anchor.target = "_blank"; }
}
}
}
window.onload = externalLinks;
