/*
    This script is used to embed AssisDent schedule calendar to a page. The script creates
    a iframe in which content is loaded. The iframe is resized when content changes on the 
    child page. This is achieved through easyXDM-messages.
    
    XDM-messages are needed because otherwise we cannot communicate with parent page about
    content resizing because of "Same Origin Policy" security feature implemented in browsers.

    The script requires jQuery and easyXDM to be loaded.
*/

var BaseUrls = {
    schedule : "http://assisdent4.entteri.fi",
    anamnesis : "http://assisdent4.entteri.fi:8081"
};

var IframeBaseUrls = {
    schedule : BaseUrls.schedule + "/Content/iframe/iframe.html?url=",
    anamnesis : BaseUrls.anamnesis + "/Content/iframe/iframe.html?url="
};

var ContentUrls = {
    schedule : "/Booking?rid=",
    anamnesis : "/Anamnesis?organizationId="
};

var mainPageUrl = "";
var contentUrl = "";

// Decide which page to show
if (assisdent_type == 1) {
    // Schedule
    mainPageUrl = IframeBaseUrls.schedule;
    contentUrl = BaseUrls.schedule + ContentUrls.schedule + assisdent_rid;
}
else if (assisdent_type == 2) {
    // Anamnesis
    mainPageUrl = IframeBaseUrls.anamnesis;
    contentUrl = BaseUrls.anamnesis + ContentUrls.anamnesis + assisdent_rid;
}

// Create divs to hold content
$(document).ready(function() {
    $('div#assisdent').append($('<div id="assisdent_mainpage"></div>'));
    $('div#assisdent').append($('<div id="assisdent_subpage"></div>'));
});

// Create XDM socket for the main page
var scheduleSocket = new easyXDM.Socket({
    remote: mainPageUrl + encodeURIComponent(contentUrl),
    swf: "http://www.entteri.fi/iframe/easyxdm.swf",
    container: "assisdent_mainpage",
    onMessage: function(message, origin) {
        var msgParts = message.split("|");

        if (msgParts[0] == "resize") {
            var height = (parseInt(msgParts[1]) + 60);
            var iframe = $("div#assisdent_mainpage > iframe");
            iframe.css({"height": height + "px", "width" : "100%"});
            
            // Need to focus iframe contents so that iPad clicks work
            if (iframe.get(0) != null && iframe.get(0).contentWindow != null) {
                iframe.get(0).contentWindow.focus();
            }
        }
        else if (msgParts[0]=="anamnesisRequested") {
            var url = msgParts[1];

            // Hide current iframe and show the new one
            $("div#assisdent_mainpage > iframe").hide();

            // Create XDM socket for the sub page
            var anamnesisSocket = new easyXDM.Socket({
                remote: IframeBaseUrls.anamnesis + encodeURIComponent(url),
                swf: "http://www.entteri.fi/iframe/easyxdm.swf",
                container: "assisdent_subpage",
                onMessage: function(message, origin) {
                    var msgParts = message.split("|");

                    if (msgParts[0] == "resize") {
                        var height = (parseInt(msgParts[1]) + 60);
                        var iframe = $("div#assisdent_subpage > iframe");
                        iframe.css({"height": height + "px", "width" : "100%"});
                        
                        // Need to focus iframe contents so that iPad clicks work
                        if (iframe.get(0) != null && iframe.get(0).contentWindow != null) {
                            iframe.get(0).contentWindow.focus();
                        }
                    }
                }
            });
        }
    }
});

