﻿/* 
Allocar - API

Copyright(C) Allocar Concepts / BcomOne New Media Solutions
Soest, The Netherlands

for support contact: support@bcomone.nl
*/

window.allocarHostFrameUrl = null;
window.allocarHostUrl = null;
window.allocarGuestUrl = null;
window.allocarDone = false;
window.allocarHostFluidCallback = null;
window.allocarHostFrameId = 'iframeAllocar';
window.allocarHostHelperUrl = '/allocarapi.htm';
window.allocarHostFrameParamName = 'url';

function allocarInitHost(hostFrameId, hostUrl, hostFrameUrl, guestUrl, hostFrameParamName) {
    allocarDone = false;
    window.allocarHostUrl = hostUrl;
    window.allocarHostFrameUrl = hostFrameUrl;
    window.allocarGuestUrl = guestUrl;
    window.allocarHostFrameId = hostFrameId;
    window.allocarHostFrameParamName = hostFrameParamName;
}

function allocarInitFluidCallback(callback) {
    if (callback !== 'undefined') {
        window.allocarHostFluidCallback = callback;
    }
}

function allocarLoadScript(url, id) {
    (function (d, scriptUrl, scriptId) {
        var js, id = scriptId; if (d.getElementById(scriptId)) { return; }
        js = d.createElement('script'); js.id = scriptId; js.async = true;
        js.src = scriptUrl;
        d.getElementsByTagName('head')[0].appendChild(js);
    } (document, url, id));
}

function allocarLoadStyleSheet(url, id) {
    (function (d, styleSheetUrl, styleSheetId) {
        var stylesheet, id = styleSheetId; if (d.getElementById(styleSheetId)) { return; }
        stylesheet = d.createElement('link'); 
        stylesheet.id = id;
        stylesheet.type = 'text/css';
        stylesheet.rel = 'stylesheet';
        stylesheet.href = styleSheetUrl;
        d.getElementsByTagName('head')[0].appendChild(stylesheet);
    } (document, url, id));
}

function allocarInitGuest(hostUrl, hostHelperUrl) {
    allocarLog('initguest: ' + hostUrl + ' - ' + hostHelperUrl);
    window.allocarHostUrl = hostUrl;
    window.allocarHostHelperUrl = hostHelperUrl;
}

function allocarLog(msg) {
    try {
        console.log(msg);
    }
    catch (e) {
    }
}
function allocarFrameUrl(url) {
    var sep = '?';
    if (window.allocarHostFrameUrl.indexOf('?') > 0) sep = '&';
    return window.allocarHostUrl + window.allocarHostFrameUrl + sep + window.allocarHostFrameParamName + "=" + encodeURIComponent(url);
}

function allocarGetParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function allocarLoadFrame() {
    url = allocarGetParameterByName(window.allocarHostFrameParamName);
    if (url != null) {
        var frame = document.getElementById(window.allocarHostFrameId);
        frame.contentWindow.location.replace(window.allocarGuestUrl + url);
    }
}

function doFluid() {
    allocarLog('dofluid');
    allocarGuestFluid();
}

function allocarGuestFluid(p) {
    allocarLog('allocarGuestFluid');
    if (window.allocarHostUrl != null && window.allocarHostHelperUrl != null) {
        if (!p) {
            p = 'h';
        }

        var fluid = document.getElementById('frameFluid');

        if (p == 'h') {
            var h = 0;
            if (document && document.body.offsetHeight) {
                h = document.body.offsetHeight;
            }
            else {
                if (a.Document && a.Document.body.scrollHeight) {
                    h = document.body.scrollHeight;
                }
            }
            var loc = window.allocarHostUrl + window.allocarHostHelperUrl + "?h=" + h;
            allocarLog(loc);
            fluid.src = loc;
        }
        else {
            var loc = window.allocarHostUrl + window.allocarHostHelperUrl + "?" + p;
            fluid.src = loc;
        }
    }
}


// proxy executes

function allocarHelperFluid() {
    allocarXSSHelper();
}

function allocarXSS() {
    if (window.parent && window.parent.parent) {
        try {
            window.parent.parent.allocarHostFluid(window.location.search);
        }
        catch (err) {
        }
    }
}

// host sets
function allocarHostFluid(p) {
    if (window.allocarHostFrameId != null) {
        if (p !== 'undefined') {
            var a = document.getElementById(window.allocarHostFrameId);
            if (p.indexOf('?') == 0) { p = p.substring(1); }
            if (p.indexOf('h=') == 0) {
                h = p.substring(2);
                a.height = h;
            }
            if (p == 'r') {
                window.location.reload(true);
            }
            if (p.indexOf('r=') == 0) {
                var url = decodeURIComponent(p.substring(2));
                //window.location = url;
                //window.location.reload(true);
            }
            if (window.allocarHostFluidCallback) {
                window.allocarHostFluidCallback();
            }
        }
    }
}

// for backwards compatibility with older fluid
function doFluidSet(p) {
    allocarHostFluid(p);
}



