﻿
jQuery(document).ready(function($) {
$("#addIngredientLink, #addPreparationLink").click(function(e) {
        var $input = $(this).prev();
        var inputval = $input.val();
        var $formItemsTable = $(this).closest("table").next().find("table.formItemsTable");
        FormAddItem($input, inputval, $formItemsTable);
        e.preventDefault();
    });
    $("a.itemLinkEdit").live("click", FormEditItem);
    $("a.itemLinkDelete").live("click", FormDeleteItem);
    $("a.itemLinkUpdate").live("click", FormUpdateItem);
    $("a.itemLinkCancel").live("click", FormCancelUpdateItem);

    //fill ingredient/preparation on pageload
    FillList($("#IngredientList"));
    FillList($("#PreparationList"));

    //when clicking checkbox 'hide Author' then prefill author fields when empty
    $("table.hideAuthorData input").click(function() {
        if ($(this).val() == "on")
        {
            FillAddressFields();
        }
    });
});


//add ingredient
function FormAddItem($input, inputval, $formItemsTable) {
    var template = $("#MultipleItemsTemplate tbody").html();
    var prefix = "-"; 
    if ($formItemsTable.attr("id") == "PreparationList")
    {
        prefix = $("#PreparationStep").text();
    }
    var row = template.replace("--TEXT--", inputval);
    row = row.replace("--INPUT--", "<input type='text' class='formTextBoxMedium' value='" + inputval + "' />");
    row = row.replace("--PREFIX--", prefix);

    $emptyRow = $("tr.emptyRow", $formItemsTable);
    if ($emptyRow.length > 0) $emptyRow.remove();
    
	$formItemsTable.append(row);
	FormFillHiddenField($formItemsTable);
	$input.val("").focus();
	FormItemCount($formItemsTable);
	return false;
}   

//edit row
function FormEditItem() {
	var $row = $(this).closest("tr");
	$(".itemNameChange, .itemUpdate", $row).show();
	$(".itemName, .itemControls", $row).hide();
	return false;
}	

//delete row
function FormDeleteItem() {
    $parent = $(this).closest("table");
    $(this).closest("tr").remove();
    FormItemCount($parent);
    FormFillHiddenField($parent);

    $("td.itemPrefix", $parent).each(function(j) {
        $(this).html(j+1);
    });
     
    return false;
}

//update row
function FormUpdateItem() {
    var $row = $(this).closest("tr");
    $(".itemNameChange, .itemUpdate", $row).hide();
    $(".itemName, .itemControls", $row).show();
    var newVal = $(".itemNameChange input", $row).val();
    $(".itemName", $row).html(newVal);
    FormFillHiddenField($(this).closest("table"));   
    return false;
}

function FormCancelUpdateItem() {
    var $row = $(this).closest("tr");
    $(".itemNameChange, .itemUpdate", $row).hide();
    $(".itemName, .itemControls", $row).show();
    return false;
}

function FormItemCount($formItemsTable) {
    var count = $("tr", $formItemsTable).length + 1;
    if ($formItemsTable.attr("id") == "IngredientList") {
        $("#IngredientStep").html(count);
    }
    else {
        $("#PreparationStep").html(count);
    }
}

function FormFillHiddenField($formItemsTable) {
    var retval = "";
    $("tr", $formItemsTable).each(function() {
        retval += $(".itemName", this).text() + "\n";
    });    
    $formItemsTable.next().val(retval);
}


function FillList($obj) {
    var list = $obj.next().html();
    var listItems = list.split(/\n/);
    var $input = $obj.closest("div").prev().find("input");

    for (var i = 0; i < listItems.length; i++)
    {
        if (listItems[i] != "")
        {
            //console.log(listItems[i]);
            FormAddItem($input, listItems[i], $obj);
        }
    }

}
