﻿// JScript File
var baseURL="https://inowcu.com";
if(location.href.indexOf("localhost")!=-1)
{
    baseURL="http://localhost:59893/inowcu.com";
}
//Movie Clipe viewer
//Updater
var xmlhttp;
var frameIndex=0;

function InitXmlHttp() {
    // Attempt to initialize xmlhttp object
    try
    {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      // Try to use different activex object
      try
      {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (E)
      {
        xmlhttp = false;
      }
    }
    // If not initialized, create XMLHttpRequest object
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
    {
      xmlhttp = new XMLHttpRequest();
    }
    // Define function call for when Request obj state has changed
    xmlhttp.onreadystatechange=pingHandler;
}
var pingCount=0;

function pingHandler()
{
    if(isDisconnect)
    {
        return;
    }
    if (xmlhttp.readyState==4)
    {
        if(xmlhttp.responseText=="True")
        {
            imageDataHandler();
        }
        else
        {   pingCount++;
            if(pingCount>10)
            {
                document.getElementById("camVeiwerStatus").innerHTML="<p class=\"b_menuSmall\">Requesting movie clip: "+(90-pingCount)+"</p>";
    
            }
            if(pingCount==90)
            {
                document.getElementById("camVeiwerStatus").innerHTML="<p class=\"b_menuSmall\"><strong>Timeout waiting for movie clip</strong></p>";
                disableConnectButton(false);
                return;
            }
            window.setTimeout("waitForMovie()",1000);
        }
    }
}
var alertIndex;
var imageFrameIndex=0;
var firstTime=true;

function imageDataHandler()
{

    D=new Date();
    rnd=D.getTime();
    img=document.getElementById("camViewer");    
    img.src=baseURL+"/iphone/iMoviePlayerHandler.ashx?f="+imageFrameIndex+"&r="+rnd+"&ai="+alertIndex;    
    
    statusDiv=document.getElementById("camVeiwerStatus");
    if(imageFrameIndex<26)
    { 
        statusDiv.innerHTML="<p class=\"b_menuSmall\">Playing frame: "+imageFrameIndex+"</p>";
        
        window.setTimeout("imageDataHandler()",1000)//;,firstTime?15000:1000);
        firstTime=false;        
        imageFrameIndex++;    
    }
    else
    {
       statusDiv.innerHTML="<p class=\"b_menuSmall\">End of movie clip</p>";
        disableConnectButton(false);
    }
}
function playMovie(index)
{
    isDisconnect=false;
    if(camID!=-1)
    {
        streamDisconnect();
    }
    
    alertIndex=index;
    imageFrameIndex=0;
    firstTime=true;    
    pingCount=0;
    //imageDataHandler();
    D=new Date();
    rnd=D.getTime();
    img=document.getElementById("camViewer");    
    img.src=baseURL+"/iphone/iMoviePlayerHandler.ashx?f="+imageFrameIndex+"&r="+rnd+"&ai="+alertIndex;        
            
    waitForMovie();
    document.getElementById("camVeiwerStatus").innerHTML="<p class=\"b_menuSmall\">Requesting movie clip: please wait</p>";
    //img.src=baseURL+"/images/320x240_blank.gif";
    img.src=baseURL+"/iphone/app-images/trans-bg.gif";
    disableConnectButton(true);
           
}
function waitForMovie()
{
    D=new Date();
    rnd=D.getTime();
    InitXmlHttp();
    //debugger;
    xmlhttp.open("GET",baseURL+"/iphone/iMoviePlayerHandler.ashx?ping=1&r="+rnd,true);
    xmlhttp.send(null);
}


//Live streaming
var xmlhttp2;
//var frameIndex=0;

function InitXmlHttpStream() {
    // Attempt to initialize xmlhttp2 object
    try
    {
      xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      // Try to use different activex object
      try
      {
        xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (E)
      {
        xmlhttp2 = false;
      }
    }
    // If not initialized, create XMLHttpRequest object
    if (!xmlhttp2 && typeof XMLHttpRequest!='undefined')
    {
      xmlhttp2 = new XMLHttpRequest();
    }
    // Define function call for when Request obj state has changed
    xmlhttp2.onreadystatechange=streamPingHandler;
}
var camID=-1;
//var imageFrameIndex=0;
var streamFirstTime=true;
var isDisconnect=false;
var streamPingCount=0;

function streamPingHandler()
{
    if(isDisconnect)
    {
        return;
    }
    if (xmlhttp2.readyState==4)
    {
        if(!streamFirstTime && xmlhttp2.responseText!=null) 
        {
            if(xmlhttp2.responseText=="True")
            {
                streamDataHandler();
                return;
            }
            streamPingCount++;
            if(streamPingCount>30)
            {
                document.getElementById("camVeiwerStatus").innerHTML="<p class=\"warning\"><strong>Error connecting to cam</strong></p>";
                return;                                
            }  
        }          
        else
        {
            streamFirstTime=false;
           
            window.setTimeout("waitForStream()",1000);
        }
    }
}


function streamDataHandler()
{
    if(isDisconnect)
    {
        return;
    }
    D=new Date();
    rnd=D.getTime();
    img=document.getElementById("camViewer");    
    img.src=baseURL+"/iphone/iCamViewerHandler.ashx?f="+imageFrameIndex+"&r="+rnd+"&camID="+camID;   
    
    statusDiv=document.getElementById("camVeiwerStatus");
    if(imageFrameIndex<1000)
    { 
        statusDiv.innerHTML="<p class=\"b_menuSmall\">Connected</p>";
        
        window.setTimeout("streamDataHandler()",500)//;,streamFirstTime?15000:1000);
       
        imageFrameIndex++;
        showDisconnectButton(true);
    }
    else
    {
       //connectButton=document.getElementById("ConnectButton");
       // if(connectButton!=null)
       // {
       //     connectButton.disabled=true;
       // }
       disableConnectButton(false);
       showDisconnectButton(false); 
       statusDiv.innerHTML="<p class=\"b_menuSmall\"><strong>Disconnected from stream</strong></p>";
       streamDisconnect();
    }
}
function streamDisconnect()
{
    isDisconnect=true;
    D=new Date();
    rnd=D.getTime();
    InitXmlHttpStream();
    xmlhttp2.open("GET",baseURL+"/iphone/iCamViewerHandler.ashx?disconnect=1&r="+rnd+"&camID="+camID,true);    
    xmlhttp2.send(null);
    //reset
    camID=-1;
    showDisconnectButton(false); 
}
function streamConnect(cameraID)
{
    camID=cameraID;
    imageFrameIndex=0;
    streamFirstTime=true;    
    isDisconnect=false;
    streamPingCount=0;
    //connectButton=document.getElementById("ConnectButton");
    //if(connectButton!=null)
    //{
    //    connectButton.disabled=true;
    //}
    
    disableConnectButton(true);
    
    D=new Date();
    rnd=D.getTime();
    img=document.getElementById("camViewer");    
    //img.src=baseURL+"/iphone/iCamViewerHandler.ashx?f="+imageFrameIndex+"&r="+rnd+"&camID="+camID;        
            
    waitForStream();
    document.getElementById("camVeiwerStatus").innerHTML="<p class=\"b_menuSmall\">Connecting to webcam, please wait</p>";
    img.src=baseURL+"/iphone/app-images/trans-bg.gif";
           
}
function waitForStream()
{
    D=new Date();
    rnd=D.getTime();
    InitXmlHttpStream();
    //debugger;
    if(streamFirstTime)
    {
        xmlhttp2.open("GET",baseURL+"/iphone/iCamViewerHandler.ashx?connect=1&r="+rnd+"&camID="+camID,true);
               
    }
    else
    {
        xmlhttp2.open("GET",baseURL+"/iphone/iCamViewerHandler.ashx?ping=1&r="+rnd,true);
    }
    xmlhttp2.send(null);
}
function disableConnectButton(yes)
{
    connectButton=document.getElementById("ConnectButton");
    if(connectButton!=null)
    {
        connectButton.disabled=yes;
    }
    
}
function showDisconnectButton(yes)
{
    disconnectButton=document.getElementById("DisconnectButton");
    if(disconnectButton!=null)
    {
        disconnectButton.style.display=yes?"block":"none";    
    }
}
function disconnectFromCam()
{
    streamDisconnect();
    disableConnectButton(false);
    showDisconnectButton(false);
    document.getElementById("camVeiwerStatus").innerHTML="<p class=\"b_menuSmall\">Disconnected</p>";
    img.src=baseURL+"/images/320x240_blank.gif";
}
//iPhone Buttons
function backToMain()
{
    playMovie(-1); 
    document.location.href=baseURL+"/iphone/iCameraSettings.aspx?refresh=1";
}
function refreshAlerts()
{
    document.location.href=baseURL+"/iphone/iCameraSettings.aspx?refresh=1";
}