






// here goes the previosu values for the form fields which may not exist yet (created in moreFields())
var expandFieldVals = new Array();
/*
<~ section name=p loop=$expandable_fields_params ~>
    expandFieldVals[<~ $smarty.section.p.iteration ~>] = new Object;
    <~ foreach from=$expandable_fields_params[p] item=val key=key ~>
        expandFieldVals[<~ $smarty.section.p.iteration ~>].<~ $key ~> = '<~ $val ~>';
    <~/foreach~>
<~/section ~>
*/

var counter = 0;

// create more fields, cloning the block 'readroot'
function moreFields()
{
    if (! document.getElementById('readroot')) return;

    counter++;
    var newFields = document.getElementById('readroot').cloneNode(true);
    newFields.id = '';
    newFields.style.display = 'block';

    setNamesIncremented(newFields);

    var insertHere = document.getElementById('writeroot');
    if (insertHere) {
        insertHere.parentNode.insertBefore(newFields,insertHere);
    }

    if (document.forms[0].elements['f_num_expandable_fields']) {
        document.forms[0].elements['f_num_expandable_fields'].value = counter;
    }
}

function setNamesIncremented(node) {

    var theName = node.name;
    if (theName) { // its a named element, presume a form thing
        node.name = theName + '_' + counter; // give it a unique ID
        if (expandFieldVals[counter]) {             // look for prev. values
            node.value = eval('expandFieldVals[counter].'+theName);
        }
    }

    var children, i;

    children = node.childNodes;

    for (i=0;i<children.length;i++)
    {
        setNamesIncremented(children.item(i));
    }

}

function initFields() {
    numInitially = 3;

    if (expandFieldVals && expandFieldVals.length > 0) {
        numInitially = expandFieldVals.length - 1; // (starts at index [1])
    }
    else if (document.forms[0] && document.forms[0].elements['f_num_expandable_fields']) {
        numInitially = document.forms[0].elements['f_num_expandable_fields'].value;
    }
    for (var i=0; i<numInitially; i++) {
        moreFields();
    }
}

$(document).ready( function() {

    $('span.helpPop').bind('click', function () {
        $(this).find('span.pop').toggle();
    });
    $(document).bind('click', function (e) {
        var isBtn = (e.target.className == 'btn' && $(e.target).parents('.helpPop'));
        if (!isBtn) {
            $('span.helpPop span.pop').fadeOut();
        }
    });


    $('div.specPriceAdmin, div.specAdminNotice').bind('click', function() {
        $(this).children('.specAdminIndicator').toggle();
    });

    if ($.browser.msie) { /* try to get around a superannoying peekaboo thing in msie7 with these notes */
        $('div.specEditItemLink').hide().show();
    }

    initFields();
});
