
// pop open a frame containing a node or portion thereof
function pop(id)
{
	$itm = $('#a'+id)
	$itm.toggle()
	$('#li'+id).attr('src',
		($itm.is(':visible') ? '/images/minus.gif' : '/images/plus.gif'))
	return false
	
}

// JS driver for the vocabulary picker

function setupDiv(divElem, formElem, vocab, currValue)
{
    divElem.comboCount = 0;
    divElem.combos = [];
    divElem.formElem = formElem;
    if (currValue)
        _setValue(divElem,vocab,currValue);
    else
        _setupCombo(divElem, vocab, 0,null);
}

///////////////// Internal functions /////////////////////////

// Recursively search for the given item ID in arr. When found, returns an array
// of parent objects with the found object as its last item.
function _findItem(arr, itemID, stack)
{
    var sl = stack.length;
    for (var j=0; j<arr.length; j++)
    {
        if (!arr[j]) break;         //IE6 sometimes misreports array.length
        if (arr[j].id == itemID)
        {
            stack.length = sl;      //truncate the last item on the stack, if present
            stack[sl] = arr[j];
            return stack;
        }
        if (arr[j].children && arr[j].children.length > 0)
        {
            stack.length = sl;
            stack[sl] = arr[j];
            var ret = _findItem(arr[j].children, itemID, stack);
            if (ret != null) return ret;        //found it!
        }
    }
    return null;
}

//set the value in the specified div to the specified vocabulary ID.
//note that this function does linear tree search, which means that it may be
//slow for large trees. however, it generally only has to run once.
function _setValue(div, arr, itemID)
{
    var aVoc;
    aVoc = _findItem(arr, itemID, []);
    if (aVoc == null) return;       //couldn't find the item
    _setupCombo(div, arr, 0, null);
    for (var i=0; i<aVoc.length; i++)
    {
        for (var j=0; j < div.combos[i].options.length; j++)
        {
            if (div.combos[i].options[j].value == aVoc[i].id)
            {
                div.combos[i].options[j].selected = true;
                if (aVoc[i].children && aVoc[i].children.length > 0)
                    _setupCombo(div, aVoc[i].children, i+1, aVoc[i].id);                
            }
        }
    }
    div.selectedVocab = itemID;
}

//insert a combobox into the specified div with members from the given array
function _setupCombo(div, arr, lvl,parentID)
{
    var cbo;
    if (div.comboCount > lvl)
    {
        for (var i=lvl; i<div.comboCount; i++)
        {
            div.removeChild(div.combos[i]);
        }
        div.comboCount = lvl;
    }
    if (arr == null) return;
    cbo = div.appendChild(document.createElement("select"));
    cbo.className = 'geo'
    div.combos[div.comboCount] = cbo;
    div.comboCount++;
    cbo.lvl = lvl;
    _populateComboItems(cbo, arr, lvl,parentID);
    cbo.onchange = function()
    {
        div.formElem.value = this.options[this.selectedIndex].value;
        //collapse if the user selects --choose--
        if (!this.options[this.selectedIndex].arrItem)
        {
            _setupCombo(this.parentNode, null,this.lvl+1);  
        }
        else if (this.options[this.selectedIndex].arrItem.children &&
            this.options[this.selectedIndex].arrItem.children.length > 0)
            _setupCombo(this.parentNode, this.options[this.selectedIndex].arrItem.children, this.lvl+1,
                       this.options[this.selectedIndex].arrItem.id);
    }
}

function _populateComboItems(cbo, arr, lvl, parentID)
{
    //'choose' gets the parent id so that selecting it generates the parent vocabulary
    //as the current selection
    cbo.options[0] = new Option("---Choose---",parentID);
    for (var j=0; j<arr.length; j++)
    {
        if (!arr[j]) break;         //IE6 sometimes misreports array.length
        cbo.options[j+1] = new Option(arr[j].name, arr[j].id);
        cbo.options[j+1].arrItem = arr[j];
        if (!arr[j].children)
        	cbo.options[j+1].className = 'chooseloc'
    }
}

function replyComment(id, bottom)
{
	if (bottom)
	{
	oAnch = document.getElementById('bottom'+id);
	}
	else
	{
	oAnch = document.getElementById('cmt'+id);
	}
	ocblk = document.getElementById('commentblock');
	olta = ocblk.getElementsByTagName("TEXTAREA");
	
	$iframe = $('#commentblock iframe')
	var oldText=''
	if ($iframe.length)
		oldText = $($iframe.get(0).contentDocument.body).text()
		
	if (oldText.length)
	{
		if (!confirm("Your comment will not be saved")) return;
	}
	
	//strangely, reusing editor instances causes the focus to not work
	if (CKEDITOR.instances.comment)
	{
		CKEDITOR.instances.comment.destroy();
	}

	document.getElementById('cmtparent').value = id;
	olta[0].value = '';
	oAnch.appendChild(ocblk);
	ocblk.style.position = 'relative';
	ocblk.style.visibility = 'visible';
	olta[0].focus();
	
	CKEDITOR.replace(olta[0],
		{
			toolbar: 'Reply',
			on:
				{
					'instanceReady': function(evt) {
					//Set the focus to your editor
					CKEDITOR.instances.comment.focus();
				}
			}
	});
	$('#previewblock').hide()

}

function showSearchForm()
{
	$('#searchForm').show()
	$('#actionButtons').hide()
	$('#keys').focus()
}

function suboptions()
{
	$('#suboptions').css('visibility','visible')
}

// variables to hold the value and ID of the button user clicked on to submit form
var clickedButton = ''
var buttonID = ''

// onsubmit handler for node create/edit form
function submitting()
{
	if (buttonID && clickedButton)
	{
		//disable the button so it won't be clicked twice
		$('#'+buttonID)
			.val('Submitting...')
			.attr('disabled', true)
			// create a hidden field to hold the submit button's name
			// have to do this because disabling the submit button means 
			// its name won't be submitted with the form
			.parent()	// not sure why we can't attach this to the form
			.append($('<input />')
				.attr('type','hidden')
				.attr('name','submitButton')
				.attr('value', clickedButton)
				)
		
	}
	return true /* still want to submit form */
}

// attach an onsubmit event handler to the node editing/creating form, plus click
// handlers for its submit buttons
$(function() {
	$('form[id*="-form"]')
		.submit(submitting)
		.children('table')	// this makes it work but I don't know why
		.find('input[type="submit"]')
			.click(clickSubmitButton)
	} )

// user clicked on a form submission button. Only some buttons have IDs
function clickSubmitButton()
{	
	clickedButton = this.value
	buttonID = this.id	
}

// show a comment preview
function showPreview()
{
	$('#cke_contents_comment iframe')
		.each( function() {
			$('#previewblock .commentbody')
				.empty()
				.append(
					$(this.contentDocument)
						.find('body')
						.html()
				 )
			$('#previewblock')
				.show()
			} )
				
}

var nodeName=''

// handle "view all" in shortened capsules
$( function() {
	$('.readall')
		.click( function() {
			$(this)
				.parent()
				.parent()
				.hide()
				.next()
				.show()
			return false
			} )
		} )


// deprecated
function verifyBroken(inappr)
{
	if (nodeName) nn = '"' + nodeName + '"'
	else nn = 'this node'
	if (inappr)
	{	aText = 'Are you sure you want to mark ' + nn + ' as inappropriate content?' } 
	else
	{ aText = 'Are you sure you want to report broken links on '+nn+'?' }
	return confirm(aText)
}

function areyousure()
{
	return confirm('Are you sure you want to delete?');
}


