// Most code written by Jay of Jay's Site.com
// Modifid and striped bu GatorLiu http://gator.hoja.idv.tw

var ext='png';
var browser='';
function init() {
//getDockHTML();
	title=_("imageTitle");
	if ((navigator.userAgent).indexOf("Firefox") != -1) {
		browser='fx';
	}
	//alert(browser);
}
smiconsize = 64;
iconsize = 128;
    
function _(id) { return document.getElementById(id); }

    //if (dockstyle == "single") realDock=false;
    // else realDock=true;
    realDock=true;



	//x=false;
	af=Math.floor;
	b_2=new Array(13);
	//aa_2=new Array(11);
	ab=10;
	t=0;
	u=0;
	n=0;
	h=5;
	c=0;
	//w=x;
	var title;

	await=0;
	var moto;
	function dockmouseout(instant) {
	  //clearTimeout(moto);
	  //clearTimeout(st);
	  moto=setTimeout("dockmouseout2('"+Number(instant)+"')",10);

	    if (ext == 'png') {
	    	_('photo').style.opacity = 1.0;
	    	_('photo').style.MozOpacity = 1.0;
	    } else {
		 _('photo').style.filter = 'alpha(opacity=100)';
	    }
	  }
	function dockmouseout2(instant) {
	  //clearTimeout(st);
	  if (!realDock) return;

	  for (i=1;i<totalIcons;i++) {
	    targetSizes[i] = smiconsize;
	    }
	  return;
	  if (instant===1 && 0) gidle(1);
	   else if (!await) {
	    setTimeout("gidle(0)",20);
	    }
	  }

	var st;
	function dockmousemove(instant) {
	  //clearTimeout(st);

          var thedk = _("thedock");
          trashit=oft=0;
          el=thedk;
          oft += el.offsetTop;
          while (el.offsetParent) { el=el.offsetParent; oft += el.offsetTop; }
          if (tMouseY+iconsize < oft) trashit=1;
          if (tMouseY-20 > thedk.offsetHeight+oft) trashit=1;
          if (trashit) return dockmouseout(instant);

	  var ofl=0;
	  var el = _("image_"+dockSetup[1]);
	  ofl += el.offsetLeft;
	  while (el.offsetParent) { el=el.offsetParent; ofl += el.offsetLeft; }

	  var wd = _("thedock").offsetWidth;
	  wd -= wd%10;
	  var clk = tMouseX-ofl;
	  if (clk<(wd/2))clk -= 10;

	  clk -= clk%3;
	  iw = Math.floor(wd/totalIcons) + 2; // iw=average Icon Width based on dock width. +2 for good luck.
	  spots=[];
	  for (i=1;i<totalIcons;i++) {
	    spots[i] = (i-1)*iw + (iw/2);
	    var dif=Math.abs(spots[i] - clk);  // based on the image's distance from the mousemove event, the image's size is calculated.
	    if (blankat!=-1 && dif < (iw/2)) {
	      no=0;
	      if (spots[i] < clk && i == blankat+2) blankat = i;
	      else if (spots[i] > clk) blankat = i-1;
	      else if (Math.abs(blankat-i)>1) blankat=i;
	      }
	    if (blankat!=-1 && Math.abs(wd-clk)<(iw/2)) {
	      blankat=totalIcons-1;
	      }
	    ui=i;
	    if (blankat!=-1 && blankat < i-1) ui=i-1;
	    if (realDock) targetSizes[ui] = Math.min(Math.max(smiconsize, iconsize-(dif/2)),iconsize);
	    }

	  if (instant===1) gidle(1);
	   else if (!await) {
	    await=1;
	    setTimeout("gidle(0)",zoomInterval+50);
	    }
	    if (ext == 'png') {
	    	_('photo').style.opacity = 0.5;
	    	_('photo').style.MozOpacity = 0.5;
	    } else {
		 _('photo').style.filter = 'alpha(opacity=50)';
	    }
	  }

	function zoomin(ac){ // these functions are still used to change the 'imageTitle' label
	    ac = dockLocation[ac];
	    //clearTimeout(moto);
	    if (!realDock) targetSizes[ac] = iconsize;
	    dockmousemove();
		c=ac;
		oc=c;
		//o=new v_2();
	    if (!await) {
	      await=1;
	      setTimeout("gidle(0)",zoomInterval);
	      }
	}
	function zoomout(ac){
	    ac = dockLocation[ac];
	    if (!realDock) targetSizes[ac] = smiconsize;
		c=0;
		//w=x;
	    if (!await) {
	      await=1;
	      setTimeout("gidle(0)",zoomInterval);
	      }
	    }
/*
	function ae(){
	    try { _("searchquery").focus(); } catch(e){}
	    enableAC=true;
		for(var j=1;j<b_2.length;j++){
			b_2[j]=35
		}
		for(i=1; i<dockSetup.length; i++){
			aa_2[i]=new Image();
			aa_2[i].src="imgs/"+dockSetup[i]+"." + ext 
		    }
		for(i=1; i<dockSetup.length; i++){
		    if (lgs[i]=="null")continue;
			aa_2[i]=new Image();
			aa_2[i].src="imgs/"+dockSetup[i]+"." + ext
		    }
	    if (!await) {
	      await=1;
	      setTimeout("gidle(0)",zoomInterval);
	      }
	}
*/
function handleError() { return true; }

zoomSpeed=8;
zoomInterval = 10;
blankMoveSpeed=20;
if (!realDock) zoomInterval=10;
//dockSetup = ["null","web","images","groups","news","froogle","local","maps","answers","labs","gmail","more"];

dockSetup = ["null","hojablog","gallery","guestbook","gblog","gnote","resume_c","resume_e","contact"];
dockLocation=[];
labelText=[];
navURL = [];
labelText["hojablog"] = "好家載";
navURL["hojablog"] = "http://blog.hoja.idv.tw";

labelText["gallery"] = "相簿";
navURL["gallery"] = "http://www.hoja.idv.tw/gallery/main.php";

labelText["guestbook"] = "留言簿";
navURL["guestbook"] = "http://www.hoja.idv.tw/guestbook2/";

labelText["gblog"] = "Gspace 部落格";
navURL["gblog"] = "http://gator.hoja.idv.tw";

labelText["gnote"] = "Gspace Note";
navURL["gnote"] = "http://wiki.hoja.idv.tw";

labelText["resume_c"] = "履歷";
navURL["resume_c"] = "http://gatorliu.googlepages.com/resume_zh_TW.html";

labelText["resume_e"] = "Resume";
navURL["resume_e"] = "http://gatorliu.googlepages.com/resume_en_US.html";

labelText["contact"] = "與我聯絡";
navURL["contact"] = "http://gatorliu.googlepages.com/contact_en_US.html";



//if (dockSetup.length < 3) dockSetup = defaultDock;
var totalIcons = dockSetup.length;

imageSizes=[];
targetSizes=[];
tcolor=255;
oc=1;
curTitle="Web Search";
var mcounter=0;
for (i=0;i<totalIcons;i++) { imageSizes.push(smiconsize); targetSizes.push(smiconsize); }
function gidle(instant) { // completely rewritten by Jay of JaysSite.com to accomodate realistic dock style
  var rep=0; // rep is whether or not gidle needs to finish any animation. we'll assume no for now.
  for (i=1; i<totalIcons; i++) { // this loop goes through each icon and animates it a little if needed
    var dif=Math.abs(imageSizes[i] - targetSizes[i]);
    zoomChange=Math.min(zoomSpeed,dif);
    if (instant) zoomChange=dif;
    // resize image:
    if (imageSizes[i] > targetSizes[i]) {
      imageSizes[i]-=zoomChange; rep=1;
      }
    else if (imageSizes[i] < targetSizes[i]) {
      imageSizes[i]+=zoomChange; rep=1;
      }
    else {
      // image didn't need resizing, so skip the rest of this loop
      continue;
      }
    var img=_("image_"+dockSetup[i]);
    if (!img) alert(" no image_"+dockSetup[i]+" (i is "+i+", dockSetup[i] is "+dockSetup[i]+")");
    var sz = imageSizes[i];
    if (sz>=50) img.src = "imgs/"+dockSetup[i]+"." + ext;
     else img.src = "imgs/"+dockSetup[i]+"-sm." + ext;
    img.height=sz;
    img.width=sz;
    }
  if (!title) {
    await=0; // await is a variable that just prevents this from function from executing on every single mousemove
    return; // this prevents an error that may happen before the page is fully loaded
    }

   if (oc>=totalIcons) oc=totalIcons-2;
   if (c>=totalIcons) c=totalIcons-2;
   var ttop=(iconsize-imageSizes[oc]) + _("thedocktable").offsetTop - 14;
   var mb = _("image_"+dockSetup[oc]).style.marginBottom;
   if (mb) {
     mb=mb.replace( /[^0-9]/g,"");
     if (mb.length > 0 && mb < 20) ttop -= mb;
     }

   ttop += 40 ;
   title.style.top= ttop + "px";

   var tleft= _("image_"+dockSetup[oc]).offsetLeft - iconsize + (_("image_"+dockSetup[oc]).offsetWidth/2);
   tleft += 40;
   title.style.left= tleft + "px";


  if (c>0) {
	  if (curTitle!=_("image_"+dockSetup[c]).alt) {

	    title.innerHTML=curTitle=_("image_"+dockSetup[c]).alt;
	    }
      }

  var notupd=0;
  if (blankat!=-1 || (c==0 && tcolor<255)) {
    tcolor+=30; // lighter (disappear)
    if (instant) tcolor=255;
    }
  else if (c>0 && tcolor>0) {
    tcolor-=30; // darker (appear)
    if (instant) tcolor=0;
    }
  else notupd=1;

  if (!notupd) {
    tcolor = Math.max(Math.min(tcolor,255),0);
    if (tcolor > 0 && tcolor < 255) rep=1;
    title.style.color = "rgb("+tcolor+","+tcolor+","+tcolor+")";
    if (tcolor==255) {
      if (title.style.visibility!="hidden" ) title.style.visibility="hidden";
      }
    else if (title.style.visibility!="visible") {
      title.style.visibility="visible";
      }
    }

  if (blankat != -1 ) {
     for (i=0;i<dockSetup.length;i++) {

       var nw=0;
       if (i==blankat) nw=80;
       var cb = _("blank_"+dockSetup[i]);
       if (!cb) alert("no blank_"+dockSetup[i]+" (i is "+i+")");
       if (cb.style.display=="none" && nw>0) {
          cb.width=0;
          cb.style.display="inline";
          }
        else if (cb.style.display=="inline" && cb.width==0) cb.style.display="none";
       if (cb.width>nw) nw = cb.width - blankMoveSpeed;
       else if (cb.width < nw) nw = cb.width + blankMoveSpeed;
       else continue;
       nw = Math.min(iconsize,Math.max(0,nw))
       rep=1;
       cb.width=nw;
       }
  	 }

  if (rep||instant) {
    await=1;
    setTimeout("gidle(0)",zoomInterval); // if animations aren't finished yet, plan to come back.
    }
    else await=0;
  }

var dcount=0;
blankat=-1;
function startDrag(el) {
  }
var tMouseX, tMouseY;
function trackMouse(e) {
  if (!e) e=window.event;
  tMouseX=e.clientX;
  tMouseY=e.clientY+ document.body.scrollTop;
  }
//var preloadpoof = new Image();
//preloadpoof.src = "/poof." + ext;
//function mouseUp() {
//  }
document.body.onmousemove = trackMouse;
//document.body.onmouseup = mouseUp;


var str = '';
function getDockHTML() {
  if (str != "") return str;
  for (i=1; i<dockSetup.length; i++) {
	dockLocation[ dockSetup[i] ] = i;
	str += "<a ondragstart='return false' class='q' id='a_"+ dockSetup[i] +"' href='" + navURL[dockSetup[i]] + "' target='_top'>";
	str += "<img id='image_"+ dockSetup[i] + "' onmouseover=zoomin('"+ dockSetup[i] +"') onmouseout=zoomout('"+ dockSetup[i] +"')  alt='"+ labelText[dockSetup[i]] +"' src='imgs/"+ dockSetup[i] +"-sm."+ ext +"' border='0' height='"+smiconsize+"' width='"+smiconsize+"' style='z-index:1000'></a>";
    }
  return str;
  }
