﻿function SC_Interstitial() {
    if (SC_Interstitial.singleton == null) { SC_Interstitial.singleton = new SC_InterstitialClass(); }
    return SC_Interstitial.singleton;
}
function SC_InterstitialClass() {
    this.debugLayer = document.getElementById('SC_DEBUGLAYER'); this.browser = 'UNKNOWN'; this.version = 0.0; this.opacity = 0.8; this.mediaPlayerClassId = '6BF52A52-394A-11D3-B153-00C04F79FAA6'; this.flashClassId = 'D27CDB6E-AE6D-11CF-96B8-444553540000'; this.cdnBase = 'http://cdn.smartclip.net/interstitial/'; this.miniPlayerUrl = this.cdnBase + 'interstitial.swf'; this.backgroundColor = '000000'; this.audioVolume = 100; this.videoAdUrl = null; this.videoAdXML = null; this.layer = null; this.layerframe = null; this.playerLayer = null; this.adisavailable = false; this.player = null; this.adWidth = 640; this.adHeight = 360; this.zIndex = 10000; this.flashHiding = true; this.polite = true; this.offSetLeft = 0; this.onStartCallback = null; this.onEndCallback = null; this.headerText = 'Weiter geht es nach einem kurzen Werbespot'; this.headerTextColor = 'FFFFFF'; this.closeText = 'Schliessen'; this.closeDelay = 0; this.borderWidth = 12; this.controlHeight = 30; this.colorSet = 0; this.timeText = 'Sekunden'; this.setDimension = function (_width, _height)
    { this.debug('- setDimension(' + _width + ', ' + _height + ')'); this.adWidth = _width; this.adHeight = _height; return this; }
    this.setColorSet = function (_colorSet)
    { this.debug('- setColorSet(' + _colorSet + ')'); this.colorSet = _colorSet; return this; }
    this.setPosLeft = function (_offSetLeft) { this.debug("- setPosLeft(" + _offSetLeft + ")"); this.offSetLeft = _offSetLeft; return this; }; this.setBorderWidth = function (_width)
    { this.debug('- setBorderWidth(' + _width + ')'); this.borderWidth = _width; return this; }
    this.setTimeText = function (_text)
    { this.debug('- setTimeText(' + _text + ')'); this.timeText = _text; return this; }
    this.setHeaderText = function (_text)
    { this.debug('- setHeaderText(' + _text + ')'); this.headerText = _text; return this; }
    this.setHeaderTextColor = function (_color)
    { this.debug('- setHeaderTextColor(' + _color + ')'); this.headerTextColor = _color; return this; }
    this.setCloseText = function (_text)
    { this.debug('- setCloseText(' + _text + ')'); this.closeText = _text; return this; }
    this.setCloseDelay = function (_delay)
    { this.debug('- setCloseDelay(' + _delay + ')'); this.closeDelay = _delay; return this; }
    this.setOnStartCallback = function (_onStartCallback)
    { this.debug('- setOnStartCallback(' + typeof (_onStartCallback) + ')'); this.onStartCallback = _onStartCallback; return this; }
    this.setOnEndCallback = function (_onEndCallback)
    { this.debug('- setOnEndCallback(' + typeof (_onEndCallback) + ')'); this.onEndCallback = _onEndCallback; return this; }
    this.setPolite = function (_polite)
    { this.debug('- setPolite(' + _polite + ')'); this.polite = _polite; return this; }
    this.setZIndex = function (_zIndex) {
        this.debug('- setZIndex(' + _zIndex + ')'); this.zIndex = new String(_zIndex); if (this.zIndex == null || !this.zIndex.match(/^[1-9][0-9]*$/)) { this.zIndex = 10000; this.debug('- Redefining z-index ' + _zIndex + ' with ' + this.zIndex + '.'); }
        return this;
    }
    this.setBackgroundColor = function (_color) {
        this.debug('- setBackgroundColor(' + _color + ')'); this.backgroundColor = _color; if (this.backgroundColor == null || typeof (this.backgroundColor.match) == 'undefined' || !this.backgroundColor.match(/^[A-Fa-f0-9]{6}$/)) { this.backgroundColor = '000000'; this.debug('- Redefining invalid color ' + _color + ' with ' + this.backgroundColor + '.'); }
        return this;
    }
    this.setAudioVolume = function (_volume) {
        this.debug('- setAudioVolume(' + _volume + ')'); this.audioVolume = _volume; if (isNaN(this.audioVolume)) { this.audioVolume = 100; this.debug('- redefining invalid volume ' + _volume + ' with ' + this.audioVolume); }
        return this;
    }
    this.setOpacity = function (_opacity) {
        this.debug('- setOpacity(' + _opacity + ')'); this.opacity = _opacity; if (isNaN(this.opacity) || this.opacity < 0.0 || this.opacity > 1.0) { this.opacity = 0.8; this.debug('- Redefining invalid opacity ' + _opacity + ' with ' + this.opacity); }
        return this;
    }
    this.setVideoAdUrl = function (_url)
    { this.debug('- setVideoAdUrl(' + _url + ')'); this.videoAdUrl = _url; return this; }
    this.setVideoAdXML = function (_xml)
    { this.debug('- setVideoAdXML(' + _xml + ')'); this.videoAdXML = _xml; return this; }
    this.disableFlashHiding = function ()
    { this.flashHiding = false; return this; }
    this.init = function ()
    { this.debug('- init(polite = ' + this.polite + ')'); var onLoad = false; var onResize = this.addEvent(window, "resize", SC_onResize); var onScroll = this.addEvent(window, "scroll", SC_onResize); var flashVer = this.getFlashVersion(); if (!onResize || !onScroll || flashVer < 9 || !this.detectBrowser()) { this.removeEvent(window, "resize", SC_onResize); this.removeEvent(window, "scroll", SC_onResize); this.endAd(); } else if (this.polite) { onLoad = this.addEvent(window, "load", SC_onLoad); if (!onLoad) { this.removeEvent(window, "load", SC_onLoad); this.removeEvent(window, "resize", SC_onResize); this.removeEvent(window, "scroll", SC_onResize); this.endAd(); } } else { this.initLayers(); } }
    this.loadAd = function ()
    { this.debug('- loadAd(' + this.videoAdUrl + ', ' + this.adWidth + ', ' + this.adHeight + ')'); if (this.videoAdUrl != null) { this.player.loadVideoAdFromUrl(this.videoAdUrl, this.audioVolume); } else { this.player.loadVideoAdFromXML(this.videoAdXML, this.audioVolume); } }
    this.startingAd = function (_width, _height) {
        this.debug('- startingAd(P[' + this.adWidth + ',' + this.adHeight + '], V[' + _width + ',' + _height + '])'); this.adisavailable = true; this.onResize(); if (this.flashHiding) { this.hideObjectsAndEmbeds(); }
        this.layer.style.visibility = 'visible'; this.makeTransparent(this.playerLayer, 1.0); if (this.onStartCallback != null) { try { this.onStartCallback(); } catch (e) { this.debug('Couldnt call on start callback function - ' + e); } } 
    }
    this.hideObjectsAndEmbeds = function ()
    { if (typeof (document.getElementsByTagName) != 'undefined' && document.getElementsByTagName != null) { var tags = ['object', 'embed', 'iframe']; for (var j = 0; j < tags.length; j++) { var objects = document.getElementsByTagName(tags[j]); for (var i = 0; i < objects.length; i++) { if (objects[i].getAttribute('id') != 'SC_InterstitialPlayer') { objects[i].setAttribute('sc_ov', objects[i].style.visibility == null ? 'visible' : objects[i].style.visibility); objects[i].style.visibility = 'hidden'; } } } } }
    this.endAd = function () {
        this.debug('- endAd()'); if (this.layer != null) { this.layer.style.visibility = 'hidden'; }
        if (this.playerLayer != null) { this.playerLayer.style.visibility = 'hidden'; }
        if (this.flashHiding) { this.showObjectsAndEmbeds(); }
        if (this.onEndCallback != null) { try { this.onEndCallback(); } catch (e) { this.debug('Couldn\'t call on end callback function'); } } 
    }
    this.showObjectsAndEmbeds = function ()
    { if (typeof (document.getElementsByTagName) != 'undefined' && document.getElementsByTagName != null) { var tags = ['object', 'embed', 'iframe']; for (var j = 0; j < tags.length; j++) { var objects = document.getElementsByTagName(tags[j]); for (var i = 0; i < objects.length; i++) { var ov = objects[i].getAttribute('sc_ov'); if (ov != null) { objects[i].style.visibility = ov; } } } } }
    this.stopAd = function ()
    { this.debug('- stopAd()'); this.player.stopVideoAd(); }
    this.onResize = function () {
        this.debug('- onResize()'); if ((this.layer != null && this.playerLayer != null) && this.adisavailable == true) {
            var scroll = this.getScrolling(); var viewport = this.getViewport(); var popWidth = viewport[0]; if (popWidth < scroll[2]) { popWidth = scroll[2]; }
            var popHeight = viewport[1]; if (popHeight < scroll[3]) { popHeight = scroll[3]; }
            this.layer.style.height = popHeight + "px"; this.layer.style.width = popWidth + "px"; this.playerLayer.style.width = this.adWidth + 2 * this.borderWidth + "px"; this.playerLayer.style.height = this.adHeight + 2 * this.borderWidth + this.controlHeight + "px"; if (this.offSetLeft != 0) { this.playerLayer.style.left = this.offSetLeft + "px"; } else { this.playerLayer.style.left = Math.round(scroll[0] + (viewport[0] - this.adWidth - 2 * this.borderWidth) / 2) + "px"; }
            this.playerLayer.style.top = Math.round(scroll[1] + (viewport[1] - this.adHeight - 2 * this.borderWidth - this.controlHeight) / 2) + "px";
        } 
    }
    this.addEvent = function (_obj, _event, _func) {
        if (_obj.addEventListener) { _obj.addEventListener(_event, _func, false); return true; } else if (_obj.attachEvent) { return _obj.attachEvent("on" + _event, _func); }
        return false;
    }
    this.removeEvent = function (_obj, _event, _func) {
        if (typeof (_obj.removeEventListener) != 'undefined') { _obj.removeEventListener(_event, _func, false); return true; } else if (typeof (_obj.detachEvent) != 'undefined') { return _obj.detachEvent("on" + _event, _func); }
        return false;
    }
    this.getViewport = function () {
        if (window.innerHeight != window.undefined) { return [window.innerWidth - (this.IsIE8Browser() ? 0 : 17), window.innerHeight]; } else if (document.compatMode == 'CSS1Compat') { return [document.documentElement.clientWidth - (this.IsIE8Browser() ? 0 : 17), document.documentElement.clientHeight - (this.IsIE8Browser() ? 0 : 17)]; } else if (document.body) { return [document.body.clientWidth - (this.IsIE8Browser() ? 0 : 17), document.body.clientHeight - (this.IsIE8Browser() ? 0 : 17)]; }
        return false;
    }
    this.makeTransparent = function (_obj, _value) { if (this.browser == 'IE') { _obj.style.filter = 'alpha(opacity=' + (_value * 100) + ')'; } else { _obj.style.opacity = _value; } }
    this.getScrolling = function ()
    { if (document.compatMode == 'CSS1Compat') { return [document.documentElement.scrollLeft, document.documentElement.scrollTop, document.documentElement.scrollWidth - (this.IsIE8Browser() ? 0 : 17), document.documentElement.scrollHeight - (this.IsIE8Browser() ? 0 : 17)]; } else { return [document.body.scrollLeft, document.body.scrollTop, document.body.scrollWidth - (this.IsIE8Browser() ? 0 : 17), document.body.scrollHeight - (this.IsIE8Browser() ? 0 : 17)]; } }
    this.initLayers = function () {
        this.debug('- initLayers()');
        if (this.browser == 'IE') {
            this.player = document.createElement('<object id="SC_InterstitialPlayer" classid="clsid:' + this.flashClassId + '" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="100%" height="' + (this.adHeight + this.borderWidth + this.controlHeight) + '" type="application/x-shockwave-flash">');
        }
        else {
            this.player = document.createElement('object');
            this.player.setAttribute('id', 'SC_InterstitialPlayer');
            this.player.setAttribute('width', '100%');
            this.player.setAttribute('height', this.adHeight + this.borderWidth + this.controlHeight);
            this.player.setAttribute('type', 'application/x-shockwave-flash');
            this.player.setAttribute('data', this.miniPlayerUrl);
        }
        this.player.appendChild(this.createParam('movie', this.miniPlayerUrl));
        this.player.appendChild(this.createParam('menu', 'false'));
        this.player.appendChild(this.createParam('scale', 'noscale'));
        this.player.appendChild(this.createParam('wmode', 'window'));
        this.player.appendChild(this.createParam('bgcolor', '#' + this.backgroundColor));
        this.player.appendChild(this.createParam('allowScriptAccess', 'always'));
        this.player.appendChild(this.createParam('FlashVars', 'adWidth=' + this.adWidth + '&adHeight=' + this.adHeight + '&format=' + 'interstitial' + '&audioVolume=' + this.audioVolume + '&frameThickness=' + this.borderWidth + '&closeText=' + this.closeText + '&closeButtonDelay=' + this.closeDelay + '&frameTextColor=' + this.headerTextColor + '&frameText=' + this.headerText + '&colorSet=' + this.colorSet + '&timeText=' + this.timeText));
        if (this.browser == 'IE') {
            //if (this.IsIE8Browser()) {
            //    this.layer = document.createElement('<div style="position:absolute; z-index:' + this.zIndex + '; top:0px; left:0px; width:100%; height:100%; filter:alpha(opacity=' + (this.opacity * 100) + '); background-color:transparent; visibility:hidden;">');
            //}
            //else {
                this.layer = document.createElement('<div style="position:absolute; z-index:' + this.zIndex + '; top:0px; left:0px; width:100%; height:100%; filter:alpha(opacity=' + (this.opacity * 100) + '); background-color:#' + this.backgroundColor + '; visibility:hidden;">');
            //}
            this.playerLayer = document.createElement('<div style="filter:alpha(opacity=1); position:absolute; z-index:' + (this.zIndex + 1) + '; left:-1000px; top:0px; width:' + this.adWidth + 'px; height:' + this.adHeight + 'px; padding:0px 0px;">');
        }
        else {
            this.layer = document.createElement('div'); this.layer.setAttribute('style', 'position:absolute; z-index:' + this.zIndex + '; top:0px; left:0px; width:100%; height:100%; opacity:' + this.opacity + '; background-color:#' + this.backgroundColor + '; visibility:hidden;');
            this.playerLayer = document.createElement('div'); this.playerLayer.setAttribute('style', 'opacity:0.01; position:absolute; z-index:' + (this.zIndex + 1) + '; left:-1000px; top:0px; width:' + this.adWidth + 'px; height:' + this.adHeight + 'px; padding:0px 0px;">');
        }
        this.layerframe = document.createElement("iframe");
        this.layerframe.setAttribute("marginwidth", "0");
        this.layerframe.setAttribute("marginheight", "0");
        this.layerframe.setAttribute("hspace", "0");
        this.layerframe.setAttribute("vspace", "0");
        this.layerframe.setAttribute("frameborder", "0");
        this.layerframe.setAttribute("scrolling", "no");
        this.layerframe.setAttribute("src", "about:blank");
        this.layerframe.style.cssText = "z-index:-1;position:absolute;width:100%;height:100%;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);";
        this.playerLayer.appendChild(this.layerframe);
        this.playerLayer.appendChild(this.player);
        document.body.insertBefore(this.playerLayer, document.body.firstChild);
        document.body.insertBefore(this.layer, document.body.firstChild);
        return true;
    }
    this.detectBrowser = function () {
        var agent = navigator.userAgent; if (agent.match(/Firefox.(\d.\d)/i) != null) { this.browser = 'FIREFOX'; return true; } else if (agent.match(/MSIE.(\d.\d)/i) != null) { this.browser = 'IE'; return true; } else if (agent.match(/Opera.(\d.\d)/i) != null) { this.browser = 'OPERA'; return true; } else if (agent.match(/Safari.(\d)/i) != null) { this.browser = 'SAFARI'; return true; }
        return false;
    }
    this.IsIE8Browser = function () {
        var rv = -1; var ua = navigator.userAgent; var re = new RegExp("Trident\/([0-9]{1,}[\.0-9]{0,})"); if (re.exec(ua) != null) { rv = parseFloat(RegExp.$1); }
        return (rv == 4);
    }
    this.createParam = function (_name, _value) {
        var param = null; if (this.browser == 'IE') { param = document.createElement('<param name="' + _name + '" value="' + _value + '">'); } else { param = document.createElement('param'); param.setAttribute('name', _name); param.setAttribute('value', _value); }
        return param;
    }
    this.getFlashVersion = function () {
        var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false; var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false; var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false; var descr = null; var versions = null; if (navigator.plugins != null && navigator.plugins.length > 0) { if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) { descr = navigator.plugins["Shockwave Flash" + (navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "")].description.split(" "); versions = descr[2].split("."); return versions[0]; } } else if (isIE && isWin && !isOpera) { try { versions = (new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7")).GetVariable("$version").split(","); return versions[0].substr(4); } catch (exception) { } }
        return 7;
    }
    this.debug = function (_message)
    { if (this.debugLayer != null) { if (this.debugLayer.innerHTML == '') { this.debugLayer.innerHTML = _message; } else { this.debugLayer.innerHTML = this.debugLayer.innerHTML + '<br>' + _message.replace('\n', '<br>'); } } }
    this.debug('SC_Interstitial()');
}
function SC_onLoad()
{ SC_Interstitial().debug('\nSC_onLoad()'); SC_Interstitial().initLayers(); }
function SC_initDone()
{ SC_Interstitial().debug('\nSC_initDone()'); SC_Interstitial().loadAd(); }
function SC_startingVideoAd(_width, _height)
{ SC_Interstitial().debug('\nSC_startingVideoAd()'); SC_Interstitial().startingAd(_width, _height); }
function SC_onResize()
{ SC_Interstitial().debug('\nSC_onResize()'); SC_Interstitial().onResize(); }
function SC_endVideoAd()
{ SC_Interstitial().debug('\nSC_endVideoAd()'); SC_Interstitial().endAd(); }
function SC_stopVideoAd()
{ SC_Interstitial().debug('\nSC_stopVideoAd()'); SC_Interstitial().stopAd(); }
