
var objFileReportTranslations = JSON.parse('{"FileReportCancelConfirm":"Do you want to cancel the previous problem report for this file?","FileReportConfirm":"Is there a problem with this content or service functionality?","ReasonPlaceholder":"Enter a description of the problem...","PleaseSelectType":"Choose the type of problem:","Type_DoesNotWork":"Does not work","Type_Bug":"Error","Type_Copyrights":"Copyrights","Type_PersonalPrivacy":"Person\u2019s privacy","Type_Hatred":"Hatred","Type_Terrosim":"Terrorism","Type_Malware":"Malware","Type_Spam":"Spam","Type_Adult":"Adult content","Type_Gore":"GORE","Type_Children":"Children","Type_Other":"Other","Button_Report":"Report","Button_Cancel":"Cancel"}');


function showFileReportModal( domLink, strHash, fnCallback )
{
    if ( typeof fnCallback === "undefined" )
    {
        fnCallback = function () {};
    }
    
    if ( $( domLink ).hasClass( 'has_reported' ) )
        return showCancelFileReportModal(domLink, strHash, fnCallback);
    
    var strInputs = '';
    strInputs += '<br />';
    strInputs += '<br />';
    strInputs += '<select class="type" tabindex="-1" style="width: 95%; font-family: AlbertaSans, Roboto, sans-serif; font-weight: 300; font-size: 14px; color: rgba(43, 61, 82, 0.8);">';
    strInputs += '  <option value="">'+ objFileReportTranslations.PleaseSelectType +'</option>';
    strInputs += '  <option value="DoesNotWork">'+ objFileReportTranslations.Type_DoesNotWork +'</option>';
	strInputs += '  <option value="BUG">'+ objFileReportTranslations.Type_Bug +'</option>';    
    strInputs += '  <option value="COPYRIGHTS">'+ objFileReportTranslations.Type_Copyrights +'</option>';
    strInputs += '  <option value="PersonalPrivacy">'+ objFileReportTranslations.Type_PersonalPrivacy +'</option>';
    strInputs += '  <option value="HATRED">'+ objFileReportTranslations.Type_Hatred +'</option>';
    strInputs += '  <option value="TERRORISM">'+ objFileReportTranslations.Type_Terrosim +'</option>';	
    strInputs += '  <option value="MALWARE">'+ objFileReportTranslations.Type_Malware +'</option>';
    strInputs += '  <option value="SPAM">'+ objFileReportTranslations.Type_Spam +'</option>';
    strInputs += '  <option value="ADULT">'+ objFileReportTranslations.Type_Adult +'</option>';
    strInputs += '  <option value="GORE">'+ objFileReportTranslations.Type_Gore +'</option>';	    
    strInputs += '  <option value="CHILD">'+ objFileReportTranslations.Type_Children +'</option>';		
    strInputs += '  <option value="OTHER">'+ objFileReportTranslations.Type_Other +'</option>';
    strInputs += '</select>';
    strInputs += '<br />';
    strInputs += '<br />';
    strInputs += '<textarea class="reason" tabindex="-1" placeholder="'+ objFileReportTranslations.ReasonPlaceholder +'" style="width: 95%; height: 70px; font-family: AlbertaSans, Roboto, sans-serif; font-weight: 300; font-size: 14px; color: rgba(43, 61, 82, 0.8);"></textarea>';
    strInputs += '<br />';

    
    fConfirm(
        objFileReportTranslations.FileReportConfirm + strInputs,
        null,
        objFileReportTranslations.Button_Report ,
        objFileReportTranslations.Button_Cancel ,
        // Submit
        function( bolSubmitted, jqModalWindow )
        {
            if( bolSubmitted === true )
            {
                var jqReason = $( '.reason', jqModalWindow );
                var jqType = $( '.type', jqModalWindow );
                makeFileReportRequest( strHash, jqReason.val(), jqType.val(), function ( strActionResponse ) {
                    finishFileReport( domLink, strActionResponse );
                    fnCallback( strActionResponse );
                });
            }
        },
        // Validate
        function ( jqModalWindow )
        {
            var jqReason = $( '.reason', jqModalWindow );
            var jqType = $( '.type', jqModalWindow );
            
            var bolOk = true;
    
            if ( $.trim( jqReason.val() ).length < 4 )
            {
                jqReason.css({'border-color': 'red'});
                bolOk = false;
            }
            else
            {
                jqReason.css({'border-color': 'inherit'});
            }
            
            if ( $.trim( jqType.val() ) == '' )
            {
                jqType.css({'border-color': 'red'});
                bolOk = false;
            }
            else
            {
                jqType.css({'border-color': 'inherit'});
            }
            
            return bolOk;
        }
    );
}


function showCancelFileReportModal(domLink, strHash, fnCallback)
{
    if ( typeof fnCallback === "undefined" )
    {
        fnCallback = function () {};
    }
     
    fConfirm(
        objFileReportTranslations.FileReportCancelConfirm,
        null,
        'OK',
        objFileReportTranslations.Button_Cancel ,
        // Submit
        function( bolSubmitted )
        {
            if( bolSubmitted === true )
            {
                makeFileReportRequest( strHash, null, null, function ( strActionResponse ) {
                    finishFileReport( domLink, strActionResponse );
                    fnCallback( strActionResponse );
                });
            }
        }
    );
}

function finishFileReport( domLink, strActionResponse )
{
    
    if ( strActionResponse == 'report' )
    {
        
        $( domLink ).addClass( 'has_reported' ).attr( 'my_title', 'GALLERY-CancelReport' );
        fSuccess('FILEREPORT-Reported');
    }
    else
    {
        
        $( domLink ).removeClass( 'has_reported' ).attr( 'my_title', 'GALLERY-Report' );
        fSuccess('FILEREPORT-Cancelled');
    }
    
}

function makeFileReportRequest( strHash, strReason, strType, fncCallback )
{
    $.ajax( {
        type: "POST",
        dataType: "json",
        url: "/ajax/report_file.php",
        data: {
            'report': true,
            'h': strHash,
            'reason': !!strReason ? strReason : null,
            'type': !!strType ? strType : null
        },
        success: function ( data )
        {
            if ( data[ 'status' ] == 'ok' )
            {
                fncCallback( data[ 'action' ] );
            }
        }
    } );
}