(function() {
  KurierPlikow.frameSrc = 'http://kurierplikow.pl/lite/uploader/panel.html';
  //-kp:obfuscate
/*
 * a backwards compatable implementation of postMessage
 * by Josh Fraser (joshfraser.com)
 * released under the Apache 2.0 license.
 *
 * this code was adapted from Ben Alman's jQuery postMessage code found at:
 * http://benalman.com/projects/jquery-postmessage-plugin/
 *
 * other inspiration was taken from Luke Shepard's code for Facebook Connect:
 * http://github.com/facebook/connect-js/blob/master/src/core/xd.js
 *
 * the goal of this project was to make a backwards compatable version of postMessage
 * without having any dependency on jQuery or the FB Connect libraries
 *
 * my goal was to keep this as terse as possible since my own purpose was to use this
 * as part of a distributed widget where filesize could be sensative.
 *
 */

// everything is wrapped in the XD function to reduce namespace collisions
var XD = function(){

    var interval_id,
    last_hash,
    cache_bust = 1,
    attached_callback,
    window = this;

    return {
        postMessage : function(message, target_url, target) {

            if (!target_url) {
                return;
            }

            target = target || parent;  // default to parent

            if (window['postMessage']) {
                // the browser supports window.postMessage, so call it with a targetOrigin
                // set appropriately, based on the target_url parameter.
                target['postMessage'](message, target_url.replace( /([^:]+:\/\/[^\/]+).*/, '$1'));

            } else if (target_url) {
                // the browser does not support window.postMessage, so set the location
                // of the target to target_url#message. A bit ugly, but it works! A cache
                // bust parameter is added to ensure that repeat messages trigger the callback.
                target.location = target_url.replace(/#.*$/, '') + '#' + (+new Date) + (cache_bust++) + '&' + message;
            }
        },

        receiveMessage : function(callback, source_origin) {

            // browser supports window.postMessage
            if (window['postMessage']) {
                // bind the callback to the actual event associated with window.postMessage
                if (callback) {
                    attached_callback = function(e) {
                        if ((typeof source_origin === 'string' && e.origin !== source_origin)
                        || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                            return !1;
                        }
                        callback(e);
                    };
                }
                if (window['addEventListener']) {
                    window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                } else {
                    window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                }
            } else {
                // a polling loop is started & callback is called whenever the location.hash changes
                interval_id && clearInterval(interval_id);
                interval_id = null;

                if (callback) {
                    interval_id = setInterval(function(){
                        var hash = document.location.hash,
                        re = /^#?\d+&/;
                        if (hash !== last_hash && re.test(hash)) {
                            last_hash = hash;
                            callback({data: hash.replace(re, '')});
                        }
                    }, 100);
                }
            }
        }
    };
}();
  //-kp:obfuscate

(function() {
var kpEmbedding = function(opts) {
    var iframe_style = "overflow:hidden; margin:0;padding:0;"
    var kppanel_style = "";
    var kpel = document.getElementById(opts.panel);
    var el = document.createElement("iframe");
    var loaderVisible = true;
    var iframeId = 'ifr'+opts.panel;
    opts.frameSrc += "#" + encodeURIComponent(document.location.href);

    /*
     iframe_style += "position:absolute;position:fixed; left: 0; top:0; zindex: 2;";
     kppanel_style += "position: absolute; left: 0; top:0; width: 100%; height:100%;" +
     "background-color: white;opacity:.7;filter:alpha(opacity=70);"
     */
    var ifattr = {
        id: iframeId, width: '100%', height: '1', 'scrolling': 'no', 'marginWidth':0,
        'marginHeight':0, 'noResize': 0,'border': 0, 'frameBorder':0, 'frameSpacing':0,
        'background': 'transparent','allowTransparency': 'allowTransparency',
        'name' : encodeURIComponent(window.location.href)+'#'+Math.random(),
        'style':iframe_style};
    for (var i in ifattr) {
        el.setAttribute(i, ifattr[i]);
    }
    kpel.appendChild(el);
    el.src=opts.frameSrc;

    XD.receiveMessage(function(message) {
        el = document.getElementById(iframeId);  // need to reload for IE6
        el.style.height = message.data;
        el.setAttribute('height',message.data);
        //kpel.appendChild(document.createElement("div"));
        if (loaderVisible) {
            loaderVisible = false;
            var kpellink = document.getElementById(opts.link);
            try {
                kpel.removeChild(kpellink);
            } catch (ie6workaround) {
                kpellink.parentNode.removeChild(kpellink);
            }
        }
    }, null); //TODO dodac source_orgin - musi to byc http[s]://host[:port] - port jesli inny niz standardowy


    //var mask = document.createElement("div");
    //mask.setAttribute("style", kppanel_style );
    //kpel.appendChild(mask);
};

//wrap with ready window event
var runOnDomReady = function(func, opts) {
    var hasRun = false;
    var run = function() {
        if(!hasRun) {
            hasRun = true;
            func(opts);
        }
    }

    //IE dom ready trick
    var scrollCheck = function() {
        if (hasRun) { return;}
    	try {
	    	// If IE is used, use the trick by Diego Perini
		    // http://javascript.nwbox.com/IEContentLoaded/
		    document.documentElement.doScroll("left");
	    } catch( domNotReady ) {
		    setTimeout( scrollCheck, 1 );
		    return;
	    }
        run(); //run when dom ready
    }
    try {
        // Catch cases where the browser event has already occurred.
        if ( document.readyState === "complete" ) {
            run(); return;
        };
		// Mozilla, Opera and webkit nightlies currently support this event
		if ( document.addEventListener ) {
			// Use the handy event callback
			document.addEventListener( "DOMContentLoaded", run, false );
			// A fallback to window.onload, that will always work
			window.addEventListener( "load", run, false );

		// If IE event model is used
		} else if ( document.attachEvent ) {
			// ensure firing before onload,
			// maybe late but safe also for iframes
			document.attachEvent("onreadystatechange", run);

			// A fallback to window.onload, that will always work
			window.attachEvent( "onload", run );

            // If IE and not a frame
			// continually check to see if the document is ready
			var toplevel = false;

			try {
				toplevel = window.frameElement == null;
			} catch(e) {}

			if ( document.documentElement.doScroll && toplevel ) {
				scrollCheck();
			}
        }
	} catch (cantCatchDomEvent) {
        run(); //nothing to loose
    }
};


window['KurierPlikowEmbed'] = function(opts) { runOnDomReady(kpEmbedding, opts); };

})();


  KurierPlikowEmbed(KurierPlikow);
})();
