var DDSPEED = 7.5;
var DDTIMER = 15;
var dflturl = "img/btn/btn_";
var dfltcarturl = "img/btn/tab_";
var maxwidth = 960;
var menuwidth = 210;
var sidemenustartpos = 780;
var cartwidth = 400;
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";

//	alert (inner.style.left + ";" + inner.style.width);

  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 + ')';
	}
}

// カート画面展開用 //

/*@cc_on 
	@if (@_jscript_version < 9)
		_d=document;eval('var document=_d')
	@end
@*/
var bCartShow = false;
var g_clientWidth;
var g_dist;
var g_doc = document;

function ddCartWindow(buttonid, screenid){
  var button = g_doc.images[buttonid];
  var h = g_doc.getElementById(buttonid);
  var c = g_doc.getElementById(screenid);
  var inner = g_doc.getElementById(screenid + '_content');
  clearInterval(c.timer);
  
  var bgstyle_over = dfltcarturl + buttonid + "_over.gif";
  var bgstyle_normal = dfltcarturl + buttonid + ".gif";
  
  g_clientWidth = g_doc.body.clientWidth;

/*
*/
//  alert (inner);
//  alert (c.style.left + ";" + c.style.width);

  if(bCartShow == false){
	button.src = bgstyle_over;
    bCartShow = true;
//	alert("show");
	// メニュー表示位置調整
	clearTimeout(h.timer);
    //if(c.maxw && c.maxw <= c.offsetWidth){return}
//    /*else*/ if(!c.maxw){
	  c.style.left = g_clientWidth + 'px';
//      c.style.right = '0px';
	  c.style.display = 'block';
      c.maxw = cartwidth;
      c.style.width = '0px';
	  g_dist = 50;
	/*@cc_on g_dist = 50; @*/  
/*	
	inner.style.display = 'block';
	inner.style.left = g_clientWidth + 'px';
	inner.style.width = '0px';
	inner.style.height = '290px';
	inner.style.top = '155px';
	inner.style.zIndex = 499;
	inner.style.position = 'fixed';
*/	  
//  }

	c.timer = setInterval(function(){ddSlideCartWindow(c, inner, 1)},DDTIMER2);
	
  }else{
	button.src = bgstyle_normal;
	h.timer = setTimeout(function(){ddCartWindowCollapse(c, inner)},50);
}
}

function getCartStatus()
{
	return bCartShow;
}

function updateCartTabImage(buttonid, mode)
{
	var button = g_doc.images[buttonid];	
	var bgstyle_over = dfltcarturl + buttonid + "_over.gif";
	var bgstyle_normal = dfltcarturl + buttonid + ".gif";	
	
	if (mode == true)
	{
		button.src = bgstyle_over;
	}
	else if (mode == false)
	{
		if (getCartStatus() == false)
		{
			button.src = bgstyle_normal;
		}
	}
}

// collapse the menu //
function ddCartWindowCollapse(c, inner){
  c.timer = setInterval(function(){ddSlideCartWindow(c,inner,-1)},DDTIMER2);
  bCartShow = false;
// alert("hide");
}

/*
// cancel the collapse if a user rolls over the dropdown //
function cancelHideCartWindow(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 = dfltcarturl + buttonid + ".gif";//"_over.gif";
  button.src = bgstyle_over;

  if(c.offsetWidth < c.maxw){
    c.timer = setInterval(function(){ddSlideCartWindow(c,inner,1)},DDTIMER2);
  }
}
*/
// incrementally expand/contract the dropdown and change the opacity //
function ddSlideCartWindow(c,inner,d){
  var currw = parseInt(c.style.width.replace('px',''));
  var dist = g_dist;

  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.right = '0px';
	  c.style.left = g_clientWidth - 5 - c.maxw + 'px';	
  }
  else
  {
	  if (currw + (dist * d) <= 0)
	  {
		  c.style.left = g_clientWidth - 5 + 'px';	
 		  c.style.right = '0px';
//		  c.style.width = '0px';
		  c.style.display = 'none';
	  }
	  else
	  {
	  	  c.style.width = currw + (dist * d) + 'px';
//		  c.style.right = '0px';
		  c.style.left = g_clientWidth - 5 - (parseInt(c.style.width.replace('px',''))) + 'px';
	  }
  }

  if (currw / c.maxw > 0.8)
  {
	  c.style.opacity = 0.80; // 0.9をMAXに変更
	  c.style.filter = 'alpha(opacity=80)';
  }
	else
  {
	  c.style.opacity = currw / c.maxw;
	  c.style.filter = 'alpha(opacity=60)';
		
  }

	if((currw < 2 && d != 1) || (currw > (c.maxw - 2) && d == 1)){
    clearInterval(c.timer);
	
	if (d == 1)
	{
//		alert (c.style.left + ";" + c.style.top);
		inner.style.left = parseInt(c.style.left.replace('px','')) + 5 + 'px';
//		inner.style.top = parseInt(c.style.top.replace('px','')) + 5 + 'px';
//	inner.style.position = 'fixed';
	inner.style.display = 'block';
//	inner.style.width = '340px';
//	inner.style.height = '290px';
//	inner.style.top = '155px';
//	inner.style.zIndex = 499;
		inner.style.opacity = 0.80;
		inner.style.filter = 'alpha(opacity=80)';

//		alert (inner.style.left + ";" + inner.style.width);

//		inner.style.opacity = 0;
//		inner.style.filter = 'alpha(opacity=0)';
//		inner.timer = setInterval(function(){ddShowInnerCartWindow(inner)},DDTIMER);
	}
  }
}

function ddShowInnerCartWindow(c)
{
	var curr_opacity = parseFloat(c.style.opacity);
	var next_opacity = curr_opacity + APPEARSPEED;
	
	if (curr_opacity + APPEARSPEED >= 0.85)
	{
		c.style.opacity = 0.9;
		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 + ')';
	}
}

