var DDSPEED = 7.5;
var DDTIMER = 15;
var dflturl = "img/btn/btn_";
var maxwidth = 960;
var menuwidth = 210;
var sidemenustartpos = 780;
var SLIDESPEED = 24;
var APPEARSPEED = 0.05;
var DDTIMER2 = 5;

// 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);
  
  var bgstyle_over = "url(" + dflturl + id + "_over.jpg)";
  var bgstyle_normal = "url(" + dflturl + id + ".jpg)";
  
  if(d == 1){
	h.style.background = bgstyle_over;	  

	// メニュー表示位置調整
	var pos = YAHOO.util.Dom.getX(h);
	var pos_base = YAHOO.util.Dom.getX(document.getElementById('container'));

	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';

	if (menuwidth + (pos - pos_base - 1) > maxwidth)
	{
		c.style.left = (maxwidth - menuwidth) + "px";
	}
  }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
	h.style.background = bgstyle_normal;	  
	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');
  clearTimeout(h.timer);
  clearInterval(c.timer);

  var bgstyle_over = "url(" + dflturl + id + "_over.jpg)";
  h.style.background = bgstyle_over;
  
  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';

  if (currh / c.maxh > 0.92)
  {
	  c.style.opacity = 0.92; // 0.9をMAXに変更
	  c.style.filter = 'alpha(opacity=' + (92) + ')';
  }
  else
  {
	  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 ddSideMenu(buttonid, screenid, d){
  var button = document.images[buttonid];
  var h = document.getElementById(buttonid);
  var c = document.getElementById(screenid);
  var inner = document.getElementById(screenid + '_content');
  clearInterval(c.timer);
  
  var bgstyle_over = dflturl + buttonid + "_over.jpg";
  var bgstyle_normal = dflturl + buttonid + ".jpg";
  
  if(d == 1){
	button.src = bgstyle_over;	  

	// メニュー表示位置調整
	clearTimeout(h.timer);
    if(c.maxw && c.maxw <= c.offsetWidth){return}
    else if(!c.maxw){
	  c.style.left = sidemenustartpos + 'px';
	  c.style.display = 'block';
	  c.style.width = 'auto';
      c.maxw = sidemenustartpos;
      c.style.width = '0px';
  }
    c.timer = setInterval(function(){ddSlideSideMenu(c, inner, 1)},DDTIMER2);
  }else{
	button.src = bgstyle_normal;	  
	h.timer = setTimeout(function(){ddSideMenuCollapse(c, inner)},50);
  }
}

// collapse the menu //
function ddSideMenuCollapse(c, inner){
  c.timer = setInterval(function(){ddSlideSideMenu(c,inner,-1)},DDTIMER2);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHideSideMenu(buttonid, screenid){
  var button = document.images[buttonid];
  var h = document.getElementById(buttonid);
  var c = document.getElementById(screenid);
  var inner = document.getElementById(screenid + '_content');  
  clearTimeout(h.timer);
  clearInterval(c.timer);

  var bgstyle_over = dflturl + buttonid + "_over.jpg";
  button.src = bgstyle_over;

  if(c.offsetWidth < c.maxw){
    c.timer = setInterval(function(){ddSlideSideMenu(c,inner,1)},DDTIMER2);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlideSideMenu(c,inner,d){
  var currw = parseInt(c.style.width.replace('px',''));
  var dist = Math.round(c.maxw / SLIDESPEED);

  if ((d != 1) && (inner.style.display != 'none'))
  {
	  inner.style.opacity = 0;
	  inner.style.filter = 'alpha(opacity=0)';
	  inner.style.display = 'none';
  }

  if (currw + (dist * d) > c.maxw)
  {
	  c.style.width = c.maxw + 'px';
	  c.style.left = '0px';
  }
  else
  {
	  if (currw + (dist * d) <= 0)
	  {
  		  c.style.left = sidemenustartpos + 'px';
		  c.style.width = '0px';
	  }
	  else
	  {
	  	  c.style.width = currw + (dist * d) + 'px';
		  c.style.left = sidemenustartpos - (parseInt(c.style.width.replace('px',''))) + 'px';
	  }
  }

  if (currw / c.maxw > 0.85)
  {
	  c.style.opacity = 0.85; // 0.9をMAXに変更
	  c.style.filter = 'alpha(opacity=' + (85) + ')';
  }
  else
  {
	  c.style.opacity = currw / c.maxw;
	  c.style.filter = 'alpha(opacity=' + (currw * 100 / c.maxw) + ')';
  }

  if((currw < 2 && d != 1) || (currw > (c.maxw - 2) && d == 1)){
    clearInterval(c.timer);
	
	if (d == 1)
	{
		inner.style.display = 'block';
		inner.style.opacity = 0;
		inner.style.filter = 'alpha(opacity=0)';
		inner.timer = setInterval(function(){ddShowInnerWindow(inner)},DDTIMER);
	}
  }
}

function ddShowInnerWindow(c)
{
	var curr_opacity = parseFloat(c.style.opacity);
	var next_opacity = curr_opacity + APPEARSPEED;
	
	if (curr_opacity + APPEARSPEED >= 1)
	{
		c.style.opacity = 1;
		c.style.filter = 'alpha(opacity=100)';
		clearInterval(c.timer);
	}
	else
	{
		c.style.opacity = curr_opacity + APPEARSPEED;
		c.style.filter = 'alpha(opacity=' + (curr_opacity + APPEARSPEED) * 100 + ')';
	}
}
