//
// Collected Worlds 2015
// Live Rail GMS Wrapper Extension
// For Defy Media
//

var lrid = "";
var lrtag = "";
var did_ad = 0;
var LiveRailVPAID,
        vpaidFrame,
        vpaidLoader,
		gmsGame = document.getElementById('gm4html5_div_id'),
        videoWrapper = document.getElementById('videoWrapper'),
        videoElement = document.getElementById('videoElement');
		
var creativeData = {}, environmentVars = {};

function LiveRailHTML5GMS_GMSINIT() {
    // Load the LiveRail VPAID ad unit
    vpaidFrame = document.createElement('iframe');
    vpaidFrame.style.display = 'none';
    vpaidFrame.onload = function() {
        vpaidLoader = vpaidFrame.contentWindow.document.createElement('script');
        vpaidLoader.src = 'http://cdn-static.liverail.com/js/LiveRail.AdManager-1.0.js';
        vpaidLoader.onload = function() {
            LiveRailVPAID = vpaidFrame.contentWindow.getVPAIDAd();
            LiveRailVPAID.handshakeVersion('2.0');
            onVPAIDLoad();
        };
        vpaidFrame.contentWindow.document.body.appendChild(vpaidLoader);
    };
    document.body.appendChild(vpaidFrame);
	
	if ( window.console && window.console.log ) {
        // console is available
        console.log('init LiveRail HTML5');
    }
}

function LiveRailHTML5GMS_Init(_Id, _tag) { 
	lrid = _Id;
	lrtag = _tag;
	
	creativeData = {},
    environmentVars = {
		slot: videoWrapper,
		videoSlot: videoElement,
		videoSlotCanAutoPlay: true,
		LR_PUBLISHER_ID: lrid,
		LR_TAGS: lrtag
    };
	
	if ( window.console && window.console.log ) {
        // console is available
        console.log('started LiveRail HTML5');
    }
}

function LiveRailHTML5GMS_Interstertial_Show() {
	// Initialize an ad
    LiveRailVPAID.initAd(videoElement.offsetWidth, videoElement.offsetHeight, 'normal', 600, creativeData, environmentVars);
	did_ad = 0;
	if ( window.console && window.console.log ) {
        // console is available
        console.log('ad request LiveRail HTML5');
    }
}

function LiveRailHTML5GMS_Poll_Status() {
	return did_ad;
}

function showAd() {
	gmsGame.style.zIndex = "100";
	videoWrapper.style.zIndex = "1000";
	// Start the ad -- this may only be called after AdLoaded is received
    LiveRailVPAID.startAd();
	if ( window.console && window.console.log ) {
        // console is available
        console.log('ad show LiveRail HTML5');
    }
}

// Reset player with content
function playerReset() {
	gmsGame.style.zIndex = "1000";
	videoWrapper.style.zIndex = "100";
}

function playerFinish() {
	playerReset();
	did_ad = 1;
	if ( window.console && window.console.log ) {
        // console is available
        console.log('ad end LiveRail HTML5');
    }
}

function playerError() {
	playerReset();
	did_ad = 2;
	if ( window.console && window.console.log ) {
        // console is available
        console.log('ad fail LiveRail HTML5');
    }
}

// Define initial VPAID setup
function onVPAIDLoad() {

	// Subscribe to events
	LiveRailVPAID.subscribe(function() { showAd(); }, 'AdLoaded');
	LiveRailVPAID.subscribe(function() { playerFinish(); }, 'AdStopped');
	LiveRailVPAID.subscribe(function() { playerError(); }, 'AdError');
	LiveRailVPAID.subscribe(function(message) { console.log(message); }, 'AdLog');

	playerReset();
}