var busy_nadel = 0;
var kennzahl = 0;
var kzahl = 0;

function moveNadel() {
    if(busy_nadel == 0) {
        busy_nadel = 1;
        var kzahl_old = kzahl;
        kzahl = kennzahl;
        if(kzahl > 400) {
            kzahl = 400;
        }
        if(kzahl_old == kzahl) {
            relaxNadel();
        }
        else {
            new Effect.Move('nadel', { x: kzahl * 1.090, duration: 0.9, mode: 'absolute', transition: Effect.Transitions.SwingFromTo, afterFinish: relaxNadel, afterUpdate: setKennzahl });
        }
    }
}

function relaxNadel(obj) {
    $('kennzahl').innerHTML = kennzahl;
    busy_nadel = 0;
}

function setKennzahl(obj) {
    var theLeft = parseInt(Element.getStyle(obj.element, 'left'));
    theLeft /= 1.090;
    $('kennzahl').innerHTML = Math.round(theLeft);
}

function formatNumber(aValue, aDigits) {
    if(aDigits == 0) {
        aValue = Math.round(aValue);
    }
    else {
        theFactor = Math.pow(10, aDigits);
        aValue = Math.floor(aValue * theFactor + 0.5) / theFactor;
    }
    var theValue = '';
    var theCount = 0;
    var theParts = String(aValue).split('.');
    for(var i = theParts[0].length - 1; i >= 0; i--) {
       theValue = theParts[0].charAt(i) + theValue;
       theCount++;
       if(theCount == 3 && i > 0) {
           theValue = '.' + theValue;
           theCount = 0;
       }
    } 
    if(aDigits != 0) {
       theValue += ',' + (isNaN(theParts[1]) ? '0' : theParts[1]);
    }
    return theValue;
}

function finishedPerform(aObj) {
    var theId = "t" + aObj.element.id.substring(1);
    if(!Element.visible(aObj.element)) {
        Element.removeClassName(theId, "trigger_up");
        Element.addClassName(theId, "trigger_down");
    }
    else {
        Element.removeClassName(theId, "trigger_down");
        Element.addClassName(theId, "trigger_up");
    }
}

function perform(aSlider) {
    new Effect.toggle(aSlider, 'blind', {duration: 0.5, afterFinish: finishedPerform});
}

Form.Element = {

    setValue: function(element, newValue) {
        element_id = element;
        element = $(element);
        if (!element){element = document.getElementsByName(element_id)[0];}
        if (!element){return false;}
        var method = element.tagName.toLowerCase();
        var parameter = Form.Element.SetSerializers[method](element, newValue);
    }
    
}

Form.Element.SetSerializers = {

    input: function(element, newValue) {
        switch (element.type.toLowerCase()) {
            case 'submit':
            case 'hidden':
            case 'password':
            case 'text':
                return Form.Element.SetSerializers.textarea(element, newValue);
            case 'checkbox':
            case 'radio':
                return Form.Element.SetSerializers.inputSelector(element, newValue);
        }
        return false;
    },

    inputSelector: function(element, newValue) {
        fields = document.getElementsByName(element.name);
        for (var i=0;i<fields.length;i++){
            if (fields[i].value == newValue){
                fields[i].checked = true;
            }
        }
    },

    textarea: function(element, newValue) {
        element.value = newValue;
    },

    select: function(element, newValue) {
        var value = '', opt, index = element.selectedIndex;
        for (var i=0;i< element.options.length;i++){
            if (element.options[i].value == newValue) {
                element.selectedIndex = i;
                return true;
            }        
        }
    }
    
}

function unpackToForm(data) {

    for (i in data){
        Form.Element.setValue(i,data[i].toString());
    }
    
}

var cookie = null;

function populateCookieValues() {
    if(cookie != null) {
        var thePairs = cookie.split("#");
        var thePair;
        for(i = 0; i < thePairs.length; i++) {
            thePair = thePairs[i].split("=");
            Form.Element.setValue(thePair[0], thePair[1]);                    
        }            
    }
}

function isShowing(aElement) {
    return Element.getStyle(aElement, 'display') != 'none';
}

function toggleMainHelp() {
    if(isShowing('help')) {
        hideMainHelp();
    }
    else {
        showMainHelp(0);
    }
}

function showMainHelp(aDelay) {
    var theHeight = $('help').getHeight();
    new Effect.Move('help', { x: 865, y: -theHeight - 10, mode: 'absolute', duration: 0.0 }); 
    new Effect.Move('help', { x: 865, y: 282, mode: 'absolute', delay: aDelay + 0.1, duration: 1.2, transition: Effect.Transitions.Bounce, beforeStart: function() { Element.show('help'); } }); 
}

function hideMainHelp() {
    var theDelay = 0;
    if(!isShowing('s0')) {
        perform('s0');
        theDelay = 0.5;
    }
    if(isShowing('s1')) {
        perform('s1');
        theDelay = 0.5;
    }
    if(isShowing('s2')) {
        perform('s2');
        theDelay = 0.5;
    }
    if(isShowing('s3')) {
        perform('s3');
        theDelay = 0.5;
    }
    if(isShowing('s4')) {
        perform('s4');
        theDelay = 0.5;
    }
    var theHeight = $('help').getHeight();
    new Effect.Move('help', { x: 865, y: -theHeight - 10, mode: 'absolute', delay: theDelay, duration: 0.8, transition: Effect.Transitions.SwingFrom, afterFinish: function() { Element.hide('help'); } });
}

Effect.Transitions.Elastic = function(pos) {
    return -1*Math.pow(4,-8*pos) * Math.sin((pos*6-1)*(2*Math.PI)/2) + 1;
};

Effect.Transitions.SwingFromTo = function(pos) {
    var s = 1.70158;
    if ((pos/=0.5) < 1) return 0.5*(pos*pos*(((s*=(1.525))+1)*pos - s));
    return 0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos + s) + 2);
};

Effect.Transitions.SwingFrom = function(pos) {
    var s = 1.70158;
    return pos*pos*((s+1)*pos - s);
};

Effect.Transitions.SwingTo = function(pos) {
    var s = 1.70158;
    return (pos-=1)*pos*((s+1)*pos + s) + 1;
};

Effect.Transitions.Bounce = function(pos) {
    if (pos < (1/2.75)) {
        return (7.5625*pos*pos);
    } else if (pos < (2/2.75)) {
        return (7.5625*(pos-=(1.5/2.75))*pos + .75);
    } else if (pos < (2.5/2.75)) {
        return (7.5625*(pos-=(2.25/2.75))*pos + .9375);
    } else {
        return (7.5625*(pos-=(2.625/2.75))*pos + .984375);
    }
};

Effect.Transitions.BouncePast = function(pos) {
    if (pos < (1/2.75)) {
        return (7.5625*pos*pos);
    } else if (pos < (2/2.75)) {
        return 2 - (7.5625*(pos-=(1.5/2.75))*pos + .75);
    } else if (pos < (2.5/2.75)) {
        return 2 - (7.5625*(pos-=(2.25/2.75))*pos + .9375);
    } else {
        return 2 - (7.5625*(pos-=(2.625/2.75))*pos + .984375);
    }
};

Effect.Transitions.EaseFromTo = function(pos) {
    if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,4);
    return -0.5 * ((pos-=2)*Math.pow(pos,3) - 2);    
};

Effect.Transitions.EaseFrom = function(pos) {
    return Math.pow(pos,4);
};

Effect.Transitions.EaseTo = function(pos) {
    return Math.pow(pos,0.25);
};

Event.observe(window, 'load', function() {
    populateCookieValues();
});

