﻿function EmbeddedFormResize(s, t) {
    //position
    if (t.offset().top != s.offset().top) {
        //move the form directly over the target div
        s.css({ "top": t.offset().top + "px" });
        //set source width to match target
        s.width(t.width());
    }
    //height
    if (t.height() != s.height()) {
        t.height(s.height());
    }
}

function EmbeddedFormSetup(sourceDiv, targetDiv) {

    $(document).ready(function() {

        //setup
        sourceDiv.css({ "position": "absolute", "display": "none" });
 
        //Initial Resize
        EmbeddedFormResize(sourceDiv, targetDiv);

        //Ensure the div is shown
        sourceDiv.show();
        
        //Add click handler for faster redraws
        sourceDiv.live('click', function() { EmbeddedFormResize(sourceDiv, targetDiv); });

        //Add interval recalculation for any changes not actioned though a click
        setInterval(function() { EmbeddedFormResize(sourceDiv, targetDiv); }, 250);

    });
}

