package lookoflink;


import java.util.Hashtable;

import scone.netobjects.LinkToken;
import scone.proxy.HtmlTokenEditor;
import scone.util.ErrorLog;
import scone.util.tokenstream.HtmlTagToken;
import scone.util.tokenstream.SconePipe;
import scone.util.tokenstream.Token;
import scone.util.tokenstream.TokenInputStream;
import scone.util.tokenstream.TokenOutputStream;


/**
 * Adds onClick-Events to all A and AREA elements.<p>
 * These Events make the little popup-Windows visible.<p>
 *
 * @author Harald Weinreich
 */

public class LinkMarkerModifyer extends HtmlTokenEditor {

    private Plugin plugin;

    private Hashtable serverlist = new Hashtable();

    public LinkMarkerModifyer(Plugin plugin) {
        this.plugin = plugin;
    }

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

            LinkToken lt = null;
            HtmlTagToken tag = null;
            Token t = null;

            while ((t = in.read()) != null) {
                if ((t instanceof LinkToken)) {
                    lt = (LinkToken) t;

                    String onMouseOver = "lol_activate(event,'"
                            + lt.getLink().getLinkId() + "');";
                    String onMouseOut = "lol_deactivate();";
                    String onClick = "lol_deactivate();";

                    if (lt.hasParam("onmouseover")) {
                        onMouseOver += lt.getParam("onmouseover");
                        lt.removeParam("onmouseover");
                    }
                    if (lt.hasParam("onmouseout")) {
                        onMouseOut += lt.getParam("onmouseout");
                        lt.removeParam("onmouseout");
                    }
                    if (lt.hasParam("onclick")) {
                        onClick += lt.getParam("onclick");
                        lt.removeParam("onclick");
                    }

                    lt.setParam("onmouseover", onMouseOver, "onMouseOver");
                    lt.setParam("onmouseout", onMouseOut, "onMouseOut");
                    lt.setParam("onclick", onClick, "onClick");

                }// is HtmlTagToken
                out.write(t);
            }// while
        } catch (Exception exc) {
            ErrorLog.log(this, "handleRequest()", "", exc);
        }
    }

}
