var res_id;
var tid;

function bizcardAPI(title_id)
{
	var apiURL = '/data/bizcardAPI.do?vtid=' + title_id; 
	var obj=new JSONscriptRequest(apiURL); 
	obj.buildScriptTag(); // Build the script tag      
	obj.addScriptTag(); // Execute (add) the script tag
}

function openBizcard()
{
	if(getCookieValue("WebXuser") != '') emailstring = '&ttemail=' + getCookieValue("WebXuser");
	else emailstring = '';
	ttPauseVideo();
	var bizcardURL = '/data/document.do?fwd=overlaybp&res_id=' + res_id + emailstring;
	tbr_show(null,bizcardURL + '&KeepThis=true&TB_iframe=true&height=500&width=600&modal=true',false);		
}

function setSessionCookie (name, value) {
	document.cookie = name + "=" + value + ";path=/" + ";domain=.bitpipe.com";
}

function getCookieValue(name) {
	var cookie = document.cookie;
	var pos = cookie.indexOf(name + "=");
	if (pos != -1) {
		var start = pos + name.length + 1;
		var end = cookie.indexOf(";",start);
		if (end == -1) end = cookie.length;
		var value = cookie.substring(start, end);
		return value;
	} else {
		return "";
	}
}

function getCurrentTitle_Result(titleDTO)
{
	tid = titleDTO.id;
	if(getCookieValue("v" + titleDTO.id) != "false") bizcardAPI(titleDTO.id);
}

function onMediaStart(pEvent)
{
	callFlash("getCurrentTitle");
}

function ttStopVideo() 
{
	callFlash("stopVideo");
}

function ttStartVideo() 
{
	callFlash("startVideo");
}

function ttPauseVideo() 
{
	callFlash("pauseVideo","true");
}

function ttUnPauseVideo() 
{
	callFlash("pauseVideo");
}

function onTemplateLoaded(message) {
	if (typeof message == "undefined") {
 		// template loaded without error
 		callFlash("addEventListener", "mediaStart", "onMediaStart");
	}
}
 
function setValues(data)
{
    var doBC = data.bizcardAPI.doBC;
	if(doBC == true) res_id = data.bizcardAPI.res_id;
	else
	{
		res_id = '';
		setSessionCookie('v' + tid,'false');
	}
	if(doBC == true) openBizcard();
}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}


