// JavaScript methods for the Recipe Form for Recipe Press.


function recipe_press_show_tab(tab) {
    /* Close Active Tab */
    activeTab = document.getElementById('active_tab').value;
    document.getElementById('recipe_press_box_' + activeTab).style.display = 'none';
    document.getElementById('recipe_press_' + activeTab).removeAttribute('class','recipe-press-selected');

    /* Open new Tab */
    document.getElementById('recipe_press_box_' + tab).style.display = 'block';
    document.getElementById('recipe_press_' + tab).setAttribute('class','recipe-press-selected');
    document.getElementById('active_tab').value = tab;
    activeTab = tab;
}

function updateTaxonomyField(field, name) {
        name = name.replace(/[^a-zA-Z0-9-_]+/g,'');
        field.value = name.toLowerCase();
        field.name = 'recipe-press-options[taxonomies][' + name + '][active]';
    }
function confirmTaxDelete(name, taxonomy) {
    field = document.getElementById(taxonomy + '_delete');
    if (field.checked) {
        if (!confirm('Are you sure you want to delete the taxonomy "' + name + '"?')) {
            document.getElementById(taxonomy + '_delete').checked = false;
        }
    }
}

function rp_add_ingredient(type) {
    var table = document.getElementById('rp_ingredients_body');
    var rows = table.rows.length;
    var row = table.insertRow(rows);
    row.id = 'rp_ingredient_' + rows;
    row.vAlign = 'top';
    var cellCount = 0;

    // Insert drag cell
    var dragCell = document.createElement('th');
    dragCell.className = 'recipe-press-header-public recipe-press-header-sort';
    controls = document.getElementById('rp_drag_icon').innerHTML;
    dragCell.innerHTML = controls.replace(/NULL/gi, rows).replace(/COPY/gi, '');
    row.appendChild(dragCell);
    ++cellCount;
    
    // Insert Quantity Cell
    var quantityCell = row.insertCell(cellCount);
    var quantity = document.createElement('input');
    quantity.type = 'text';
    quantity.name = 'ingredients[' + rows + '][quantity]';
    quantity.className = 'recipe-press-quantity';
    quantityCell.appendChild(quantity);
    ++cellCount;

    // Insert Size Cell
    var sizeCell = row.insertCell(cellCount);
    var contents = document.getElementById('rp_size_column').innerHTML;
    sizeCell.innerHTML = contents.replace(/NULL/gi, rows).replace(/COPY/gi, '');
    ++cellCount;

    // Insert item cell
    var itemCell = row.insertCell(cellCount);
    var item = document.createElement('input');
    item.type = 'text';
    item.name = 'ingredients[' + rows + '][item]';

    if (type == 'admin') {
        item.className = 'recipe-press-ingredients';
    } else {
        item.className = 'recipe-press-ingredients-public';
    }

    itemCell.appendChild(item);
    ++cellCount;

    // Insert notes cell
    var notes = document.createElement('input');
    notes.type = 'text';
    notes.name = 'ingredients[' + rows + '][notes]';
    if (type == 'admin') {
        notes.className = 'recipe-press-ingredients-notes';
    } else {
        notes.className = 'recipe-press-ingredients-notes-public';
    }
    itemCell.appendChild(notes);

    
    // Insert Page Cell
    if (type == 'admin') {
        var pageCell = row.insertCell(cellCount);
        contents = document.getElementById('rp_page_column').innerHTML;
        pageCell.innerHTML = contents.replace(/NULL/gi, rows);
    }

    table.appendChild(row);
}

function rp_add_divider(type) {
    var table = document.getElementById('rp_ingredients_body');
    var rows = table.rows.length;
    var row = table.insertRow(rows);
    row.id = 'rp_ingredient_' + rows;
    row.className = 'rp_size_type_divider';
    row.vAlign = 'top';
    var cellCount = 0;

    // Insert drag cell
    var dragCell = document.createElement('th');
    dragCell.className = 'recipe-press-header-public recipe-press-header-sort';
    controls = document.getElementById('rp_drag_icon').innerHTML;
    dragCell.innerHTML = controls.replace(/NULL/gi, rows).replace(/COPY/gi, '');
    row.appendChild(dragCell);
    ++cellCount;
    
    // Insert Quantity Cell
    var quantityCell = row.insertCell(cellCount);
    quantityCell.style.width = '60px';
    ++cellCount;

    // Insert Size Cell
    var sizeCell = row.insertCell(cellCount);
    var item = document.createElement('input');
    item.type = 'text';
    item.name = 'ingredients[' + rows + '][size]';
    item.value = 'divider';
    item.style.width = '55px';
    item.readOnly = true;
    sizeCell.appendChild(item);
    ++cellCount;

    // Insert Item cell
    var itemCell = row.insertCell(cellCount);
    item = document.createElement('input');
    item.type = 'text';
    item.name = 'ingredients[' + rows + '][item]';
    if (type == 'admin') {
        item.className = 'recipe-press-ingredients';
    } else {
        item.className = 'recipe-press-ingredients-public';
    }
    itemCell.appendChild(item);
    ++cellCount;

    // Insert Page Cell
    if (type == 'admin') {
        var pageCell = row.insertCell(cellCount);
    }

    table.appendChild(row);
}

function rp_delete_row(elem) {
    if (confirm('You are about to delete this ingredient row, are you sure?') ) {
        var tableRow = document.getElementById(elem);
        var row = tableRow.rowIndex - 1;
        document.getElementById('rp_ingredients_body').deleteRow(row);
    }
}

function selectMeasurementType(type) {
    document.getElementById('serving-sizes-metric').style.display = 'none';
    document.getElementById('serving-sizes-standard').style.display = 'none';
    document.getElementById('serving-sizes-'+type).style.display = '';
}

/* Set up the ingredient sorting. */
try {
    jQuery(document).ready(function(){
        try {
            jQuery("#rp_ingredients tbody").sortable({
                helper: fixHelper,
                cursor: 'crosshair',
                items: 'tr',
                axis: 'y',
                distance:  15
            });
        } catch(error) {}
    });

} catch(error) {}

var fixHelper = function(e, ui) {
    ui.children().each(function() {
        jQuery(this).width(jQuery(this).width());
    });
    return ui;
};

