$(document).ready( function() {
    $("a.imageList").button( {icons: {primary: "ui-icon-image"}} );
    $("a.optionList").button( {icons: {primary: "ui-icon-circlesmall-plus"}} );
    $("a.delete").button( {icons: {primary: "ui-icon-trash"}} );
    $("a.new").button( {icons: {primary: "ui-icon-plus"}});
} );

function makeChange() {
    var make = $('#make').val();
    if(make == -1) {
        newMake();
    } else {
        $.getJSON('/admin/get/modellist/'+$('#make').val(), function(data) {
            $('#model').empty().append( new Option('- selecteer model -',0));
            $.each(data, function(val, text) {
                $('#model').append( new Option(text,val) );
            });
            disableType();
        });
    }
    $('#model').removeAttr('disabled');
}

function optionList(id) {
    $("#options").dialog( {
        height: 480, width: 640,
        modal: true,
        buttons: {
            "Opties aan occasion toevoegen": function() {
                $("#optionsform #id").val(id);
                $("#optionsform").submit();
            },
            "Nieuwe optie toevoegen": function() {
                var opt = $("#newoption").val();
                $.ajax({
                    type: "POST",
                    url: "/admin/post/newoption/"+opt,
                    data: '',
                    success: function(data) {
                        $("#newoption").val('');
                        location.reload();
                    }
                });
            },
            "Annuleren": function() {
                $(this).dialog("close");
            }
        }
    } );
}

function imageList(id) {
    $("#files").val('');
    $("#id").val(id);
    $("#photos *").show();
    $("#photos").dialog( {
        height: 300, width: 400,
        modal: true,
        buttons: {
            "Uploaden": function() {
                $("#photoform").submit();
            ///    $('#other').click(function() {
            //    $('#target').submit();
            },
            "Annuleren": function() {
                $(this).dialog("close");
            }
        }
    } );
}

function newMake() {
    $("#popup label").html('Merk: ');
    $("#popup input").val('');
    $( "#popup" ).attr("title","Merk toevoegen aan database").dialog( {
        height: 200, width: 400,
        modal: true,
        buttons: {
            "Merk toevoegen": function() {
                var make = $("input#prop").val();
                $.ajax({
                    type: "POST",
                    url: "/admin/post/make/"+make,
                    data: '',
                    success: function(data) {
                        $('#make').empty().append( new Option('- selecteer merk -'));
                        $.each(data['list'], function(val, text) {
                            $('#make').append( new Option(text, val) );
                        });
                        $('#make').val(data['active']);
                    }
                });
                $(this).dialog("close");
            },
            "Annuleren": function() {$(this).dialog("close");}
        }
    });
}

function modelChange() {
    var model = $('#model').val();
    if ( model == -1 ) {
        newModel();
    } else {
        $.getJSON('/admin/get/typelist/'+$('#model').val(), function(data) {
            $('#type').empty().append( new Option('- selecteer type -', 0));
            $.each(data, function(val, text) {
                $('#type').append( new Option(text,val) );
            });
        });
    }
    $('#type').removeAttr('disabled');
}

function newModel() {
    $("#popup label").html('Model: ');
    $("#popup input").val('');
    $( "#popup" ).attr("title","Model toevoegen aan database").dialog( {
        height: 200, width: 400,
        modal: true,
        buttons: {
            "Model toevoegen": function() {
                var make = $("#make").val();
                var model = $("input#prop").val();
                $.ajax({
                    type: "POST",
                    url: "/admin/post/model/"+model+'/make_id/'+make,
                    data: '',
                    success: function(data) {
                        $('#model').empty().append( new Option('- selecteer model -'));
                        $.each(data['list'], function(val, text) {
                            $('#model').append( new Option(text, val) );
                        });
                        $('#model').val(data['active']);
                    }
                });
                $(this).dialog("close");
            },
            "Annuleren": function() {$(this).dialog("close");}
        }
    });
}

function disableType() {
    $('#type').attr('disabled','disabled').empty()
        .append(new Option('- selecteer type -', 0))
        .append(new Option('type toevoegen ...', -1));
}

function typeChange() {
    var type = $('#type').val();
    if(type == -1) {
        newType();
    }
}

function newType() {
    $("#popup label").html('Type: ');
    $("#popup input").val('');
    $( "#popup" ).attr("title","Type toevoegen aan database").dialog( {
        height: 200, width: 400,
        modal: true,
        buttons: {
            "Type toevoegen": function() {
                var model = $("#model").val();
                var type = $("input#prop").val();
                $.ajax({
                    type: "POST",
                    url: "/admin/post/type/"+type+'/model_id/'+model,
                    data: '',
                    success: function(data) {
                        $('#type').empty().append( new Option('- selecteer type -'));
                        $.each(data['list'], function(val, text) {
                            $('#type').append( new Option(text, val) );
                        });
                        $('#type').val(data['active']);
                    }
                });
                $(this).dialog("close");
            },
            "Annuleren": function() {$(this).dialog("close");}
        }
    });
}

function colorChange() {
    var color = $('#color').val();
    if(color == -1) {
        newColor();
    }
}

function newColor() {
    $("#popup label").html('Kleur: ');
    $("#popup input").val('');
    $( "#popup" ).attr("title","Kleur toevoegen aan database").dialog( {
        height: 200, width: 400,
        modal: true,
        buttons: {
            "Kleur toevoegen": function() {
                var color = $("input#prop").val();
                $.ajax({
                    type: "POST",
                    url: "/admin/post/color/"+color,
                    data: '',
                    success: function(data) {
                        $('#color').empty().append( new Option('- selecteer kleur -'));
                        $.each(data['list'], function(val, text) {
                            $('#color').append( new Option(text, val) );
                        });
                        $('#color').val(data['active']);
                    }
                });
                $(this).dialog("close");
            },
            "Annuleren": function() {$(this).dialog("close");}
        }
    });
}

function deleteOccasion(id) {
    $.ajax({
        type: "POST",
        url: "/admin/post/delete/"+id,
        data: '',
        success: function() {
            location.reload();
        }
    });
}

function setSold(el, id) {
    $(el).attr('disabled', 'disabled');
    var sold = 0;
    if ( $(el).attr('checked') ) {
        sold = 1;
    }
    $.ajax( {
        type: "POST",
        url: "/admin/post/sold/"+sold+"/id/"+id,
        data: '',
        success: function() {
            $(el).removeAttr('disabled');
        }
    });
}

