/* This notice must be untouched at all times.
  
  time pad, copyright 2008, David C. Lindhout, all rights reserved
  
  Yea, like that's gonna do any good...
  It's beauty is in it's simplicity, and it's elegance is it's minimal footprint.
  In other words, there's not much here. And that's a good thing.
  If you find a use for this I hope you give me credit. Stop by
  info-tran.com to tell me about it, I have a forum. Better yet,
  send me a few bucks, there's a convenient way at:

      http://info-tran.com/timePad.html

  I hope this saves everybody a few keystrokes
  
  version 1.0, January 16,2008
*/
function TimePad(timeID) {
this.id=timeID
this.startY=0
this.endY=0
this.pixelMin=0
this.pixelMax=144
this.hourArray=new Array(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
this.hourMax=12
this.minuteArray=new Array('00', '10', '20', '30', '40', '50')
this.minuteMax=6
this.amArray=new Array('am', 'pm')
this.amMax=2
this.startTime={}
this.startHour=0
this.startMinute=0
this.startAm=0
this.startHourValue=''
this.startMinuteValue=''
this.startAmValue=''
this.endTime={}
this.endHour=0
this.endMinute=0
this.endAm=0
this.endHourValue=''
this.endMinuteValue=''
this.endAmValue=''
this.timeSpan={}
this.saveTimeSpan={}
this.elapsedTime={}
this.started=0 //0-initialized ,1-started, 2-ended
this.initialized=false
this.scrollYms=0
this.offsetY=0
if(false){
  if(!document.all){
}else{
  window.onscroll=function(){this.scrollYms=event.screenY;rating=document.getElementById('rating');rating.value=event.screenY;}
}
}

TimePad.prototype.Initialize=function(){
    this.initialized=true
    this.obj=document.getElementById(this.id)
    var classs=''
 //   if(this.id=='widget'){
      this.timeSpan=document.getElementById('timeSpan')
      this.saveTimeSpan=this.timeSpan
      this.elapsedTime=document.getElementById('elapsedTime')
      this.start=document.getElementById('start')
      this.end=document.getElementById('end')
      this.startField=document.getElementById('startField')
      this.endField=document.getElementById('endField')
 //     alert('start '+this.startField.value)
//   }else{
if(false){
var fields = document.getElementsByTagName('input')
    var i=0
    for (i=0;i<fields.length;i++){ 
        if(fields[i].className==this.id){
          if(fields[i].name=='startField'){
            this.startTimeField=fields[i]
          }else if(fields[i].name=='endField'){
            this.endTimeField=fields[i]
//          }else if(fields[i].name=='timeSpan'){
//            this.timeSpanField=fields[i]
//            this.saveTimeSpanField=this.timeSpan
//          }else if(fields[i].name=='elapsedTime'){
//            this.elapsedTimeField=fields[i]
          } 
        }
  //  }
    var displays = document.getElementsByTagName('span')
    var i=0
    for (i=0;i<displays.length;i++){ 
        if(displays[i].className==this.id){
          if(displays[i].name=='start'){
            this.startTime=displays[i]
          }else if(displays[i].name=='end'){
            this.endTime=displays[i]
          }else if(displays[i].name=='timeSpan'){
            this.timeSpan=displays[i]
            this.saveTimeSpan=this.timeSpan
          }else if(displays[i].name=='elapsedTime'){
            this.elapsedTime=displays[i]
          } 
        }
    }
    }
}
//    this.timeSpan=this.startTime
}

TimePad.prototype.StartTime=function(event){
if(this.started>1){//previously ended
  this.endX=0
  this.endY=0
  this.started=0
}
if(this.started<1){//initialized, ready to set this.started to 1
 this.startY=this.getTimePosition(event.clientY)
if(!event){
}else{
//  alert("#"+this.id+"!"+event+'*'+event.clientY+"*")
}
   var timeString=this.pixelsToTime(this.startY,'am',this.startField)
  this.start.value=timeString+":00"
  setClass(this.elapsedTime,'timepad')
  this.elapsedTime.innerHTML="Start Time: "+timeString
  this.started=1
//  this.timeSpan=this.endTime
}
}
TimePad.prototype.EndTime=function(event){
  if(this.started<2){
    if(this.started>0){ //no up yet
      this.endY=this.getTimePosition(event.clientY)
      var timeString=this.pixelsToTime(this.endY,'am',this.endField)//update end time
      this.end.value=timeString+":00"
      var elapsed=this.calculateElapsedTime(this.startY,this.endY)
      if(elapsed>0){
        var timeString=this.pixelsToTime(elapsed,'')//update message
 //       var msg=document.getElementById("timespan")
         this.timeSpan=this.saveTimeSpan
 //        var msg=document.getElementById("elapsedTime")
    if(this.id=='widget'){
        this.elapsedTime.innerHTML=timeString+'  total'
    }else{
//        this.elapsedTimeField.value=timeString+'  total'
      var start=this.pixelsToTime(this.startY,'am')
      var end=this.pixelsToTime(this.endY,'am')
        this.elapsedTime.innerHTML="Fished from "+start+" to "+end+" ("+timeString+")"
    }
      }
    }
  }
  if(elapsed>0){
    if(this.id=='widget'){
      var fields = document.getElementsByTagName('input')
      for (i=0;i<fields.length;i++){ 
        if(fields[i].className==this.id){
          if(fields[i].name=='start'){
//            fields[i].value=this.pixelsToTime(this.startY,'am')
          }else if(fields[i].name=='end'){
            fields[i].value=this.pixelsToTime(this.endY,'am')
          }else if(fields[i].name=='timeSpan'){
            fields[i].value=this.timeSpan.innerHTML
          }else if(fields[i].name=='elapsedTime'){
            fields[i].value=this.elapsedTime.innerHTML
          } 
        }
    }
    }
        this.started=3
  }
}

TimePad.prototype.HoverTime=function(event){
  if(!this.initialized){
    this.Initialize()
  }
//  var obj=document.getElementById(this.id)
  var work=this.getTimePosition(event.clientY) //establish time of current mouse position
  var timeString=this.pixelsToTime(work,'am')
  if(this.id=='widget'){
    this.timeSpan.innerHTML=timeString
  }else{
//    this.timeSpanField.value=timeString
    this.timeSpan.innerHTML=timeString
  }
//  msg=document.getElementById("timespan")
  if(this.started==1){
    var elapsed=this.calculateElapsedTime(this.startY,work)
    var timeStringE=this.pixelsToTime(elapsed,'')//update message
//    msg=document.getElementById("elapsedTime")
if(this.started>0){
  this.elapsedTime.innerHTML=this.start.value+' to '+timeString+' ('+timeStringE+")"
}
if(false){
  if(this.id=='widget'){
    this.elapsedTime.innerHTML=timeString
  }else{
//    this.elapsedTimeField.value=timeString
    this.elapsedTime.innerHTML=timeString
  }
  }
  }
}
TimePad.prototype.Clear=function(){
//if(this.startY>0){
//  this.offsetY=this.offsetY+this.startY
//}
 msg=document.getElementById("timeSpan")
  msg.innerHTML='time pad'
//  msg=document.getElementById("elapsedTime")
//  msg.innerHTML=' '
if(this.started<2){
//  this.elapsedTime.value=''
}
this.saveTimeSpan.innerHTML='time pad'
  this.started=0
  this.initialized=false
}

TimePad.prototype.getTimePosition=function (y){
//    var child=document.getElementById('rating')
//    child.value=y
  var child=document.getElementById(this.id)
   var off=this.offset(child,0)
  //navigator.appName+navigator.appVersion
//  alert('y='+y+' offset='+off+' w.sY='+window.scrollY+' w.pYO='+window.pageYOffset+" dbsT="+document.body.scrollTop+" ddesT="+document.documentElement.scrollTop)
  y=y-off
//    var child=document.getElementById('fishCount')
//    child.value=off
  y=y+document.documentElement.scrollTop
  //window.scrollY and window.pageYOffset don't work in IE. document.body.scrollY doesn't work anywhere
if(false){  if(navigator.appName!="Microsoft Internet Explorer"){
    //((this.id!='widget')&&){
    y=y+window.scrollY
  }else{
    y=y+this.scrollYms
  }
}
//    var child=document.getElementById('fishKept')
//    child.value=document.documentElement.scrollTop
//alert('y='+y+' offset='+off+' scroll='+window.scrollY+"&"+this.scrollYms+"&")
  y=y+this.offsetY
if(y<this.pixelMin){
    y=this.pixelMin
    this.offsetY=this.offsetY+1//this the the auto calibration
  }
  if(y>this.pixelMax){
    y=this.pixelMax
    this.offsetY=this.offsetY-1//this the the auto calibration
  }
//    var child=document.getElementById('peopleCount')
//    child.value=this.offsetY
  return y
}
TimePad.prototype.offset=function(child,accumulator){
  if(child.tagName!='BODY'){
    accumulator=accumulator+child.offsetTop
    var parent=child.offsetParent
    var accumulator=this.offset(parent,accumulator)
  }
  return accumulator
}
TimePad.prototype.ChangeStartHour=function(delta){
  this.startHour=this.startHour+delta
  if(this.startHour<0){ //if we fall below zero, start at the end
    this.startHour=this.hourMax-1
  }else if (this.startHour>=this.hourMax){ //and if we go past the end, start at the beginning
    this.startHour=0
  }
  var msg=document.getElementById("startHourValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.hourArray[this.startHour]
}

TimePad.prototype.ChangeStartMinute=function(delta){
  this.startMinute=this.startMinute+delta
  if(this.startMinute<0){ //if we fall below zero, start at the end
    this.startMinute=this.minuteMax-1
  }else if (this.startMinute>=this.minuteMax){ //and if we go past the end, start at the beginning
    this.startMinute=0
  }
  var msg=document.getElementById("startMinuteValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.minuteArray[this.startMinute]
}

TimePad.prototype.ChangeStartAm=function(delta){
  this.startAm=this.startAm+delta
  if(this.startAm<0){ //if we fall below zero, start at the end
    this.startAm=this.amMax-1
  }else if (this.startAm>=this.amMax){ //and if we go past the end, start at the beginning
    this.startAm=0
  }
  var msg=document.getElementById("startAmValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.amArray[this.startAm]
}

TimePad.prototype.ChangeEndHour=function(delta){
  this.endHour=this.endHour+delta
  if(this.endHour<0){ //if we fall below zero, start at the end
    this.endHour=this.hourMax-1
  }else if (this.endHour>=this.hourMax){ //and if we go past the end, start at the beginning
    this.endHour=0
  }
  var msg=document.getElementById("endHourValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.hourArray[this.endHour]
}

TimePad.prototype.ChangeEndMinute=function(delta){
  this.endMinute=this.endMinute+delta
  if(this.endMinute<0){ //if we fall below zero, start at the end
    this.endMinute=this.minuteMax-1
  }else if (this.endMinute>=this.minuteMax){ //and if we go past the end, start at the beginning
    this.endMinute=0
  }
  var msg=document.getElementById("endMinuteValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.minuteArray[this.endMinute]
}

TimePad.prototype.ChangeEndAm=function(delta){
  this.endAm=this.endAm+delta
  if(this.endAm<0){ //if we fall below zero, start at the end
    this.endAm=this.amMax-1
  }else if (this.endAm>=this.amMax){ //and if we go past the end, start at the beginning
    this.endAm=0
  }
  var msg=document.getElementById("endAmValue")//display the value from hourArray[starthour]
  msg.innerHTML=this.amArray[this.endAm]
}

TimePad.prototype.pixelsToTime=function (pixels,ampm,field){
var hr=Math.round(pixels/6)
  if((hr*6)>pixels){
    hr=hr-1
  }
  var min=pixels-(hr*6)
  var amIndex=0
  var hrIndex=hr
  if(field!=null){
    if(hr<10){
      var hour='0'+hr
    }else{
      var hour=hr
    }
    var minute=min+'0'
    field.value=hour+":"+minute
  }
  if(hr>11){
    hrIndex=hrIndex-12
    amIndex=1
    if(hr>12 & ampm>''){
      hr=hr-12
    }
    if(hrIndex>11){
      hrIndex=0
      hr=0
      min=0
    }
  }
  if(hr<0){
    hrIndex=0
    hr=0
    min=0
  }
  if(hr==0 & ampm>''){ //midnight is 12am
    hr=12
  }
  if(ampm>''){
    if(this.started<1){
      this.startAm=amIndex//index to TimePad arrays
      this.startHour=hrIndex
      this.startMinute=min
    }else if(this.started<2){
      this.endAm=amIndex//index to TimePad arrays
      this.endHour=hrIndex
      this.endMinute=min
    }
  } 
  if(this.id=='widget'){
    if(ampm>''){//only do for time, not elapsed time
      if(this.started==1){
        hour=document.getElementById("endHourValue")
        minute=document.getElementById("endMinuteValue")
        am=document.getElementById("endAmValue")
      }else{
        hour=document.getElementById("startHourValue")
        minute=document.getElementById("startMinuteValue")
        am=document.getElementById("startAmValue")
      }
      if(this.started<2){
        hour.innerHTML=this.hourArray[hrIndex]
        minute.innerHTML=this.minuteArray[min]
        am.innerHTML=this.amArray[amIndex]
        msg=document.getElementById("elapsedTime")
        msg.innerHTML=' '/*IE7 doesn't work with '', space is required*/
      }
    }
    if(this.started<3){
      return hr+" : "+this.minuteArray[min]+' '+this.amArray[amIndex]
    }else{
      this.started=2
      return ''
    }
  }else{
    if(ampm>''){//only do for time, not elapsed time
      return hr+":"+this.minuteArray[min]+' '+this.amArray[amIndex]
    }else{
      return hr+":"+this.minuteArray[min]
    }
  }  
}

TimePad.prototype.calculateElapsedTime=function(start,end){
  var elapsed=end-start
  if(elapsed>this.pixelMax){
    elapsed=this.pixelMax
  }
    return elapsed
  }
}

