// Diese Datei als Script konfigurator_js.js abspeichern

function changeMaterial(id) {
    $('#color_line li').removeClass('active');  // alle Farben deaktivieren
    $('#color_line li#material_'+id).addClass('active'); // geklickte Farbe aktivieren

    var img=$('#color_line li#material_'+id+' span.detail_img').html(); // grosses Materialvorschaubild holen
    var topimg=$('#color_line li#material_'+id+' span.detail_topimg').html(); // Topbild holen
    var descr=$('#color_line li#material_'+id+' span.detail_descr').html();  // Detailbeschreibung holen

    $('#configurator_Detail img').attr('src','/'+PID+'/Schirmbezuege/cms/'+img); // grosses Materialvorschaubild anzeigen
    $('#img_topflash').attr('src','/'+PID+'/Serien_topbilder/cms/'+topimg);  // Topbild anzeigen
    $('#configurator_Detail p.detail_material_descr').html(descr); // Detailbeschreibung anzeigen
    $('#materialid').val(id);  // hidden field setzen

    // Update Schirme
    $.ajax({
            url: "/project/"+VID+"/customer_configurator/l0b0/konfigurator.html",
            data: {  "ac": "getForms", "katid":$('#katid').val(), "materialid": id, "formid":$('#formid').val()},
            cache: false,
            type: "POST",
            dataType: "json",
            success: function(data, textStatus, jqXHR) {
                // ersten LI Knoten holen
                var li=$('#form_line li:first').clone(true);
                // Alle Formen löschen
                $('#form_line li').remove();
                // LIs neu aufbauen
                $.each(data.forms, function(ndx,d) {
                        li.removeClass('active').attr('id','form_'+d.REC_ID).find('a').attr({'href':'javascript:changeForm('+d.REC_ID+')','title':(d.descr.length>0) ? d.descr : d.name});
                        li.find('img').attr('src','/'+PID+'/Schirmformen/cms/'+d.icon);                        
                        $('#form_line ul').append(li);
                        // Dieses LI klonen
                        li=$('#form_line li:last').clone(true);
                    }
                );
                //if(data.changeForm) 
                changeForm(data.changeForm);
                retrycount=0;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                if(retrycount<2) {
                    changeMaterial(id);
                    retrycount++;
                }
                if(show_error) alert('Error :'+errorThrown+' in changeMaterial ('+textStatus+')');
            }
    });

}
function changeForm(id) {
    $('#form_line li').removeClass('active');
    $('#form_line li#form_'+id).addClass('active');
    $('#formid').val(id);
    $.ajax({
            url: "/project/"+VID+"/customer_configurator/l0b0/konfigurator.html",
            data: {  "ac": "getForm", "formid":id, "materialid":$('#materialid').val()},
            cache: false,
            type: "POST",
            dataType: "json",
            success: function(data, textStatus, jqXHR) {
                if(data.error) {
                    alert(data.error);
                } else {
                    $('#schirm_preview').attr('src','/'+PID+'/Lampenschirme/cms/'+data.schirm_img);
                    $('#schirm_cache').attr('src','/'+PID+'/Lampenschirme/original/'+data.schirm_img);
                    $('#configurator_Detail p.detail_form_descr').html(data.form_descr);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                if(show_error) alert('Error :'+errorThrown+' in changeForm ('+textStatus+')');
            }
    });
}

function changeStand(id) {
    $('#stand_line li').removeClass('active');
    $('#stand_line li#stand_'+id).addClass('active');
    $('#standid').val(id);

    $.ajax({
            url: "/project/"+VID+"/customer_configurator/l0b0/konfigurator.html",
            data: {  "ac": "getStand", "standid":id},
            cache: false,
            type: "POST",
            dataType: "json",
            success: function(data, textStatus, jqXHR) {
                if(data.error) {
                    alert(data.error);
                } else {
                    $('#stand_preview').attr('src','/'+PID+'/Leuchtenfuesse/cms/'+data.fuss_img);
                    $('#stand_cache').attr('src','/'+PID+'/Leuchtenfuesse/original/'+data.fuss_img);
                    $('#configurator_Detail p.detail_stand_descr').html(data.stand_descr);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                if(show_error) alert('Error :'+errorThrown+' in changeStand ('+textStatus+')');
            }
    });
}

function changeDimmer(id) {
    $('#dimmer_line li').removeClass('active');
    $('#schalter_line li').removeClass('active');
    $('#dimmer_line li#dimmer_'+id).addClass('active');
    $('#schalter_line li#dimmer_'+id).addClass('active');
    $('#dimmerid').val(id);
}

function changeCable(id) {
    $('#cable_line li').removeClass('active');
    $('#cable_line li#cable_'+id).addClass('active');
    $('#cableid').val(id);
}

function toggleDimmerLine() {
    if($('#switch_dimmer').attr('checked')) {
        $('.schalter_visible').removeClass('schalter_visible').addClass('schalter_hidden');
        $('.dimmer_hidden').removeClass('dimmer_hidden').addClass('dimmer_visible');
    } else {
        $('.schalter_hidden').removeClass('schalter_hidden').addClass('schalter_visible');
        $('.dimmer_visible').removeClass('dimmer_visible').addClass('dimmer_hidden');
    }
    $('#cable_line').removeClass('dimmer_hidden').addClass('dimmer_visible');
}

function checkvals() {
    if($('#materialid').val()=='') {
        alert("Sie müsse erst einen Schirmbezug auswählen.");
        return false;
    } else if($('#formid').val()=='') {
        alert("Sie müssen erst eine Schirmform auswählen.");
        return false;
    } else if($('#standid').val()=='') {
        alert("Sie müsse erst einen Leuchtenfuß  auswählen.");
        return false;
    } else if($('input[@name=variante]:checked').length==0) {
        alert("Wählen Sie die Metallteile in matt oder glänzend aus.");
        return false;
    } else if($('[name="switch"]').length>0 && !$('#switch_switch').attr('checked') && !$('#switch_dimmer').attr('checked')) {
        alert("Wählen Sie entweder die Dimmer- oder Schaltervariante aus.");
        return false;
    } else if($('[name="switch"]').length>0 && $('#dimmerid').val()=='') {
        alert("Bitte wählen Sie eine Dimmer- oder Schalterfarbe aus.");
        return false;
    } else if($('#cable_line').length>0 && $('#cableid').val()=='') {
        alert("Bitte wählen Sie eine Kabelfarbe aus.");
        return false;
    } else {
        return true;
    }
}

var scroller;
var scrollinterval;
var scroller_init_done=false;
var prev_box;
var prev_x=0;
var prev_y=0;
var big_x=0;
var big_y=0;
var ratio_x=0;
var ratio_y=0;
var offset;
var retrycount=0;

    //var lupe=$('#lupe');

function init_scroller() {
    if(!scroller_init_done) {
        scroller=$('#configurator_detail_zoom');
        prev_box=$('#configurator_detail_preview_boundingbox');
        prev_x=prev_box.width();
        prev_y=prev_box.height();
        big_x=$('#zoom_schirm').width();
        big_y=$('#zoom_schirm').height()+$('#zoom_fuss').height();
        ratio_x= (big_x-scroller.width()) / prev_x;
        ratio_y= (big_y-scroller.height()) / prev_y;
        offset=prev_box.offset();

        prev_box.mousemove(
            function(e) {
                //lupe.offset({left:e.pageX,top:e.pageY});
                scroller.scrollTop((e.pageY-offset.top) * ratio_y);
                scroller.scrollLeft((e.pageX-offset.left) * ratio_x);
            }
        );

        scroller.scrollLeft(450);
        scroller.scrollTop(500);
        scroller_init_done=true;
    } else {
        if(scrollinterval) window.clearInterval(scrollinterval);
    }
}


