'use strict';
var popupN;  // used for id of popup
var ewz_win;
var ewzG, ajaxurl;
/*************** Functions for the List page ************************************/


function show_full( imgpath ){
    if(  !ewz_win || ewz_win.closed || ( undefined == ewz_win  ) || ( ewz_win == null ) ) {
        ewz_win = window.open(imgpath);
    } else {
        ewz_win.location= imgpath;
    }
    return false;
}

/* popup for image info */
function ewz_info(infostring) {

   jQuery("#info-text").html(infostring);
   return jQuery("#info-text").dialog({
      'title': 'Extra Data Uploaded by Administrator',
      'width': 500,
      'maxHeight': 700,
      'resizable': true,
      'autoOpen': false,
      'closeOnEscape': true
   }).dialog('open');
}

jQuery(function() {
    jQuery('#ewz_overlay').hide();
    if( ewzG.do_refresh ){
        location.reload();
    }
});

function processForm(formId) {
    var jform = jQuery("#"+formId);
    jQuery('#doaction').prop("disabled", true );
    jQuery('#doaction2').prop("disabled", true );
    if(( jform.find('select[name="action"]').val() === 'ewz_attach_imgs') ||
        ( jform.find('select[name="action2"]').val() === 'ewz_attach_imgs') ){
        if( !(jform.find('input[name="ifield"]').filter(":hidden").length > 0) &&
            !(jform.find('input[type="radio"][name="ifield"]:checked').length > 0) ){
            jQuery('#doaction').prop("disabled", false );
            jQuery('#doaction2').prop("disabled", false );
            ewz_alert('I001', "Please select an image column.", ++popupN);
            return false;
        }
        if( !jform.find('select[id="ewz_page_sel"]').val() ){
            jQuery('#doaction').prop("disabled", false );
            jQuery('#doaction2').prop("disabled", false );
            ewz_alert('I002', "Please select a destination page.", ++popupN);
            return false;
        }
        if( !jform.find('select[id="img_size"]').val() ) {
            jQuery('#doaction').prop("disabled", false );
            jQuery('#doaction2').prop("disabled", false );
            ewz_alert('I003', "Please select an image size.", ++popupN);
            return false;
        }
    }

    if( ( jform.find('select[name="action"]').val() === 'ewz_save_edits' ) || ( jform.find('select[name="action2"]').val() === 'ewz_save_edits') ) {
        jform.find('input[id^="ewz_check"]').attr('checked', true);
    }


    if( ( jform.find('select[name="action"]').val() === 'ewz_batch_delete_all') ||
        ( jform.find('select[name="action2"]').val() === 'ewz_batch_delete_all')||
        ( jform.find('select[name="action"]').val() === 'ewz_select_for_edit') ||
        ( jform.find('select[name="action2"]').val() === 'ewz_select_for_edit')||
        ( jform.find('select[name="action"]').val() === 'ewz_save_edits')||
        ( jform.find('select[name="action2"]').val() === 'ewz_save_edits')||
        ( jform.find('select[name="action"]').val() === 'ewz_batch_delete_data') ||
        ( jform.find('select[name="action2"]').val() === 'ewz_batch_delete_data')||
        ( jform.find('select[name="action"]').val() === 'ewz_attach_imgs') ||
        ( jform.find('select[name="action2"]').val() === 'ewz_attach_imgs') ){
        return true;

    } else {
        jQuery('#doaction').prop("disabled", false );
        jQuery('#doaction2').prop("disabled", false );
        ewz_alert('I004', "Please select an action", ++popupN);
        return false;
    }
}

function processIPP(formId) {
    var jform = jQuery("#"+formId);
    jform.append(jQuery('#ewz_numc').html() );

    jQuery('#ippmsg').html('<img alt="loading" src="' + ewzG.load_gif + '"/>');
    // NB: reload only after response is received
    jQuery.post( ajaxurl,
                 jform.serialize(),
                 function (response) {
                     jQuery('#ippmsg').html('');
                     document.location.href = document.location.href + '&paged=1';
                 }).fail( function( jqXHr, textStatus, errorThrown ){ewz_alert( 'I006', 'Sorry, there was a server error: ' + jqXHr.responseText, ++popupN );} );
    return false;
}

jQuery(function(){
    fixConsole();
    popupN = 0;
    if(ewzG.message){
        ewz_alert('I005',ewzG.message, ++popupN);
    }
    jQuery('div.bulkactions').prepend('<img  class="ewz_ihelp" src="' + ewzG.helpIcon + '" onClick="ewz_help(\'bulk\');">&nbsp;');
});
