﻿/*----------------------------------------------------------------------------\
|                This script is created by William Tam Yuen Hei               |
|                          (http://tambot.no-ip.com)                          |
|-----------------------------------------------------------------------------|
|                Copyright (c) by 1997 - 2011 TamBot.com Workshop             |
|-----------------------------------------------------------------------------|
| Created 2011-09-01 |                  All rights reserved                   |
\----------------------------------------------------------------------------*/

var fromicon, setLayerOpacity, setWidth, setHeight, setLeft, setTop;
var stopclosing = false;
var loadingsrc = "<table style='width:100%;height:100%;'><tr><td style='text-align: center;'><img src='images/loading.gif'></td></tr></table>";
var preid = -1; nextid = -1; toid = -1;

function loadIicon(dishid, icon, q) {
  if (q==null) q = "";
  var box = document.getElementById('lightbox');
  if (!isPhone&&!isIE) { 
    if (box.style.display!="block") setLightBox(icon);
    jah("dish.php?id="+dishid+q, "lightbox_content", "stopclosing=false;");
  } else {
    if (box.style.display!="block") {
      loadLightBox("dish.php?id="+dishid+q);
      fromicon = icon;
      if (document.getElementById("iconContainer")) getPreNextId();
    }
  }
}

function loadIicon2(moveNext, dishid) {
  if (dishid!=-1) {
    stopclosing = true;
    fromicon.style.visibility ='visible';
    fromicon = document.getElementById("icon"+dishid);
    getPreNextId();
    toid = dishid;
    if (toid.lastIndexOf("-")>-1) toid = toid.slice(toid.lastIndexOf("-")+1, toid.length);
    //if (isIE||isPhone) {
      jah("dish.php?id="+toid, "lightbox_content", "stopclosing=false;showOnlyOneCate(document.getElementById('cateid').value);");
    //} else lightBoxMove(moveNext);
  } else {
    var m = (moveNext) ? "後" : "前";
    m = "這已經是最"+m+"一項了。";
    if (isPhone) {
      alert(m);
    } else {
      tdba = document.getElementById("thisdishbkalerm");
      tdba.className = "alert";
      tdba.innerHTML = m;
      tdba.style.display="block";
    }
  }
}

function loadLightBox(lnk, run, run2) {
  var box = document.getElementById('lightbox');
  setLightBoxFigure();
  if (isIE6) setSelectState('hidden');

  setOpacity(box, 0);
  box.style.height = setHeight + "px";
  box.style.width = setWidth + "px";
  box.style.top = "50%";
  box.style.left = "50%";
  box.style.marginTop = setHeight/2*-1 + "px";
  box.style.marginLeft = setWidth/2*-1 + "px";
  box.style.display='block';
  
  document.getElementById('bfade').style.display='block';
  jah(lnk, "lightbox_content", run);
  if (!isIE&&!isPhone) {
  for (var i=0;i<=setLayerOpacity;i++) setTimeout('setOpacity(document.getElementById(\'bfade\'), '+i+');',50*i);
  for (var i=0;i<=10;i++) {
    setTimeout('setOpacity(document.getElementById(\'lightbox\'), '+i+')',50*i);
    if (run2!=null&&run2.length!=0) {
      if (i>=10) setTimeout(run2, 50*i);
    }
  }
  } else {
    setOpacity(box, 10);
    setOpacity(document.getElementById('bfade'),setLayerOpacity);
  }
}

function setLightBoxFigure() {
  setLayerOpacity = 8;
  //setWidth = pageWidth() * 0.6;
  //setHeight = pageHeight() * 0.7;
  setWidth = 600;
  setHeight = 450;
  setLeft = pageWidth()/2 - setWidth/2;
  setTop = pageHeight()/2 - setHeight/2 - 25;
  if (isIE||isPhone) {
    setTop += posTop();
    var flayer = document.getElementById('bfade');
    flayer.style.height = ((document.body.scrollHeight>pageHeight()) ? document.body.scrollHeight : pageHeight()) + "px";
    //if (isIE6) flayer.style.width = document.body.scrollWidth + "px";
  }
}

function setLightBox(icon) {
    icon.style.visibility = "hidden";
    var box = document.getElementById('lightbox');
    fromicon = icon;
    topval = findPosY(icon);
    if (isIE6) setSelectState('hidden');
    //if (!isIE6) topval -= posTop();
    topval -= (isIE) ? document.getElementById("wrapper").scrollTop : posTop();
    box.style.width = getWidthHeight("w", icon) + "px";
    box.style.height = getWidthHeight("h", icon) + "px";
    box.style.top = topval + "px";
    box.style.left = findPosX(icon) + "px";
    box.style.marginTop = 0 + "px";
    box.style.marginLeft = 0 + "px";
    box.style.display = "block";
    setOpacity(box, 10);
    stopclosing = true;
    expandLightbox(topval, findPosX(icon));
}

function expandLightbox(icontop, iconleft) {
    var box = document.getElementById('lightbox');
    var sto = false;
    setLightBoxFigure();
    
    if (icontop<setTop) {
      if (parseInt(box.style.top)<setTop) {box.style.top = (parseInt(box.style.top) + 6) + "px"; sto = true;}
    } else if (icontop>setTop) {
      if (parseInt(box.style.top)>setTop) {box.style.top = (parseInt(box.style.top) - 12) + "px"; sto = true;}
    }
    if (iconleft<setLeft) {
      if (parseInt(box.style.left)<setLeft) {box.style.left = (parseInt(box.style.left) + 6) + "px"; sto = true;}
    } else if (iconleft>setLeft) {
      if (parseInt(box.style.left)>setLeft) {box.style.left = (parseInt(box.style.left) - 12) + "px"; sto = true;}
    }
    if (parseInt(box.style.width)<setWidth) {box.style.width = (parseInt(box.style.width) + 15) + "px"; sto = true;}
    if (parseInt(box.style.height)<setHeight) {box.style.height = (parseInt(box.style.height) + 10) + "px"; sto = true;}
    
    if (sto) {
      setTimeout("expandLightbox("+icontop+", "+iconleft+")", 1);
    } else {
      document.getElementById('bfade').style.display='block';
      if (!isIE) {
        for (var i=0;i<=setLayerOpacity;i++) setTimeout('setOpacity(document.getElementById(\'bfade\'), '+i+')',50*i);
      } else {
        setOpacity(document.getElementById('bfade'),setLayerOpacity);
      }
      if (document.getElementById("iconContainer")) getPreNextId();
    }
}

function setOpacity(obj, value) {
  obj.style.opacity = value/10;
  obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function getPreNextId() {
  var container = document.getElementById("iconContainer");
  var cicons = container.getElementsByTagName("div");
  var iconid = new Array();
  
  for(i=0;i<cicons.length;i++){
    if (cicons[i].className&&cicons[i].className=="icon") iconid.push(cicons[i].id);
  }
  for(j=0;j<iconid.length;j++) {
    if (iconid[j]==fromicon.id) {
      if (j-1>=0) {
        preid = iconid[j-1];
        preid = preid.replace("icon", "");
      } else {
        preid = -1;
      }
      if (j+1<iconid.length) {
        nextid = iconid[j+1];
        nextid = nextid.replace("icon", "");
      } else {
        nextid = -1;
      }
      break;
    }
  }
}

function lightBoxMove(moveLeft) {
    var box = document.getElementById('lightbox');
    var sto = false;
    stopclosing = true;
    document.getElementById('bfade').style.cursor = 'wait';
    setLightBoxFigure();
    if (moveLeft) {
      setLeft2 = 0 - setWidth;
      if (parseInt(box.style.left)>setLeft2) {box.style.left = (parseInt(box.style.left) - 20) + "px"; sto = true;}
    } else {
      setLeft2 = pageWidth();
      if (parseInt(box.style.left)<setLeft2) {box.style.left = (parseInt(box.style.left) + 20) + "px"; sto = true;}
    }
    if (sto) {
       setTimeout("lightBoxMove("+moveLeft+")", 0);
    } else {
      box.style.left = (moveLeft ? pageWidth() : (0 - setWidth)) + "px";
      jah("dish.php?id="+toid, "lightbox_content", "stopclosing = false;showOnlyOneCate(document.getElementById('cateid').value);");
      lightBoxMove2(moveLeft)
    }
}

function lightBoxMove2(moveLeft) {
    var box = document.getElementById('lightbox');
    var sto = false;
    setLightBoxFigure();
    if (moveLeft) {
      if (parseInt(box.style.left)>setLeft) {box.style.left = (parseInt(box.style.left) - 20) + "px"; sto = true;}
    } else {
      if (parseInt(box.style.left)<setLeft) {box.style.left = (parseInt(box.style.left) + 20) + "px"; sto = true;}
    }
    if (sto) setTimeout("lightBoxMove2("+moveLeft+")", 0)
    else {
      document.getElementById('bfade').style.cursor = 'pointer';
    }
}

function closelightbox() {
  if (!stopclosing) {
    stopclosing = true;
    if (fromicon) fromicon.style.visibility ='visible';
    if (!isIE&&!isPhone) {
      for (var i=0;i<=10;i++) {
        setTimeout('setOpacity(document.getElementById(\'lightbox\'), '+(10-i)+');',50*i);
        if (i>=10) setTimeout('closelightbox2()', 50*i);
      }
      for (var j=0;j<=setLayerOpacity;j++) setTimeout('setOpacity(document.getElementById(\'bfade\'), '+(setLayerOpacity-j)+')',50*j);
    } else {
      if (isIE6) setSelectState('');
      setOpacity(document.getElementById('bfade'),0);
      closelightbox2();
    }
  }
}

function closelightbox2() {
  stopclosing = false;
  document.getElementById('lightbox_content').innerHTML = '';
  document.getElementById('lightbox').style.display = 'none';
  document.getElementById('bfade').style.display = 'none';
  preid = -1; nextid = -1;
}

function adjustlightbox() {
  var box = document.getElementById('lightbox');
  if (box.style.display=="block"&&stopclosing==false) {
    setLightBoxFigure();
    box.style.width = setWidth + "px";
    box.style.height = setHeight + "px";
    box.style.top = "50%";
    box.style.left = "50%";
    box.style.marginTop = setHeight/2*-1 + "px";
    box.style.marginLeft = setWidth/2*-1 + "px";
  }
}

function fadeChangeDiv(divout, divin, run) {
  if (run==null) run = "";
  if (!isIE&&!isPhone) {
  for (var i=0;i<=10;i++) {
    setTimeout('setOpacity(document.getElementById(\''+divout+'\'), '+(10-i)+');',50*i);
    if (i>=10) setTimeout('document.getElementById(\''+divout+'\').style.display=\'none\';fadeChangeDiv2(\''+divin+'\', \''+run+'\');', 50*i);
  }
  } else {
    document.getElementById(divout).style.display='none';
    document.getElementById(divin).style.display='block';
  }
}

function fadeChangeDiv2(divin, run) {
  setOpacity(document.getElementById(divin), 0)
  document.getElementById(divin).style.display='block';
  for (var i=0;i<=10;i++) {
    setTimeout('setOpacity(document.getElementById(\''+divin+'\'), '+i+');',50*i);
    if (i>=10&&run.length>0) setTimeout(run, 50*i);
  }
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

 function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function getWidthHeight(wh, divobj) {
 var viewportwidth;
 var viewportheight;
 
 if (typeof divobj.clientHeight != 'undefined') {
      viewportwidth = divobj.clientWidth;
      viewportheight = divobj.clientHeight;
 } else if (typeof divobj.offsetHeight != 'undefined') {
      viewportwidth = divobj.offsetWidth;
      viewportheight = divobj.offsetHeight;
 }
 if (wh=="w") {
   return viewportwidth;
 } else if (wh=="h") { 
   return viewportheight;
 }
}

function setSelectState(state) {
  var objl=document.getElementsByTagName('select');
  for(var i=0;i<objl.length;i++) {
    objl[i].style.visibility=state;
  }
}

window.onresize = adjustlightbox;
if (isIE) window.onscroll = adjustlightbox;
