var vpHt=0;
var vpWd=0;
var docHt=0;
var scrollTop=0;
var searchMode=false;
var shopt, schterm, schtermbox;
var ltappmain=null, ltapp=null, ltapptop=null, ltappbot=null;
var ltappin=null, ltappout=null, ltappscroll=null, shadefit=null;
var content=null, ltappbr=null;
var shade=null, ltapptr=null;

(new Image).src='/images/ltapptop.gif';
(new Image).src='/images/ltappbot.gif';
(new Image).src='/images/ltapptl.gif';
(new Image).src='/images/ltapptr.gif';
(new Image).src='/images/ltappbr.gif';
(new Image).src='/images/ltappbl.gif';
(new Image).src='/images/ltschback.gif';
(new Image).src='/images/arrow.png';

function dimensions() {
  var wd=Math.max(vpWd, 800);
  ltappmain.style.height=(vpHt-62)+"px";
  shopt.resizeNotify(vpHt-130, wd-300);
  ltapp.style.width=(wd-275)+"px";
  ltapptop.style.width=(wd-287)+"px";
  ltappbot.style.width=(wd-287)+"px";
  ltapptr.style.left=(wd-325)+"px";
  ltappbr.style.left=(wd-285)+"px";
}

function startSchMode() {
  if (!searchMode) {
    if (schterm.value=='Type here...')
      schterm.value='';

    vpWd=YAHOO.util.Dom.getViewportWidth();
    vpHt=YAHOO.util.Dom.getViewportHeight();
    scrollTop=getScrollTop();
    contWd=Math.max(Math.max(shadefit ? shadefit.offsetWidth : 0, vpWd), content.offsetWidth);
    contHt=Math.max(Math.max(shadefit ? shadefit.offsetHeight : 0, vpHt), content.offsetHeight);
    dimensions();

    shade.style.height=contHt+"px";
    shade.style.width=contWd+"px";
    shade.style.display='block';
    ltapp.style.top=(scrollTop+20)+"px";
    if (schtermbox) {
      schtermbox.style.top=(scrollTop+20)+"px";
      schtermbox.style.display='block';
      schterm.focus();
    }

    if (ltappout.isAnimated())
      ltappout.stop();
    ltapp.style.height="14px";
    ltapp.style.display='block';
    ltappin.attributes.height.to=vpHt-50;
    ltappin.animate();
    searchMode=true;
  }
}

function endSchMode() {
  if (searchMode) {
    shopt.turnOff();
    if (ltappin.isAnimated())
      ltappin.stop();
    shade.style.display="none";
    ltappout.animate();

    if (schtermbox) {
      schtermbox.style.display='none';
    }

    searchMode=false;
    if (schterm.value=='')
      schterm.value='Type here...';
  }
}

function lightOn() {
  setTimeout(checkString, 200);
  setTimeout(checkDims, 1000);
  shopt.turnOn();
}

function lightOff() {
  ltapp.style.display="none";
}

YAHOO.util.Event.onAvailable('content', function() {
  shopt=new Shoptivate(endSchMode);
  DOMhelp.appendContent(this, shopt.outerDOMelements());

  var o=DOMhelp.createElement;
  ltapptop = o('div#ltapptop');
  ltappmain = o('div#ltappmain', shopt.DOMelements());
  ltappbot = o('div#ltappbot');
  ltapptr = o('div#ltapptr');
  ltappbr = o('div#ltappbr');
  ltapp = o('div.ltapp#ltapp', ltapptop, ltappmain, ltappbot, o('div#ltapptl'),
            ltapptr, ltappbr, o('div#ltappbl'));
  this.appendChild(ltapp);
  content=this;
  shade=document.getElementById('shade');
  shadefit=document.getElementById('shmeasure');

  var scrollelts = [ltapp];
  schtermbox = document.getElementById('schtermbox');
  if (schtermbox) {
    scrollelts.push(schtermbox);
  }

  ltappin = new YAHOO.util.Anim(ltapp, {height: {from: 14, to: 100}}, 0.6, YAHOO.util.Easing.easeIn);
  ltappin.onComplete.subscribe(lightOn);
  ltappout = new YAHOO.util.Anim(ltapp, {height: {to: 14}}, 0.6, YAHOO.util.Easing.easeOut);
  ltappout.onComplete.subscribe(lightOff);
  ltappscroll = new YAHOO.util.Anim(scrollelts, {top: {to: 100}}, 0.4, YAHOO.util.Easing.easeOut);

  schterm = document.getElementById('schterm');
  schterm.onfocus=startSchMode;
  schterm.onblur=null;
  if (window.schFocused) {
    startSchMode();
  } else if (window.location.search) {
    if (window.location.search=='?search') {
      schterm.focus();
    } else if (window.location.search.substring(0,8)=='?search=') {
      schterm.value=unescape(window.location.search.substring(8));
      startSchMode();
    }
  }
  shade.onclick=endSchMode;
  ltapptr.onclick=endSchMode;
});

function checkString() {
  if (!searchMode)
    return;
  shopt.txtupdate(schterm.value);
  setTimeout(checkString,200);
}

function getScrollTop() {
  return document.documentElement.scrollTop || document.body.scrollTop;
}

function checkDims() {
  if (!searchMode)
    return;
  var newVpHt = YAHOO.util.Dom.getViewportHeight();
  var newVpWd = YAHOO.util.Dom.getViewportWidth();
  var newContWd = Math.max(Math.max(shadefit ? shadefit.offsetWidth : 0, newVpWd), content.offsetWidth);
  var newContHt = Math.max(Math.max(shadefit ? shadefit.offsetHeight : 0, newVpHt), content.offsetHeight);
  var newScrollTop = getScrollTop();

  setTimeout(checkDims, 500);
  if (newVpWd!=vpWd || newVpHt!=vpHt) {
    vpWd=newVpWd;
    vpHt=newVpHt;
    dimensions();
    ltapp.style.height=(vpHt-50)+"px";
  }
  if (scrollTop!=newScrollTop) {
    scrollTop=newScrollTop;
    if (ltappscroll.isAnimated())
      ltappscroll.stop();
    ltappscroll.attributes.top.to=scrollTop+20;
    ltappscroll.animate();
  }
  if (contWd!=newContWd) {
    contWd=newContWd;
    shade.style.width=contWd+"px";
  }
  if (contHt!=newContHt) {
    contHt=newContHt;
    shade.style.height=contHt+"px";
  }
}
