function EbookList( initLibs, initDlg, initDirect ) {
   //alert( "constructor called" );
   initLibs ? this.libs= true: this.libs= false;
   initDlg ? this.dlg = true: this.dlg = false;
   initDirect ? this.direct = true: this.direct = false;
   //alert( "initial direct=" + this.direct );
      
   this.isbne = "Not available";
   this.editions = {};
   this.dlg_link = "";

   EbookList.prototype.setLibs = function( available )  {
      available ? this.libs = true: this.libs = false;
   }

   EbookList.prototype.getLibs = function() {
      return this.libs;
   }

   EbookList.prototype.setDlg = function( isbn ) {
      this.dlg = true;
      isbn = isbn.replace( /-/g,"" ); // strip all hyphens from ISBN so we can use in URL
      this.dlg_link = "http://dlg.galileo.usg.edu/ugapressbks/do-pdf:ugp" + isbn //  .pdf is appeneded automatically
   }

   EbookList.prototype.getDlg = function( ) {
      return this.dlg;
   }

   EbookList.prototype.getDlgLink = function() {
      return this.dlg_link;
   }

   EbookList.prototype.setDirect = function( available ) {
      available ? this.direct = true: this.direct = false;
   }

   EbookList.prototype.getDirect = function( ) {
      return this.direct;
   }

   EbookList.prototype.setIsbne = function( in_isbn )  {
       this.isbne = in_isbn;
   }

   EbookList.prototype.getIsbne = function() {
      return this.isbne;
   }

   EbookList.prototype.addEdition = function( supplier, identifier ) {
      //alert( "addEdition called" );   
      if ( supplier && identifier ) {
         this.editions[ supplier ] = identifier;
      }
      //alert( "addEdition finished" );
   }

   EbookList.prototype.getEditionId = function( supplier ) {
      //alert( "getEditionId called" );
      if ( supplier ) return this.editions[ supplier ];
   }

   //alert( "constructor finished" );
}

var wroteEbooks = false;

function addEventHandlers() {
    //alert( "addEventHandlers called" );
    //alert( "location= " + document.location );
    var iframe = document.getElementById( "ebook_iframe" );
    iframe.addEventListener( "mouseover", showEbookFrame() );
}

function showEbookFrame( ) {
    if ( wroteEbooks == false ) {
      writeEbooks();
      wroteEbooks = true;
    }
    //alert( "showIFrame called" );    
    var iframe = document.getElementById( "ebook_iframe" );
    //alert( "showIframe iframe= " + iframe );
    iframe.style.visibility = "visible";
}

function hideEbookFrame() {
    //alert( "hideIFrame called" );
    var iframe = document.getElementById( "ebook_iframe" );
    //alert( iframe );
    if ( iframe.style.visibility == "visible" ) {
	iframe.style.visibility = "hidden";
    }
}

function openEbookWin( ebooks ) {
       //alert( "openEbookWin called" );
       //alert( "opener= " + parent.location);
       //alert( "parent.ebooks = " + parent.ebooks );
        var ebook_win = window.open("http://www.ugapress.org/index.php/ugapress_embeds/ebook_popup_window", "ebooks");
	//ebook_win.moveTo(0,350); // position the window in the lower left of the screen
       // writeEbooks( ebooks );
}

function writeEbooks( ) {
   ebook_window = frames[0];

   // get the document nodes and assign variables
   var header = ebook_window.document.getElementById( "ebook_header" );
   var libs = ebook_window.document.getElementById( "ebook_libs" );
   var dlg = ebook_window.document.getElementById( "ebook_dlg" );
   var direct = ebook_window.document.getElementById( "ebook_direct" );
   var isbn = ebook_window.document.getElementById( "ebook_isbn" );

   // availability constants

   // libraries
   var libs_msg = document.createElement( "td" );
   libs_msg.className = "textbody";
   var LIBS_HAVE  = ebook_window.document.createTextNode( "This title is available as an ebook in select libraries through ebook providers including Ebrary, MyiLibrary, and NetLibrary. Please ask your local library if this edition is available." );
   var LIBS_NOT_HAVE = ebook_window.document.createTextNode( "Not available" );

    // DLG
    var dlg_msg = ebook_window.document.createElement( "td" );
    dlg_msg.className = "textbody";
    var DLG_HAS  = ebook_window.document.createTextNode( "This title is available as a free downloadable PDF through the University of Georgia Press." );
    var DLG_NOT_HAS = ebook_window.document.createTextNode( "Not available." );

    // direct
    var direct_msg = ebook_window.document.createElement( "td" );
    direct_msg.className = "textbody";
    var DIRECT_HAS  = ebook_window.document.createTextNode( "This title is available direct to customers through the following:" );
    var DIRECT_NOT_HAS = ebook_window.document.createTextNode( "Not available." );

    // write
    if ( ebooks[ "libs" ] == true ) {
	//alert( "libs true" );
        // alert( YES.nodeValue );
        //libs.appendChild( LIBS_YES );
        //alert( "still true" );
	libs_msg.appendChild( LIBS_HAVE );
    }
    else {
	//alert( "libs false" );
        //libs.appendChild( LIBS_NO );
	libs_msg.appendChild( LIBS_NOT_HAVE );
        libs.style.visibility = "hidden";
    }

    //alert( "outside libs" );
    libs.appendChild( libs_msg );

    // allert( "if dlg reached" );
    if ( ebooks[ "dlg" ] == true ) {
	//alert("DLG YES");
        //alert( dlg );
       // dlg.appendChild( DLG_YES );
        var link = document.createElement( "a" );
        link.href = ebooks.getDlgLink();
        link.target = window.parent;
        //alert( link.href + " " + link.target );
        link.appendChild( DLG_HAS );
        dlg_msg.appendChild( link );
        dlg.appendChild( dlg_msg );
    }
    else {
	//alert("DLG NO");
        //dlg.appendChild( DLG_NO );
	dlg_msg.appendChild( DLG_NOT_HAS );
        dlg.style.visibility = "hidden";
    }

    dlg.appendChild( dlg_msg );

   //alert( ebooks[ "direct" ] );

   //Create the ISBN number row
   if ( ebooks[ "isbne" ] != "" ) {
        var isbn_text = ebook_window.document.createTextNode( ebooks.isbne );
        var isbn_td = document.createElement( "td" );
        isbn_td.appendChild( isbn_text );
        isbn.appendChild( isbn_td );
   }

   /* The way this code is structured, it is essential that the EE record for the book has the property setting for its "ebook_direct" field,
       otherwise we will fail to add the links */
   if ( ebooks[ "direct" ] == true ) {
         //var DIRECT_YES = document.createTextNode("YES");
         //direct.appendChild( DIRECT_YES );
	var list = ebook_window.document.createElement( "ul" ); // needs to be defined here to be accessible after the next if and for statements?
	// alert( "before for" );

        // alert( "ebooks.editions= " + ebooks.editions );
	for ( src in ebooks.editions ) {
	    // alert( "for reached" );
	    // alert( list );
	    // alert( ebooks.editions[src] );
	    var src_text = "Provider not specified";
	    var link_label = ebook_window.document.createTextNode( src_text );
	    var link = document.createElement( "a" );
	    var list_entry = ebook_window.document.createElement( "li" );
   
	    switch( src ) {
	    case "book_asin": 
		// alert( "case asin reached" );
		link_label = ebook_window.document.createTextNode( "Amazon Kindle" );
		//alert( link_label );
		link.href = "http://amazon.com/gp/product/" + ebooks.editions["book_asin"];
		link.target = window.parent;
		// alert( link.href );
		link.appendChild( link_label );
		list_entry.appendChild( link );
		list.appendChild( list_entry );
		// alert( list_entry );
		break;
            case "book_scribd": 
		// alert( "case scribd reached" );
		link_label = ebook_window.document.createTextNode( "Scribd" );
		//alert( link_label );
		link.href = "http://www.scribd.com/doc/" + ebooks.editions["book_scribd"];
		link.target = window.parent;
		// alert( link.href );
		link.appendChild( link_label );
		list_entry.appendChild( link );
		list.appendChild( list_entry );
		// alert( list_entry );
		break;
	    } // don't need a default case because src_text defined beforehand
	}
	direct_msg.appendChild( DIRECT_HAS );
	direct_msg.appendChild( list );
        direct.appendChild( direct_msg );
    }
    else {
	//alert("DIRECT NO");
        //var DIRECT_NO = ebook_window.document.createTextNode("NO");
        //direct.appendChild( DIRECT_NO );
	direct_msg.appendChild( DIRECT_NOT_HAS );
        direct.appendChild( direct_msg );
        direct.style.visibility = "hidden";
    }
}