/**
 * JavaScript include file for launching NHL TV and deep linking to videos
 */
 
// The following constants are possible values for the "tab" parameter of the function "openTVTeamPlayer"
var TV_TAB_GAMEDAY = 0;
var TV_TAB_INSIDER = 1;
var TV_TAB_GAMES = 2;
var TV_TAB_VOD = 4;
var TV_TAB_FEATURED = 5;
var TV_TAB_ARCHIVE = 6;

var CAT_HOCKEYSHOW = 2;
var CAT_LIVEWIRE = 616;
var CAT_NHLRADIO = 617;
var CAT_NHLPRODPRES = 618;
var CAT_PODCAST = -4;
var CAT_PLAYOFFS = 619;
var CAT_HOTCLIPS = 3;

/**
 * Launches NHL TV and deep links to highlights
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVHighlights(isFrench, teamName)
{
	var url = "hl=true";
	openTV(url, isFrench, teamName);
}

/**
 * Launches NHL TV and deep links to a highlight game
 * @param season (String) - season of game, eg: "20062007"
 * @param gameType (String/int) - type of game, eg: 2
 * @param gameNumber (String/int) - game number, eg: 114
 * @param eventId (String) - optional - ID of event video in game to play
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVHighlightGame(season, gameType, gameNumber, eventId, isFrench, teamName)
{
	var url = "hlg="+season+","+gameType+","+gameNumber;
	if(eventId!=null)
		url += "&event=" + eventId;
	openTV(url, isFrench, teamName);
}

/**
 * Launches NHL TV and deep links to a highlight player
 * @param playerId (String/int) - player ID, eg: "8468481"
 * @param eventId (String) - optional - ID of event video of player to play
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVHighlightPlayer(playerId, eventId, isFrench, teamName)
{
	var url = "hlp="+playerId;
	if(eventId!=null)
		url += "&event=" + eventId;
	openTV(url, isFrench, teamName);
}

/**
 * Launches NHL TV and optionally deep links to a video and optionally
 * chooses what content to display on the right hand side.  For the right
 * hand side content, "tab" parameter will supercede "categoryId" parameter.
 * If neither "tab" nor "categoryId" is specified, the first category that the
 * video belongs to will appear on the right hand side.  If video ID is also not
 * specified, right hand side will default to "Game Day".
 * @param id (String/int) - optional - video ID, eg: 849
 * @param tab (Constant) - optional - one of the TV_TAB_XXX constants defined above, specifies what content to display on the right hand side of NHL TV, supercedes the "categoryId" parameter
 * @param categoryId (String/int) - optional - category ID of category to show on the right hand side of NHL TV, if "tab" is also specified, this parameter is ignored, eg: 6
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVTeamPlayer(id, tab, categoryId, isFrench, teamName)
{
	var qstr = new Array();
	if(id!=null && String(id).length>0)
	{
		qstr[qstr.length] = "type=fvod";
		qstr[qstr.length] = "id=" + id;
	}
	if(tab!=null && String(tab).length>0)
		qstr[qstr.length] = "tab=" + tab;
	else if(categoryId!=null && String(categoryId).length>0)
		qstr[qstr.length] = "catid=" + categoryId;
	qstr = (qstr.length>0)?qstr.join("&"):"";
	openTV(qstr, isFrench, teamName);
}

/**
 * Launches NHL TV in Center Ice mode
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVCenterIce(isFrench, teamName)
{
	openTV("product=SEASON", isFrench, teamName);
}

/**
 * Launches NHL TV in In Market mode
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVInMarket(isFrench, teamName)
{
	openTV("product=INMARKET", isFrench, teamName);
}

/**
 * Launches NHL TV in Center Ice Registration mode
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVCenterIceRegistration(isFrench, teamName)
{
	openTV("product=SEASON", isFrench, teamName, false, true);
}

/**
 * Launches NHL TV in Center Ice Registration mode with tracking code
 * @param trackingCode (String) - tracking code
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVCenterIceTrack(trackingCode, isFrench, teamName)
{
	openTV("product=SEASON&cmpid="+trackingCode, isFrench, teamName, false, true);
}

/**
 * Launches NHL TV in In-Market Registration mode, if team supports In-Market games
 * @param isFrench (Boolean) - optional - true if NHL TV should be launched in French.  By specifying "true", French is NOT guaranteed.  French will only be used if Team supports French AND user didn't previously choose English
 * @param teamName (String) - optional - team name of team player, if not supplied will be determined by current URL, eg: "islanders", "mapleleafs"
 */
function openTVInMarketRegistration(isFrench, teamName)
{
	openTV("product=INMARKET", isFrench, teamName, true);
}

/**
 * Launches NHL TV using Third Party Tracking
 * @param teamName (String) - team name of team player, eg: "islanders", "mapleleafs"
 * @param referral (String) - the referral code
 */
function openTVByReferral(teamName, referral)
{
	openTVWindow("http://"+teamName+TV_REFERRAL_PATH+referral);
}

/**
 * Internal helper methods
 */
function openTV(qstr, isFrench, teamName, register, login)
{
	var urlinfo = getBaseUrl(teamName, register, login, (qstr.indexOf("product=")!=-1));
	var url = urlinfo[0];
	if(qstr!=null && qstr.length>0)
	{
		if(url.indexOf("?")>0)
			url += "&" + qstr;
		else
			url += "?" + qstr;
	}
	if(isFrench)
	{
		if(url.indexOf("?")>0)
			url += "&fr=true";
		else
			url += "?fr=true";
	}
	openTVWindow(url,urlinfo[1]);	
}

function getBaseUrl(teamName, register, login, product)
{
	var isV3 = false;
	var url = (register)?"https://":"http://";
	var tname = teamName;
	if(teamName==null || teamName.length==0)
		tname = location.hostname.substr(0, location.hostname.indexOf("."));
	url += tname;	
	if(register)
		url += TV_REGISTER_PATH;
	else if(login)
		url += TV_LOGIN_PATH;
	else if(!product)
	{
		url += TV_CONSOLE3_PATH;
		isV3 = true;
	}
	else
		url += TV_CONSOLE_PATH;
	return [url, isV3];
}
function openTVWindow(url, isV3)
{
	if(isV3)
	{
		//window.location.href = url;
		var win = window.open(url,"nhltvconsole","scrollbars=no,menubar=no,height=652,width=1020,top=0,left=0,resizable=yes,toolbar=no,location=no,status=yes");
		if(win!=null)
			win.focus();
	}
	else
	{
		var win = window.open(url,"nhltvconsole","scrollbars=yes,menubar=no,height=652,width=1000,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
		if(win!=null)
			win.focus();
	}
}

var TV_CONSOLE_PATH = ".nhl.tv/team/console";
var TV_CONSOLE3_PATH = ".nhl.tv/team/console.jsp";
var TV_REGISTER_PATH = ".nhl.tv/team/secure/registerform";
var TV_LOGIN_PATH = ".nhl.tv/team/secure/loginform";
var TV_REFERRAL_PATH = ".nhl.tv/team/servlets/refer/";