var hp_ddspeed = 10;
var hp_ddtimer = 15;
var hp_hp_openspeed = 20;
var hp_banner_timerid;
var hp_curtain_timerid;
var hp_change_timerid;
var hp_open=-1;
function hp_load(){
	hp_change();
}

function hp_change(){
	if(typeof hp_curtain_timerid == "undefined") {
		hp_refresh_banner();
		hp_curtain_timerid=setInterval("hp_change()",hp_ddtimer);
	}

	if(hp_curtain_timerid=="cleared") {
		clearInterval(hp_change_timerid)
		document.getElementById("banner_1").style.zIndex=0;
		document.getElementById("banner_2").style.zIndex=0;
		document.getElementById("banner_3").style.zIndex=0;
		hp_curtain_timerid=setInterval("hp_change()",hp_ddtimer);
	}

	if(hp_open == 1 && document.getElementById("curtainleft").offsetLeft >= 0 && document.getElementById("curtainright").offsetLeft <= 300){
		hp_refresh_banner();
	}

	if(document.getElementById("curtainleft").offsetLeft >= 0 && hp_open != -1) {
		hp_open=-1;
	}

	var dist;

	if(hp_open==-1) {
		dist=Math.round((300+document.getElementById("curtainleft").offsetLeft)/hp_hp_openspeed);
	} else {
		dist=Math.round((0-document.getElementById("curtainleft").offsetLeft)/hp_hp_openspeed);
	}
	if(dist<=1 && hp_open!=-1) {
		dist=1;
	}

	if((document.getElementById("curtainleft").offsetLeft > -290 && hp_open == -1) || (document.getElementById("curtainleft").offsetLeft < 0 && hp_open != -1)) {
		document.getElementById("curtainleft").style.left =document.getElementById("curtainleft").offsetLeft + (dist * hp_open) + 'px';
		document.getElementById("curtainright").style.left =document.getElementById("curtainright").offsetLeft + ((dist * hp_open)*-1) + 'px';
	}

	if((document.getElementById("curtainleft").offsetLeft <= -290 && hp_open == -1)) {
		clearInterval(hp_curtain_timerid);
		hp_curtain_timerid="cleared";
	}

	if(document.getElementById("curtainleft").offsetLeft <= -290 && hp_open == -1) {
		hp_open=1;
		hp_change_timerid=setInterval("hp_change()",option_Banner_Refresh_Interval*1000);
		document.getElementById("banner_1").style.zIndex=2;
		document.getElementById("banner_2").style.zIndex=2;
		document.getElementById("banner_3").style.zIndex=2;
	}
}

function hp_refresh_banner(){
	var url=window.location.protocol+"//"+window.location.host+window.location.pathname+"?"+url_ajax_refresh_banner;
	var div=document.getElementById("banner");
	var temp=ajax_sendRequest(url,false);
	div.innerHTML="<img src='images/bannertop.gif' alt='Curtain top border'/><div id='curtcont'><img id='curtainleft' src='images/curtain.gif' alt='Curtain Left'/><img id='curtainright' src='images/curtain.gif' alt='Curtain Right'/></div>"+temp;
	for(var i=1;i<4;i++){
		var insideDiv=document.getElementById("banner_"+i);
		insideDiv.onmouseover=function(){
			clearInterval(hp_banner_timerid);
			clearInterval(hp_change_timerid);
			clearInterval(this.timer);
			hp_Menu(this,1);
		}
		insideDiv.onmouseout=function(){
			hp_Menu(this,-1);
			hp_change_timerid=setInterval("hp_change()",option_Banner_Refresh_Interval*1000);

		}
		insideDiv.style.height="60px";
	}
}

// main function to handle the mouse events //
function hp_Menu(c,d){
  var h = document.getElementById('banner');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(c.remtimer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
	  c.style.visibility='hidden';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '60px';
	  c.style.visibility='visible';
    }
    c.timer = setInterval(function(){hp_Slide(c,1)},hp_ddtimer);
  }else{
    c.remtimer = setTimeout(function(){hp_Collapse(c)},50);
  }
}

// collapse the menu //
function hp_Collapse(c){
  c.timer = setInterval(function(){hp_Slide(c,-1)},hp_ddtimer);
}

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