var currentPopup;


if (document.forms['searchForm']) {
    document.forms['searchForm'].elements['lookfor'].focus();
}

function toggleClass(elem,cssClass) {
    if (elem.className == cssClass) {
        elem.className = '';
    }
    else {
        elem.className = cssClass;
    }
}

function switchMenu(obj) {
	var el = getElem(obj);
	if ( el.style.display != 'none' ) {
		collapse(obj);
	}
	else {
		expand(obj);
	}
}

function collapse(obj) {
	var el = getElem(obj);
	var elPar = getElem(obj + 'Img');
	el.style.display = 'none';
	elPar.src = "../images/plustop.gif";
}

function expand(obj) {
	var el = getElem(obj);
	var elPar = getElem(obj + 'Img');
	el.style.display = '';
	elPar.src = "../images/minustop.gif";
}

function collapseAll(objs) {
	var i;
	for (i=0;i<objs.length;i++ ) {
		if (getElem(objs[i])) {
			collapse(objs[i]);
		}
	}
}

function expandAll(objs) {
	var i;
	for (i=0;i<objs.length;i++ ) {
		if (getElem(objs[i])) {
			expand(objs[i]);
		}
	}
}

function getElem(id)
{
    return (document.getElementById) ? document.getElementById(id) : document.all[id];
}

function popupMenu(elemId)
{
    var messagebox = document.getElementById(elemId);
    var lightbox = document.getElementById('lightbox');

    if (messagebox) {
        if (messagebox.style.display == 'block') {
            messagebox.style.display='none';
            currentPopup = null;
        } else {
            if (currentPopup) {
                hideLightbox();
            }
            //hideSelects('hidden');
            currentPopup = elemId;

            lightbox.style.top=document.body.scrollTop;
            lightbox.style.display='block';

            messagebox.style.top=document.body.scrollTop+300;
            messagebox.style.display='block';
        }
    }
}

function hideLightbox()
{
    hideSelects('visible');
    document.getElementById('lightbox').style.display='none';
    document.getElementById(currentPopup).style.display='none';
}

function hideSelects(visibility){
	selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}

function showTagForm()
{
    if (document.getElementById('tagForm').style.display == 'block') {
        document.getElementById('tagForm').style.display='none';
    } else {
        document.getElementById('tagForm').style.display='block';
        document.forms['tagForm'].elements['tag'].focus();
    }
}

function fadeoutThis(id)
{
	new Effect.Fade(id,{ duration: 3 });
}

function highlightThis(id)
{
	new Effect.Highlight(id,{ startcolor:'#ffffff',endcolor:'#c8ffc7', delay: 0.1, duration: 0.75});
}

function showDisclaimer(start, end)
{
	new Effect.Highlight('disclaimer',{ startcolor:start,endcolor:end, delay: 0.1, duration: 0.75});
}

function toggleFields (showId, hideId)
{
	document.getElementById(hideId).style.display = 'none';
	document.getElementById(showId).style.display = 'inline';
}


function toggleCoverLabel ( id )
{
  var label = document.getElementById ( id );
  if ( label.style.visibility == 'visible' )
  {
    label.style.visibility = 'hidden';
  }
  else
  {
    label.style.visibility = 'visible';
  }
}

function imposeMaxLength(obj, maxlen)
{
  return (obj.value.length <= maxlen);
}

function getTimestamp ()
{
  return new Date().getTime();
}



var extdata_bibmap = new Object ();


var extdata = function ()
{
    var populated_hooks = new Array ();
    var info = new Object();
    var sources = new Array ('gbooks', 'shop');
    var populated = false;


    function receive (bookinfo, type)
    {
        info[type] = bookinfo;
        populateThumbnails (false);
    }


    function populateGoogleBooks (seen)
    {
        for (var isbn in info['gbooks']) {
            var bookinfo = info['gbooks'][isbn];
            var bibid = extdata_bibmap[bookinfo.bib_key];

            if (seen.find (function (elt) { return (elt == bibid); })) {
                continue;
            }

            var coverSpan = $("cover-" + bibid);
	
            if (coverSpan && bookinfo.thumbnail_url) {
		var formatSpan = $('gbooks_format_' + bibid);
		if (bookinfo.preview != 'noview' && formatSpan) {
		    formatSpan.style.visibility = 'visible';
		    formatSpan.style.display = 'inline';
		}

                coverSpan.innerHTML = buildCoverFragment (bibid,
                                                          bookinfo.preview_url,
                                                          bookinfo.thumbnail_url,
                                                          "Google books");

                seen.push (bibid);
            }
        }

        return seen;
    }


    function buildCoverFragment (bibid, link, thumbnail, caption)
    {
        return "<div align='center' class='resultsCover'>" +
        "  <a href='" + link + "'>" +
        "    <img  onmouseover='javascript:toggleCoverLabel(\"coverlabel-" + bibid + "\");' " +
        "          onmouseout='javascript:toggleCoverLabel(\"coverlabel-" + bibid + "\");'" +
        "          class='resultsCover' src='" + thumbnail + "' />" +
        "  </a>" +
        "  <span  id='coverlabel-" + bibid + "'  class='coverlabel'>" +
        "    <a href='" + link +  "' style='font-size:7pt;'>" + caption + "</a>" +
        "  </span>" +
        "</div>";
    }


    function populateOnlineShop (seen)
    {
        for (var isbn in info['shop']) {
            var bookinfo = info['shop'][isbn];
            var bibid = extdata_bibmap["ISBN:" + isbn];

            if (seen.find (function (elt) { return (elt == bibid) })) {
                continue;
            }

            var coverSpan = $("cover-" + bibid);

            if (!bookinfo.image.match (/noimage/)) {
                coverSpan.innerHTML = buildCoverFragment (bibid,
                                                          bookinfo.itemLink,
                                                          bookinfo.thumbnail,
                                                          "NLA Online Shop");
           }


            seen.push (bibid);
        }

        return seen;
    }


    function sourcesPending () {
        return sources.find (function (elt) { return !info[elt] });
    }


    function populateThumbnails (force)
    {
        if (populated) {
            return;
        }

        if (!force && sourcesPending ()) {
            return;
        }

        var seen = Array ();
        seen = populateOnlineShop (seen);
        seen = populateGoogleBooks (seen);

        populated_hooks.each (function (hook) {
                hook (info);
            });

        populated = true;
    }

    return {
        'add_hook' : function (fn) {
            populated_hooks.push (fn);
        },
        'receive' : receive
    };
}();



function receiveGoogleBooks (bookinfo)
{
    extdata.receive (bookinfo, 'gbooks');
}


function receiveOnlineShop (bookinfo)
{
    extdata.receive (bookinfo, 'shop');
}


