function popup(popURL, popW, popH) {
	window.open(popURL, 'popup', 'width=' + popW + ', height=' + popH + ', toolbar=no');
}

function colljump(sel) {
	location.href = sel.options[sel.selectedIndex].value;
}

function popset(targeturl) {
	window.open(targeturl, 'popset', 'width=640, height=350, toolbar=no');
	return false;
}

function popjudge(targeturl) {
	window.open(targeturl, 'judgeview', 'toolbar=no, resizable=yes');
}

function showhide(targetElement) {
	var layermode = document.getElementById(targetElement).style;
	layermode.display = (layermode.display == 'none') ? "block" : "none";
}

function toggletext(targetElement,newText,oldText) {
	targetElement.innerHTML = (targetElement.innerHTML == oldText) ? newText : oldText;
}

function confirmjump(targeturl) {
	if (confirm('Are you sure?')) {
		location.href = targeturl;
	}
	//follow with return false in onclick
}

// Remove all empty params from query string on submission
function simplifySubmit() {
    var qs = $(this).serialize();
    qs = qs.replace(/(^|&)[^&=]+=(?=&|$)/g, '');
    if (qs) qs = '?' + qs;
    location.href = this.action + qs;
    return false;
}

$( function () {

    function confirmDelete() {
        // Should replace with jQuery UI dialog
        return confirm('Are you sure you want to delete the citation ' +
            'from your collection?');
    }

    function openPopup() {
        // Should replace with jQuery UI dialog
        window.open(this.href, this.target || 'popup',
            'width=690,height=400,scrollbars=1');
        return false;
    }

    window.addButtons = function (selector, collectionCode, documentCode,
        names) {
        var i, name, url, label, popup, target, link, html,
            span = $('<span></span>');
        for (i = 0; i < names.length; i++) {
            name = names[i];
            label = name.charAt(0).toUpperCase() + name.substr(1);
            popup = false;
            target = '';
            url = '/' + collectionCode + '/' + documentCode;
            switch (name) {
            case 'pdf':
                label = 'PDF';
                url += '.pdf';
                // Do we need to handle getting PDFs from Legacy?
                break;
            case 'tiff':
                label = 'TIFF';
                url += '.tif';
                break;
            case 'text':
                url += '.txt';
                break;
            case 'cite':
                popup = true;
                url = '/' + collectionCode + '/cite.php?document_code=' +
                    encodeURIComponent(documentCode);
                break;
            case 'edit':
                target = 'WorkFrame';
                url += '.edit.html';
                break;
            case 'email':
                popup = true;
                url += '.email.html';
                break;
            case 'bookmark':
                popup = true;
                url += '.bookmark.html';
                break;
            case 'delete':
                url = 'xxx/' + collectionCode + '/?mode=deletexxx&document_code=' +
                    encodeURIComponent(documentCode);
            }
            link = $('<a></a>').attr('href', url).text(label)
                .addClass('button');
            if (popup) {
                target = name;
                link.addClass('popup');
            }
            if (target) {
                link.attr('target', target);
            }
            if (name == 'delete') {
                link.addClass('delete');
            }
            span.append(link).append(' ');
        }
        html = span.html();
        $(selector).html(html).find('a.popup').click(openPopup)
            .end().find('a.delete').click(confirmDelete);
    }

    $('form.listingsearch').submit(simplifySubmit);

} );

