var flashReady=false;
var flashLoading=false;
var hasFlashVersion=null;
var defaultFlashActivity=null;
var dateKey='';
var homepageID=AFFINITY;
var deep_linked_tab=TAB;
var profileKey='base-4';
var isCardHolder=null;
var IS_WINNER = null;
var defaultChecklistActivity = "";
var flashTimedOut=false;
var reloadFlashContainer = false;
var adTimeout = null;
var nextactivity = null;

_userIsLoggedIn = function() {
	return (Cookie.get("Login.LoggedIn") == "true");
}

_getFlashTabID = function() {
	if (deep_linked_tab != '') {
		// Can only use the deep-linked tab once:
		// on shift to different activity, use the default.
		var temp = deep_linked_tab;
		deep_linked_tab = '';
		return temp;
	}

	if (!_userIsLoggedIn())
		return 'learn';

	return 'play';
}

_wsdlUrl = function(activityid) {
	var use_activityid = activityid;
	if (activityid == "jeopardydaily" || activityid == "jeopardybonus")
		use_activityid = "Jeopardy";

	return (siteroot + "services/flash/" + use_activityid + "Service.asmx?wsdl");
}


loadFlashActivity = function(activityID)
{
	if (adTimeout != null && activityID != 'spinid')
	{
		clearTimeout(adTimeout);
		adTimeout = null;
	}

	if (flashLoading) return;
	flashLoading = true;

	if (flashReady)
	{
		if (hasFlashVersion != null && !reloadFlashContainer)
		{
			if (hasFlashVersion && IS_WINNER != true)
			{
				if (activityID != "spinid")
				{
					var checklistRow = $(activityID + '_Row');
					if (!isEmpty(checklistRow.getAttribute('largecontent')))
					{
						// there is a large content, and it has not been played yet
						eval(checklistRow.getAttribute('largecontent'));
					}
					else
					{
						var myFlashMovie = document.getElementById("containerSWF");
						if (myFlashMovie)
						{
								myFlashMovie.switchActivity(activityID, checklistRow.getAttribute('playswf'), siteroot + "elements/flash_data/activity/play/" + activityID, checklistRow.getAttribute('otherswf'), siteroot + "elements/flash_data/activity/play/" + activityID, checklistRow.getAttribute('titleswf'), _wsdlUrl(activityID), (dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey, profileKey, _getFlashTabID());
						}
					}

					finishedLoadingFlashActivity(activityID);
				}
				else
				{
					loadSmallContentActivity(activityID);
				}
			}
		}
		else
		{
			var pointsDontShowSession = Cookie.get('pointsDontShowSession');

			if (IS_WINNER == true)
			{
				var d = new Date();
				_createFlashWindow(
					'winners',
					WINNERFLASH,
					siteroot + "elements/flash_data/interstitial/winner/?rand=" + d.getTime(),
					'play',
					true,
					'', '', '', '',
					(dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey,
					profileKey, homepageID,
					TABCOLOR, SELECTEDTABCOLOR, TITLEBARCOLOR);

				reloadFlashContainer = false;
			}
			/*else if (isCardHolder && pointsDontShowSession != "1")
			{
				reloadFlashContainer = true;

				var d = new Date();
				_createFlashWindow(
					'sonycard',
					SONYCARDFLASH,
					siteroot + "elements/flash_data/interstitial/sonycard/?rand=" + d.getTime(),
					'play',
					true,
					'',
					siteroot + "services/flash/SonyCardService.asmx?wsdl",
					'', '',
					(dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey,
					profileKey, homepageID,
					TABCOLOR, SELECTEDTABCOLOR, TITLEBARCOLOR);

				Cookie.set("pointsDontShowSession", "1", null, '/');

				adTimeout = setTimeout(function() { loadFlashActivity(activityID); }, 8000);
			}*/
			else
			{
				var checklistRow = $(activityID + '_Row');

				if (!isEmpty(checklistRow.getAttribute('largecontent')))
				{
					// there is a large content, and it has not been played yet
					eval(checklistRow.getAttribute('largecontent'));
				} else
				{
					_createFlashWindow(
						activityID,
						checklistRow.getAttribute('playswf'),
						siteroot + "elements/flash_data/activity/play/" + activityID,
						_getFlashTabID(),
						false,
						checklistRow.getAttribute('titleswf'),
						_wsdlUrl(activityID),
						(activityID != "tutorial" ? checklistRow.getAttribute('otherswf') : ''),
						(activityID != "tutorial" ? siteroot + "elements/flash_data/activity/play/" + activityID : ''),
						(dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey,
						profileKey, homepageID,
						TABCOLOR, SELECTEDTABCOLOR, TITLEBARCOLOR);
				}

				reloadFlashContainer = false;
			}
		}
	}
	flashTimedOut = false;
	flashLoading = false;

	var top = $("dashboard").cumulativeOffset()[1];
	var scroll = document.documentElement.scrollTop || document.body.scrollTop;
	var isBody = document.documentElement.scrollTop ? false : true;
	if (top < scroll) document[isBody ? "body" : "documentElement"].scrollTop = top - 20;

	$('CheckListTable').select("tr.selected").invoke("removeClassName", "selected");

	$('CheckListTable').select("tr.selected").invoke("removeClassName", "selected");

	var completed_selected = $('CompletedCheckListTable').select("tr.selected");
	if (completed_selected) completed_selected.invoke("removeClassName", "selected");

	var row = $(activityID + '_Row');

	if (row && activityID != "tutorial")
		row.addClassName("selected");
}

_createFlashWindow = function(activityID, 
	playContentURL, playXmlURL, 
	tabID, isFullContent, 
	titleContentURL, serviceURL, 
	otherContentURL, otherXmlURL,
	dateKey, profileKey, homepageID,
	tabColor, selectedTabColor, titleBarColor,
	adLinkURL, isNewWindowAd
) {

	var so = new SWFObject(CONTAINER_SWF_PATH, 'containerSWF', '935', '295', '8', '#000');
	so.addParam("allowScriptAccess", "always");
	so.addParam("wmode", "transparent");

	so.addVariable("activityID", activityID);
	so.addVariable("playContentURL", playContentURL);
	if (!isEmpty(playXmlURL))
		so.addVariable("playXmlURL", playXmlURL);
	so.addVariable("tabID", tabID);
	
	if (isFullContent) {
		so.addVariable("isFullContent", isFullContent ? "true" : "false");
	}
	else
	{
		so.addVariable("titleContentURL", titleContentURL);
		if (!isEmpty(otherContentURL))
			so.addVariable("otherContentURL", otherContentURL);
		if (!isEmpty(otherXmlURL))
			so.addVariable("otherXmlURL", otherXmlURL);
	}
	
	if (!isEmpty(serviceURL))
		so.addVariable("serviceURL", serviceURL);

	if (!isEmpty(adLinkURL))
		so.addVariable("adLinkURL", adLinkURL);

	if (!isEmpty(isNewWindowAd))
		so.addVariable("isNewWindowAd", isNewWindowAd);  

	so.addVariable("dateKey", dateKey);
	so.addVariable("profileKey", profileKey);
	so.addVariable("homePageID", homepageID);
	so.addVariable("tabColor", tabColor);
	so.addVariable("selectedTabColor", selectedTabColor);
	so.addVariable("titleBarColor", titleBarColor);

	if (so.installedVer != null && typeof (so.installedVer) != 'undefined' && so.installedVer.major < 8)
	{
		hasFlashVersion = false;
		$("needflash").setStyle({ display: "block" });
	}
	else
	{
		hasFlashVersion = true;
		so.write("flashcontent");
	}
}


finishedLoadingFlashActivity=function(activityID){
	var the_row = $(activityID + '_Row');
	if (activityID != 'winners' && activityID != 'sonycard') {
		if (ACTIVITY == 'spinid')
		{
			loadFlashActivity(ACTIVITY);
			ACTIVITY = '';
			return;
		}
	
		var smallcontent = null;
		if (!isCardHolder) { smallcontent = the_row.getAttribute('noncardjs'); }
		else { smallcontent = the_row.getAttribute('cardjs'); }
		if (smallcontent && smallcontent != '') eval(smallcontent);
	}

	nextactivity = getNextActivityElement(activityID);

	if (nextactivity == null)
		nextactivity = $(_getFirstNonSpinIDActivityInTable('CheckListTableBody') + '_Row')
}

initializeFlashContainer = function () {
	var affs = getAffinityArray();
	isCardHolder = affs != null && affs.indexOf('182') > -1;
	var hcookie = getHomepageAffinity();
	if (hcookie == null || hcookie == '') hcookie = 'base';
	profileKey = hcookie+'-'+getAffinities();
	flashReady=true;
	loadFlashActivity(defaultChecklistActivity);
}

timeoutFlashActivity=function(activityID) {
	if (!flashTimedOut) {
		flashTimedOut=true;
		var el = nextactivity;
		if (el != null) {
			var timeoutSWF = el.getAttribute('timeoutswf');
			if (timeoutSWF && timeoutSWF != '') loadSmallContentAd(timeoutSWF, null, "javascript:loadFlashActivity('" + el.id.replace('_Row','') + "')");
			var timeoutIMG = el.getAttribute('timeoutimg');
			if (timeoutIMG && timeoutIMG != '') loadSmallContentAd(null, timeoutIMG, "javascript:loadFlashActivity('" + el.id.replace('_Row','') + "')");
		}
	}
}

loadNextFlashActivity=function(activityID) {
	var el = nextactivity;
	nextactivity = null;	
	if (el != null) {
		loadFlashActivity(el.id.replace('_Row',''));
	}
}

_getIncompleteActivityArray = function()
{
	var checklistRows = $('CheckListTableBody').childNodes;
	var checklistLength = checklistRows.length;
	var el = null;
	var validElements = new Array();
	for (var i = 0; i < checklistLength; i++)
	{
		var r = checklistRows[i];
		if (typeof (r.id) != 'undefined'
				&& r.id != null
				&& r.id != ''
				&& r.id != 'spinid_Row' 
				&& r.getAttribute('iscustomactivity') != 'true')
		{
			validElements.push(r.id);
		}
	}

	return validElements;
}

getNextActivityElement=function(activityID) {

	var validElements = _getIncompleteActivityArray();
	
	if (validElements.length > 0) {
		var i = validElements.indexOf(activityID+'_Row');
		if (i >= 0) {
			if (i+1 < validElements.length) {
				return $(validElements[i+1]);
			}
			else {
				return $(validElements[0]);
			}
		}
	}
	return null;
}

getActivityElement = function(activityID)
{
	var validElements = _getIncompleteActivityArray();

	if (validElements.length > 0)
	{
		var i = validElements.indexOf(activityID + '_Row');
		return $(validElements[i]);
	}

	return null;
}

_checkActivityCompletedCookie = function(activityID) {
	var c = Cookie.get('CompletedActivities');
	var c_pairs = c.split('&');
	for (var i = 0; i < c_pairs.length; ++i) {
		var activity_val = c_pairs[i].split('=');
		if (activity_val[0] == activityID)
			return (activity_val[1].split('_')[1] == 'yes');
	}
	return false;
}


// function for flash to update specific content
completeFlashActivity = function(activityid) {
	if (refreshinfographic) {
		if (((activityid == "spttrivia") && ($("info_sptlevel") != null)) ||
				 (activityid == "jeopardydaily") ||
				 (activityid == "jeopardybonus"))
			refreshinfographic();
	}

	var timer_activity = (activityid == "deal" || activityid == "producthighlight" || activityid == "jeopardybonus");
	if (timer_activity && _userIsLoggedIn()) {
		new Ajax.Request('/en/home/_set_activity_completed', { method: "GET", parameters: { activityID: activityid } });
	}

	refreshpoints();  // on dashboard

	if (activityid != "spinid")
		timeoutFlashActivity(activityid);

	_moveCompletedActivity(activityid);
}

loadSmallContentAd=function(contentSWF, imageFile, clickURL){
	var myFlashMovie = document.getElementById("containerSWF");
	if (myFlashMovie) myFlashMovie.switchSmallContent(contentSWF, imageFile, clickURL);
}

loadSmallContentActivity=function(activityID) {
	var myFlashMovie = document.getElementById("containerSWF");
	var checklistRow = $(activityID+'_Row');
	if (myFlashMovie && checklistRow && activityID == "spinid") 
			myFlashMovie.spinIDSmallContent(activityID, checklistRow.getAttribute('playswf'), siteroot+"elements/flash_data/activity/play/spinid", _wsdlUrl(activityID));
}

var loadCustomActivityClicked = false;
loadCustomActivity = function(activityID, gameID, clickURL, isLearn, newWindow)
{
	if (clickURL == "" || loadCustomActivityClicked)
		return;

	loadCustomActivityClicked = true;

	var func = function()
			{
				if (newWindow)
				{
					_moveCompletedActivity(activityID);
					window.open(clickURL);
					loadCustomActivityClicked = false;
				}
				else
					location.href = clickURL;
			};
			
	if ((Cookie.get('CompletedActivities').indexOf(activityID + '=' + gameID + '_' + 'yes') > -1)
			|| !_userIsLoggedIn()) {
		func();
		return;
	}

	new Ajax.Request('/en/home/_set_customactivity_completed',
		{ method: "GET",
			parameters: { activityID: activityID,
				gameID: gameID
			},
			onComplete: func
		});
}

loadLargeContentAd = function(activityID, contentSWF, titleSWF, clickURL, timeoutSeconds, isNewWindowAd)
{

	var titlebarswf = titleSWF;
	
	if (null == titleSWF)
	{
		var el = $(activityID + '_Row');
		
		if(el != null)
			titlebarswf = el.getAttribute('titleswf');
	}
		
	if (hasFlashVersion == null || reloadFlashContainer == true)
	{
		reloadFlashContainer = false;
		
		_createFlashWindow(
			activityID,
			contentSWF,
			"",
			_getFlashTabID(),
			false,
			titlebarswf, 
			"", 
			"", 
			"",
			(dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey,
			profileKey, homepageID,
			TABCOLOR, SELECTEDTABCOLOR, TITLEBARCOLOR, clickURL, isNewWindowAd);
	} else if (hasFlashVersion && IS_WINNER != true) {
		var myFlashMovie = document.getElementById("containerSWF");
		if (myFlashMovie)
		{
			myFlashMovie.switchActivity(activityID, contentSWF, "", "", "", titlebarswf, "",
				(dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey,
				 profileKey, _getFlashTabID(), (clickURL != '' ? clickURL : null), isNewWindowAd);
		}
	}

	adTimeout = setTimeout(
		function()
		{
			var myFlashMovie = document.getElementById("containerSWF");
			if (myFlashMovie && el != null)
					myFlashMovie.switchActivity(activityID, el.getAttribute('playswf'), siteroot + "elements/flash_data/activity/play/" + activityID, el.getAttribute('otherswf'), siteroot + "elements/flash_data/activity/play/" + activityID, el.getAttribute('titleswf'), _wsdlUrl(activityID), (dateKey.length == 12 && activityID != "wofbonuspuzzle") ? dateKey.substring(0, 10) : dateKey, profileKey, _getFlashTabID());
			
		}, timeoutSeconds * 1000);
}

_processActivityListPrizes = function(id, o) {
	var result = _processAjaxRequestData(o);
	if (result != null) {
		for(var i in result) {
			var activityPrize = result[i];
			if (!$(i+"_Prize"))
				continue;

			if (activityPrize.OverrideDescription) {
				$(i+"_Prize").innerHTML = activityPrize.Description;
			} else {
				var prizeNode = $('TemplatePrize').cloneNode(true);
				prizeNode.id = 'TemplatePrize_'+i;
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__ID__/g, i);
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__TinyImagePath__/g, activityPrize.TinyImagePath);
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__SKU__/g, activityPrize.SKU);
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__ACTIVITYID__/g, activityPrize.ACTIVITYID);
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__PRODUCT_MODULE_URL__/g, activityPrize.ProductModuleUrl);
				prizeNode.innerHTML = prizeNode.innerHTML.replace(/__Headline__/g, activityPrize.Headline);
				$(i+"_Prize").appendChild(prizeNode);
				$('TemplatePrizeImage'+i).src = $('TemplatePrizeImage'+i).getAttribute('tinypath');
				
				$('TemplatePrize_'+i).removeClassName("boxHide");
			}
		}
	}
}

_processActivityListWinners = function(id, o) {
	var result = _processAjaxRequestData(o);
	if (result != null) {
		for(var i in result) {
			if ($(i+"_Winner"))
				$(i+"_Winner").innerHTML = result[i];
		}
	}
}

_processActivityListIsWinner = function(id, o) {
	var result = _processAjaxRequestData(o);
	if (result != null) {
		IS_WINNER = result.IsWinner;
	}
}

isEmpty = function( inputStr ) { 
	return ( null == inputStr || "" == inputStr );
}

_processActivityListLargeContent = function(id, o) {
	var result = _processAjaxRequestData(o);
	if (result != null) {
		for(var i in result) {
			if ($(i+"_Row") && result[i] != "") {
				$(i+"_Row").setAttribute("largecontent", result[i]);
			}
		}
	}
}

_fixTableRowColors = function(table_id) {
	var checklistRows = $(table_id).getElementsByTagName('tr');
	for (var i = 0; i < checklistRows.length; i++) {
		var row = $(checklistRows[i].id);
		if (i % 2 == 0) {
			row.removeClassName('even');
			row.addClassName('odd');
		}
		else {
			row.removeClassName('odd');
			row.addClassName('even');
		}
	}
}

_recolorActivityRows = function() {
	_fixTableRowColors('CheckListTable');
	_fixTableRowColors('CompletedCheckListTable');
}

_moveCompletedActivity = function(completed_activity_type) {
	
	if (!_userIsLoggedIn())
		return;

	var row = $(completed_activity_type + '_Row');
	var completechecklist = $('CompletedCheckListTableBody');
	
	if (row && row.parentNode.id == 'CheckListTableBody'){
		
		var i = 0;
		
		for (i = 0; i < completechecklist.rows.length; i++)
			if (row.getAttribute('activityindex') < completechecklist.rows[i].getAttribute('activityindex'))
			{
				completechecklist.insertBefore(row, completechecklist.rows[i]);
				break;
			}
		
		if(completechecklist.rows.length <= i)	
			completechecklist.appendChild(row);
		
		_recolorActivityRows();
		$('completedactivities_div').style.visibility = 'visible';
	}
}

_setActivityCompletedCookie = function(activityID, gameID, completed)
{
	var c = Cookie.get('CompletedActivities');
	var i = c.indexOf(activityID, 0);
	if (i > -1)
	{
		var end = c.indexOf('&', i + activityID.length);
		var tmp = c.substring(i, (end > -1 ? end : null));
		c = c.replace(tmp, activityID + '=' + (gameID != null && gameID.length > 0 ? gameID : ' ') + '_' + (completed ? 'yes' : 'no'));
	}
	else
	{
		c = c + '&' + activityID + '=' + (gameID != null && gameID.length > 0 ? gameID : ' ') + '_' + (completed ? 'yes' : 'no');

	}

	Cookie.set('CompletedActivities', c);
}

// Returns the first activity, or null if not found.
_getFirstNonSpinIDActivityInTable = function(table_body_id) {
	var rows = $(table_body_id).getElementsByTagName('tr');
	var count = rows.length;
	for(var i = 0; i < count; i++) {
		if (rows[i].nodeType == 1 
			&& rows[i].id != 'spinid_Row'
			&& rows[i].getAttribute('iscustomactivity') != 'true')
			{
			return rows[i].id.replace('_Row','');
		}
	}
	return null;
}

_getDefaultChecklistActivity = function() {
	// If the activity was specified in the URL, or it was configured as
	// the default, use that.
	if (ACTIVITY != '' && ACTIVITY != 'spinid')
		return ACTIVITY;

	if (!_userIsLoggedIn())
		return 'tutorial';

	// if the member is not opted in to the affinity, should default
	// to the club tutorial
	if (AFFINITY != "base" && !isAffinity(AFFINITY))
		return 'tutorial';

	// Return the first row in the activity list
	var tmp = _getFirstNonSpinIDActivityInTable('CheckListTableBody');
	if (tmp != null) return tmp;

	tmp = _getFirstNonSpinIDActivityInTable('CompletedCheckListTableBody');
	if (tmp != null) return tmp;

	// If we got here, something is off ... no activities?
	return 'tutorial';
}

// Move the completed activities to the completed table.
_processCompletedActivities = function(id, o) {
	
	var result = _processAjaxRequestData(o);
	if (result != null) {
		for(var completed_activity_type in result) {
			_moveCompletedActivity(completed_activity_type);
		}
	}

	defaultChecklistActivity = _getDefaultChecklistActivity();
}

_processActivityListDate = function(id, o) {
	var result = _processAjaxRequestData(o);
	if (result != null) {
		if ($(id))
			$(id).innerHTML = result.date;
	}
}

_processDateKey = function(id, o) {
	dateKey=o;
	initializeFlashContainer();
}

_processAds = function(id, o) {
	AjaxPageRequests.Updater(id,o);
	if ($('adsRotating'))
		if ($F('adsRotating')=="true" && setRotatingAds)
			if ($('adsRotatingTime'))
				setRotatingAds($F('adsRotatingTime'));
	    else
				setRotatingAds(5);
}

_processTopActivity = function(id, o) {

	var result = _processAjaxRequestData(o);
	if (result != null && result.topactivity != "")
		_promoteActivityToTop(result.topactivity);
}

_promoteActivityToTop = function(activityID)
{
	var checklist = $('CheckListTableBody');
	var activityrow = $(activityID + '_Row');
	if (activityrow.parentNode == checklist)
	{
		checklist.insertBefore(activityrow, checklist.rows[0]);
		activityrow.setAttribute('activityindex', '0');
		_fixTableRowColors('CheckListTableBody');
	}
}

// -------------------------------------------
// Functions called by flash button/ad clicks

call_f_with_params = function(suspect_json_string) {
  var s = suspect_json_string.replace(/^\s+|\s+$/g, "");

	// __QUOTE__ string placeholders: quotes (or apostrophes, double
	// apostrophes, html char classes, etc.) in the json would break any
	// view (eg, the flash ads), as the rendering engine would see the
	// quote/apos/whatever as ending the string.  Since the
	// suspect_json_string is passed as a string argument, replace the
	// placeholder with actual quotes.
	s = s.replace(/__QUOTE__/g, "\"");

	// Same with &.
	s = s.replace(/__AMP__/g, "&");

  if (s.substring(0, 1) == '{') {  // Must be a hash.
    h = eval('(' + s + ')');
    if (h.f) {
      h.f.call(null, h.params);
    }
  }
}

redirect_to = function(params) {
  if (params.url == undefined) {
    return;
  }
	if (params.url == '') {
		return;
	}
  if (params.new_win) {
    window.open(params.url, 'new_win');
  }
  else {
	  document.location = params.url;
  }
}

show_product_highlight = function(params) {
		if (params.sku) {
			openProductModule('/en/redeem/product/index?pid=' + params.sku)
		}
		else if (params.id) {
			openProductModule('/elements/widgets/cust_product_highlight/index/' + params.id)
		}
}

// End functions called by flash button clicks.
// -----------------------


initializeHomepageContent = function(){

	// if logged in, ajax ads
	if (_userIsLoggedIn()) {
		// display logged in learn cells
		var elements = $('ActivityListPanel').getElementsByClassName('learnLoggedIn');
		for (var i = 0; i < elements.length; i++) {
			elements[i].removeClassName('boxHide');
		}
				
		// ajax member specific ads
		AjaxPageRequests.Add('AdsPanel', '/elements/widgets/adds/' + AFFINITY + '/', "_processAds");
		
		var iswinnercookie = Cookie.get("Winner");
		if (iswinnercookie=="0" || iswinnercookie=="1") {
			IS_WINNER = (iswinnercookie=="1");
		} else {
			AjaxPageRequests.Add('ActivityListIsWinner', '/en/home/'+AFFINITY+'/_activitylist_iswinner/', "_processActivityListIsWinner");
		}
		
		// if the member is not opted in to the affinity, should default to the club tutorial
		if (AFFINITY != "base" && !isAffinity(AFFINITY)) {
			defaultChecklistActivity = "tutorial";
		}
	} else {  // Not logged in

		// Ensure LOGINREDIRECT is not empty, or we'll keep redirecting
		// back here.
		if ((ACTIVITY != '') && (LOGINREDIRECT != '')) {
				showAjaxedModalDialog('modalLogin', '/en/login/modal_login/?time='+(new Date()).getTime(),'350');
		}
		else {
			ACTIVITY = '';
		}

		// display logged out learn cells
		var elements = $('ActivityListPanel').getElementsByClassName('learnLoggedOut');
		for (var i = 0; i < elements.length; i++) {
			elements[i].removeClassName('boxHide');
		}

		// display default not logged in ads
		$('AdsPanel').innerHTML = $('AdsPanelHtml').value;
		if ($('adsRotating'))
			if ($F('adsRotating')=="true" && setRotatingAds)
  			if ($('adsRotatingTime'))
	  			setRotatingAds($F('adsRotatingTime'));
	      else
			  	setRotatingAds(5);

		$('AdsPanel').removeClassName('boxHide');
	}

	AjaxPageRequests.Add('ActivityPanelWinners', siteroot + 'en/home/' + AFFINITY + '/_activitylist_winners/', "_processActivityListWinners");
	AjaxPageRequests.Add('ActivityPanelPrizes', siteroot+'en/home/'+AFFINITY+'/_activitylist_prizes/', "_processActivityListPrizes");
	AjaxPageRequests.Add('ActivityPanelDate', siteroot+'elements/widgets/ajaxpagerequests/getdate', "_processActivityListDate");
	AjaxPageRequests.Add('ActivityPanelLargeContent', siteroot+'en/home/'+AFFINITY+'/_activitylist_largecontent/', "_processActivityListLargeContent");
	AjaxPageRequests.Add('PromoteActivity', siteroot + 'en/home/' + AFFINITY + '/_topactivity/', "_processTopActivity");
	AjaxPageRequests.Add('CompletedActivities', siteroot + 'en/home/' + AFFINITY + '/_activitylist_completed/', "_processCompletedActivities");
	AjaxPageRequests.Add('DateKey', 'datekey', "_processDateKey");

	_initializeDashboard();
	AjaxPageRequests.Execute('POST');
};

open_new_window = function(url) {
	window.open(url);
}

Event.observe(window, "load", initializeHomepageContent);
