var DM_Widget_Video_Preview_AdminButtons = 
{
    state: 'hide',

    toggleRules: 
    {
        'div.dmpi_video_preview_adminbuttons' : function(element)
        {
            if (DM_Widget_Video_Preview_AdminButtons.state == 'hide')
            {
                element.hide();
            } 
            else
            {
                element.show();
            }
        }
    },
    
    initButtons: 
    {
        // displaying embed player
        'div.dmpi_video_preview_adminbuttons div.play_video a' : function(element)
        {
           element.onclick = function()
           {
               var videoId = findIdInClass(element.parentNode);
               var video_preview = element.parentNode.parentNode.parentNode.parentNode.parentNode;
               
               // hide preview
               video_preview.hide();
               
               // delete old preview_player
               if($('video_preview_player'))
               {
                   $('video_preview_player').associate_video_preview.show();
                   $('video_preview_player').remove();
               }
               
               // add a new preview_player
               var video_preview_player = video_preview.parentNode.insertBefore(document.createElement('div'), video_preview);
               video_preview_player.setAttribute('id', 'video_preview_player');
               video_preview_player.setAttribute('class', 'id_' + videoId);
               
               video_preview_player.associate_video_preview = video_preview;
               
               var player = new SWFObject('/swf/' + videoId + '&autoPlay=1', 'video_player', '100%', '100%', '7');
               player.addParam("allowFullScreen", "true");
               player.addParam("allowScriptAccess", "always");
               player.write('video_preview_player');
               
               return false;
           }
        },
        
        'div.dmpi_video_preview_adminbuttons div.censor_video a' : function(element)
        {
           element.onclick = function()
           {
               var videoId = findIdInClass(element.parentNode);
               
               ajax_call(
                   'admin', 'admin_toggle', 'video', videoId, 'moderation', 
                    function(status) 
                    {
                        DM_Widget_Video_Preview_AdminButtons.updateItem(status, element.parentNode);
                    }
               );
               
               return false;
           }
        },
        
        'div.dmpi_video_preview_adminbuttons div.explicit_video a' : function(element)
        {
           element.onclick = function()
           {
               var videoId = findIdInClass(element.parentNode);
               
               ajax_call(
                   'admin', 'admin_toggle', 'video', videoId, 'explicit', 
                    function(status) 
                    {
                        DM_Widget_Video_Preview_AdminButtons.updateItem(status, element.parentNode);
                    }
                   
               );
               
               return false;
           }
        },
        
        'div.dmpi_video_preview_adminbuttons div.copyright_video a' : function(element)
        {
           element.onclick = function()
           {
               var videoId = findIdInClass(element.parentNode);
               
               ajax_call(
                   'admin', 'admin_toggle', 'video', videoId, 'moderation_copyright', 
                    function(status) 
                    {
                        DM_Widget_Video_Preview_AdminButtons.updateItem(status, element.parentNode);
                    }
                   
               );
               
               return false;
           }
        },
        
        'div.dmpi_video_preview_adminbuttons div.get_admin_panel_video a' : function(element)
        {
           element.onclick = function()
           {
                setCookie('admin_panel_status', 1);
                DM_Widget.get('AdminPanel', hex2str(findParamInClass('url', element.parentNode)), $('admin_panel_container').firstDescendant());
                toggleElement($('admin_panel_container'), 'show');
               
                return false;
           }
        },
        
        'div.dmpi_video_preview_adminbuttons div.copyright_claim_video a' : function(element)
        {
           element.onclick = function()
           {
               var videoId = findIdInClass(element.parentNode);
               ajax_call(
                   'copyrightowner', 'send_abuse', videoId,
                    function(status) 
                    {
                        DM_Widget_Video_Preview_AdminButtons.updateItem(status, element.parentNode);
                    }
                   
               );
               
               return false;
           }
        }
    },
    
    updateItem: function(status, button)
    {
        var video_preview = button.parentNode.parentNode.parentNode.parentNode;
        
        if(status == 1)
        {
            video_preview.style.opacity = 0.7; 
            Element.addClassName(button, 'cancel');
        }
        else
        {
            video_preview.style.opacity = 1; 
            Element.removeClassName(button, 'cancel');            
        }
    },
    
    toggleButtons: function()
    {
        if(getCookie('video_preview_adminbuttons') == '1')
        {
            setCookie('video_preview_adminbuttons', 0);
            DM_Widget_Video_Preview_AdminButtons.state = 'hide';
        }
        else
        {
            setCookie('video_preview_adminbuttons', 1);
            DM_Widget_Video_Preview_AdminButtons.state = 'show';
        }
        
        EventSelectors.start(DM_Widget_Video_Preview_AdminButtons.toggleRules);
    },
    
    initialize: function()
    {
        Event.onDOMReady(
            function() {
                
                if ($$('div.dmpi_video_preview_adminbuttons') == '')
                {
                    return false;
                }
                
                if(getCookie('video_preview_adminbuttons') != 1)
                {
                    EventSelectors.start(DM_Widget_Video_Preview_AdminButtons.toggleRules);
                }
                
                EventSelectors.start(DM_Widget_Video_Preview_AdminButtons.initButtons);
            }
        );
        
        // add key shortcut
        DM_KeyHandler.registerAction('m', DM_KeyHandler.shift, DM_Widget_Video_Preview_AdminButtons.toggleButtons);
    }
};

DM_Widget_Video_Preview_AdminButtons.initialize();

