<?php 
/**
 * Javascript implementation of liverail.
 */
class mCachedb extends Memcache {
	static private $instance = null;
	static private $port = 11211;
	static function mGet() {
		if (self::$instance == null) {
			self::$instance = new Memcache;
			self::$instance->addServer("192.168.30.51", self::$port);
		}
		return self::$instance;
	}
}

include_once 'lib/php/include/db.php';

$ssl = "http";
if(trim($_SERVER['HTTPS']) != '')
	$ssl = "https";

$subid = $_GET['id'];
$width = $_GET['width'];
$height = $_GET['height'];



function getLRID($aid){
	$aid = addslashes($aid);
	$aSql = "select lr_id from AffiliateIDs where id = {$aid} limit 1";
	$mkey = 'aff_lrid_'.md5($aid);
	$expire = 60*20;
	if(!$aff_lrid = mCachedb::mGet()->get($mkey)){
		$db = ConnectToRead();
		$aRes = mysql_query($aSql, $db);
		if($aRes){
			$aRow = mysql_fetch_assoc($aRes);
			if($aRow && $aRow['lr_id'] != ''){
				$aff_lrid = $aRow['lr_id'];
				if($aff_lrid > 0)
					mCachedb::mGet()->set($mkey, $aff_lrid, 0, $expire);
				else
					mCachedb::mGet()->set($mkey, "NONE000", 0, $expire);
			}
		}
		mysql_close($db);
	}
	if(!is_numeric($aff_lrid)){
		$aff_lrid = 0;
	}
	return intval($aff_lrid);
}

$t = explode("_", $subid);
$aid = $t[1];

$liverail_publisher_id = getLRID($aid);
if(!$liverail_publisher_id){
	$liverail_publisher_id = 109659;
}

?>
<html>
<head>
<style>
body {
	margin:0px;
}
</style>
</head>

<body>
<div class="lr_preroll" id="lr_preroll" style="display:block;width:<?=$width?>px;height:<?=$height?>px;float:left; position:relative;"><img style="display:block;width:31px;height:31px;position:absolute;left:50%;top:50%;margin: -15px 0 0 -15px;" id="prerollLoader" src="<?=$ssl?>://b36df47b3d.site.internapcdn.net/preroll/video-loading.gif" width="31" height="31"></div>
<script>
var mkey = "<?=$_GET['mkey']?>";

function consoleLog(obj) {
	if (typeof console != 'undefined') {
		if(typeof console.info == 'function')
			console.info(obj)
	}
}

function loadScript(url, callback)
{
	var head = document.getElementsByTagName("head")[0];
    addLater = false;
    if(!head){
	head = document.createElement("head");
	addLater = true;
    }
    var script = document.createElement("script");
	script.src = url;
    script.type = "text/javascript";
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){			
            callback();
        };
    }
 	head.appendChild(script);
    if(addLater){
		document.documentElement.appendChild(head);
    }
}

var preroll_cleanup = function() {
	document.getElementById('lr_preroll').innerHTML = '';
	if(mkey != '')
		loadScript("http://include.ldcnetwork.com/prerollCache.php?action=set&mkey="+mkey, function() {});
}

var load_liverail = function(){
	var LiveRailVPAID,
	vpaidFrame,
	vpaidLoader,
	adElement = document.getElementById("lr_preroll");
	   
	function onVPAIDLoad() {
		  
	   // Subscribe to events
	   LiveRailVPAID.subscribe(function() { 
		    consoleLog("LR-wr 1");			
		   	LiveRailVPAID.startAd();  
			
		   	//fireAdSafePixel();
		   	//logStreams('LR');
	   }, 'AdLoaded');
	   LiveRailVPAID.subscribe(function() {
		   preroll_cleanup();
		   consoleLog("LR-wr 0");
	   }, 'AdStopped');
	   LiveRailVPAID.subscribe(function() {
		   preroll_cleanup();
		   consoleLog("LR-wr -1");
	   }, 'AdError');
	   
	   LiveRailVPAID.initAd(adElement.offsetWidth, adElement.offsetHeight, 'normal', 600, creativeData, environmentVars);
	}
	 
	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);
	
	var creativeData = {},
	environmentVars = {
	    slot: adElement,
	    LR_ENVIRONMENT: 'flash',
	    //LR_PUBLISHER_ID: 1331,
	    LR_PUBLISHER_ID: <?=$liverail_publisher_id?>,
	    LR_VIDEO_AMID: "<?=$subid?>",
	    LR_TAGS: 'wrad',
	    LR_AUTOPLAY: 1,
	    LR_CONTENT:1,
	    LR_VERTICALS:'wrad'						
	};
}

window.onload = load_liverail();


</script>
</body>
</html>