package hyperscout;


import java.util.Hashtable;

import scone.netobjects.Link;
import scone.netobjects.LinkToken;
import scone.netobjects.NetNode;
import scone.proxy.HtmlTokenEditor;
import scone.util.ErrorLog;
import scone.util.tokenstream.HtmlTextToken;
import scone.util.tokenstream.SconePipe;
import scone.util.tokenstream.Token;
import scone.util.tokenstream.TokenInputStream;
import scone.util.tokenstream.TokenOutputStream;


/**
 * Create these magic popup windows...
 *
 * @author Harald Weinreich
 * @author Volkert Buchmann
 */

public class PopupAdder extends HtmlTokenEditor {
    public static final String COPYRIGHT = "(C) Harald Weinreich & Volkert Buchmann";

    private String hyperScoutBaseUrl = "http://_hyperscout.scone.de/res/";

    public void handleRequest(SconePipe pipe) {
        try {
            TokenInputStream in = pipe.getTokenInputStream();
            TokenOutputStream out = pipe.getTokenOutputStream();

            NetNode baseNode = (NetNode) in.getMetaInfo().get("baseNode");
            Hashtable popupNumbers = new Hashtable();
            StringBuffer popups = new StringBuffer(1024);

            popups.append("\n<SCRIPT Language='JavaScript1.2' SRC='http://_hyperscout.scone.de/popups");
            popups.append("?baseNodeId=" + baseNode.getNodeId());
   		
            Link link = null;
            Token t = null;
            boolean islink = false;

            while ((t = in.read()) != null) {      
   		   
                if (t instanceof LinkToken) {   		
                    link = ((LinkToken) t).getLink();
                    // Was already a popup created for this link Id?
                    if (!popupNumbers.containsKey(link.getLinkId())) {
                        popupNumbers.put(link.getLinkId(), link.getLinkId());
                        popups.append("&" + link.getLinkId());
                    }
                }// if LinkToken
                out.write(t);
            }// while
            popups.append("'></SCRIPT>");
            out.write(new HtmlTextToken(popups.toString()));
            pipe.close();
        } catch (Exception exc) {
            ErrorLog.log(this, "handleRequest()", "", exc);
        }       
    }

}
