/*
   Project:   Client-Side Photo Album/Slide Show  v1.02 (latest update: 07/19/04 10:23PM)
   Author:    Thomas Ballard - thomas2003*esqsoft^om (antibot: *=@, ^=.c) - http://esqsoft.com
   Notes:     Adapted for Mega Web Services, a United Online company.
*/




window.picture_shows = new Array()
window.current_index = 0
window.mDelay = 5
window.mTmpDelay = window.mDelay
window.mTimer = null
window.create_shortcut_keys = false
window.mShow = null




window.handle_autopilot = function(objStr){
  // toggle the autopilot status

  if(!objStr){ alert('error: Must pass target as string or reference to handle_autopilot method'); return }

  if(typeof(objStr)=='object'){ o=objStr }
  else{ obj=document.getElementById(objStr); if(!obj) return }

  // default picture show to on ... change this later to use cookie or form value on start or change
  //  if(!obj.init){ obj.init = 1; obj.running = 1; obj.runningResetInit = 1 }

  if(obj.running){ 
    if(obj.runningResetInit){ obj.runningResetInit = 0; //if first time here, set value correctly
    }
    else{
      if(window.mTimer){ clearTimeout(window.mTimer); window.mTimer=null }
      obj.running = 0
    }
    window.show_running = 0
    obj.innerHTML='stopped'
    obj.className='autopilot_stopped'
  }
  else{
    window.show_running = 1
    obj.running = 1
    obj.innerHTML='running'
    obj.className='autopilot_running'
    if(window.nextPicture) window.nextPicture('picHTML','idxHTML','capHTML')
  }
}





window.process_keypress = function(override){
  var condition = 0
  try{ condition=event.keyCode; } catch(e){}
  if(override) condition=override;
  if(!condition){ alert('error: Unable to grab event properties'); return }
  switch(condition){
    case 60: // < (skip to beginning)
        window.current_index=-1; process_timer('autopilot_seconds_left','autopilot_seconds_total',1); if(window.nextPicture) window.nextPicture(); break;
    case 62: // > (skip to end)
        window.current_index=(window.current_array.length-3); process_timer('autopilot_seconds_left','autopilot_seconds_total',1); break;
    case 63: // ? (help)
        if(window.showHide) window.showHide('help_table'); break;
    case 48: // 0 (previous)
        window.current_index-=2; if(window.current_index<0) window.current_index=(window.current_array.length-3); process_timer('autopilot_seconds_left','autopilot_seconds_total',1); break;
    case 46: // . (next)
        process_timer('autopilot_seconds_left','autopilot_seconds_total',1); break;
    case 43: // + (+1 second)
      window.mDelay++; break;
    case 45: // - (-1 second)
      window.mDelay--; if(window.mDelay<1) window.mDelay=1; break;
    case 13: // [enter]
      handle_autopilot('autopilot_status'); break;
    default:
      alert(event.keyCode); //DEBUG
      break;    
  }
}




window.handlePicOnload = function(objStr){
  if(typeof(objStr)=='object') obj = objStr
  else{ obj = document.getElementById(objStr); if(!obj) return }

  //expose(obj)
}


window.nextPicture = function(objStrPic, objStrIdx, objStrCap){
  // Increment picture counter and load in next picture

  if(!objStrPic){ alert('error: Must pass target as string or reference to nextPicture method'); return }

  if(typeof(objStrPic)=='object') obj = objStrPic 
  else{ obj = document.getElementById(objStrPic); }
  if(!obj) obj = document.images[objStrPic];

  if(!obj){ alert('error: Unable to resolve image object reference'); return }

  // setup extra props for IMG object
    if(!obj.onload){
      obj.onload=window.handlePicOnload
    }

  // change the slide show IMG to the new picture
    ++window.current_index; 
    if(window.current_index >= window.current_array.length-2) window.current_index=1;
//    if(!window.current_array[window.current_index].match(/\.(jpg|gif|png)$/i) window.current_index=1;
    obj.src= window.currentShowPath + '/' + window.current_array[window.current_index];

  // optional: show "current pic of total pictures (ex: 1/10)"
    if(objStrIdx){
      if(typeof(objStrIdx)=='object'){obj = objStrIdx }
      else{ obj = document.getElementById(objStrIdx); if(!obj) return }
      obj.innerHTML=''+(window.current_index)+' / '+(window.current_array.length-3)
    }

  // optional: show "path/picture"
    if(objStrCap){
      if(typeof(objStrCap)=='object'){ obj = objStrCap }
      else{ obj = document.getElementById(objStrCap); if(!obj) return }
      obj.innerHTML=window.current_array[window.current_array.length-1]+'/'+window.current_array[window.current_index]
    }

  if(window.process_timer) window.process_timer('autopilot_seconds_left','autopilot_seconds_total')
}





window.process_timer = function(objStrRemain, objStrTotal, bReset){
  // If the slide show is on autopilot, handle the timer

  if(window.mTimer){ clearTimeout(window.mTimer); window.mTimer=null }

  // show seconds remaining
    if(typeof(objStrRemain)=='object') obj = objStrRemain 
    else obj = document.getElementById(objStrRemain) 
    if(obj) obj.innerHTML = window.mTmpDelay

  // show total seconds
    if(typeof(objStrTotal)=='object') obj = objStrTotal 
    else obj = document.getElementById(objStrTotal) 
    if(obj) obj.innerHTML = window.mDelay

  // reduce time to next picture
    window.mTmpDelay--; 
    if(window.mTmpDelay<0 || bReset){
      window.mTmpDelay=window.mDelay;
      if(window.nextPicture) window.nextPicture('picHTML','idxHTML','capHTML');
      return;
    }

  if(window.show_running) 
    window.mTimer=setTimeout("process_timer('autopilot_seconds_left','autopilot_seconds_total')",1000);
}





window.showHide = function(objStr, hide, strHideParent){
  // toggle a passed container (or its parent) between hidden and visible states

  if(!objStr){ alert('error: Need to pass target object as string or reference to showHide method'); return }

  if(typeof(objStr)=='object') obj = objStr 
  else{ obj = document.getElementById(objStr); }
  if(!obj){ alert('error: Unable to resolve object in showHide'); return }

  if(strHideParent){
    // look for a parent element instead (parent will have attribute "hideable")
    var objParent = obj
    var attempts = 10; // safety catch... max parents to traverse before bailing out of loop
    while(--attempts > 0){
      if(objParent && objParent.getAttribute && (objParent.getAttribute("hideable") != null)){
        obj = objParent; 
        attempts = 0; 
      }
      else objParent = objParent.parentElement || 0
    }
  }

  if(obj.style){
    if(hide) obj.mhidden = 0; //force to hide

    if(!obj.mhidden){ 
      obj.mhidden=1; obj.style.visibility = 'hidden' 
      if(strHideParent) obj.style.position = 'absolute'
    }
    else{ 
      obj.mhidden=0; obj.style.visibility = 'visible' 
      if(strHideParent) obj.style.position = ''
    }
  }
}





window.herror = function(eStr){
  // central error presentation mechanism

  if(!eStr){ alert('error: No error passed to herror method'); return }

  str ='<div align="left" style="margin: 5px;">'
  str+='<div class="help_heading"><b>An error occured</b></div>'
  str+='<ul>'
  str+='<br></div>'

  o = document.getElementById('picture_cell')
  if(o) o.innerHTML = str
  else alert(eStr.replace(/<br>/,'\n'))
}





window.pictureShow = function(show,objStrTitle){
  // get a picture show

  //ERROR- Must define window.current_array
  if(!window.current_array){
    //herror('<li>Variable "window.current_array" has no definition.<br>It should be defined in your data file.')
    return;
  }

  if(show){    
    if(window.mTimer){ clearTimeout(window.mTimer); window.mTimer = null; }
    window.current_array = show;
    window.current_index = 0;
    window.mTmpDelay = window.mDelay;
  }

  //resolve a file path... support legacy arrays which had the path as the last element in the array
    window.currentShowPath = window.current_array[window.current_array.length-1]
    if(window.currentShowPath == '') window.currentShowPath = window.current_array[window.current_array.length-2]
  


  // optional: show title of the show
  if(objStrTitle){
    if(typeof(objStrTitle)=='object') obj = objStrTitle
    else obj = document.getElementById(objStrTitle)
    if(obj) obj.innerHTML = window.current_array[0];  
  }

  if(window.handle_autopilot) window.handle_autopilot('autopilot_status');
  if(window.nextPicture) window.nextPicture('picHTML','idxHTML','capHTML');
}





window.buildOtherShowsList = function(pArr,oStr){
  if(!pArr){ alert('error: Must pass picture array to buildOtherShowsList method'); return }

  window.quick_keys=new Array();         //quick key (alt-[key]) shortcut to links
  var str='<b>Other Slide Shows:</b><hr>';
  for(var i=0; i<pArr.length; i++){

    add_chr_str = pArr[i][0]
    //try to create shortcut keys automatically (if user has set this var in the datafile)
//    for(var y=0; y<pArr[i][0].length; y++){ 
//        var item=add_chr_str;
//        var chr=item.charAt(y);
//        var tmp_str = window.quick_keys.toString();
//          if(tmp_str.indexOf(chr)==-1){
//            //we haven't added this letter yet, so add it
//            window.quick_keys[window.quick_keys.length]=chr;
//            add_chr=chr;
//            if(y<item.length) post_chr=item.substr(y+1,item.length);
//            y=pArr[i][0].length;
//            add_chr_str=pre_chr+'<u>'+add_chr+'</u>'+post_chr;
//        }
//        pre_chr+=chr;
//    }

    str+='<br><a href="window.pictureShow(window.picture_shows['+i+'])" style="border:0px;" '
//    if(add_chr) str+='  accesskey="'+add_chr+'"
    str+='  _nmouseover="window.status=\'Show '+pArr[i][0]+'\'; return true"'
    str+='  _nmouseout="window.status=\'\'; return true"'
    str+='  onclick="if(window.pictureShow){ window.pictureShow(window.mShowList['+i+']); this.className=\'clicked\' } return false"'
    str+='  onfocus="blur()"'
    str+='>'+add_chr_str+'</a>'
  }

  o = document.getElementById(oStr); if(!o) return 
  o.innerHTML = str

}






window.autoPilotStatus = function(oStr){
  if(!oStr){ alert('error: Must pass target object as string or reference to autoPilotStatus method'); return }

  if(typeof(oStr)=='object'){ o=oStr }
  else{ o = document.getElementById(oStr); if(!o) return }

  o.innerHTML='running';
  o.className='autopilot_running';        
}






window.handle_close = function(obj){
  alert('error: handle_close is depricated to showHide'); return
}





window.mMain = function(){
  // connect to remote picture show data and begin a show

  if(!window.mMainInit){
    window.mMainInit = 1
    window.mMainInitAttempts = 5
  }

  // attempt to connect to a remote show
    if(!window.mShow){
      try{ window.mShow = window.pic } catch(e){}
     
      if(!window.mShow){
        try{ window.mShow = pic } catch(e){}
        //alert('error: unable to connect to a picture show object')
      }
    } 

  // attempt to connect to a remote show list of shows
    if(!window.mShowList){
      try{ window.mShowList = window.picture_shows } catch(e){}

      if(!window.mShowList){
        try{ window.mShowList = picture_shows } catch(e){}
      }
    }

  // still not connected? try again in a second? (slow load or 404 on data file)
    if(!window.mShow || !window.mShowList){
      if(--window.mMainInitAttempts > 0){ 
        window.mTimer = setTimeout('window.mMain()',1000)
      }
      else{ 
        alert('error: Maximum mMain init attempts reached, \nfailed to connect to a picture show object and/or list of shows'); 
      }
      return 
    }


  // parse get for query string
    if(location.search){
      window.mForm = new Object()
      window.mForm_raw = location.search.substring(1,location.search.length)
      window.mForm_raw = window.mForm_raw.split(/&/)
      for(var i=0; i<window.mForm_raw.length; i++){
        temp=window.mForm_raw[i].split(/=/)
        window.mForm[temp[0]] = temp[1]
      }
    }


  // check the query string for requested album
    if(window.mForm){
      if(window.mForm["album"]){
        for(var i=0; i<window.mShowList.length; i++){
          var albumStr = window.mForm["album"].toLowerCase()
          if(window.mShowList[i][0].toLowerCase().indexOf(albumStr)!=-1){
            window.current_array = window.mShowList[i];
          }
        }
      }
    }

  // start the show
    if(window.buildOtherShowsList){
      window.buildOtherShowsList(window.mShowList,'other_shows')
    }
    if(window.pictureShow){
      window.pictureShow(window.mShowList[i]);
    }

  //document.onkeypress=window.process_keypress()
} 



/* MAIN */

window.mMain()
