/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.corejsf;

import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

/**
 *
 * @author Ivan
 */
public class AccordionRenderer extends Renderer {

    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {

        ResponseWriter writer = context.getResponseWriter();
        processIncludeScripts(writer);
        writeEnclosingDiv(component, writer);
    }

//ubacujemo script biblioteke
    public void processIncludeScripts(ResponseWriter writer) throws IOException {

        writer.write("<script type=\"text/javascript\" src=\"prototype.js\"></script>");
        writer.write("<script type=\"text/javascript\" src=\"rico-1.1.2.js\"></script>");
        writer.write("<script type=\"text/javascript\" src=\"scriptaculous.js\"></script>");

    }
    //metoda koja otvara spoljni div u njega ce ici unutrasnji div za panel

    public void writeEnclosingDiv(UIComponent component, ResponseWriter writer) throws IOException {
        String id = (String) component.getAttributes().get("name");
        String styleClass = (String) component.getAttributes().get("styleClass");
        //id je obavezan koristi se da bi se rico biblioteci prosledio kao argument!
        writer.write("<div id='" + id + "' ");
        if (styleClass != null) {
            writer.write("class='" + styleClass + "'>");
        }
    }

    public boolean getRendersChildren() {
        return false;
    }

    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        writer.write("</div>");
        String divName = (String) component.getAttributes().get("name");
        writer.write("<script type=\"text/javascript\">");
        writer.write("new Rico.Accordion($('" + divName + "') ,");
        writer.write("{");
        String panelHeight = (String) component.getAttributes().get("panelHeight");
        if(panelHeight==null)panelHeight="100";
        writer.write("   panelHeight: " + panelHeight);
        writer.write(" } ");
        writer.write(");");
        writer.write("</script>");


    }
}
