﻿// JScript File
//depends on util.js

var currentPosition = 0;
var elapsedTime= 0;
var currentState;
var currentLoad;
var totalminutes = 0;
var totalseconds = 0;
var totalSecondsByTenths = 0;

var player;

function playerReady(obj){    
	player = document.getElementById(obj['id']);	
	addListeners();
	
	
}

function addListeners() {

    //alert('adding listeners');
    
    if (player){
        player.addModelListener("STATE", "stateListener");
        player.addModelListener("TIME", "positionListener");
    }
    else {
        setTimeout("addListeners()", 100);
    }
}

function stateListener(obj){
    var currentState = obj.newstate;
    var previousState = obj.oldstate;
               
    if (currentState == "PAUSED"){
       
    }
    
}

function positionListener(obj){    
    //this function is supposedly called every 1/10th of a second.
    //but it seems slower than the player changing

    totalSecondsByTenths += 1;

    
                                       
    if (totalSecondsByTenths == 9)
    {        
        totalseconds += 1;
        
        if ( (totalseconds % 60) == 0)
        {
            totalminutes += 1;
            
            CaptureMinutes(1);
        }
        
        totalSecondsByTenths = 0;
    }

    if (totalseconds == 30) {                
        checkC();                         
    }
    
        
}


    function PlayVideo(titleid,URL,w,h)
    {
		//window.gQueryString = new QueryString();
		window.gCookies = new Cookies();
		//gQueryString.Read();
		gCookies.Read();
    
		var titleid = gQueryString.GetValue("TitleID");
		var custid = parseInt(gCookies.GetValue("", "CUST%5FID"));
      
      	//PostVodHistory(titleid, custid);
      	
      	    
		//scrollbars=no,resize=no
        window.open(URL, 'videowindow', 'toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resize=no, width='+w+', height='+h+'');
    }
       
    function checkC(){
        //window.gQueryString = new QueryString();
		window.gCookies = new Cookies();
		window.gQueryString = new QueryString();
		gQueryString.Read();
		gCookies.Read();
    
        if (gQueryString.GetValue("samplepage") == null){
		    //var titleid = gQueryString.GetValue("Title_ID");
		    if (gCookies.GetValue("", "CUST%5FID")== null ){
		        player.sendEvent('STOP');    
		        document.location.href="/PreviewPlayer.aspx"; 
		    }
		 }
		 		
    }
    
    function checkP(){
        //window.gQueryString = new QueryString();
		window.gCookies = new Cookies();		
		gCookies.Read();
				    
		    if (gCookies.GetValue("", "CUST%5FID")== null ){
		    
		        if (gCookies.GetValue("", "trkP") == 0){
		            player.sendEvent('STOP');    
		            document.location.href="/PreviewPlayer.aspx"; 
		        }
		    }		 		
    }
    
    function CaptureMinutes(minutesWatched)
    {
        window.gQueryString = new QueryString();
        window.gCookies = new Cookies();
        gQueryString.Read();
        gCookies.Read();
        
        var titleid = gQueryString.GetValue("TitleId");
        var custid = parseInt(gCookies.GetValue("", "CUST%5FID"));
        
        PostMinuteUpdate(titleid, custid, minutesWatched);
    }
    
    
    function PostVodHistory(titleid, custid){
	
	var xmlHttp;
	
	try
	{
		// firefox, opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
			alert("Your Browser does not support AJAX!");
			return false;
			}

		}
	}
	
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)
		{
			var responsetxt = xmlHttp.responseText;
			
			//alert(responsetxt);
		}			
		
	}
	
	xmlHttp.open("GET", "vodhistory.aspx?tid=" + titleid + "&cid=" + custid + "&m=I" , true);	
	xmlHttp.send(null);
	
}

function PostMinuteUpdate(titleid, custid, minutesWatched)
{
 var xmlHttp;
	
	try
	{
		// firefox, opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
			alert("Your Browser does not support AJAX!");
			return false;
			}

		}
	}
	
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)
		{
			var responsetxt = xmlHttp.responseText;
			
			//alert(responsetxt);
		}			
		
	}
	
	xmlHttp.open("GET", "vodhistory.aspx?tid=" + titleid + "&cid=" + custid + "&minutes=" + minutesWatched + "&m=U" , true);	
	xmlHttp.send(null);   
}