jQuery(function ($) {
    $('.gallery-controls > .icon_download, .gallery-controls > .icon_share').live('click', function() {
        var popup = $(this).siblings('.fly-out');
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();
//        $('.fly-out-move').hide();

        if (hidden) {
            popup.show();
        } else {
            popup.hide();
        }
    });

    $('.gallery-controls > .icon_edit, .gallery-controls > .icon_none').live('click', function() {
        var id = $(this).attr('id');
        var index = id.indexOf('-');
        var flyoutId = id.substr(index+1);
        var popup = $('#fly-out-' + flyoutId);
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();
//        $('.fly-out-move').hide();

        if (hidden) {
            popup.show().position({
                "my": "center top",
                "at": "center bottom",
                "of": this,
                "offset": "0 10",
                "collision": "none none",
                "bgiframe": true
            });
        } else {
            popup.hide();
        }
    });

    $('.gallery-controls > .icon_playlist').live('click', function() {
        $("#currentPlaylistItem").attr("id", "");
        var container = $(this).siblings('.fly-out').children('.gallery-controls-add-playlist-wrapper').children('.playlist_command_wrapper');
        var successful = container.children('.playlist_success.active');
        if (successful.length > 0) {
            successful.removeClass('active');
            successful.siblings('.playlist_already').addClass('active');
        } else {
            container.children('.playlist_command').attr("id", "currentPlaylistItem");
        }
        var popup = $(this).siblings('.fly-out');
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();

        if (hidden) {
            popup.show();
        } else {
            popup.hide();
        }
    });

    $('.fly-out .close').live('click', function() {
        $(this).parents('.fly-out').hide();
    });

    //close .fly-out when clicked outside of .fly-out
    $(document).bind('mousedown.fly-out', function(e) {
        if (!$(e.target).is('.fly-out') && $(e.target).parents('.fly-out').length == 0) {
            $('.fly-out').each(function() {
                var $this = $(this);
                if (!$this.is(':hidden')) {
                    $this.hide();
                }
            });
        }
    });

    $.fn.addedToPlaylist = function(result, message) {
        jQuery('#currentPlaylistItem').removeClass('active');
        if (result > 0) {
            jQuery("#edit-change-playlist-id").change();
            jQuery('#currentPlaylistItem').siblings('.playlist_success').addClass('active');
        } else if (result == 0) {
            jQuery('#currentPlaylistItem').siblings('.playlist_already').addClass('active');
        } else {
            alert(message);
        }
    };

    $.fn.removedFromPlaylist = function(pliid) {
      $('.fly-out').hide();
      $("#" + pliid).parents('tr').remove();
    };

    $.fn.resetPlaylistIcon = function() {
        jQuery('.playlist_success.active').removeClass('active');
        jQuery('.playlist_already.active').removeClass('active');
        jQuery('.playlist_command').addClass('active');
    };
});


;

