$(document).ready(function() {
    var selected_company = null;
    var selected_wood = null;
    var selected_style = null;
    var selected_color = null;
    var position = -1;
    


    $.getJSON('../../include/cabinettool.php?get=company', function(data) {
        $.each(data, function(i, item){
            $("<option/>").attr({
                value: item.id
            }).text(item.name).appendTo("#company");        
        });
        $("#company").change(function(){
            selected_company = $(this).val();
            reload_selected_boxes(3);
            fillSamples();
        });
        $("#company").val(6);
        selected_company = 6;
    });    
    $.getJSON('../../include/cabinettool.php?get=wood', function(data) {
        $.each(data, function(i, item){
            $("<option/>").attr({
                value: item.id
            }).text(item.name).appendTo("#wood");        
        });
        $("#wood").change(function(){
            selected_wood = $(this).val();
            if(selected_company > 0) reload_selected_boxes(2);
            fillSamples();
        });
    }); 
    $.getJSON('../../include/cabinettool.php?get=style', function(data) {
        $.each(data, function(i, item){
            $("<option/>").attr({
                value: item.id
            }).text(item.name).appendTo("#style");        
        });
        $("#style").change(function(){
            selected_style = $(this).val();
            if(selected_company > 0 && selected_wood > 0) reload_selected_boxes(1);
            fillSamples();
        });
    }); 
    $.getJSON('../../include/cabinettool.php?get=color', function(data) {
        $.each(data, function(i, item){
            $("<option/>").attr({
                value: item.id
            }).text(item.name).appendTo("#color");        
        });
        $("#color").change(function(){
            selected_color = $(this).val();
            fillSamples();
        });
    }); 
    
    var reload_selected_boxes = function(what) {
        if(what > 2)
        $.getJSON('../../include/cabinettool.php?get=wood&company='+selected_company, function(data) {
            $("#wood")[0].length = 0;
            $("#wood")[0][0] = new Option('All', '0');
            $.each(data, function(i, item){
                $("<option/>").attr({
                    value: item.id
                }).text(item.name).appendTo("#wood");        
            });
        }); 
        if(what > 1)
        $.getJSON('../../include/cabinettool.php?get=style&company='+selected_company+'&wood='+selected_wood, function(data) {
            $("#style").empty();
            $("#style")[0][0] = new Option('All', '0');
            $.each(data, function(i, item){
                $("<option/>").attr({
                    value: item.id
                }).text(item.name).appendTo("#style");        
            });
        }); 
        if(what > 0)
        $.getJSON('../../include/cabinettool.php?get=color&company='+selected_company+'&wood='+selected_wood+'&style='+selected_style, function(data) {
            $("#color").empty();
            $("#color")[0][0] = new Option('All', '0');
            $.each(data, function(i, item){
                $("<option/>").attr({
                    value: item.id
                }).text(item.name).appendTo("#color");        
            });
        }); 
    }   
    
    var fillSamples = function() {
             $('.cabinet_content').empty();
             $(".cabinet_content").fadeIn('fast');
             $('.cabinet_content').html("Loading");
             $(".search").fadeIn('fast');

         $.getJSON('../../include/cabinettool.php?company='+selected_company+'&wood='+selected_wood+'&style='+selected_style+'&color='+selected_color, function(data) {
             $('.cabinet_content').empty();
             if(data.length > 0) {
                 $.each(data, function(i, item){
                    $("<img/>").attr({
                        src: '../../upload/new_cabinet/'+item.id+'.jpg',
                        id: item.id     
                    }).addClass("image").appendTo(".cabinet_content");        
                });
             } else {
                $('.cabinet_content').html("No Results Found.");    
             }
             
            
            
            $(".cabinet_content").children().each(function(){
                $(this).click(function(){
                    if($('#'+$(this).attr('id')+'_big').length > 0) {
                        return false;
                    }
                    $(this).css({
                        opacity: '0.5'
                    });
                    var div = $("<div/>").attr("id", $(this).attr('id') + '_big');
                    var span = $("<span/>").addClass('close_button').click(function(){
                        temp_id = $(this).parent().attr('id').split('_');
                        $(this).parent().remove();
                        $('#'+temp_id[0]).css({
                            opacity: '1'   
                        });
                    }).appendTo(div);            
                    var img = $("<img/>");
                    img.attr({
                        src: $(this).attr('src')
                    }).appendTo(div);
                    div.appendTo("#photo_sets_container").addClass("image_large").draggable({ 
                        containment: '#photo_sets_container',
                        stack: '#photo_sets_container div' 
                    });
                });    
            });
        });
               
    };
    
    
    
    $('#cabinettool').children('.title').toggle(function() {
        $(this).parent().animate({
            height: '340px'
        }, 500,  
        function() {
            fillSamples();     
        })
    }, function() {
        $(".cabinet_content img").fadeOut('fast');
        $(".search").hide();
        $(".cabinet_content").hide();
        $(this).parent().animate({
            height: '30px'
        }, 500)
    });
});
