
/**
 * AlcoholTester (Mar 2011)
 *
 * @author MMA
 *
 */
var AlcoholTester = {

    //premenne
    waist: null,
    r: null,
    beta: null,
    
    
    
    //funkcia prepocita promile
    computePromile: function() {
        
        //vaha cloveka
        this.waist = $('#weight').val(); 
        
        //koeficient vody v tele
        this.r = $('.radio-btn.selected-radio[rel="gender"]').attr('value') == "male" ? 0.7 : 0.6;
        
        //koeficient odburavania alkoholu
        var beta = $('.radio-btn.selected-radio[rel="gender"]').attr('value') == "male" ? 0.083 : 0.068;
        
        var metabolicRate = parseFloat($('#metabolic').val());
        this.beta = beta + metabolicRate;
        
        
        //vypocitame hodnoty
        var weight = this.getAlcoholContent();
        var timeWeight = this.getAlcoholOverTime(weight); 
        var promile = this.getPromile(timeWeight);
        var soberTime = this.getSoberTime(timeWeight);

        $('#promile-content-percent').text(promile.toFixed(2).replace(".", ","));
        $('#sobber-time').text(soberTime);
        
        this.alcoholEffects(promile);
    },
    
    //vypocet promile
    getPromile: function(weight) {
        
        //vypocitame promile
        var promile = weight / (this.waist * this.r);
        promile = promile < 0 ? 0 : promile;
        
        return promile;
    },
    
    //ziska hmotnost alkoholu v napojoch
    getAlcoholContent: function() {
        
        var weight = 0;
        
        //mnozstvo alkoholu
        var sbeer = $('#sbeer').val() * 300;
        var beer4 = $('#beer4').val() * 500;
        var beer45 = $('#beer45').val() * 500;
        var beer5 = $('#beer5').val() * 500;
        
        //obsah cisteho etanolu v gramoch
        sbeer = (sbeer * 5 * 0.8) / 100;
        beer4 = (beer4 * 4 * 0.8) / 100;
        beer45 = (beer45 * 4.5 * 0.8) / 100;
        beer5 = (beer5 * 5 * 0.8) / 100;
        
        //spocitame vahu alkoholu
        weight = weight + sbeer + beer4 + beer45 + beer5;
        
        
        
        //mnozstvo alkoholu
        var vine = $('#vine').val() * 100;
        var sampus = $('#sampus').val() * 100;
        var portske = $('#portske').val() * 100;
        var vermut = $('#vermut').val() * 100;
        
        //obsah cisteho etanolu v gramoch
        vine = (vine * 8 * 0.8) / 100;
        sampus = (sampus * 6 * 0.8) / 100;
        portske = (portske * 12 * 0.8) / 100;
        vermut = (vermut * 18 * 0.8) / 100;
        
        //spocitame vahu alkoholu
        weight = weight + vine + sampus + portske + vermut;
        
        
        
        //mnozstvo alkoholu
        var alc20 = $('#alc20').val() * 100;
        var alc30 = $('#alc30').val() * 100;
        var alc40 = $('#alc40').val() * 100;
        var alc70 = $('#alc70').val() * 100; 
        
        //obsah cisteho etanolu v gramoch
        alc20 = (alc20 * 20 * 0.8) / 100;
        alc30 = (alc30 * 30 * 0.8) / 100;
        alc40 = (alc40 * 40 * 0.8) / 100;
        alc70 = (alc70 * 70 * 0.8) / 100;
        
        //spocitame vahu alkoholu
        weight = weight + alc20 + alc30 + alc40 + alc70;
         
        
        return weight;     
    },
    
    //vypocita hmotnost alkoholu v zavislosti na case
    getAlcoholOverTime: function(weight) {
        
        //dnes alebo vcera
        var now = new Date();
        var yesterday = parseInt($('.radio-btn.selected-radio[rel="drink-day"]').attr('value'));   
        
        //cas
        var time = $('#time').val().split(':');
        var then = new Date(now.getFullYear(), now.getMonth(), now.getDate(), time[0] * 1, time[1] * 1, 0, 0);
        
        if(yesterday) then.setDate(then.getDate() - 1);
        
        //rozdiel casu v hodinach
        var difference = now.getTime() - then.getTime();
        diference = Math.floor(difference / 1000 / 60 / 60);
        diference = diference < 0 ? 0 : diference;

        //celkova hmotnost alkoholu v gramoch po odburani v case
        timeWeight = weight - (diference * this.waist * this.beta);
        timeWeight = timeWeight < 0 ? 0 : timeWeight;
        
        return timeWeight;
    },
    
    //vobrazi ucinky alkoholu v zavislosti od promile
    alcoholEffects: function(promile) {
        
        var title = alcoholEffect0.title;
        var text = alcoholEffect0.text;
        var color = alcoholEffect0.color;
        var image = alcoholEffect0.image;
        
        switch(true) {
            case (0 < promile && promile <= 0.29):
                title = alcoholEffect1.title;
                text = alcoholEffect1.text;
                color = alcoholEffect1.color;
                image = alcoholEffect1.image;
                break;
                
            case (0.29 < promile && promile <= 0.59):
                title = alcoholEffect2.title;
                text = alcoholEffect2.text;
                color = alcoholEffect2.color;
                image = alcoholEffect2.image;
                break;
                
            case (0.59 < promile && promile <= 0.9):
                title = alcoholEffect3.title;
                text = alcoholEffect3.text;
                color = alcoholEffect3.color; 
                image = alcoholEffect3.image;
                break;
                
            case (0.9 < promile && promile <= 1.9):
                title = alcoholEffect4.title;
                text = alcoholEffect4.text;
                color = alcoholEffect4.color;
                image = alcoholEffect4.image;
                break;
                
            case (1.9 < promile && promile <= 2.9):
                title = alcoholEffect5.title;
                text = alcoholEffect5.text;
                color = alcoholEffect5.color;
                image = alcoholEffect5.image;
                break;
                
            case (2.9 < promile && promile <= 3.9):
                title = alcoholEffect6.title;
                text = alcoholEffect6.text;
                color = alcoholEffect6.color;
                image = alcoholEffect6.image;
                break;
                
            case (3.9 < promile):   
                title = alcoholEffect7.title;
                text = alcoholEffect7.text;
                color = alcoholEffect7.color;
                image = alcoholEffect7.image;
                break;  
        } 
        
        $('#alcohol-state-title').text(title).css({ color: color });
        $('#alcohol-state-text').text(text);
        $('#promile-content').css({ 'background-image': "url('" + image + "')" });
    },
    
    //vypocita cas kedy clovek vytriezvie
    getSoberTime: function(timeWeight){
        
        var soberTime = timeWeight / (this.waist * this.beta) * 3600;
        
		var time = [0, 0, soberTime]; 
        for(var i = 2; i > 0; i--){
            time[i - 1] = Math.floor(time[i] / 60);
            time[i] = time[i] % /**/60;
            if(time[i] < 10) {
                time[i] = '0' + time[i];
            }
        };
        
        time[0] = time[0] + "h";
        time[1] = time[1] + "min";
        
        //odstranim sekundy
        time.pop();
        
        return time.join(':');    
    } 
}




if(typeof jQuery != 'undefined') {
    //co sa stane ked sa nacita cely dokument?
    $(document).ready(function(){
        
        //krazny custom radio btn
        $('.radio-btn').click(function(){
            if($(this).hasClass("selected-radio")) return false;
            
            var rel = $(this).attr('rel');
            
            $('.radio-btn[rel="' + rel + '"]').removeClass('selected-radio');
            $(this).addClass('selected-radio');    
            
            AlcoholTester.computePromile();
        });
        
        
        //inicializacia slideru
        $(".be-alcohol-slider").slider({ 
            animate: true,
            min: 0,
            max: 10,
            step: 1,
            range: "min",
           
            //funkcia zmeni hodnotu pri pohybe slideru
            slide: function(event, ui) { 
                if(this.opts){
                    $(this)
                        .siblings('.slider-info')
                        .children('span')
                        .text(this.opts["option-" + ui.value].name);
                    
                    $(this)
                        .siblings('input')
                        .val(this.opts["option-" + ui.value].value);        
                } else {
                    $(this)
                        .siblings('.slider-info')
                        .children('span')
                        .text(ui.value.toString().replace(".", ","));
                    
                    $(this)
                        .siblings('input')
                        .val(ui.value);       
                } 
                
                AlcoholTester.computePromile();
            },
            
            //funkcia zmeni hodnotu pri pohybe slideru
            change: function(event, ui) { 
                if(this.opts){
                    $(this)
                        .siblings('.slider-info')
                        .children('span')
                        .text(this.opts["option-" + ui.value].name);
                    
                    $(this)
                        .siblings('input')
                        .val(this.opts["option-" + ui.value].value);        
                } else {
                    $(this)
                        .siblings('.slider-info')
                        .children('span')
                        .text(ui.value.toString().replace(".", ","));
                       
                    $(this)
                        .siblings('input')
                        .val(ui.value);         
                }
                
                AlcoholTester.computePromile();
            },
            
            //zmenime options pri inicializacii slideru
            create: function(event, ui) { 
                
                //zistime textove options
                var optIm = $(this).attr('options');
                var options = new Object();
                
                if(optIm != null) {
                    var opts = optIm.split("|");
                    
                    for(i = 0; i < opts.length; i++) {
                        var opt = opts[i].split(":");  
                        
                        options["option-" + i] = {
                            name: opt[0],
                            value: opt[1]
                        } 
                    } 
                    
                    this.opts = options;    
                }
                
                //maximalna hodnota
                var maxIm = parseInt($(this).attr('max'));
                maxIm = isNaN(maxIm) ? 0 : maxIm;
                
                //minimalna hodnota
                var minIm = parseInt($(this).attr('min'));
                minIm = isNaN(minIm) ? 0 : minIm;
                
                //default hodnota
                var defIm = parseInt($(this).attr('def'));
                defIm = isNaN(defIm) ? 0 : defIm;  
                
                //maximalna hodnota
                var step = parseFloat($(this).attr('step'));
                step = isNaN(step) ? 1 : step;
                
                $(this).slider("option", {
                    min: minIm,
                    max: maxIm,
                    value: defIm,
                    step: step 
                });
            }
        });
        
        
        //specialny slider, ktory sluzi na vyber casu
        $('#drink-hours-slider').slider({    
            //funkcia zmeni hodnotu pri pohybe slideru
            slide: function(event, ui) { 
                
                var hour = Math.floor(ui.value / 4);
                var min = (ui.value % 4) * 15; 
                var time = (hour < 10 ? ("0" + hour) : hour) + ':' + (min < 10 ? ("0" + min) : min);
                
                $(this)
                    .siblings('.slider-info')
                    .children('span')
                    .text(time);
                
                $(this)
                    .siblings('input')
                    .val(time); 
                          
                
                AlcoholTester.computePromile();
            },
            
            //funkcia zmeni hodnotu pri pohybe slideru
            change: function(event, ui) { 
                
                var hour = Math.floor(ui.value / 4);
                var min = (ui.value % 4) * 15; 
                var time = (hour < 10 ? ("0" + hour) : hour) + ':' + (min < 10 ? ("0" + min) : min);
                
                $(this)
                    .siblings('.slider-info')
                    .children('span')
                    .text(time);
                   
                $(this)
                    .siblings('input')
                    .val(time);
                    
                    
                AlcoholTester.computePromile();
            }
        });
    });
}

