(function ($) {

    $.fn.imageList = function (options) {
        var opts = $.extend({}, $.fn.imageList.defaults, options);
        $.getJSON(opts.connector, null, function (result) {
            //if (result.hasOwnProperty('url')) {
            //    console.log('Y');
            //}
            //else { console.log('N'); }
            var imgconn = '';
            for (let i = 0; i < result.length; i++) {
                if (result[i].url) {
                    imgconn = $("<ul>").attr({
                        "class": "thumbnails"
                    }).appendTo("#" + opts.imgcontainer);
                    $("#ddlindex").val('Load More..');
                    break;
                } else { $("#ddlindex").val('No Data found'); }
               
            }
            var htmldata = '';
            var imglist = '';
            var imglink = '';
            var i = 0;
            $.each(result, function (i, field) {
                if (field.type != "Folder") {

                     imglist = $('<li />').appendTo(imgconn);
                     imglink = $("<a href='#' title='Select' onclick='apply(\"" + field.name + "\", 1)' />").appendTo(imglist);

                     var imageurl= '';
                     if (field.type == "image/gif" || field.type == "image/jpeg" || field.type == "image/jpg" || field.type == "image/png") {
                         imageurl = field.url;
                     }
                     else if(field.type =="application/vnd.ms-excel" || field.type=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || field.type=="application/vnd.ms-excel") {
                         imageurl = "/Areas/admin/content/img/excel.jpg";
                     }
                     else if(field.type =="application/msword" || field.type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
                         imageurl = "/Areas/admin/content/img/word.png";
                     }
                     else if(field.type =="application/pdf") {
                         imageurl = "/Areas/admin/content/img/pdf.png";
                     }
                     else if(field.type =="application/vnd.ms-powerpoint" || field.type=="application/vnd.openxmlformats-officedocument.presentationml.presentation") {
                         imageurl = "/Areas/admin/content/img/ppt.png";
                     }
                     else
                     {
                         imageurl = field.url;
                     }
                     $("<img />").attr({
                         id: "image-" + field.name,
                         src: imageurl,
                         "data-src": field.url,
                         title: field.name,
                         "class": "thumbnail"
                     }).appendTo(imglink);

                    $('<span class="spanfiletitle">' + field.name + '</span>').appendTo(imglink);
                }
                if (field.type == "Folder") {
                    htmldata += "<li><input type='button' class='btn btn-link' value=\"" + field.name.trim() + "\" onclick='getFloderData(\"" + field.name.trim() + "\" )' /></li>";
                    //$("#floderlist").appendTo('<input type="button" value="' + field.name + '" />');
                }

            });
            $("#floderlist").html("<ol>"+ htmldata +"</ol>");
        });
        
    };

    $.fn.imageList.defaults = {
        connector: null,
        imgcontainer: ""
    };


    
}(jQuery));