function SetCookie (name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                ((expires == null) ? "" : ("; expires=" +expires.toGMTString())) +
                ((path == null) ? "" : ("; path=" + path)) +
                ((domain == null) ? "" : ("; domain=" + domain)) +
                ((secure == true) ? "; secure" : "");
}
function AndyFunction(command, args) {
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
	// set expiry date to 1 year from now.
	var largeExpDate = null;
    SetCookie('Andycookie',command,largeExpDate,myDomain);
}
function getCookieVal (offset) {
	  var endstr = document.cookie.indexOf (";", offset);
	  if (endstr == -1)
		endstr = document.cookie.length;
	  return unescape(document.cookie.substring(offset, endstr));
	}
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		  return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	  }
	return null;
}

	//  Function to delete a cookie. (Sets expiration date to current date/time)
	//    name - String object containing the cookie name
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

var msg0,msg1,msg2;

function getUD() {
	if ( readCookie('SPS_UD') != null && readCookie('SPS_UD') != "" ) {
		var temp = unescape(readCookie('SPS_UD')).split('|');
		msg0='Hi, <b>' + temp[1] + '</b>!<br /><br /><a href="bananaslide.htm"><img src="../images/btn_play.gif" alt="Play the Game" /></a><br /><br />Not ' + temp[1] + '? <a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?logout=true&finalSuccessURL=' + location.href + '" style="font-weight:bold;" title="Sign in and play Mr. Brainfright&rsquo;s Banana Slide">Sign In</a>';
		msg1='Hi, <b>' + temp[1] + '</b>!<br />Not ' + temp[1] + '? <a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?logout=true&finalSuccessURL=' + location.href + '" style="font-weight:bold;" title="Sign in and play Mr. Brainfright&rsquo;s Banana Slide">Sign In</a>';
		msg2='Hey there, <b>' + temp[1] + '</b> &#151; Click the "Add" button to add the widget to your Profile page! <br /><a href="http://kids.scholastic.com/community/myStacks.htm"><img src="/andygriffiths/images/btn_add.gif" alt="Add the Andy Griffiths Widget" border="0" title="Add the Andy Griffiths Widget" class="button"></a><br />Not ' + temp[1] + '? <a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?logout=true&finalSuccessURL=' + location.href + '">Sign In</a>';
	}
	else {
		msg0='You must be signed in to save your high score.<br /><a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?finalSuccessURL=' + location.href + '" style="font-weight:bold;" title="Sign in and play Mr. Brainfright&rsquo;s Banana Slide">Sign In Now</a><br /><br /><a href="bananaslide.htm" title="Sign in and play Mr. Brainfright&rsquo;s Banana Slide"><img src="../images/btn_play.gif" alt="Play the Game" /></a>';
		msg1='You need to be signed in to save your high score.<br /><br /><a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?finalSuccessURL=' + location.href + '" style="font-weight:bold;" title="Sign in and play Mr. Brainfright&rsquo;s Banana Slide">Sign In Now!</a>';
		msg2='Sign in to add the Wacky Facts widget<br />to your profile.<br /><a href="http://my.scholastic.com/sps_my_account/accmgmt/FlashLogin.jsp?finalSuccessURL=' + location.href + '">Sign In Now</a>';
	}
}
function displaySigninMsg() {
	getUD();
	jQuery('div#kms2').html(msg2);
	jQuery('div#kms0').html(msg0);
	jQuery('div#kms1').html(msg1);
}
function updateSigninMsg() {
	getUD();
	jQuery('div#kms2').html(msg2);
	jQuery('div#kms0').html(msg0);
	jQuery('div#kms1').html(msg1);
}

addLoadEvent(displaySigninMsg);
addLoadEvent(function(){setMyAccountCloseHook(function(){updateSigninMsg();});});
addLoadEvent(function(){setOnSignOutHook(function(){updateSigninMsg();});});