var __doc=document;
function $(id){return __doc.getElementById(id);}
function node(type){return __doc.createElement(type);}
function evalJS(e){eval(e.responseText);}
function getHTTPObject() {
  var xmlhttp=false;
  if(window.XMLHttpRequest) {
    try {xmlhttp = new XMLHttpRequest();} catch(e) {xmlhttp = false;}
  // branch for IE/Windows ActiveX version
  } else if(window.ActiveXObject) {
   	try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {
    try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {xmlhttp = false;}}
  }return xmlhttp;
}
function rm(obj){
while (obj && obj.childNodes.length>0) obj.removeChild( obj.firstChild );
}
function read(func,file,sync){
  var _reader=getHTTPObject();
  try{
    _reader.open('GET',file,!sync);
    if(!sync){
      _reader.onreadystatechange = function(){
        if(_reader.readyState==4){
          if(_reader.status!=200){
            readRetry(func,file,sync);
            return false;
          }
          if(func) func(_reader);
        }
      }
    }
    _reader.send(null);
    if(sync) {
      if(_reader.status!=200){
        readRetry(func,file,sync);
        return false;
      }
      if(func) func(_reader);
    }
    return true;
  }
  catch(e){
    floatWin(null,file+","+sync+","+e.message);
    readRetry(func,file,sync,e.message);
    return false;
  }
}
function readRetry(func,file,sync,errorMessage){
  var label=$('retryRead');
  if(label) {
    if(errorMessage) label.em=errorMessage;
    else label.em='No Error';
    label.style.display='';
  }
}
function dge(id){return __doc.getElementById(id);}
function setCookie(sName, sValue){
var date = new Date((new Date()).getTime() + 10*365*24*3600*1000);
document.cookie = sName + "=" + encodeURIComponent(sValue) + "; expires=" + date.toGMTString()+";path=/";
}

function changeLang(sel){
  var newlang=sel.options[sel.selectedIndex].value;
  setCookie('lang',newlang);
  //remove hash and reload url, with a hash the new url cannot be reloaded
  var newloc=location.toString().substr(0,location.toString().length-location.hash.length);
  var split=newloc.split('/');
  if(newlang=='en-us') split[4]='en';
  else if(newlang=='zh-tw') split[4]='zh-TW';
  else if(newlang=='zh-cn') split[4]='zh-CN';
  newloc=split.join('/');
  location=newloc;
}
function findPos(obj) {
	var curleft=obj.offsetLeft;
  var curtop=obj.offsetTop;
  var lastObj=obj;
  while (obj = obj.offsetParent){//assignment operator, not mistake
    lastObj=obj;
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
  }
  return [curleft,curtop];
}

var dropWinTimer,timedDropper,currentDrop,timedOpenDropper,timedOpenAnchor,dropWinOpenTimer;
function mDropWin(anchor,dropper,close,right,waitTime){
  if(!anchor || !dropper) return;
  if(!waitTime) waitTime=100;
  if(close) {
    //close it in a while to make sure it's not closed accidentally
    timedDropper=dropper;
    dropWinTimer=setTimeout("timedDropper.style.display='none'",waitTime);
    clearTimeout(dropWinOpenTimer);
  }
  else{
    //opening a dropper, and the dropper is one that was about to close, cancel the closing
    if(timedDropper && timedDropper.id==dropper.id) clearTimeout(dropWinTimer);
  }

  if(!close && dropper.style.display=='none') {
    //if we want to show a dropper, we close the last dropper and clear its closing timer first
    clearTimeout(dropWinTimer);
    if(timedDropper) timedDropper.style.display='none';

    //show and position the new dropper
    timedOpenDropper=dropper;
    timedOpenAnchor=anchor;
    dropWinOpenTimer=setTimeout("mDropWin_display("+right+");",waitTime);
  }
}
function mDropWin_display(right){
  timedOpenDropper.style.display='';
  var pos=findPos(timedOpenAnchor);
  timedOpenDropper.style.top=(pos[1]+timedOpenAnchor.offsetHeight+5)+'px';
  if(right) timedOpenDropper.style.left=(pos[0]-timedOpenDropper.offsetWidth+timedOpenAnchor.offsetWidth)+'px';
  else timedOpenDropper.style.left=pos[0]+'px';

}
/*
function dropWinUp(anchor,dropper,close){
  if(!anchor || !dropper) return;
  if(close) {
    timedDropper=dropper;
    dropWinTimer=setTimeout("timedDropper.style.display='none'",10);
  }
  else {
    dropper.style.display='';
    if(timedDropper==dropper) clearTimeout(dropWinTimer);
  }
  var pos=findPos(anchor);
  dropper.style.top=pos[1]-dropper.clientHeight-5;
  dropper.style.left=pos[0];
}
*/
var TimeToFade = 500.0;

//2: originally displayed
//-1: hiding
//-2: originally hidden
//1: showing
var currentId=1;
var skip=0;
var timer;
function fade(id){
  if(id) {
    currentId=id;
    clearInterval(timer);
    timer=setInterval("fade();",8000);
  }
  else {
    if(skip>0) {
      skip--;
      return;
    }

    currentId++;
    if(currentId>5 || currentId<1){
      currentId=1;
    }
    else if(currentId==5 && skip==0) {
      //skip=2;
    }
  }

  var eid='banner'+currentId;
  var element = $(eid);
  if(!element) return;

  element.style.opacity = 0;
  element.style.filter = 'alpha(opacity = 0)';
  element.parentNode.appendChild(element);

  element.FadeState=1;
  element.FadeTimeLeft = TimeToFade;
  setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  $('fadeSwitch1').innerHTML='○';
  $('fadeSwitch2').innerHTML='○';
  $('fadeSwitch3').innerHTML='○';
  $('fadeSwitch4').innerHTML='○';
  $('fadeSwitch5').innerHTML='○';
  $('fadeSwitch'+currentId).innerHTML='●';
}
function animateFade(lastTick, eid){
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;

  var element = $(eid);

  //finish
  if(element.FadeTimeLeft <= elapsedTicks){
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }

  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1) newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}
function showTab(parent,show){
  var i;
  for(i=0;i<parent.childNodes.length;i++){
    var node=parent.childNodes[i];
    if(node.nodeType==1){
      node.style.display='none';
    }
  }
  show.style.display='';
}
function showTour(index){
  var tourNavList=$('tourNavList'),i;
  for(i=0;i<tourNavList.childNodes.length;i++){
    var anc=tourNavList.childNodes[i].firstChild;
    if(!anc || anc.nodeType!=1) continue;
    anc.className='tourLeftNav';
  }
  $('a'+index).className='tourLeftNavSelected';

  var tourRight=$('tourRight');
  var anchor=$('a'+index).href.split('#');
  for(i=0;i<tourRight.childNodes.length;i++){
    var tab=tourRight.childNodes[i];
    if(!tab || tab.nodeType!=1) continue;
    tab.style.display='none';
  }
  $('t'+anchor[1]).style.display='';
}
function loadYoutube(caption){
  var cstring;
  if(!caption || caption=='en_US') cstring='&amp;hl=en_US';
  else cstring='&amp;cc_load_policy=1&amp;hl='+caption;

  var str='<object width="640" height="505">' +
      '<param name="movie" value="http://www.youtube.com/v/_Yy1W6J7ocE?fs=1'+cstring+'&amp;rel=0&amp;hd=1"></param>' +
      '<param name="allowFullScreen" value="true"></param>' +
      '<param name="allowscriptaccess" value="always"></param>' +
      '<embed src="http://www.youtube.com/v/lIrM7yxlY7Q?fs=1'+cstring+'&amp;rel=0&amp;hd=1&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>';
  var mov=$('mov'),movclose=$('movClose');
  if(movclose) movclose.style.display='';
  $('mov').style.display='';
  $('mov').innerHTML=str;
}
function loadMovie(id,bg,width,height,autoStart){
  if(!bg) bg='1a1a1a';
  if(!width) width=458;
  if(!height) height=355;
  if(!autoStart) autoStart='false';
  var str='<object id="csSWF" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+width+'" height="'+height+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0">'+
      '<param name="src" value="'+id+'"/>'+
      '<param name="quality" value="best"/>'+
      '<param name="allowScriptAccess" value="always"/>'+
      '<param name="allowFullScreen" value="true"/>'+
      '<param name="scale" value="showall"/>'+
      '<param name="flashVars" value="autostart='+autoStart+'#&thumb=FirstFrame.png&thumbscale=45&showstartscreen=false&color=0x'+bg+',0x'+bg+'"/>'+
      '<embed name="csSWF" src="'+id+'" width="'+width+'" height="'+height+'" bgcolor="#'+bg+'" quality="best" allowScriptAccess="always" allowFullScreen="true" scale="showall" flashVars="autostart='+autoStart+'&thumb=FirstFrame.png&thumbscale=45&showstartscreen=false&color=0x'+bg+',0x'+bg+'" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'+
  '</object>';
  var mov=$('mov'),movclose=$('movClose');
  if(movclose) movclose.style.display='';
  $('mov').style.display='';
  $('mov').innerHTML=str;
}
function floatWin(func,text){
  $('floatingBox').style.display='';
  $('floatingBox').innerHTML=text;
}
function installApp(sourcePath,apname,ifUpdate){
  read(evalJS,'/sims/appInstall.jsp?sp='+sourcePath+(apname?('&a='+apname):'')+(ifUpdate?'&update':''));
  if($('floatingWin')) $('floatingWin').style.display='none';
  if(parent.$('floatingWin')) parent.$('floatingWin').style.display='none';
}
/*
function installApp(sourcepath){
  $('overcast').style.display='';
  $('floatingPanel').style.display='';
  $('floatingIframe').src='/sims/install.jsp?sp='+sourcepath;
}
*/
