/******************************************************************

Copyright © Global Knowledge Software LLC 2007.  All Rights Reserved.

This program is protected by U.S. and International Copyright and
Patent Laws.  Unauthorized duplication and/or distribution of this
program is strictly prohibited. Distribution and duplication of this
program are limited by license. If you do not currently have a valid
license from Global Knowledge for this program, any copying or
distribution of the program is unauthorized.  If you do have a current
license from Global Knowledge to utilize this program, your use is
strictly limited by the terms of that license.

Patent Pending.

******************************************************************/

var FatPlayerPrefs = false;
var FatPrefBgColor = "#CCCCCC";
var FatPrefFontFace = "MS Shell Dlg";
var FatPrefFontSize = "8";

function UserPreferences()
{
	this.EnablePreferences=true;
	this.TimeStamp="000000000000";
	this.PlayAudio="all";
	this.ShowLeadIn="all";
	this.MarqueeColor="red";
	this.TryIt=new Object;
	this.TryIt.EnableSkipping=true;
	this.DoIt=new Object
	this.DoIt.HotKey=new Object;
	this.DoIt.HotKey.Ctrl="L";
	this.DoIt.HotKey.Shift="N";
	this.DoIt.HotKey.Alt="L";
	this.Tutorial_DontShow_TOC=false;
	this.Tutorial_DontShow_SeeIt=false;
	this.Tutorial_DontShow_TryIt=false;
	this.Tutorial_DontShow_KnowIt=false;
	this.Tutorial_DontShow_DoIt=false;
}

UserPreferences.prototype.Copy = function(src)
{
	this.TimeStamp=src.TimeStamp;
	this.PlayAudio=src.PlayAudio;
	this.ShowLeadIn=src.ShowLeadIn;
	this.MarqueeColor=src.MarqueeColor;
	this.TryIt.EnableSkipping=src.TryIt.EnableSkipping;
	this.DoIt.HotKey.Ctrl=src.DoIt.HotKey.Ctrl;
	this.DoIt.HotKey.Shift=src.DoIt.HotKey.Shift;
	this.DoIt.HotKey.Alt=src.DoIt.HotKey.Alt;
	this.Tutorial_DontShow_TOC=src.Tutorial_DontShow_TOC;
	this.Tutorial_DontShow_SeeIt=src.Tutorial_DontShow_SeeIt;
	this.Tutorial_DontShow_TryIt=src.Tutorial_DontShow_TryIt;
	this.Tutorial_DontShow_KnowIt=src.Tutorial_DontShow_KnowIt;
	this.Tutorial_DontShow_DoIt=src.Tutorial_DontShow_DoIt;
}

UserPreferences.prototype.Compare = function(src)
{
	if (this.PlayAudio!=src.PlayAudio)	return true;
	if (this.ShowLeadIn!=src.ShowLeadIn)	return true;
	if (this.MarqueeColor!=src.MarqueeColor)	return true;
	if (this.TryIt.EnableSkipping!=src.TryIt.EnableSkipping)	return true;
	if (this.DoIt.HotKey.Ctrl!=src.DoIt.HotKey.Ctrl)	return true;
	if (this.DoIt.HotKey.Shift!=src.DoIt.HotKey.Shift)	return true;
	if (this.DoIt.HotKey.Alt!=src.DoIt.HotKey.Alt)	return true;
	if (this.Tutorial_DontShow_TOC!=src.Tutorial_DontShow_TOC) return true;
	if (this.Tutorial_DontShow_SeeIt!=src.Tutorial_DontShow_SeeIt) return true;
	if (this.Tutorial_DontShow_TryIt!=src.Tutorial_DontShow_TryIt) return true;
	if (this.Tutorial_DontShow_KnowIt!=src.Tutorial_DontShow_KnowIt) return true;
	if (this.Tutorial_DontShow_DoIt!=src.Tutorial_DontShow_DoIt) return true;
	return false;
}

UserPreferences.prototype.GetCookie = function()
{
	return new Cookie(document,"OnDemandPlayer",365,"/");
}

UserPreferences.prototype.LoadCookie = function()
{
	var cookie = this.GetCookie();
	cookie.Load();
	if (UserPrefs.TimeStamp!="000000000000" && (!cookie.TimeStamp || UserPrefs.TimeStamp>cookie.TimeStamp))
		return;
		
	if (cookie.PlayAudio)
		this.PlayAudio=cookie.PlayAudio;
	if (cookie.ShowLeadIn)
		this.ShowLeadIn=cookie.ShowLeadIn;
	if (cookie.MarqueeColor)
		this.MarqueeColor=cookie.MarqueeColor;
	if (cookie.TryIt_EnableSkipping)
		this.TryIt.EnableSkipping=(cookie.TryIt_EnableSkipping=="true");
	if (cookie.DoIt_HotKey_Ctrl)
		this.DoIt.HotKey.Ctrl=cookie.DoIt_HotKey_Ctrl;
	if (cookie.DoIt_HotKey_Shift)
		this.DoIt.HotKey.Shift=cookie.DoIt_HotKey_Shift;
	if (cookie.DoIt_HotKey_Alt)
		this.DoIt.HotKey.Alt=cookie.DoIt_HotKey_Alt;
	if (cookie.Tutorial_DontShow_TOC)
		this.Tutorial_DontShow_TOC=cookie.Tutorial_DontShow_TOC;
	if (cookie.Tutorial_DontShow_SeeIt)
		this.Tutorial_DontShow_SeeIt=cookie.Tutorial_DontShow_SeeIt;
	if (cookie.Tutorial_DontShow_TryIt)
		this.Tutorial_DontShow_TryIt=cookie.Tutorial_DontShow_TryIt;
	if (cookie.Tutorial_DontShow_KnowIt)
		this.Tutorial_DontShow_KnowIt=cookie.Tutorial_DontShow_KnowIt;
	if (cookie.Tutorial_DontShow_DoIt)
		this.Tutorial_DontShow_DoIt=cookie.Tutorial_DontShow_DoIt;
}

UserPreferences.prototype.GetUrlParamString = function()
{
	return "PP_SOUND="+this.PlayAudio+
		"&PP_LEADIN="+this.ShowLeadIn+
		"&PP_SKIP="+(this.TryIt.EnableSkipping?"true":"false")+
		"&PP_MARQUEE="+this.MarqueeColor+
		"&PP_ENABLE="+(this.EnablePreferences?"true":"false")+
		"&PP_HOTKEY="+this.DoIt.HotKey.Ctrl+this.DoIt.HotKey.Shift+this.DoIt.HotKey.Alt;
}

UserPreferences.prototype.ParseUrlParamString = function(param_str)
{
	var params=param_str.split('&');
	for (var p=0; p<params.length; p++)
	{
		var paritem=params[p].split("=");
		if (paritem[0].toUpperCase()=="PP_SOUND")
			this.PlayAudio=paritem[1];
		else if (paritem[0].toUpperCase()=="PP_LEADIN")
			this.ShowLeadIn=paritem[1];
		else if (paritem[0].toUpperCase()=="PP_MARQUEE")
			this.MarqueeColor=paritem[1];
		else if (paritem[0].toUpperCase()=="PP_SKIP")
			this.TryIt.EnableSkipping=(paritem[1]=="true");
		else if (paritem[0].toUpperCase()=="PP_ENABLE")
			this.EnablePreferences=(paritem[1]=="true");
		else if (paritem[0].toUpperCase()=="PP_HOTKEY")
		{
			this.DoIt.HotKey.Ctrl=paritem[1].charAt(0);
			this.DoIt.HotKey.Shift=paritem[1].charAt(1);
			this.DoIt.HotKey.Alt=paritem[1].charAt(2);
		}
	}
}

function UserRolesClass()
{
	this.Filtering=false;
	this.Roles=new Array;
}

UserRolesClass.prototype.Copy = function(src)
{
	this.Filtering=src.Filtering;
	this.Roles=new Array;
	for (var r=0; r<src.Roles.length; r++)
		this.Roles[r]=src.Roles[r];
}

UserRolesClass.prototype.Compare = function(src)
{
	if (this.Filtering!=src.Filtering) return true;
	if (this.Roles.length!=src.Roles.length) return true;
	for (var r=0; r<this.Roles.length; r++)
	{
		if (this.Roles[r]!=src.Roles[r]) return true;
	}
	return false;
}

UserRolesClass.prototype.GetCookie = function()
{
	return new Cookie(document,"OnDemandPlayer",365,"/");
}

UserRolesClass.prototype.LoadCookie = function()
{
	var cookie = this.GetCookie();
	cookie.Load();
	if (cookie.Filtering)
		this.Filtering=(cookie.Filtering=="true");
	if (cookie.Roles)
		this.Roles=cookie.Roles.split("+");
}

UserRolesClass.prototype.StoreCookie = function()
{
	var cookie = this.GetCookie();
	cookie.Filtering=this.Filtering?"true":"false";
	cookie.Roles=this.Roles.join("+");
	cookie.Store();
}

UserRolesClass.prototype.Persist = function()
{
	if (FatPlayerPrefs)
		FatPlayerCommand("SaveRoles",this.GetUrlParamString());
	else
		this.StoreCookie();
}

UserRolesClass.prototype.GetUrlParamString = function()
{
	return "PP_ROLEFILTERING="+(this.Filtering?"true":"false")+"&PP_ROLES="+encodeURIComponent(this.Roles.join('+'));
}

UserRolesClass.prototype.ParseUrlParamString = function(param_str)
{
	var params=param_str.split('&');
	for (var p=0; p<params.length; p++)
	{
		var paritem=params[p].split("=");
		if (paritem[0].toUpperCase()=="PP_ROLEFILTERING")
			this.Filtering=(paritem[1]=="true");
		else if (paritem[0].toUpperCase()=="PP_ROLES")
		{
			this.Roles=new Array;
			if (paritem[1])
				this.Roles=paritem[1].split('+');
		}
	}
}

var UserPrefs = new UserPreferences();
var UserRoles = new UserRolesClass();

var paramstr=unescape(document.location.hash.substring(1));
if (paramstr=="")
	paramstr=unescape(document.location.search.substring(1));
var pars=paramstr.split('&');
for (var p1=0; p1<pars.length; p1++)
{
	var paritem=pars[p1].split("=");
	if (paritem[0].toUpperCase()=="FATPLAYER")
		FatPlayerPrefs=true;
	else if (paritem[0].toUpperCase()=="BGCOLOR")
		FatPrefBgColor='#'+paritem[1];
	else if (paritem[0].toUpperCase()=="FONTFACE")
		FatPrefFontFace=paritem[1];
	else if (paritem[0].toUpperCase()=="FONTSIZE")
		FatPrefFontSize=paritem[1];
}

if (!FatPlayerPrefs)
{
	if (window.SetDefaultPreferences)
		SetDefaultPreferences();
	if (UserPrefs.EnablePreferences)
		UserPrefs.LoadCookie();
	UserRoles.LoadCookie();
}

UserPrefs.ParseUrlParamString(paramstr);
UserRoles.ParseUrlParamString(paramstr);

function OpenPreferencesDialog(path,nosound)
{
	if (!UserPrefs.EnablePreferences)
		alert(R_preferences_disabled);
	else
	{
		if (FatPlayerPrefs)
		{
			var szMode="";
			if (window.playMode)
			{
				if (playMode=="D")
					szMode="Mode=D";
			}
			FatPlayerCommand("Preferences",szMode);
		}
		else
		{
			var p=path+"/preferences/preferences.html";
			if (nosound)
				p+="?nosound";
			window.open(p,"prefwin","width=500,height=290,resizable=0,scrollbars=0,top="+(screen.availHeight-290)/2+",left="+(screen.availWidth-500)/2);
		}
	}
}

function OpenRolesDialog(path)
{
	if (FatPlayerPrefs)
		FatPlayerCommand("Roles");
	else
		window.open(path+"/preferences/roles.html","roleswin","width=500,height=286,resizable=0,scrollbars=0,top="+(screen.availHeight-286)/2+",left="+(screen.availWidth-500)/2);
}

function ApplyFatPreferences(fat_preferences)
{
	if (window.OnUpdatePreferences)
	{
		var NewPrefs = new UserPreferences();
		NewPrefs.Copy(UserPrefs);
		NewPrefs.ParseUrlParamString(fat_preferences);
		OnUpdatePreferences(NewPrefs);
	}
	else
		UserPrefs.ParseUrlParamString(fat_preferences);
}

function SetFatPrefsAndRoles(fat_prefs_and_roles)
{
	UserPrefs.ParseUrlParamString(fat_prefs_and_roles);
	UserRoles.ParseUrlParamString(fat_prefs_and_roles);
}

function ApplyFatRoles(fat_roles)
{
	if (window.OnUpdateRoles)
	{
		var NewRoles = new UserRolesClass();
		NewRoles.Copy(UserRoles);
		NewRoles.ParseUrlParamString(fat_roles);
		OnUpdateRoles(NewRoles);
	}
	else
		UserRoles.ParseUrlParamString(fat_roles);
}

function ToggleRoleFiltering(path)
{
	if (!UserRoles.Filtering && UserRoles.Roles.length==0)
		OpenRolesDialog(path);
	else
	{
		var NewRoles = new UserRolesClass();
		NewRoles.Copy(UserRoles);
		NewRoles.Filtering=!NewRoles.Filtering;
		NewRoles.Persist();
		if (window.OnUpdateRoles)
			OnUpdateRoles(NewRoles);
		else
			UserRoles.Filtering=NewRoles.Filtering;
	}
}

if (FatPlayerPrefs)
	FatPlayerCommand("GetPrefsAndRoles");

///////////////////////////////////////////////////////////////////////
//// Tutorial funtions
///////////////////////////////////////////////////////////////////////

var tutorialDialog=null;

function RunTutorialDialog(playmode,dialogmode,path, fromTOC)
{
	if (!path)
		path="";
	if (!fromTOC)
		fromTOC=false;
	if (!FatPlayerPrefs && dialogmode==0)
	{
		var cookie=new Cookie(document,"OnDemandPlayer",365,"/");
		cookie.Load();
		switch(playmode)
		{
			case "TOC" :
					if (GetCookieValue(cookie.Tutorial_DontShow_TOC)!="false")
						return;
					break;
			case "S" :
					if (GetCookieValue(cookie.Tutorial_DontShow_SeeIt)!="false")
						return;
					break;
			case "T" :
					if (GetCookieValue(cookie.Tutorial_DontShow_TryIt)!="false")
						return;
					break;
			case "K" :
					if (GetCookieValue(cookie.Tutorial_DontShow_KnowIt)!="false")
						return;
					break;
			case "D" :
					if (GetCookieValue(cookie.Tutorial_DontShow_DoIt)!="false")
						return;
					break;
		}
	}
	var playModeStr="";
	var playModeDescription="";
	switch(playmode)
	{
		case "TOC":
				playModeStr="TOC - Tutorial";
				playModeDescription="TOC outline";
				break;
		case "S":
				playModeStr="See It! - Tutorial";
				playModeDescription="See It mode player";
				break;
		case "T":
				playModeStr="Try It! - Tutorial";
				playModeDescription="Try It mode player";
				break;
		case "K":
				playModeStr="Know It? - Tutorial";
				playModeDescription="Know It mode player";
				break;
		case "D":
				playModeStr="Do It! - Tutorial";
				playModeDescription="Do It mode player";
				break;
	}
	if (FatPlayerPrefs && !fromTOC)
	{
		var s="Mode='"+playmode+"'&Str='"+playModeStr+"'&Desc='"+playModeDescription+"'&Dialog='"+dialogmode+"'&w=350&h=200";
		FatPlayerCommand("TUTORIAL",s);
	}
	else
	{
		if (this.SeeItPause)
			SeeItPause();
		var s=path+"../preferences/tutorial.html?"+playmode+"&"+playModeStr+"&"+playModeDescription+"&"+dialogmode;
		if (!fromTOC)
			CloseTutorialDialog();
		var params="width=350,height=200,resizable=0,scrollbars=0";
		if (!fromTOC)
			params+=",top=100,left=150";
		tutorialDialog=window.open(s,"",params);
	}
}

function CloseTutorialDialog()
{
	if (tutorialDialog)
	{
		if (!tutorialDialog.closed)
		{
			tutorialDialog.close();
		}
	}
}

function GetCookieValue(k)
{
	if (!k)
		return ("false");	
	return k;
}

function SetDontShowAgain(playmode,k)
{
//	alert("SetDontShowAgain "+playmode+" "+k);
	if (k)
	{
		var cookie=new Cookie(document,"OnDemandPlayer",365,"/");
		cookie.Load();
		if (playmode=="TOC")
			cookie.Tutorial_DontShow_TOC=true;
		if (playmode=="S")
			cookie.Tutorial_DontShow_SeeIt=true;
		if (playmode=="T")
			cookie.Tutorial_DontShow_TryIt=true;
		if (playmode=="K")
			cookie.Tutorial_DontShow_KnowIt=true;
		if (playmode=="D")
			cookie.Tutorial_DontShow_DoIt=true;
		cookie.Store();
	}	
}