

var hc2EmbedIframe = document.createElement('iframe');

function hc2LoadEmbed(){

        if(typeof beforeHc2LoadEmbed === "function") {
            beforeHc2LoadEmbed(hc2EmbedIframe);
        }

		var embedAnchor = document.getElementById('hc2-embed');
		var existingEmbedIframe = document.getElementById('hc2-embed-iframe');
		if(existingEmbedIframe) {
		    hc2EmbedIframe = existingEmbedIframe;
		}

        hc2EmbedIframe.setAttribute('id', 'hc2-embed-iframe');
        hc2EmbedIframe.setAttribute('frameborder', '0');
        hc2EmbedIframe.setAttribute('allowfullscreen', '');
        hc2EmbedIframe.setAttribute('webkitallowfullscreen', '');
        hc2EmbedIframe.setAttribute('mozallowfullscreen', '');
        hc2EmbedIframe.setAttribute('width', '100%');
        hc2EmbedIframe.setAttribute('onload', 'hc2LoadIframeResizer(this)');

        var embedData = {};
        if(embedAnchor) {
            for (var i = 0; i < embedAnchor.attributes.length; i++) {
                var attr = embedAnchor.attributes[i];
                if(attr.name !== 'id' && attr.name !== 'href')  {

                    if(attr.name.indexOf("data-app-") === 0) {
                        var dataAttrName = attr.name.substring(9);
                        var hyphenPos = dataAttrName.indexOf('-');
                        if(hyphenPos > 0) {
                            if(!embedData[dataAttrName.substring(0, hyphenPos)]) {
                                embedData[dataAttrName.substring(0, hyphenPos)] = {};
                            }
                            embedData[dataAttrName.substring(0, hyphenPos)][dataAttrName.substring(hyphenPos+1)]= attr.value;
                        } else {
                            embedData[dataAttrName] = attr.value;
                        }
                    } else {
                        hc2EmbedIframe.setAttribute(attr.name, attr.value);
                    }
                }
            }
        }

        
            var iframeUrl = 'https://91295.hc-apps.de/app/view/test/91295'+
            (window.location.search ? window.location.search + '&d=true&e=true' : '?d=true&e=true') +
                '';
            if(window.location.hash && window.location.hash.indexOf("#data-contentId") === 0) {
                iframeUrl += '&data-contentId=' + window.location.hash.split("=")[1];
            }
            var embedDataParam = JSON.stringify(embedData);
            if(embedDataParam.length > 2) {
                iframeUrl += '&ad='+encodeURIComponent(embedDataParam);
            }

            hc2EmbedIframe.setAttribute('src', iframeUrl);
        

        if(embedAnchor) {
            embedAnchor.parentNode.replaceChild(hc2EmbedIframe, embedAnchor);
        }

        if(!existingEmbedIframe) {
            var appleIframeStyles = document.createElement('style');
            appleIframeStyles.innerText = '#hc2-embed-iframe{width: 1px;min-width: 100%;*width: 100%;}';
            hc2EmbedIframe.parentNode.insertBefore(appleIframeStyles, hc2EmbedIframe);
        }
}

function hc2IFrameReadyCallback() {
    hc2SendParentWindowInfo();
    if(typeof afterHc2LoadEmbed === "function") {
        afterHc2LoadEmbed(hc2EmbedIframe);
    }
    hc2SendGaClientId(0);
}
function hc2LoadIframeResizer (hc2EmbedIframe) {
    window.hc2EmbedIframe = hc2EmbedIframe;
    hc2LoadScript("https://91295.hc-apps.de/assets/plugins/iframe-resizer/iframeResizer.min.js", function() {
                var iframes = iFrameResize( {messageCallback:hc2MessageHandler, initCallback:hc2SendParentWindowInfo}, hc2EmbedIframe );
                window.addEventListener("resize", hc2SendParentWindowInfo);
            });
}

function hc2MessageHandler(messageObject) {
    switch(messageObject.message.messageId) {
        case "contentIdChange":
            this.location.hash = messageObject.message.val ? "data-contentId=" + messageObject.message.val : "";
            break;
        case "didStepChange":
            var pos = hc2FindPos(hc2EmbedIframe);
            var scrollToOffset = 0;
            window.scrollTo(0,pos-scrollToOffset);
            break;
        case "didTransition":
            hc2SendParentWindowInfo();
            break;
        case "ready":
            hc2IFrameReadyCallback();
            break;
        case "event":
            if(typeof onHc2Event === "function") {
                onHc2Event(messageObject.message);
            }
            break;
    }
}
function hc2SendParentWindowInfo() {
    var hc2ParentPageInfo = {
        messageId:"parentWindowInfo",
        width:window.innerWidth,
        height:window.innerHeight,
        location: window.location,
    };
    if(typeof hc2EmbedOrientation !== 'undefined') { hc2ParentPageInfo.hc2EmbedOriantation = hc2EmbedOrientation }
    hc2EmbedIframe.iFrameResizer.sendMessage(
        hc2ParentPageInfo
    );
}
function hc2SendGaClientId(loop){
    var ga = window.ga;
    var gaClientId = ga && ga.getAll && ga.getAll() && ga.getAll()[0] && ga.getAll()[0].get('clientId');
    if(gaClientId) {
        hc2EmbedIframe.iFrameResizer.sendMessage(
            {messageId:"setGaClientId", clientId:gaClientId}
        );
    } else {
    if(loop < 20)
        setTimeout(function() { hc2SendGaClientId(++loop); }, 500);
    }
}
function hc2LoadScript(url, callback)
{
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement("script");
	script.src = url;

	// Attach handlers for all browsers
	var done = false;
	script.onload = script.onreadystatechange = function()
	{
		if( !done && ( !this.readyState
					|| this.readyState == "loaded"
					|| this.readyState == "complete") )
		{
			done = true;

			// Continue your code
			callback();

			// Handle memory leak in IE
			script.onload = script.onreadystatechange = null;
			head.removeChild( script );
		}
	};

	head.appendChild(script);
}
function hc2FindPos(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    return [curtop];
    }
}

hc2LoadEmbed();