/**
 * Preisverwalung für Inputfelder
 *
 * @copyright  Copyright (c) 2009 M&M Online Service - www.melster-melster.de
 */

jQuery.fn.extend({

    MMOS: {
        option_nummer_breite: function(){ return 159; },
        option_nummer_farbe: function(){ return 160; },
        option_nummer_hoehe: function(){ return 161; },
        option_nummer_antriebsart: function(){ return 164; },
        option_nummer_antriebsseite: function(){ return 163; },
        option_nummer_montageart: function(){ return 162; },
        option_nummer_gestellfarbe: function(){ return 165; },
        option_nummer_ral: function(){ return 167; },

        option_nummer_standard_antriebsart_kurbel: function(){ return 309; },
        option_nummer_standard_antriebsseite_links: function(){ return 2; },
        option_nummer_standard_gestellfarbe_ral: function(){ return 4; },
        option_nummer_standard_gestellfarbe_ral_value: function(){ return 312; },
        option_nummer_standard_montageart_wand: function(){ return 303; },

        maxbreiteDetail: function() { return 600; },

        ausfall: function() { return (new Function( "return( " + jQuery('#mmos_ausfallmasse').val() + " );" ))(); },

        minbreite: function(){
            var tmp = jQuery().MMOS.ausfall();
            if(jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val() == '' || tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['minbreite'] == 'undefined') {
                return 0;
            }
            return tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['minbreite'];
        },
        maxbreite: function(){
            var tmp = jQuery().MMOS.ausfall();
            if(jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val() == '' || tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['maxbreite'] == 'undefined') {
                return 0;
            }
            return tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['maxbreite'];
        },
        getAusfall: function(){
            var tmp = jQuery().MMOS.ausfall();
            if(jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val() == '' || tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['cm'] == 'undefined') {
                return 0;
            }
            return tmp[jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val()]['cm']
        },
        loadPreisFromMasse: function(){
            var date = new Date();
            jQuery('#mmos_produktpreis').load(jQuery('#mmos_preis_url').val(), { p: jQuery('#mmos_product_id').val(), x: jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val(), y: jQuery().MMOS.getAusfall(), t: date.getTime() }, function(responseText){
                jQuery().MMOS.setPreis(jQuery().MMOS.option_nummer_breite(), responseText, null);
            });

            jQuery('#mmos_preis').val(jQuery('#mmos_produktpreis').text());
        },
        setPreis:function(element_id, price, element_id2){
            if(element_id2 != null){
                opConfig.config[element_id][element_id2] = new Number(price);
            } else {
                opConfig.config[element_id] = new Number(price);
            }
            opConfig.reloadPrice();
        }
    }

});

jQuery(document).ready(function(){

    var check_hoehe = false;
    var check_breite = false;

    jQuery(".product-custom-option").each(function() {

        if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_hoehe()+']' /* Hoehe/Ausfall Markise */) {

            jQuery(this).blur(function(){

                var date = new Date();

                if(jQuery(this).val().length == 0) {
                    check_hoehe = false;
                } else {
                    if(/^[0-9]+$/.test(jQuery().MMOS.getAusfall()) == false) {
                        check_hoehe = false;
                    } else {

                        if(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() > 0) {
                            if(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() >= jQuery().MMOS.minbreite() && jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() <= jQuery().MMOS.maxbreite()) {
                                check_hoehe = true;
                            } else {
                                jQuery.noticeAdd({ text: jQuery().MMOS_LANG.breite_min1()+jQuery().MMOS.minbreite()+jQuery().MMOS_LANG.breite_min2()+jQuery().MMOS.maxbreite()+jQuery().MMOS_LANG.breite_min3(), stay: false, stayTime: 4000, type: 'error' });
                                jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').parent('dd').prev().click();
                                check_hoehe = false
                            }
                        } else {
                            check_hoehe = false;
                        }

                        if(check_hoehe == true) {
                            jQuery().MMOS.loadPreisFromMasse();
                        }
                    }
                }

                return false;
            });

        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_breite()+']' /* Breite Markise */) {

            jQuery(this).before('<input type="text" id="mmos_preis" style="display: none;" name="mmos_preis" value="" />');
            jQuery(this).before('<div id="mmos_produktpreis"></div>');
            jQuery('#mmos_produktpreis').css({'position':'absolute', 'left':-1000});

            jQuery(this).blur(function(){
                var date = new Date();
                if(jQuery(this).val().length == 0) {
                    check_breite = false;
                } else {
                    if(/^[0-9]+$/.test(jQuery(this).val()) == false) {
                        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.angabe_nicht_korrekt(), stay: false, stayTime: 6000, type: 'error' });
                        check_breite = false;
                    } else {

                        if(jQuery().MMOS.getAusfall() > 0) {
                            if(jQuery(this).val() >= jQuery().MMOS.minbreite() && jQuery(this).val() <= jQuery().MMOS.maxbreite()) {
                                check_breite = true;
                            } else {
                                jQuery.noticeAdd({ text: jQuery().MMOS_LANG.breite_min1()+jQuery().MMOS.minbreite()+jQuery().MMOS_LANG.breite_min2()+jQuery().MMOS.maxbreite()+jQuery().MMOS_LANG.breite_min3(), stay: false, stayTime: 4000, type: 'error' });
                                check_breite = false
                            }
                        } else {
                            check_breite = false;
                        }

                        if(check_breite == true) {
                            jQuery().MMOS.loadPreisFromMasse();
                        }
                    }
                }

                return false;
            });

        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_farbe()+']' /* Stoffe und Farben */) {

            var date = new Date()
            var inputid = jQuery(this).attr('id');

            jQuery(this).before('<input type="button" name="stoff_btn" class="buttonstyle" id="stoff_btn" value="'+jQuery().MMOS_LANG.farbe_waehlen()+'"/>');
            jQuery(this).css({'position':'absolute', 'left':-1000});
            jQuery(this).after('<img src="/media/spacer.gif" class="stoffbilder" id="stoff_thumb" align="center" width="150" height="56" alt=" " border="0" />');
            jQuery('#stoff_thumb').css({'margin-top': '15px'});

            jQuery('#stoff_btn').click(function(){
                jQuery('#mmos_farben_stoff').overlay({

                    // some expose tweaks suitable for modal dialogs
                    expose: {
                        color: '#fff',
                        loadSpeed: 200,
                        opacity: 0.8
                    },

                    closeOnClick: false,
                    api: true
                }).load();

                var buttons = jQuery("img.stoffbilder").click(function(e) {
                    var pfad = jQuery(this).attr('src').split('/');
                    var name = pfad[pfad.length-1];
                    var ral = name.split('.');

                    jQuery('#options_'+jQuery().MMOS.option_nummer_farbe()+'_text').val(ral[0]);
                    jQuery('#stoff_thumb_txt').remove();
                    jQuery('#stoff_thumb').after('<span id="stoff_thumb_txt">'+ral[0]+'</span>');
                    jQuery('#stoff_thumb').attr('src', jQuery(this).attr('src'));
                    jQuery('#stoff_thumb').attr('alt', jQuery(this).attr('alt'));
                    jQuery(this).qtip('hide');
                    jQuery('#mmos_farben_stoff').overlay().close();
                });

                jQuery('div.stoffe_farben_tabs_content div').hide();

                jQuery(".stoffe_farben_tabs li").live('click', function(){
                    jQuery('div.stoffe_farben_tabs_content div').hide();
                    jQuery(".stoffe_farben_tabs li").removeClass('activ_tab');
                    jQuery(this).addClass('activ_tab');
                    jQuery('#'+jQuery(this).attr('id')+'_content').show();
                });

                jQuery('.stoffe_farben_tabs li:first').click();

            });

        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_antriebsart()+']' /* Antriebsart */) {

            jQuery(this).children('option').each(function(i){
                if(jQuery(this).val() == jQuery().MMOS.option_nummer_standard_antriebsart_kurbel()) {
                    jQuery(this).attr("selected", "selected");
                }
            });

        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_ral()+']' /* Eigene Gestellfarbe RAL */) {

            var dd = jQuery(this).parent('dd');
            var dt = jQuery(dd).prev();

            var ral_html = jQuery(dd).html();
            var ral_ueberschrift_html = jQuery(dt).html();

            jQuery(dd).remove();
            jQuery(dt).qtip('disable').remove();

            jQuery('#options-'+jQuery().MMOS.option_nummer_gestellfarbe()+'-list').append('<li>'+ral_html+'</li>');
            jQuery('#options_'+jQuery().MMOS.option_nummer_gestellfarbe()+'_'+jQuery().MMOS.option_nummer_standard_gestellfarbe_ral()).change(function(){
                jQuery('#options_'+jQuery().MMOS.option_nummer_ral()+'_text').focus();
            });

        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_antriebsseite()+']' /* Antriebsseite */) {
                jQuery('#options_'+jQuery().MMOS.option_nummer_antriebsseite()+'_'+jQuery().MMOS.option_nummer_standard_antriebsseite_links()).attr("checked", "checked");
        } else if(jQuery(this).attr('name') == 'options['+jQuery().MMOS.option_nummer_montageart()+']' /* Montageart */) {
            jQuery(this).children('option').each(function(i){
                if(jQuery(this).val() == jQuery().MMOS.option_nummer_standard_montageart_wand()) {
                    jQuery(this).attr("selected", "selected");
                }
            });

        }
    });
});

//Pruefen der angaben vor dem senden
function MMOS_validate(){

    //Hoehe pruefen
    if(jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).val() < 1) {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.hoehe_erforderlich(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#select_'+jQuery().MMOS.option_nummer_hoehe()).parent('dd').prev().click();
        return false;
    }

    //Breite pruefen
    if(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val().length == 0) {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.breite_angeben(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').parent('dd').prev().click();
        return false;
    }
    if(/^[0-9]+$/.test(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val()) == false) {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.angabe_nicht_korrekt(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').parent('dd').prev().click();
        return false;
    }
    if(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() < jQuery().MMOS.minbreite() || jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() > jQuery().MMOS.maxbreite()) {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.breite_min1() + jQuery().MMOS.minbreite() + jQuery().MMOS_LANG.breite_min2() + jQuery().MMOS.maxbreite() + jQuery().MMOS_LANG.breite_min3(), stay: false, stayTime: 6000, type: 'error' });
        jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').parent('dd').prev().click();
        return false;
    }

    if(jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').val() > 400 && jQuery('#options_'+jQuery().MMOS.option_nummer_hoehe()+'_text').val() > 400) {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.keine_fertigung(), stay: false, stayTime: 6000, type: 'error' });
        jQuery('#options_'+jQuery().MMOS.option_nummer_breite()+'_text').parent('dd').prev().click();
        return false;
    }

    //Antriebsart pruefen
    if(jQuery('#select_'+jQuery().MMOS.option_nummer_antriebsart()).val() < 1){
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.angabe_nicht_korrekt(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#select_'+jQuery().MMOS.option_nummer_antriebsart()).parent('dd').prev().click();
        return false;
    }

    //Farbe und Muster pruefen
    if(jQuery('#options_'+jQuery().MMOS.option_nummer_farbe()+'_text').val().length == 0){
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.farbe_erforderlich(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#options-'+jQuery().MMOS.option_nummer_farbe()+'-list').parent('dd').prev().click();
        return false;
    }

    //Gestellfarbe pruefen
    if(jQuery('#options-'+jQuery().MMOS.option_nummer_gestellfarbe()+'-list li input:radio:checked').size() == 0){
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.angabe_erforderlich(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#options-'+jQuery().MMOS.option_nummer_gestellfarbe()+'-list').parent('dd').prev().click();
        return false;
    }

    if(jQuery('#options-'+jQuery().MMOS.option_nummer_gestellfarbe()+'-list li input:radio:checked').val() == jQuery().MMOS.option_nummer_standard_gestellfarbe_ral_value() && jQuery('#options_'+jQuery().MMOS.option_nummer_ral()+'_text').val() == '') {
        jQuery.noticeAdd({ text: jQuery().MMOS_LANG.angabe_ral_erforderlich(), stay: false, stayTime: 4000, type: 'error' });
        jQuery('#options_'+jQuery().MMOS.option_nummer_ral()+'_text').parent('dd').prev().click();
        return false;
    }

    jQuery('#mmos_preis').val(jQuery('#mmos_produktpreis').text());

    return true;
}
