
// Create the VoloMedia Url Array
var voloUrlArray = new Array();
var voloReportServer = "http://ticks.podbridge.com";
var voloImageServer = "http://plugins.volomedia.com/new_agent";
var voloScriptServer = "http://plugins.volomedia.com/new_agent";
var voloDefaultOfferUrl = "http://www.volomedia.com/rss/plugin-download-video.xml";
var voloJSRelease = "new_agent";
var voloJSVersion = "1.2";
var voloClientID = VoloGenerateGUID();
var voloAlreadyOffered = false;

function VoloS4() 
{
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

// Function to help generate a pseudo unique client id
function VoloGenerateGUID()
{
	return (VoloS4()+VoloS4()+"-"+VoloS4()+"-"+VoloS4()+"-"+VoloS4()+"-"+VoloS4()+VoloS4()+VoloS4());
}

// Push a VoloMedia Url onto the array stack. These urls will be called once the agent is accessible
function VoloPushUrl(url)
{
	// First check to see if this exact url is already in the stack
	var found = false;
	for ( i = 0; i < voloUrlArray.length; i++ )
	{
		if ( voloUrlArray[i] == url )
		{
			found = true;
			break;
		}
	}
	
	if ( !found )
		voloUrlArray.push(url);
}

function VoloReportEvent()
{
	// This function is going to accept a variable number of arguments in the following order
	// event -> the string representing the event type
	// channel -> the url for the feed or channel that was requested
	// client_id -> a unique id to represent this client
	// referrer -> the referring page for this code.
	var voloEvent = null;
	var voloChannel = null;
	var voloClient_ID = null;
	var voloReferrer = null;
	
	// at the very least we need an event argument
	if ( arguments.length < 1 )
		return;
	voloEvent = arguments[0];
	
	if ( arguments.length > 1 )
		voloChannel = encodeURIComponent(arguments[1]);
		
	if ( arguments.length > 2 )
		voloClient_ID = arguments[2];
	else
		voloClient_ID = voloClientID; 
		
	if ( arguments.length > 3 )
		voloReferrer = encodeURIComponent(arguments[3]);
	else
		voloReferrer = encodeURIComponent(document.location);
	
	var id = "voloImg";
	var voloBodyLoc = document.getElementsByTagName("body").item(0);
	// Cleanup so we aren't littering the DOM with new elements
	var imgObj = document.getElementById(id);
	if ( null != imgObj )
		voloBodyLoc.removeChild(imgObj);
	
	var voloReportUrl = voloReportServer + "/vm.gif";
	var voloImgObj = document.createElement("img");
	var voloRand = parseInt(Math.random()*99999999); // random number to get around caching issues
	
	voloReportUrl += "?event=" + voloEvent;
	if ( voloReferrer != null )
		voloReportUrl += "&referrer=" + voloReferrer;

	voloReportUrl += "&rand=" + voloRand;
	if ( voloChannel != null )
		voloReportUrl += "&channel_url=" + voloChannel;
	
	if ( voloClient_ID != null )
		voloReportUrl += "&client_id=" + voloClient_ID;
	
	voloReportUrl += "&PBver=" + voloJSRelease;
	voloReportUrl += "&PB_jsVer=" + voloJSVersion;

	voloImgObj.setAttribute("src", voloReportUrl);
	voloImgObj.setAttribute("id", id);
	voloBodyLoc.appendChild(voloImgObj);
	
	//alert("calling report url: " + voloReportUrl);
}

function VoloCallUrl(url)
{
	//alert("calling url: " + url);
	var id = "voloAgentScript";
	var voloHeadLoc = document.getElementsByTagName("head").item(0);
	// Cleanup so we aren't littering the DOM with new elements
	var scriptObj = document.getElementById(id);
	if ( null != scriptObj )
		voloHeadLoc.removeChild(scriptObj);
		
	var voloScriptObj = document.createElement("script");
    
    // Add script object attributes
    voloScriptObj.setAttribute("type", "text/javascript");
    voloScriptObj.setAttribute("src", url);
    voloScriptObj.setAttribute("language", "javascript");
    voloScriptObj.setAttribute("id", id);
    voloHeadLoc.appendChild(voloScriptObj);
}

function VoloPingAgent()
{
	var voloUrl = "http://127.0.0.1:10930/?cmd=Ping&ret=js";
	//alert("Ping Agent: " + voloUrl);
	VoloCallUrl(voloUrl);
}

function VoloNotifyRegComplete()
{
	var voloUrl = "http://127.0.0.1:10930/?cmd=RegistrationComplete";
	//alert("Notify Registration Complete: " + voloUrl);
	VoloCallUrl(voloUrl);
}

function VoloIsAgentInstalled()     
{   
	return ( typeof(volomediaVersion) != "undefined" );
}
   
function VoloIsMac()
{
    return navigator.userAgent.toLowerCase().indexOf("mac") != -1;
}

function VoloIsWindows()
{
	return navigator.userAgent.toLowerCase().indexOf("windows") != -1;
}

function VoloIsSupported()
{
	// Only Mac and Windows are supported at this time
	return (VoloIsMac() || VoloIsWindows());
}

function VoloMediaSubscribe(url, title, logo)
{
    if ( typeof(url) == "undefined" ) 
		return false;
	
    if ( !VoloIsSupported() )
    {   
		//  navigate to itpc://
        if (url.indexOf("http://") == 0)   //replace with itpc
            url = url.replace("http://", "itpc://");
		else if (url.indexOf("HTTP://") == 0)   //replace with itpc
            url = url.replace("HTTP://", "itpc://");
        else
            url = "itpc://" + url;
        window.location = url;
        return false;
    }
        
    var request = "";
        
    if ( VoloIsAgentInstalled() )  //  Subscribe for an RSS feed
	{
		VoloReportEvent("GPC_OLD", url);
		request = "http://127.0.0.1:10930/?cmd=Subscribe&ret=js";
	}
	else    //  Install and subscribe for an RSS feed
	{
		VoloReportEvent("GPC_NEW", url);
		request = voloScriptServer + "/install_popup.php?cmd=Install";
		VoloReportEvent("DPV", url);
	}
		
	if ( logo != undefined )
		request += "&logo=" + encodeURIComponent(logo);
	if ( title != undefined )
		request += "&title=" + encodeURIComponent(title);
	if ( url != undefined )
		request += "&url=" + encodeURIComponent(url);
	// add the client id and referrer to the url
	request += "&client_id=" + voloClientID;
	request += "&referrer=" + encodeURIComponent(document.location);
    
	VoloCallUrl(request);
	
	return false;
}

function VoloMediaSubscribeYesNo(url, title, yesNoImage, logo)
{
    if ( typeof(url) == "undefined" ) 
		return false;
	
    if ( !VoloIsSupported() || (!VoloIsAgentInstalled() && voloAlreadyOffered) )
    {   
		//  navigate to itpc://
        if (url.indexOf("http://") == 0)   //replace with itpc
            url = url.replace("http://", "itpc://");
		else if (url.indexOf("HTTP://") == 0)   //replace with itpc
            url = url.replace("HTTP://", "itpc://");
        else
            url = "itpc://" + url;
        window.location = url;
        return false;
    }
        
    var request = "";
    
    if ( VoloIsAgentInstalled() )  //  Subscribe for an RSS feed
	{
		VoloReportEvent("GPC_OLD", url);
		request = "http://127.0.0.1:10930/?cmd=Subscribe&ret=js";
	}
	else    //  Install and subscribe for an RSS feed
	{
		VoloReportEvent("GPC_NEW", url);
		request = voloScriptServer + "/yes_no_popup.php?cmd=Install";
		VoloReportEvent("DPV", url);
	}
	
	if ( yesNoImage != undefined )
		request += "&yesNo=" + encodeURIComponent(yesNoImage);
	if ( logo != undefined )
		request += "&logo=" + encodeURIComponent(logo);
	if ( title != undefined )
		request += "&title=" + encodeURIComponent(title);
	if ( url != undefined )
		request += "&url=" + encodeURIComponent(url);
	// add the client id and referrer to the url
	request += "&client_id=" + voloClientID;
	request += "&referrer=" + encodeURIComponent(document.location);
    
	VoloCallUrl(request);
	
	return false;
}

function VoloMediaSubscribeOffer(url, title, logo, offer_url)
{
	if ( !VoloIsAgentInstalled() )
	{
		if ( typeof(offer_url) == "undefined" ) 
			offer_url = voloDefaultOfferUrl;
		encoded_offer_url = encodeURIComponent(offer_url);
		push_url = 'http://127.0.0.1:10930/?cmd=Subscribe&url=' + encoded_offer_url;
		VoloPushUrl(push_url);
		VoloMediaSubscribe(url, title, logo);
	}
	else
	{
		VoloMediaSubscribe(url, title, logo);
	}
	return false;
}

function VoloLaunchUrls()
{
	if ( VoloIsAgentInstalled() )
	{
		//alert("Check Agent is launch urls: " + voloUrlArray);
		// Check if there are any subscriptions that need to go out
		while ( voloUrlArray.length > 0 )
		{
			var url = voloUrlArray.shift();
			//alert("Add Url: " + url);
			VoloCallUrl(url);
		}
	}
	else
	{
		//alert("Agent isn't running");
		setTimeout("VoloCheckAgent()", 2000);
	}
}

function VoloCheckAgent()
{
	//alert("VoloCheckAgent");
	volomediaVersion = undefined;
	VoloPingAgent();
	if ( voloUrlArray.length > 0 )
	{
		// set a timeout to check the agent
		setTimeout("VoloLaunchUrls()", 500);
	}
}

// Ping the agent to see if it's running
VoloPingAgent();
