/* -------------------------*/    
/*   Timers                 */
/* -------------------------*/
function updateTimer( id , timediff , refresh , regen ) {
    // Vars
    var tm=new Date(timediff*1000)
    var days=Math.floor(timediff/86400); 
    var hours=tm.getUTCHours(); 
    var minutes=tm.getUTCMinutes(); 
    var seconds=tm.getUTCSeconds(); 
    var timeString="";
    if(days>0){timeString=timeString+days+" days, ";}
    if(hours>0){timeString=timeString+hours+" hours, ";}
    if(minutes>0){timeString=timeString+minutes+" minutes, ";}
    timeString=timeString+seconds+" seconds";
    $("#"+id).text( timeString ); 
    
    // Over or not?
    if( timediff > 0 ){
        setTimeout(
            function(){
                updateTimer(  id , (timediff-1) , refresh , regen );
            }
            , 1000
        );
        
        
    }
    else{
        if( refresh == "true" ){
             location.reload();
             return;
        }
        else if( refresh == "updateProfile" ){     
            var cur_hea = parseFloat($("#curheafield").text());
            var max_hea = parseFloat($("#maxheafield").text());
            var cur_cha = parseFloat($("#curchafield").text());
            var max_cha = parseFloat($("#maxchafield").text());
            var cur_sta = parseFloat($("#curstafield").text());
            var max_sta = parseFloat($("#maxstafield").text());

            var new_hea = (cur_hea + regen < max_hea) ? cur_hea + regen : max_hea;
            var new_cha = (cur_cha + regen < max_cha) ? cur_cha + regen : max_cha;
            var new_sta = (cur_sta + regen < max_sta) ? cur_sta + regen : max_sta;
          
            $("#curheafield").text( new_hea.toFixed(2) );
            $("#curchafield").text( new_cha.toFixed(2) );
            $("#curstafield").text( new_sta.toFixed(2) );  
                      
            $("#healthbar").width( Math.round(((new_hea/max_hea)*100)*100)/100+'%' );
            $("#chakrabar").width( Math.round(((new_cha/max_cha)*100)*100)/100+'%' );
            $("#staminabar").width( Math.round(((new_sta/max_sta)*100)*100)/100+'%' );
            
            updateTimer(  id , 60 , refresh , regen );                                        
        }
        else{
            $("#"+id).text("Timer Completed");
        }                     
    }
}       

/* -------------------------*/    
/*   Google Analytics       */
/* -------------------------*/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2323038-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); 
                
