/*
 * This file collects the code that is shared among the provided demo applications.
 * You can remove the optional snippets and customize any part of the code.
 * You can also move this code inside your Push-Page.
 */
 

//////////////// Master Push-Page Creation and Configuration

// Create the PushPage object and configure it by defining some callbacks.
// Then seek or create the Lightstreamer Engine.

var pushPage = new PushPage();

pushPage.context.setDebugAlertsOnClientError(false);
pushPage.context.setRemoteAlertsOnClientError(true);
pushPage.context.setDomain("lightstreamer.com");

// if the page that includes this script does not define a reference to an
// already existing Master Push-Page, let's define the callback needed to create
// the Engine (but used only if this page is actually the first Master Push-Page;
// see createEngine below)
if (typeof(masterRef) == "undefined") {
  pushPage.onEngineCreation = function(lsEngine) {
    lsEngine.context.setDebugAlertsOnClientError(false);
    lsEngine.context.setRemoteAlertsOnClientError(true);
    lsEngine.connection.setLSHost("push.lightstreamer.com");
    lsEngine.connection.setLSPort(80);
    lsEngine.connection.setAdapterName("DEMO"); // the name of the Adapter Set
    //lsEngine.policy.setMaxBandwidth(40); // would be ignored in Moderato and Allegro editions
    lsEngine.changeStatus("STREAMING"); // let's try with a streaming connection
  };
}

// define the OPTIONAL callbacks needed to display the connection status
// (the attachStatusManager function is defined at the end of this file)
attachStatusManager(pushPage);

// the PushPage object is now configured and ready to be processed by the framework
pushPage.bind();

// if the page that includes this script does not define a reference to an
// already existing Master Push-Page, let's use createEngine to see if a compatible
// Master Push-Page can be found automatically (or, if it cannot, to create it);
// otherwise let's use seekEngine with the provided reference to directly attach
// to the Master Push-Page
if (typeof(masterRef) == "undefined") {
  // create the LightstreamerEngine, possibly sharing it with other applications
  // that are based on the "DEMO" Adapter Set; note that all Engine attributes
  // (and the onEngineCreation event handler code in particular) should be
  // identical for all the involved demos.
  // If a similar Engine already exists on another Master Push-Page, it will be
  // leveraged rather than creating a new one
  pushPage.createEngine("OnlineDemoCommonEngine", "/demo/commons/lightstreamer"+/^\d*\.\d*\.(.*)$/.exec(Lightstreamer.version)[1]+"/", "SHARE_SESSION", true);
} else {
  // seek the LightstreamerEngine created by another Master Push-Page
  pushPage.seekEngine("OnlineDemoCommonEngine", masterRef);
}


//////////////// (OPTIONAL) Block Escape Key	

// This optional code snippet blocks the pressure of the Escape key,
// to prevent the stream connection from being interrupted.

document.onkeydown = checkEscape;
document.onkeypress = checkEscape;
function checkEscape(e) {
  if (!e) e = event;
  if (e.keyCode == 27) return false;
}


function attachStatusManager(pushPage) {

  // we get the reference to the current Engine (be it here or on another Push-Page)
  pushPage.onEngineReady = function(lsEngine) {

    // define the callback on the Engine status change and change the image accordingly
    lsEngine.onStatusChange = function(newStatus) {
      // if the page that includes this script defines the onStatusChangeExtension function,
      // let's execute it as the final part of the lsEngine.onStatusChange() implementation
      if (typeof(onStatusChangeExtension) != "undefined") {
        onStatusChangeExtension(newStatus);
      }
    };
    
    // if the page that includes this script defines the onEngineReadyExtension function,
    // let's execute it as the final part of the pushPage.onEngineReady() implementation
    if (typeof(onEngineReadyExtension) != "undefined") {
      onEngineReadyExtension(lsEngine);
    }
    
    // take the very current engine status into considerations
    lsEngine.onStatusChange(lsEngine.getStatus());
    
  };

  // the Engine has been lost; change the status image accordingly
  pushPage.onEngineLost = function() {
    // if the page that includes this script defines the onEngineLostExtension function,
    // let's execute it as the final part of the pushPage.onEngineLost() implementation
    if (typeof(onEngineLostExtension) != "undefined") {
      onEngineLostExtension();
    }
  };
}


//////////////// Google Analytics Tracker

function googleAnalytics() {
  try {
    var pageTracker = _gat._getTracker("UA-11325642-1");
    pageTracker._trackPageview();
  } catch(err) {}
}

setTimeout("googleAnalytics()", 10000);

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

