// JavaScript Document

$(document).ready(function(){
    
    //updateLibraryContent(0);
    
    /*$("ul#library-icons li").mouseover(function(){
        if(!$(this).hasClass("disabled"))
        {
            buttonFocus(this.id);
        } 
    });*/
    
    /*$("ul#library-icons li").mouseout(function(){
        buttonRemoveFocus(this.id);
    });*/
    
    
    /*$("ul#library-icons > li#parentButton").click(function(){

        var parentDir = $("div#library-manager > div.browse").attr("id").split("-")[1];

        if(parentDir != "")
        {
            updateLibraryContent(parentDir);
        }
    });*/
       
    
    /*$("ul#library-icons > li#downloadButton").click(function(){

        if(!$(this).hasClass("disabled"))
        {
            window.location.href = "/admin/library/download.php?file=" + $("div.active").attr("id").split("-")[1] + "";
        }
    });*/
    
    
    /*$("ul#library-icons > li#renameButton").click(function(e){

        e.preventDefault();

        if($(this).hasClass("file"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            renameFile("", function () {
                window.location.href = '';
                    });
        }
        else if($(this).hasClass("dir"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            renameDir("", function () {
                window.location.href = '';
                    });
        }
        return false;

    });*/

    /*$("ul#library-icons > li#moveButton").click(function(e){

        e.preventDefault();

        if($(this).hasClass("file"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            moveFile("", function () {
                window.location.href = '';
            });
        }
        else if($(this).hasClass("dir"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            moveDir("", function () {
                window.location.href = '';
            });
        }
        return false;
    });*/

    /*$("ul#library-icons > li#deleteButton").click(function(e){

        e.preventDefault();

        if($(this).hasClass("file"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            deleteFile("", function () {
                window.location.href = '';
            });
        }
        else if($(this).hasClass("dir"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            deleteDirectory("", function () {
                window.location.href = '';
            });
        }
        return false;

    });*/
    
    /*$("ul#library-icons > li#permissionButton").click(function(e){
    
        e.preventDefault();
    
        if($(this).hasClass("file"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            permissionFile("", function () {
                window.location.href = '';
            });
        }
        else if($(this).hasClass("dir"))
        {
            // example of calling the confirm function
            // you must use a callback function to perform the "yes" action
            permissionDirectory("", function () {
                window.location.href = '';
            });
        }
        return false;
    
    });*/
    
    /*$("ul#library-icons > li#createButton").click(function(e){

        e.preventDefault();

        // example of calling the confirm function
        // you must use a callback function to perform the "yes" action
        createDirectory("", function () {
                window.location.href = '';
        });
        return false;
    });*/

     /*$("ul#library-icons > li#uploadButton").click(function(e){

        e.preventDefault();

        // example of calling the confirm function
        // you must use a callback function to perform the "yes" action
        uploadFile("", function () {
                window.location.href = '';
        });
        return false;
    });*/


}); 

/*function buttonEnable(id, icon, type)
{
    $("ul#library-icons > li#" + id + " > img").attr({
        src :   "/themes/default/graphics/admin/library/buttons/" + icon + ".png"
    });
    
    $("ul#library-icons > li#" + id + "").removeClass("disabled file dir");
    $("ul#library-icons > li#" + id + "").addClass(type);
}*/

/*function buttonDisable(id, icon, type)
{
    //disable
    $("ul#library-icons > li#" + id + " > img").attr({
        src :   "/themes/default/graphics/admin/library/buttons/" + icon + "-disabled.png"
    });
    
    $("ul#library-icons > li#" + id + "").removeClass("file dir");
    $("ul#library-icons > li#" + id + "").addClass("disabled");
}*/


/*function buttonFocus(icon)
{
    $("ul#library-icons > li#" + icon).addClass("hover");
}*/

/*function buttonRemoveFocus(icon)
{
    $("ul#library-icons > li#" + icon).removeClass("hover");
}*/

/* ======================================================================= */

function updateLibraryContent(directory)
{
    $.ajax({
        type    :   "GET",
        url     :   "/library/browse.php",
        cache   :   false,
        data    :   "directory=" + directory + "",
        success :   function(html){
            $("div#fileshare-window").html(html);
            
            /*if(directory > 0)
            {
                buttonEnable("parentButton", "parent", "dir");
            }
            else
            {
                buttonDisable("parentButton", "parent", "dir");
            }*/
            
            //disable buttons [rename|move]
            /*buttonDisable("downloadButton", "download", "file");
            buttonDisable("moveButton", "move", "dir");
            buttonDisable("moveButton", "move", "file");
            buttonDisable("renameButton", "rename", "dir");
            buttonDisable("renameButton", "rename", "file");
            buttonDisable("deleteButton", "delete", "dir");
            buttonDisable("deleteButton", "delete", "file");
            buttonDisable("permissionButton", "permission", "dir");
            buttonDisable("permissionButton", "permission", "file");
            buttonDisable("createButton", "create-dir", "dir");
            buttonDisable("uploadButton", "upload", "file");*/            
            
            /*if($("div#library-manager > div").hasClass("dir-writable"))
            {
                buttonEnable("createButton", "create-dir", "dir");
            }*/
            
            /*if($("div#library-manager > div").hasClass("file-writable"))
            {
                buttonEnable("uploadButton", "upload", "file");
            }*/

        }
    });
}

function updateLibraryContentFileInfo(parent, file)
{
    $.ajax({
        type    :   "GET",
        url     :   "/admin/library/browse.php",
        cache   :   false,
        data    :   "directory=" + parent + "",
        success :   function(html){
            $("div#fileshare-window").html(html);
            //highlight line on post back...
            $("div#document-" + file + "").addClass("active");
            //get document info
            getFileInfo(file);
        }
    });
}

function updateLibraryContentDirInfo(parent, directory)
{
    $.ajax({
        type    :   "GET",
        url     :   "/admin/library/browse.php",
        cache   :   false,
        data    :   "directory=" + parent + "",
        success :   function(html){
            $("div#fileshare-window").html(html);
            //highlight line on post back...
            $("div#directory-" + directory + "").addClass("active");
            //get directory info...
            getDirectoryInfo(directory);
        }
    });
}

function getFileInfo(document)
{

    $.ajax({
        type:       "GET",
        url:        "/admin/library/file-info.php",
        cache:      false,
        data:       "document=" + document + "",
        success:    function(html)
                    {
                        $("div#information").html(html);
                    }
        });
}

function getDirectoryInfo(directory)
{
    $.ajax({
        type:       "GET",
        url:        "/admin/library/directory-info.php",
        cache:      false,
        data:       "directory=" + directory + "",
        success:    function(html)
                    {
                        $("div#information").html(html);
                    }
    });       
        
}

function createDirectory(message, callback) {
    $('div#create-dir').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'createDirModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });
}

function uploadFile(message, callback) {
        $('div#upload-file').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'uploadFileModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                            // call the callback
                            if ($.isFunction(callback)) {
                                    callback.apply();
                            }
                            // close the dialog
                            $.modal.close();
                    });

              //if the user clicks "no"
              dialog.data.find('.no').click(function() {
            //close the dialog
            $.modal.close();
          });
                }
        });
}

function renameFile(message, callback) {
    $('div#rename-file').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'createDirModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });
}

function renameDir(message, callback) {
    $('div#rename-dir').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'createDirModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });
}

function moveFile(message, callback) {
    $('div#move-file').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'moveModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });
}

function moveDir(message, callback) {
    $('div#move-dir').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'moveModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });
}

function permissionFile(message, callback) {
    $('div#permission-file').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'permissionModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });

}

function permissionDirectory(message, callback) {
    $('div#permission-dir').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'permissionModalContainer',
            onShow: function (dialog) {
                    dialog.data.find('.message').append(message);

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        if ($.isFunction(callback)) {
                            callback.apply();
                        }
                        // close the dialog
                        $.modal.close();
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });

}

function deleteFile(document, parent) {
    $('div#delete-file').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'deleteModalContainer',
            onShow: function (dialog) {

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        /*if ($.isFunction(callback)) {
                            callback.apply();
                        }*/
                        
                        //DO AJAX CALL - ROB
                        $.ajax({
                            type    :   "GET",
                            url     :   "/admin/library/delete-file.php",
                            cache   :   false,
                            data    :   "document=" + document + "&parent=" + parent + "",
                            success :   function(html){
                                    $("div#delete-form").html(html);
                                }
                        });
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });

}

function deleteDirectory(directory, parent) {
    $('div#delete-dir').modal({
            close:false,
            overlayId:'fileshareModalOverlay',
            containerId:'deleteModalContainer',
            onShow: function (dialog) {

                    // if the user clicks "yes"
                    dialog.data.find('.yes').click(function () {
                        // call the callback
                        /*if ($.isFunction(callback)) {
                            callback.apply();
                        }*/
                        
                        //DO AJAX CALL - ROB
                        $.ajax({
                            type    :   "GET",
                            url     :   "/admin/library/delete-directory.php",
                            cache   :   false,
                            data    :   "directory=" + directory + "&parent=" + parent + "",
                            success :   function(html){
                                    $("div#delete-form").html(html);
                                }
                        });
                    });

                    //if the user clicks "no"
                    dialog.data.find('.no').click(function() {
                        //close the dialog
                        $.modal.close();
                    });
            }
    });

}

//Move to master JS
function UCFirst(str){
   // split string
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);

   // convert case
   firstChar = firstChar.toUpperCase(); 
   remainChar = remainChar.toLowerCase();

   return firstChar + remainChar

}
