﻿ var tick;
 //var stime='17-11-2009 12:24:00';
// var cycle=125;
 //var bwidth=220;
 //var c;
 //var span;
 function stop() 
 {
   clearTimeout(tick);
 }

function initializeTimeBar(startTime,cycleminutes,serverTime, tcol,id)
{
   var bwidth=220;
    //stime = startTime;
    //cycle = cycleminutes;
    
    var strInput=serverTime;
    var strArray= strInput.split(" ");
    var strTime=strArray[1].split(":");
    var strDate=strArray[0].split("-");
    
    var span = new Date();

    var c = new Date(strDate[2],parseInt("10",strDate[1])-1,strDate[0],strTime[0],strTime[1],strTime[2]);
    //alert('c time:' + c);
    
    var strHTML="";
    
    strHTML+= "    <h4 class=\"bar_title\" ><span>(<span id=\"" + id + "cycle-minutes\"></span>)</span>&nbsp;</h4>";
    
    strHTML+= "  <INPUT TYPE=HIDDEN id=\"" + id + "c\" value=\"" + c.getTime().toString() + "\"> <INPUT TYPE=HIDDEN id=\"" + id + "span\" value=\"" + span.getTime().toString() + "\">   <div id=\"" + id + "bar\" class=\"bar\">";
    strHTML+= "        <div id=\"" + id + "decBar\" class=\"decBar\" >&nbsp;";
    strHTML+= "        </div><div style=\"text-align: center;width:222px;\"><span id=\"" + id + "time-remaining\" class=\"fg_current_mark\"></span></div>";
    strHTML+= "     </div>";
        
  //set control html  
  document.getElementById(id).innerHTML=strHTML;
  // set bar width  
  document.getElementById(id).style.width=bwidth  + "px";
  document.getElementById(id + 'bar').style.width=bwidth  + "px";
  document.getElementById (id + 'bar').style.borderColor = tcol ;
  if(cycleminutes>=1440)
  {
    var hour = cycleminutes  / (60);
    var da=(hour/24);
    var days=parseInt(hour/24);
    
    //var minutes = (hour -parseInt(hour))* 60;
    var minutes = cycleminutes - (parseInt(hour) * 60);
    hour=(da -parseInt(da))* 24;
    var se = (minutes-parseInt(minutes))  * 60;
                
         document.getElementById(id + 'cycle-minutes').innerHTML= parseInt(days) + (parseInt(days)>1 ? " days" : " day") + (parseInt(hour)>0 ? ", " + parseInt(hour) + (parseInt(hour)>1 ? " hours" : " hour") : "") + (parseInt(minutes)>0 ? " " + parseInt(minutes) + (parseInt(minutes)>1 ? " minutes" : " minute") : "");
   }
  else if(cycleminutes >59)
  {
    var hour = cycleminutes  / (60);
    //var minutes = (hour -parseInt(hour))* 60;
    var minutes = cycleminutes - (parseInt(hour) * 60);
    var se = (minutes-parseInt(minutes))  * 60;
                
    document.getElementById(id + 'cycle-minutes').innerHTML= parseInt(hour) + (parseInt(hour)>1 ? " hours" : " hour") + (parseInt(minutes)>0 ? " " + parseInt(minutes) + (parseInt(minutes)>1 ? " minutes" : " minute") : "");
  }
  else
  {

    document.getElementById(id + 'cycle-minutes').innerHTML= cycleminutes + (parseInt(cycleminutes)>1 ? " minutes" : " minute");
  }
   
}

function timeBar(id,cycleminutes,startTime)
{
     
        var bwidth=220;    
        
        var strInput=startTime;
        var strArray= strInput.split(" ");
        var strTime=strArray[1].split(":");
        var strDate=strArray[0].split("-");
               
       
        try{
     
                var c = document.getElementById(id + 'c').value;
                var span = document.getElementById(id + 'span').value;
                
                document.getElementById(id + 'decBar').style.backgroundColor = '#BBCCDD' ;
                //y, m, d, h, i, s
                
                //Create start date on the basis of input date string
                var s = new Date(strDate[2],parseInt("10",strDate[1])-1,strDate[0],strTime[0],strTime[1],strTime[2]);
                
                //Create end date by adding cycle minutes in start date 
        
                var e=new Date();
                e.setTime(s.getTime() + (cycleminutes * 60 * 1000));
                //Calculate date difference by subtracting  current time from end time
    
                var difDate = e.getTime() - c;
              
                   //Convert the time difference in hour
                var minu = difDate / (60*1000);
               
                var hour = difDate / (60*60*1000);
                var da=hour/24;
                
                var days=parseInt(hour/24);
              
                //Convert the time difference in minutes
                var minutes = (hour -parseInt(hour))* 60;
    
                hour=parseInt((da-parseInt(da))* 24);
               
               
                //Convert fractional part of minutes into seconds
                var se = (minutes-parseInt(minutes))  * 60;
                //Calculate minutes percentage
                var percent=(minu/cycleminutes);
                percent=percent*100;
                //Calculate bar width using the minutes percentage
                var barWidth=(percent/100)*bwidth
                
                document.getElementById(id + 'c').value=(parseInt(c) + (new Date()).getTime() - parseInt(span));
                document.getElementById(id + 'span').value = (new Date()).getTime().toString();
                //If minutes are less then or equal to 0 then clear the time out and stop reccurent execution of this function
                //alert(minu);
                if(minu<=0)
                {
                   //clearTimeout(tick);
                   document.getElementById(id + 'time-remaining').innerHTML="Expired";
                   document.getElementById(id + 'decBar').style.width= "0px";
                   
                }
                //else excute the function recurrently with the interval of 1/2 second.
                else
                {
                  if(parseInt(days)>0)
                  {
                                
                        document.getElementById(id + 'time-remaining').innerHTML=parseInt(days) + (parseInt(days)>1 ? " days" : " day") + (parseInt(hour)>0 ? (parseInt(hour)<10 ? ", 0" : ", ") + parseInt(hour) + ":" : ", 00:") + (parseInt(minutes)>0 ? (parseInt(minutes)<10 ? "0" : "") + parseInt(minutes)+ ":"  : "00:")  + (parseInt(se)>0 ? (parseInt(se)<10 ? "0" : "") + parseInt(se) : "00")
                      
                    
                  }
                 else if(parseInt(hour)>0)
                  {
                    document.getElementById(id + 'time-remaining').innerHTML=(parseInt(hour)<10 ? "0" : "") + parseInt(hour) +  ":" + (parseInt(minutes)>0 ? (parseInt(minutes)<10 ? "0" : "") + parseInt(minutes) + ":" : "00:") + (parseInt(se)>0 ? (parseInt(se)<10 ? "0" : "") + parseInt(se) : "00")
                  }
                  else
                  {
                    document.getElementById(id + 'time-remaining').innerHTML=(parseInt(minutes)<10 ? "0" : "") + parseInt(minutes)  +  ":" + (parseInt(se)>0 ? (parseInt(se)<10 ? "0" : "") + parseInt(se) : "00")
                  }
                   document.getElementById(id + 'time-remaining').innerHTML = document.getElementById(id + 'time-remaining').innerHTML  + " <span style='color:#8A8A8A;'>&nbsp;</span>"
                   document.getElementById(id + 'decBar').style.width=barWidth  + "px";
                  //alert(id);
//                   tick=setTimeout("timeBar('" + id + "');",500);   
                  
                 
                }

         }
         catch(e)
         {
  
         }       
}


