function stripslashes(str) {
str=str.replace(/\//g,'');
return str;
}

OutSourcingCalc = new Class({
    options: {
        containerID: 'outsourcing_calc',
        ajaxURL: 'http://www.pharmonia.sk/typo3conf/ext/class.outsourcing_calc.php'
    },

    result: {
        administrationPrice: '',
        economyPrice: '',
        standartPrice: '',
        keyPrice: ''
    },

    params: {
        administrationSalary: '',
        administrationMonthCount: '',
        administrationEmployeeCount: '',
        economyEmployeeCount: '',
        economyMonthCount: '',
        standartEmployeeCount: '',
        standartMonthCount: '',
        keyEmployeeCount: '',
        economyPrice: '',
        standartPrice: '',
        keyPrice: ''
    },

    initialize: function( container ){

        if( container !== null ){
            this.setContainer(container);
        }else{
            return false;
        }
        this.bindEvents();
        this.getAllValues();
        
        this.calculate();
        this.updateAdministration();
        this.updateEconomy();
        this.updateStandart();
        this.updateKey();

        return true
    },

    getAllValues: function(){
        this.params['administrationSalary'] = $('salary').getSelected().get('value')[0];
        this.params['administrationEmployeeCount'] = $('administration_employee_count').get('value');
        this.params['administrationMonthCount'] = $('administration_month_count').get('value');
        this.params['economyEmployeeCount'] = $('economy_employee_count').get('value');
        this.params['economyMonthCount'] = $('economy_month_count').get('value');
        this.params['standartEmployeeCount'] = $('standart_employee_count').get('value');
        this.params['standartMonthCount'] = $('standart_month_count').get('value');
        this.params['keyEmployeeCount'] = $('key_employee_count').get('value');
        this.params['keyMonthCount'] = $('key_month_count').get('value');
        
    },

    bindEvents: function(){
        calculatorObject = this;

        $('salary').addEvent('change', function(event) {
            calculatorObject.params['administrationSalary'] = $('salary').getSelected().get('value')[0];//[0]
            calculatorObject.params['administrationEmployeeCount'] = $('administration_employee_count').get('value');
            calculatorObject.params['administrationMonthCount'] = $('administration_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateAdministration();
        });

        $('administration_employee_count').addEvent('keyup', function(event) {
            calculatorObject.params['administrationSalary'] = $('salary').getSelected().get('value')[0];//[0];
            calculatorObject.params['administrationEmployeeCount'] = $('administration_employee_count').get('value');
            calculatorObject.params['administrationMonthCount'] = $('administration_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateAdministration();
        });

        $('administration_month_count').addEvent('keyup', function(event) {
            calculatorObject.params['administrationSalary'] = $('salary').getSelected().get('value')[0];
            calculatorObject.params['administrationEmployeeCount'] = $('administration_employee_count').get('value');
            calculatorObject.params['administrationMonthCount'] = $('administration_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateAdministration();
        });
        
        $('economy_employee_count').addEvent('keyup', function(event) {
            calculatorObject.params['economyEmployeeCount'] = $('economy_employee_count').get('value');
            calculatorObject.params['economyMonthCount'] = $('economy_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateEconomy();
        });

        $('economy_month_count').addEvent('keyup', function(event) {
            calculatorObject.params['economyEmployeeCount'] = $('economy_employee_count').get('value');
            calculatorObject.params['economyMonthCount'] = $('economy_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateEconomy();
        });

        $('standart_employee_count').addEvent('keyup', function(event) {
            calculatorObject.params['standartEmployeeCount'] = $('standart_employee_count').get('value');
            calculatorObject.params['standartMonthCount'] = $('standart_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateStandart();
        });

        $('standart_month_count').addEvent('keyup', function(event) {
            calculatorObject.params['standartEmployeeCount'] = $('standart_employee_count').get('value');
            calculatorObject.params['standartMonthCount'] = $('standart_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateStandart();
        });

        $('key_employee_count').addEvent('keyup', function(event) {
            calculatorObject.params['keyEmployeeCount'] = $('key_employee_count').get('value');
            calculatorObject.params['keyMonthCount'] = $('key_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateKey();
        });

        $('key_month_count').addEvent('keyup', function(event) {
            calculatorObject.params['keyEmployeeCount'] = $('key_employee_count').get('value');
            calculatorObject.params['keyMonthCount'] = $('key_month_count').get('value');
            calculatorObject.calculate();
            calculatorObject.updateKey();
        });


    },

    setContainer: function( element ){
        if( element !== null ){
            this.options.containerID = element;
        }
    },

    updateAdministration: function (){
        var admin_price = Number.from(this.result['administrationPrice']);
        $('administration_price').set('text',admin_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " Kč"
        }));
        
        var reg = /^\/(sk|sken)\//g
        if(location.pathname.search(reg) == 0 || location.pathname == "/pre-firmy/kalkulacka-outsourcingu/" || location.host == "www.pharmonia.sk"){
          $('administration_price').set('text',admin_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " EUR"
          }));
        
        }
        //@TODO: Can be removed.
        /*$('administration_employee_count').set('value',this.result['administrationEmployeeCount']);
        $('administration_month_count').set('value',this.result['administrationEmployeeMonth']);*/
    },

    updateEconomy: function (){
         var economy_price = Number.from(this.result['economyPrice']);
        $('economy_price').set('text',economy_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " Kč"
        }));
        
        var reg = /^\/(sk|sken)\//g
        if(location.pathname.search(reg) == 0 || location.pathname == "/pre-firmy/kalkulacka-outsourcingu/" || location.host == "www.pharmonia.sk"){
          $('economy_price').set('text',economy_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " EUR"
        }));
        
        }
        //@TODO: Can be removed.
        /*$('economy_employee_count').set('value',this.result['economyEmployeeCount']);
        $('economy_month_count').set('value',this.result['economyEmployeeMonth']);*/
    },

    updateStandart: function (){
        var standart_price = Number.from(this.result['standartPrice']);
        $('standart_price').set('text',standart_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " Kč"
        }));
        
        var reg = /^\/(sk|sken)\//g
        if(location.pathname.search(reg) == 0 || location.pathname == "/pre-firmy/kalkulacka-outsourcingu/" || location.host == "www.pharmonia.sk"){
          $('standart_price').set('text',standart_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " EUR"
        }));
        
        }
        //@TODO: Can be removed.
        /*$('standart_employee_count').set('value',this.result['standartEmployeeCount']);
        $('standart_month_count').set('value',this.result['standartEmployeeMonth']);*/
    },

    updateKey: function (){
        
        
        var key_price = Number.from(this.result['keyPrice']);
        $('key_price').set('text',key_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " Kč"
        }));
        var reg = /^\/(\.sk|sken)\//g
        if(location.pathname.search(reg) == 0 || location.pathname == "/pre-firmy/kalkulacka-outsourcingu/" || location.host == "www.pharmonia.sk"){
          var key_price = Number.from(this.result['keyPrice']);
            $('key_price').set('text',key_price.format({
            decimal: ".",
            group: " ",
            decimals: 0,
            prefix: "",
            suffix: " EUR"
        }));
        
        }
        
        //@TODO: Can be removed.
        /*$('key_employee_count').set('value',this.result['keyEmployeeCount']);
        $('key_month_count').set('value',this.result['keyEmployeeMonth']);*/
    },

    getContainer: function(){
        return this.options.containerID;
    },


    calculate: function(){
        calculatorObject = this;
        var urlJSON = 'http://www.pharmonia.cz/typo3conf/class.outsourcing_calc.php';
        
        var reg = /^\.sk/g;
        
        if(location.pathname.search(reg) == 0 || location.pathname == "/pre-firmy/kalkulacka-outsourcingu/" || location.host == 'www.pharmonia.sk'  ){
          urlJSON = 'http://www.pharmonia.sk/typo3conf/class.outsourcing_calc.php';
          calculatorObject.params['mut'] = 'sk';
        }
  
        var reg = /^\/sken\//g
        if(location.pathname.search(reg) == 0 || location.host == 'www.pharmonia.sk' ){
          urlJSON = 'http://www.pharmonia.sk/typo3conf/class.outsourcing_calc.php';
          calculatorObject.params['mut'] = 'sk';
        }

            
        var jsonReq = new Request.JSON({
            url: urlJSON,
            async: false,
            data: {
                'administrationSalary' : calculatorObject.params['administrationSalary'],
                'administrationEmployeeCount': calculatorObject.params['administrationEmployeeCount'],
                'administrationMonthCount' : calculatorObject.params['administrationMonthCount'],
                'economyEmployeeCount' : calculatorObject.params['economyEmployeeCount'],
                'standartEmployeeCount' : calculatorObject.params['standartEmployeeCount'],
                'keyEmployeeCount' : calculatorObject.params['keyEmployeeCount'],
                'economyMonthCount' : calculatorObject.params['economyMonthCount'],
                'standartMonthCount' : calculatorObject.params['standartMonthCount'],
                'keyMonthCount' : calculatorObject.params['keyMonthCount'],
                'mut' : calculatorObject.params['mut']
            },
            method: 'post',
            onSuccess: function( response ) {

                if( response.administrationPrice!== null ){
                    calculatorObject.result['administrationPrice'] = response.administrationPrice;
                }else{
                    calculatorObject.result['administrationPrice'] = null;
                }

                if( response.economyPrice!== null ){
                    calculatorObject.result['economyPrice'] = response.economyPrice;
                }else{
                    calculatorObject.result['economyPrice'] = null;
                }

                if( response.standartPrice!== null ){
                    calculatorObject.result['standartPrice'] = response.standartPrice;
                }else{
                    calculatorObject.result['standartPrice'] = null;
                }

                if( response.keyPrice!== null ){
                    calculatorObject.result['keyPrice'] = response.keyPrice;
                }else{
                    calculatorObject.result['keyPrice'] = null;
                }
            }
        }).send();
    }
});
/**/
Fx.SlideExt = new Class({

    Extends: Fx,
    options: {
        mode: 'vertical'
    },

    initialize: function(element, options){
        this.addEvent('complete', function(){
            this.open = (this.wrapper['offset' + this.layout.capitalize()] !=
                0);
            if (this.open && Browser.Engine.webkit419) this.element.dispose
                ().inject(this.wrapper);
        }, true);
        this.element = this.subject = document.id(element);
        this.parent(options);
        var wrapper = this.element.retrieve('wrapper');
        this.wrapper = wrapper || new Element('div', {
            styles: $extend(this.element.getStyles('margin', 'position'),
            {
                overflow: 'hidden'
            })
        }).wraps(this.element);

        this.now = [];
        this.open = true;
    },

    vertical: function(){
        this.margin = 'margin-top';
        this.layout = 'height';
        this.wrMargin = 'margin-top'
        this.offset = this.element.offsetHeight;
    },

    horizontal: function(){
        this.margin = 'margin-left';
        this.layout = 'width';
        this.wrMargin = 'margin-right'
        this.offset = this.element.offsetWidth;
    },

    set: function(now){
        this.wrapper.setStyle(this.layout,now[1]);
        this.wrapper.setStyle(this.wrMargin,now[2]);
        return this;
    },

    compute: function(from, to, delta){
        return [0, 1, 2].map(function(i){
            return Fx.compute(from[i], to[i], delta);
        });
    },

    start: function(how, mode){
        if (!this.check(how, mode)) return this;
        this[mode || this.options.mode]();
        var margin = this.element.getStyle(this.margin).toInt();
        var layout = this.wrapper.getStyle(this.layout).toInt();
        var wrMargin = this.wrapper.getStyle(this.wrMargin).toInt();
        var caseIn = [[margin, layout, wrMargin], [0, this.offset, 0]];
        var caseOut = [[margin, layout, wrMargin], [this.offset, 0,
        this.offset]];
        var start;
        switch (how){
            case 'in':
                start = caseIn;
                break;
            case 'out':
                start = caseOut;
                break;
            case 'toggle':
                start = (layout == 0) ? caseIn : caseOut;
        }

        return this.parent(start[0], start[1], start[2]);
    },

    slideIn: function(mode){
        return this.start('in', mode);
    },

    slideOut: function(mode){
        return this.start('out', mode);
    },

    hide: function(mode){
        this[mode || this.options.mode]();
        this.open = false;
        return this.set([this.offset, 0, this.offset]);
    },

    show: function(mode){
        this[mode || this.options.mode]();
        this.open = true;
        return this.set([0, this.offset, 0]);
    },

    toggle: function(mode){
        return this.start('toggle', mode);
    }

});

Element.Properties.slide = {
    set: function(options){
        var slide = this.retrieve('slide');
        if (slide) slide.cancel();
        return this.eliminate('slide').store('slide:options', $extend({
            link:
            'cancel'
        }, options));
    },

    get: function(options){
        if (options || !this.retrieve('slide')){
            if (options || !this.retrieve('slide:options')) this.set('slide',
                options);
            this.store('slide', new Fx.SlideExt(this, this.retrieve
                ('slide:options')));
        }
        return this.retrieve('slide');
    }
};

/**
 * Formularovy prvek input v indexSearch pluginu
 */ 
function inputIndexSearch() {
    var cont = $('vyhledavani');
    
    if(cont === null)
        return;
    
    var lang = new Array('cz','czen','sken', 'sk');
    
    var element = cont.getElement('input[type=text]');
    var values = new Array('Vyhledávání', 'Search', 'Search','Vyhľadávanie');
    setFormElementValue(element, values, lang);
}

function inputDate() {
    var cont = $('work');

    if(cont === null)
        return;

    var lang = new Array('cz','czen','sken','sk');

    //var element = cont.getElement('input[name=tx_odpharmonia[works][work1][work_from]]');
    var elementFrom = cont.getElement('input.date_from');
    var valuesFrom = new Array('(celé datum) nap\u0159. 20.1.1991', 'for example: 20.1.1991', 'for example: 20.1.1991','(celé datum) napr. 20.1.1991');
    setFormElementValue(elementFrom, valuesFrom, lang);

    var elemenTill = cont.getElement('input.date_till');
    var valuesTill = new Array('(celé datum) nap\u0159. 20.1.1991', 'for example: 20.1.1991' , 'for example: 20.1.1991','(celé datum) napr. 20.1.1991');
    setFormElementValue(elemenTill, valuesTill, lang);
}

function inputSchoolDate() {
    var cont = $('education');

    if(cont === null)
        return;

    var lang = new Array('cz','czen','sken','sk');

    var elementFrom = cont.getElement('input.date_from');
    var valuesFrom = new Array('(jen rok) nap\u0159. 1991', 'for example: 1991', 'for example: 1991', '(len rok) napr. 1991');
    setFormElementValue(elementFrom, valuesFrom, lang);

    var elemenTill = cont.getElement('input.date_till');
    var valuesTill = new Array('(jen rok) nap\u0159. 1991', 'for example: 1991', 'for example: 1991', '(len rok) napr. 1991');
    setFormElementValue(elemenTill, valuesTill, lang);
}

/**
 * Formularovy prvek input v FE login pluginu - jmeno
 */
function inputLoginUsernameBox() {
    var cont = $('userlogin');

    if(cont === null){
        return;
    }

    var lang = new Array('cz','czen','sken','sk');

    var element = cont.getElement('input[type=text]');    
    var values = new Array('P\u0159ihlašovací jméno', 'Username', 'Username', 'Prihlasovacie meno');
    setFormElementValue(element, values, lang);
}

/**
 * Formularovy prvek input v FE login pluginu - jmeno
 */
function inputLoginUsernameObsahBox() {
    var cont = $('userlogin_obsah');
    
    if(cont === null){
        return;
    }

    var lang = new Array('cz','czen','sk','sken');

    var element = cont.getElement('input[type=text]');
    console.log(element);
    var values = new Array('P\u0159ihlašovací jméno', 'Username', 'Prihlasovacie meno', 'Username');
    setFormElementValue(element, values, lang);
}

/**
 * Formularovy prvek input v FE login pluginu - heslo
 */
function inputLoginPasswordBox() {
    var cont = $('userpass_obsah');

    if(cont === null)
        return;

    var lang = new Array('cz','czen','sk','sken');

    var element = cont.getElementById('pass');
    if( element === null ){
        return;
    }
    var values = new Array('Va\u0161e heslo', 'Password','Va\u0161e heslo','Password');
    setFormElementPasswordValue(element, values, lang);
}

/**
 * Formularovy prvek input v FE login pluginu - heslo
 */
function inputLoginPasswordObsahBox() {
    var cont = $('userpass_obsah');

    if(cont === null)
        return;

    var lang = new Array('cz','czen','sk','sken');

    var element = cont.getChildren()[0];
    //element = $$('#userpass_obsah input#pass');
    if( element === null ){
        return;
    }
    
    var values = new Array('Va\u0161e heslo', 'Password','Va\u0161e heslo','Password');
    setFormElementPasswordValue(element, values, lang);
}

function inputLoginPasswordCompanyBox(){
    var cont = $('userpass');

    if(cont === null)
        return;

    var lang = new Array('cz','czen','sk','sken');

    var element = cont.getElementById('pass_company');
    var values = new Array('Vá\u0161 kód', 'Your code', 'Vá\u0161 kód', 'Your code');
    if( element === null ){
        return;
    }
    setFormElementPasswordValue(element, values, lang);
}

/**
 * Nastaveni formularovemu elementu hodnotu
 */ 
function setFormElementValue(element, values, lang) {    
    var value = getValue(values, lang);
    
    if( element.value == '' ){
      element.set('value', value);
    }
    
    element.addEvent('focus', function(event){
        value = getValue(values, lang);
        if(this.get('value') == value)
            this.set('value', '');
    });
    
    element.addEvent('blur', function(event) {
        value = getValue(values, lang);
        if(this.get('value') == '')
            this.set('value', value);
    });   
    
    //console.log(value);                
}

function setFormElementPasswordValue(element, values, lang) {
    var value = getValue(values, lang);

    if(!Browser.Engine.trident){
        element.set('type','text');
    }

    element.set('value', value);

    element.addEvent('focus', function(event) {
        value = getValue(values, lang);
        if(this.get('value') == value){
            if( !Browser.Engine.trident ){
                this.set('type','password');
            }
            this.set('value', '');
        }
    });

    element.addEvent('blur', function(event) {
        value = getValue(values, lang);
        if(this.get('value') == ''){
            if( !Browser.Engine.trident ){
                this.set('type','text');
            }
            this.set('value', value);
        }
    });
}

function setFormElementDateValue(element, values, lang) {
    var value = getValue(values, lang);

    element.set('value', value);

    element.addEvent('focus', function(event) {
        value = getValue(values, lang);
        if(this.get('value') == value){
            this.set('value', '');
        }
    });

    element.addEvent('blur', function(event) {
        value = getValue(values, lang);
        if(this.get('value') == ''){
            this.set('value', value);
        }
    });
}


/**
 * Hodnota pro formularovy prvek
 */ 
function getValue(values, lang) {
    var reg = /^\/(czen|de|pl|ru|sk|fr|sp|es|hr|hu|ro|cz|cs|sken)\//gi
    if(location.pathname.search(reg) == 0){
        var langParam = location.pathname.match(reg);
        var langParam = stripslashes(langParam[0]);
    }else if( document.domain == "www.pharmonia.sk" ){   
        var langParam = 'sk';
    }else{ 
        langParam = null;
    }
    
    if(langParam === null)
        value = values[0];
    else
        value = values[lang.indexOf(langParam)];    
    return value;   
}

/**
 * Efekt pro informacni okno loginboxu.
 */
function showLoginInfo(){
    element = $('info');
    
    if( element ){
        h3 = element.getChildren('h3');
        if( h3 ){
           var emptyText = h3.get('text')
        }
    }

    if( element ){
        var mySlide = new Fx.SlideExt('info',{
            duration: 1500,
            transition: Fx.Transitions.Pow.easeOut
        });
        mySlide.cancel();
        mySlide.hide()
    }

    loginForm = $('formPanel');
 
    if( loginForm && emptyText != ''  ){
           
        loginForm.addEvents({
            mouseenter: function(){
                if( mySlide ){
                    mySlide.cancel();
                    mySlide.slideIn();
                }
            },
            mouseleave: function(){
                if( mySlide ){
                    mySlide.cancel();
                    mySlide.slideOut();
                }
            }
        });
    }
}

function languageDomainHover(){
     element = $('vyber_domenu');

     if( element ){
        element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
        element.tween('opacity', 0);

        element.addEvents({
            mouseenter: function(){
                element = $('vyber_domenu');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
                    element.tween('opacity', 100);
                }
            },
            mouseleave: function(){
                element = $('vyber_domenu');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeIn, duration: 150});
                    element.tween('opacity', 0);
                }
            }
        });
    }

    domainSelect = $('vybrana_domena');

    /* -- odkomentovat --*/ if( domainSelect ){

        domainSelect.addEvents({
            mouseenter: function(){
                element = $('vyber_domenu');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
                    element.tween('opacity', 100);
                }
            },
            mouseleave: function(){
                element = $('vyber_domenu');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeIn, duration: 150});
                    element.tween('opacity', 0);
                }
            }
        });
    }/**/
}

function languageMutationHover(){
     element = $('vyber_mutaci');

     if( element ){
        element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
        element.tween('opacity', 0);

        element.addEvents({
            mouseenter: function(){
                element = $('vyber_mutaci');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
                    element.tween('opacity', 100);
                }
            },
            mouseleave: function(){
                element = $('vyber_mutaci');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeIn, duration: 150});
                    element.tween('opacity', 0);
                }
            }
        });
    }

    domainSelect = $('vybrana_mutace');

    if( domainSelect ){

        domainSelect.addEvents({
            mouseenter: function(){
                element = $('vyber_mutaci');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeOut, duration: 150});
                    element.tween('opacity', 100);
                }
            },
            mouseleave: function(){
                element = $('vyber_mutaci');
                if( element ){
                    element.set('tween', {transition: Fx.Transitions.Cubic.easeIn, duration: 150});
                    element.tween('opacity', 0);
                }
            }
        });
    }
}

function logoRotator( containerClass ){
    if( containerClass != '' ){
        var rotator = new LogoRotator( containerClass );
        
        if( rotator != null ){
            rotator.start();
        }
    }
}

window.addEvent('domready', function() {
    languageDomainHover();
    languageMutationHover();
    inputIndexSearch();
    inputLoginUsernameBox();
    inputLoginPasswordBox();
    inputLoginUsernameObsahBox();
    inputLoginPasswordObsahBox();
    inputLoginPasswordCompanyBox();
    showLoginInfo.delay(0,$('info'));
    inputDate();
    inputSchoolDate();
    logoRotator('telo_leve_banner_loga');

    if( $('outsourcing_calc') !== null ){
       var calc = new OutSourcingCalc('outsourcing_calc');
    }
});

