$(document).ready(function () { 
    /*$( ".glow" ).mouseover( function() {  
        $( this ).animate({  
            opacity: 1  
        }, 200, "swing", function() {  
            });  
    }).mouseout( function() {  
        $( this ).animate({  
            opacity: 0.35  
        }, 200, "swing", function() {  
            });
    });*/
    /*$( ".glow" ).mouseover( function() {  
        $( this ).fadeTo(250, 1)  
    }).mouseout( function() {  
        $( this ).fadeTo(250, 0.35)
    });*/
    
    
    
    /*$('.glow').bind('mouseover', function() {
        $( this ).fadeTo(250, 1)
    });
    $('.glow').bind('mouseout', function() {
        $( this ).fadeTo(250, 0.35)
    });*/
    
    $( '.lock').bind('mouseover', function() {
        $( this ).fadeTo(250, 0.35)  
    });
    $( '.lock').bind("mouseout", function() {
        $( this ).fadeTo(250, 1)
    });
    $( '.glow').bind('mouseover', function() {
        $( this ).fadeTo(250, 1)  
    });
    $( '.glow').bind("mouseout", function() {
        $( this ).fadeTo(250, 0.35)
    });
    
    

    
    
    /*$('#folders_link').click(function() {
        $('div.folders').slideToggle('fast', function() {});
        return false;
    });*/

    $('div.idm').mousedown(function() {
        var cssObj = {
            "opacity": "0.6",
            "z-index": "200"
        }
        $(this).css(cssObj);
    }).mouseup(function() {
        $(this).css("opacity", "1");
    });
    
    $("#folder_title").mouseover(function() {
        $(this).css("backgroundColor", "#FFFFD3");
    }).mouseout(function() {
        $(this).css("backgroundColor", "#FFF");
    }).dblclick(function() {
        $(this).hide();
        //$("#delflink").hide();
        $("#edit").show();
    //t = $("#"+$(this).attr('id')+"h").html();
    /*alert(  $("#"+$(this).attr('id')+"h").val().replace('"', "&quot;") );
        $('#fb'+ $( this ).attr('id') ).append('<div id="editdiv"><input name="fname" id="' + $( this ).attr('id') + '_value" class="n" type="text" value="' +$("#"+$(this).attr('id')+"h").val().replace('"', "&quot;")+'" size="16" maxlength="25" /></div>');
        $('#' + $( this ).attr('id') + '_value').select();
        load_jq();*/
    }).click(function() {
       // $('#folders_link').click();
    });
    
    $("#edit_folder_title").mouseover(function() {
        $(this).css("backgroundColor", "#FFFFD3");
    }).mouseout(function() {
        $(this).css("backgroundColor", "#FFF");
    }).click(function() {
        $('#folder_title').dblclick();
    });

    load_jq();

});

function fedit_cancel() {
    $("#folder_title").show();
    //$("#delflink").show();
    $("#edit").hide();
}


var img_title;
function edit_title() {
    img_title = document.getElementById("img_title_value").value;

    document.getElementById('img_title').style.display = 'none';
    document.getElementById('edit').style.display = 'block';
    document.getElementById("img_title_value").focus();
}
function edit_cancel() {
    document.getElementById("img_title_value").value = img_title;
    document.getElementById('img_title').style.display = 'block';
    document.getElementById('edit').style.display = 'none';
}
function save_title(q, f) {
    document.getElementById('img_title').innerHTML = '<i id="grey">saving ...</i>';
    document.getElementById('img_title').style.display = 'block';
    document.getElementById('edit').style.display = 'none';
    
    $.get(
        '/js/save_title.php',
        "img_title=" + document.getElementById("img_title_value").value + '&q=' + q + '&f='+f,
        function (result) {
            if (result.type == 'error') {
                //alert('error');
                return(false);
            }
            else {
                if(result.content == '')
                    document.getElementById('img_title').innerHTML = "<i id='grey'>click here to add a title</i>";
                else
                    document.getElementById('img_title').innerHTML = result.content;
                document.getElementById("img_title_value").value = result.content;
            }
        },
        "json"
        );
}

//function lock(id) { // remove?
//    var e = document.getElementById('l' + id).src;
/*if(e.indexOf('/images/lock.jpg') == -1)
        document.getElementById('l' + id).src='/images/lock.jpg';
    else
        document.getElementById('l' + id).src='/images/unlock.jpg';*/
//}
function lock_img(id) {
    // var e = document.getElementById('l' + id).src;

    $.get(
        '/js/lock_img.php',
        "id=" + id,
        function (result) {
            if (result.type == 'error') {
                //alert('error');
                return(false);
            }
            else {
                $('#l' + id).unbind('mouseover mouseout');
                if(result.content == 'lock') {
                    $( '#l' + id ).attr("title", "Show on userpage");
                    $( '#l' + id ).bind('mouseover', function() {
                        $( this ).fadeTo(250, 0.35)  
                    });
                    $( '#l' + id ).bind("mouseout", function() {
                        $( this ).fadeTo(250, 1)
                    });
                }
                else { // unlock
                    $( '#l' + id ).attr("title", "Hide from userpage");
                    $( '#l' + id ).bind('mouseover', function() {
                        $( this ).fadeTo(250, 1)  
                    });
                    $( '#l' + id ).bind("mouseout", function() {
                        $( this ).fadeTo(250, 0.35)
                    });
                }
            }
        },
        "json"
        );
   
    return false;
}
function del_img(id) {
    //if (confirm("Are you sure you want to delete image?")) {
    $.get(
        '/js/del_img.php',
        "id=" + id,
        function (result) {
            if (result.type == 'error') {
                //alert('error');
                return(false);
            }
            else {
                document.getElementById(id).style.display = "none";
            }
        },
        "json"
        );
    /*}
    else*/
    return false;
}

function add_folder() {
    document.getElementById('add_folder').style.display = 'none';
    $.get(
        '/js/add_folder.php',
        '',
        function (result) {
            if (result.type == 'error') {
                //alert('error');
                return(false);
            }
            else {
                /*var newDiv = document.createElement('div');
                newDiv.className = 'droppable';
                newDiv.id = "droppable";
                newDiv.innerHTML = '<div class="folder_title">New Folder</div>';*/
                $('#folders').append('<div class="droppable ui-droppable" id="fid'+result.content+'"><a class="im" href="/user/'+result.user+'/f'+result.content+'"><img width="100" height="76" src="/images/folder.png"></a><span class="folder_title" title="New Folder">New Folder</span></div>');
                load_jq();
            }
        },
        "json"
        );
    return false;
}

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
        e.style.display = 'none';
    else
        e.style.display = 'block';
}

function load_jq() {
    $(function() {
        $( ".nimg" ).draggable({
            revert: "invalid",
            cursorAt: {
                cursor: "move", 
                top: 56, 
                left: 56
            }//,
        //start: function(event, ui) {$('div.folders').fadeIn(400)}
        });
        $( "div.droppable" ).droppable({
            //accept: "div.ui-widget-content",
            //activeClass: "ui-state-hover",
            hoverClass: "droppable_act",
            drop: function( event, ui ) {
                img_to_folder($( this ).attr('id'), ui.draggable);
            /*$( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                .html( "Dropped!" );*/
            }
        });
    });
    
/*$(".n").focusout(function() {
        var id = $( this ).attr('id').replace("_value", "");
        save_foldername($("#"+id).attr('id'), $('#' + $( this ).attr('id')).val());
        $("#editdiv").remove();
        $("#"+id).show();
    })
    $("#folder_name").keyup(function(e) {
        if (e.keyCode == 13) {
            save_fname();
        }
    });*/

}
function img_to_folder(folder_id, $img) {
    $img.remove();

    $.get(
        '/js/img_to_folder.php',
        "fid=" + folder_id + "&img_id=" + $img.attr('id'),
        function (result) {
            if (result.type == 'error') {
                alert('You need login first.');
                return(false);
            }
            else
                document.getElementById($img.attr('id')).style.display = "none";  
        },
        "json"
        );
    
}

function save_fname(id) {
    $.get(
        '/js/save_foldername.php',
        "id=" + id + '&name=' + $('#folder_name').val(),
        function (data) {
            if (data.type == 'error') {
                $("#folder_title").html("New Folder");
                $("#folder_name").val("New Folder");
            }
            else {
                //alert( $("#"+data.id).text());
                $("#folder_title").html(data.fname);
            //$("#folder_name").val(data.fname.replace('"', "&quot;"));
            }
            fedit_cancel();

        },
        "json"
        );
}


