package com.yourdomain.earth.client;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.types.Visibility;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;

public class ShimPeer extends Canvas {
	
	private Canvas parent;
	
	public ShimPeer(Canvas parent) {
		super();
		this.parent = parent;
		this.addDrawHandler(new DrawHandler() {
			@Override
			public void onDraw(DrawEvent event) {
		        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
		            
		            @Override
		            public void execute() {
						attachShim();
			        }
		        });
			}			
		});
	}
	
	private void attachShim() {
        int top = parent.getAbsoluteTop();
        int left = parent.getAbsoluteLeft();
        int width = parent.getVisibleWidth();
        int height = parent.getVisibleHeight();
        int zindex = parent.getZIndex()-1;
		this.setPosition(Positioning.ABSOLUTE);
		this.setTop(top);
		this.setLeft(left);
		this.setWidth(width);
		this.setHeight(height);
		this.setOverflow(Overflow.VISIBLE);
		this.setVisibility(Visibility.INHERIT);
		this.setZIndex(zindex);
		this.setContents(
				"<iframe height='100%' width='100%' scrolling='no' allowtransparency='yes' border='0' frameborder='0' marginwidth='0' marginheight='0' tabIndex='-1' tabStop='false' src=\"javascript:void(0);\"></iframe>");
	}
}
