// TVE Test
// Copyright 2008 Adobe Systems Incorporated
// All Rights Reserved.

/* Handle to the tve access swf node   */
var accessSWFNode;
var checkVSgetAuth = null;

var currentToken;
var accessToken;
var authVidId;
var tokenData = currentToken + "&mpvd=" + authVidId;

/* Callback indicating that the access swf has been initialized */
function swfLoaded() {
//Client - Adobe
    if (logAdobe == 'true') { alert('adobe swf loaded'); }
    accessSWFNode = document.getElementById('AccessEnabler');
    registerClient();
    checkVSgetAuth = 'check';
// TBS/TNT code
//	checkAuthorization(provider);
	// cartoon custom; template code does not work
    checkAuthorization("CartoonNetwork");
	if (getProvider.MVPD != null) {
		distributor	= getProvider.MVPD;
	} else {
		distributor	= "turner";
	}
}

/* a helper function used to find a DOM node containing a Flash Player SWF */
function setAccessFlashMovieNode(movieName) {
	accessSWFNode = document.getElementById(movieName);
}

/* Function called to register the hosting application with the access SWF.
   inClientID - the predetermined ID of the hosting site.
*/
function registerClient() {
	if (accessSWFNode != null) {
		accessSWFNode.setRequestor(provider);
	}
}

/** Callback that sets the authorization token as a result of an authorization token
request.
    inRequestedResourceID - ID of resource for which authorization was requested.
    inToken - authorization token.
**/
function setToken(inRequestedResourceID, inToken) {
	if (logAdobe == 'true') { alert('auth success - ' + inRequestedResourceID + ' - ' + inToken); }
// following commented code fails silently every time.  probably a name issue with TURNERPLAYER
/*
	if (authBool == false) {
		TURNERPLAYER.setAdSection(distributor + '_tve');
	}
*/
    authBool = true;
	accessToken = inToken + '&resourceID=' + inRequestedResourceID + '&requestorID='+ provider ;
//	isAuthenticatedUser();

	// cartoon custom;
	document.getElementById('logoutBtnBox').style.display = 'block';
	document.getElementById('smLoginBox').style.display = 'none';
	document.getElementById('adBox').style.display = 'block';
	currentToken = inToken;
	
	// init with first item in latest video playlist
	Dramavision.playScene(firstVid);

}


/** Callback that indicates a failed authorization token request.
    inRequestedResourceID - ID of resource for which authorization was requested.
**/
function tokenRequestFailed(inRequestedResourceID) {
    if (checkVSgetAuth == 'check') {
        swfLoadedvar = true;
        if (logAdobe) { alert('initial check authorization for ' + inRequestedResourceID + ' failed'); }
        document.getElementById('smLoginBox').style.display = 'block';
    } else {
        document.getElementById('smLoginBox').style.display = 'block';
    }
}

function checkAuthorization(resourceID) {
    if (logAdobe) { alert('checking auth for ' + resourceID);	}
    if (accessSWFNode != null) {
        accessSWFNode.checkAuthorization(resourceID);
    }
}

/** Request authorization for a given resource.
    inResourceID - ID of resource for which authorization is needed.
**/
function getAuthorization(inResourceID) {
    checkVSgetAuth = 'get';
    if (accessSWFNode != null) {
        if (logAdobe == 'true') { alert('calling get auth for ' + inResourceID); }
        accessSWFNode.getAuthorization(inResourceID);
    }
}

function logout() {
	if (logAdobe == 'true') { alert('logout');	}
	accessSWFNode.logout();
	location.reload(true);
}


/** Helper function that is used to invoke the access swf directly for authorization
    using a query param in the url. This is used when the user is redirected back to
    content site after successful authentication.
**/
function inspectAccessCheckQueryParam() {
    var qsParm = new Array();
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            qsParm[key] = val;
        }
    }

    if (qsParm["access_resource_id"] != null)
    {
        getAuthorization(qsParm["access_resource_id"]);
    }
}

 function setMovieDimensions(inWidth, inHeight) {
     if (accessSWFNode != null) {
        accessSWFNode.width = inWidth;
        accessSWFNode.height = inHeight;
    }
 }

function createIFrame(inWidth, inHeight) {
	// Move the div to be centered on the page relative to the size of the iframe
	document.getElementById('mvpddiv').style.position="absolute";
	document.getElementById('mvpddiv').style.top="50px";
	document.getElementById('mvpddiv').style.left="50%";
	document.getElementById('mvpddiv').style.zIndex="98";
	document.getElementById('mvpddiv').style.marginLeft="-"+inWidth/2+"px";
	// Create the iframe to the specified width and height for the auth page
	ifrm = document.createElement("IFRAME");
	ifrm.style.width = inWidth+"px";
	ifrm.style.height = inHeight+"px";
	//ifrm.style.width = "1px";
	//ifrm.style.height = "1px";
	ifrm.name = "mvpdframe";
	ifrm.id = "mvpdframe";
	ifrm.style.frameborder = "0";
	ifrm.style.border = "0";
	document.getElementById('mvpddiv').appendChild(ifrm);
	window.frames["mvpdframe"].name = "mvpdframe";// Force the name into the DOM since it is still not refreshed, for IE
}

function sendTrackingData (trackingEventType,trackingData) {
	// cartoon custom
	authVidId = trackingData[1];

    switch (trackingEventType)
    {
        case "authorizationDetection" :
            /*  [0] Whether the token request was successful [true/false]
             *       and if true:
             *  [1] MVPD ID [string]
             *  [2] User ID (md5 hashed) [string]
             *  [3] Whether it was cached or not [true/false]
             */
            // DEBUG
            if (logAdobe) {alert("trackingEventType = " + trackingEventType + "; " + trackingData);}
            // populate Omniture Metrics
            if (trackingData[0] == true) {
                // if cached == true
                if(trackingData[3] == true) {
                    trackAlreadyLoggedInPage(trackingData);
                } else {
                    // else just redirected back from authorization
                    trackAuthenticationComplete(trackingData);
                }
            } else {
                if (!hasSetFirstMetric) {
                    // populate Omniture Metrics
                    trackNotLoggedInPage();
                    hasSetFirstMetric = true;
                }
            }
        break;
        case "authenticationDetection" :
            /*  [0] Whether the token request was successful [true/false]
             *       and if true:
             *  [1] MVPD ID [string]
             *  [2] User ID (md5 hashed) [string]
             *  [3] Whether it was cached or not [true/false]
             */
            //alert("trackingEventType = " + trackingEventType + "; " + trackingData);
        break;
        case "mvpdSelection" :
            /*  [0] MVPD ID */
            // DEBUG
            if (logAdobe) {alert("trackingEventType = " + trackingEventType + "#1; " + trackingData);}
            // populate Omniture Metrics
            trackAuthenticationStart(trackingData);
            if (logAdobe) {alert("trackingEventType = " + trackingEventType + " #2; " + trackingData);}
        break;
        default:
            //alert("trackingEventType = " + trackingEventType + "; " + trackingData);
        break;
    }

}

