// http://ds.serving-sys.com/BurstingRes/CustomScripts/OAD_Microsoft_RioTracking.js.js?adid=[%tp_adid%]&type=rb&cell=1111


var ebScriptFileName = "OAD_Microsoft_RioTracking.js";

//http://snipplr.com/view/354/parse-query-string-from-script-elements-src-attribute/
/**
 * Helper object to parse the query string variables from 
 * <script> element's src attribute.
 * 
 * For example, in test.html:
 *
 *   <script src="test.js?name=value"></script>
 *
 * and in test.js, you can get query as name/value pairs:
 * 
 *   var gEbQueries = new ebScriptQuery('test.js').parse();
 *   for (var name in gEbQueries) {
 *     var values = gEbQueries[name]; // property is Array instance.
 *     ...
 *   }
 * 
 * If you would like to avoid array manipulation.
 * ebScriptQuery also provides flatten method, which returns 
 * only first value for each properties.
 * 
 *   var gEbQueries = new ebScriptQuery('test.js').flatten();
 *   for (var name in gEbQueries) {
 *     alert(gEbQueries[name]); // property is simply string
 *   }
 */

var ebScriptQuery = function(scriptPath) {
  this.scriptPath = scriptPath;
}
ebScriptQuery.prototype = {
  get: function() {	
    var srcRegex = new RegExp(this.scriptPath.replace('.', '\\.') + '(\\?.*)?$');
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      var script = scripts[i];
      if (script.src && script.src.match(srcRegex)) {
        var query = script.src.match(/\?([^#]*)(#.*)?/);
        return !query ? '' : query[1];
      }
    }
    return '';
  },
  parse: function() {	
    var result = {};
    var query = this.get();
    var components = query.split('&');
 
    for (var i = 0; i < components.length; i++) {
      var pair = components[i].split('=');
      var name = pair[0], value = pair[1];
 
      if (!result[name]) result[name] = [];
      // decode
      if (!value) {
        value = 'true';
      } else {
        try {
          value = decodeURIComponent(value);
        } catch (e) {
          value = unescape(value);
        }
      }
 
      // MacIE way
      var values = result[name];
      values[values.length] = value;
    }
    return result;
  },
  flatten: function() {	
    var queries = this.parse();
    for (var name in queries) {
      queries[name] = queries[name][0];
    }
    return queries;
  },
  toString: function() {
    return 'ebScriptQuery [path=' + this.scriptPath + ']';
  }
}

//verify by Ad ID or Flight ID
try{
	var gEbQueries = new ebScriptQuery(ebScriptFileName).flatten();	
	if(gEbQueries["type"] == 'oob'){ // out-of-banner/floating ad
		if(typeof(gEbEyes) != "undefined") {			
			// check is the same as the ad is defined in the script
			if(gEbQueries["adid"]){
				for(i = gEbEyes.length-1; i>-1; i--){
					if(gEbEyes[i].adData.nAdID == gEbQueries["adid"]){
						gEbEyes[i].adData.customEventHandler = new ebCCustomEventHandlers();
						break;
					}
				}
			}
			if(gEbQueries["flightid"]){
				for(i = gEbEyes.length-1; i>-1; i--){					
					if(gEbEyes[i].adData.nFlightID == gEbQueries["flightid"]){
						gEbEyes[i].adData.customEventHandler = new ebCCustomEventHandlers();
						break;
					}
				}			
			}
		}
	} else{ //rich banner / default
		if(typeof(gEbBanners) != "undefined"){			
			if(gEbQueries["adid"]){	
				for(i = gEbBanners.length-1; i>-1; i--){
					if(gEbBanners[i].adData.nAdID == gEbQueries["adid"]){
						gEbBanners[i].adData.customEventHandler = new ebCCustomEventHandlers();
						break;
					}
				}
			}
			if(gEbQueries["flightid"]){
				for(i = gEbBanners.length-1; i>-1; i--){			
					if(gEbBanners[i].adData.nFlightID == gEbQueries["flightid"]){
						gEbBanners[i].adData.customEventHandler = new ebCCustomEventHandlers();
						break;
					}
				}	
			}
			
		}
	}
}catch(e){}


 
/* ---------------------------------------    copy normal customeventhandlers definition below this line ----------------------------------*/



function ebCCustomEventHandlers()
{	
	
	// ------------------------ GENERAL EVENTS ---------------------------------
	this.onClientScriptsLoaded = function(objName)
	{
		// do something			
	}

	this.onBeforeAddRes = function(objName)
	{
		// do something		
	}

	this.onHandleInteraction = function(objName, intName, strObjID)
	{
		try
		{
			//create display unit variable
			var myDU = eval(objName);
			
			//get cell code from querystring
			var myEbCellCodeVar; 
			//debugger;
			if(gEbQueries["cell"]){
				//alert(gEbQueries["cell"]);
				myEbCellCodeVar = gEbQueries["cell"];
			}
                       
                       
            //pass cell code to MSFT Rio Tracker only on video interactions
                         
                           
            var myEBVideoInteractions = new Array("ebVideoStarted","eb25Per_Played","eb50Per_Played","eb75Per_Played","ebVideoFullPlay","ebVideoPause","ebVideoReplay","ebVideoMute","ebVideoUnmute");
                
            for(var i=0;i<myEBVideoInteractions.length;i++)
		    {
			    //Send Video Interaction
			    if(myEBVideoInteractions[i] == intName)
			    {
				    //check if any interactions left to report
				    if(myDU.interactions[myEBVideoInteractions[i].toLowerCase()].numLeftToReport > 0)
				    {
					    //send interaction
					    RioTracking.processEvent(myDU.adData.nFlightID, myEbCellCodeVar, i+1);
				    }
			    }                             
		    }        
                       
        }
        catch(e){}               
	}

	// ------------------------ BANNER EVENTS ---------------------------------
	this.onBeforeDefaultBannerShow = function(objName)
	{
		// do something		
	}

	this.onAfterDefaultBannerShow = function(objName)
	{
		// do something			
	}

	this.onBeforeRichFlashShow = function(objName)
	{
		// do something		
	}

	this.onAfterRichFlashShow = function(objName)
	{
		// do something	
	}
	
	// ------------------------ PANEL EVENTS ---------------------------------
	this.onBeforePanelShow = function(objName, panelName)
	{
		// do something
		try
		{
		    gEbDisplayPage.TI.getDoc().getElementById('ad').style.visibility = "hidden";
		}
		catch(e){}
	}

	this.onAfterPanelShow = function(objName, panelName)
	{
		// do something
	}
	
	this.onBeforePanelHide = function(objName, panelName)
	{
		// do something
	}

	this.onAfterPanelHide = function(objName, panelName)
	{
		// do something
		try
		{
		    gEbDisplayPage.TI.getDoc().getElementById('ad').style.visibility = "visible";	
		}
		catch(e){}	
	}

	// ------------------------ AD CLOSE EVENTS ---------------------------------
	this.onBeforeAdClose = function(objName)
	{
		// do something
	}

	this.onAfterAdClose = function(objName)
	{
		// do something
	}

	// ------------------------ INTRO/REMINDER/MINISITE EVENTS ---------------------------------
	this.onBeforeIntroShow = function(objName)
	{
		// do something
	}

	this.onAfterIntroShow = function(objName)
	{
		// do something
	}
	
	this.onBeforeIntroHide = function(objName)
	{
		// do something
	}

	this.onAfterIntroHide = function(objName)
	{
		// do something
	}
	
	
	this.onBeforeRemShow = function(objName)
	{
		// do something
	}

	this.onAfterRemShow = function(objName)
	{
		// do something
	}
	
	this.onBeforeRemHide = function(objName)
	{
		// do something
	}

	this.onAfterRemHide = function(objName)
	{
		// do something
	}
	
	this.onBeforeMiniSiteShow = function(objName)
	{
		// do something
	}

	this.onAfterMiniSiteShow = function(objName)
	{
		// do something
	}
	
	this.onBeforeMiniSiteHide = function(objName)
	{
		// do something
	}

	this.onAfterMiniSiteHide = function(objName)
	{
		// do something
	}
	

}
//*************************** RIO Tracking Manager - Rich Media Tracking *************************************
function RioTrackingManagerRmt() {

    // Initialize tracking URLs.
    try
	{
        this.atlasTrackingUrl = document.location.protocol + '//view.atdmt.com/jaction/mrtity_RioRichMedia_10/v3/ato.-1';
        this.crsTrackingUrl = document.location.protocol + '//www.microsoft.com/click/services/Tracking/RichMedia.ashx';
    }
    catch(e){}
}

RioTrackingManagerRmt.prototype.processEvent = function(placementId, cellCode, eventId) {

    try
	{
        this.fireAtlasTag(placementId, cellCode, eventId);
        this.fireCrsTag(placementId, cellCode, eventId);
    }
    catch(e){}
}

RioTrackingManagerRmt.prototype.fireAtlasTag = function(placementId, cellCode, eventId) {
    
    try
	{
        var scriptSrc = this.atlasTrackingUrl;
        scriptSrc += '/atc1.' + placementId.toString();
        scriptSrc += '/atc2.' + cellCode.toString();
        scriptSrc += '/atc3.' + eventId.toString();

        this.attachScript(scriptSrc);
    }
    catch(e){}
}

RioTrackingManagerRmt.prototype.fireCrsTag = function(placementId, cellCode, eventId) {
    
    try
	{
        // Create a random number for "cache-busting".
        var timestamp = new Date();
        var rnd = Math.ceil(Math.random() * 99999999) + "" + timestamp.getUTCFullYear() + timestamp.getUTCMonth() + timestamp.getUTCDate() + timestamp.getUTCHours() + timestamp.getUTCMinutes() + timestamp.getUTCSeconds() + timestamp.getUTCMilliseconds();
        
        var scriptSrc = this.crsTrackingUrl;
        scriptSrc += '?pid=' + placementId.toString();
        scriptSrc += '&cc=' + cellCode.toString();
        scriptSrc += '&eid=' + eventId.toString();
        scriptSrc += '&rnd=' + rnd.toString();

        this.attachScript(scriptSrc);
    }
    catch(e){}
}

RioTrackingManagerRmt.prototype.attachScript = function(scriptSrc) {
    
    try
    {
        var scriptObj = document.createElement("script");
        scriptObj.type = "text/javascript";
        scriptObj.src = scriptSrc;
        
        document.getElementsByTagName("head")[0].appendChild(scriptObj);
    }
    catch(e){}
}

// Instantiation of RIO Tracking Object
try
{
    var RioTracking = new RioTrackingManagerRmt();
}
catch(e){}