var AFNusername = "AFNusername";

/* cookienames
AFNfavoriteslabels
AFNfavoritesurls
AFNwidescreen
AFNnewsboxes
AFNrealitycheck
*/

// date data
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var daysOfWeek = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var thedate = new Date(); var theday; var themonth; var theyear;
var boxesindex = 0;
var windowwidth;
var justwideenough = 894;
var lastupDate = new Date("April 21, 2006");
var unshuffledboxes = new Array ("news","E","entertainment","E","employment","E","advocacy","E","profiles","E");
var bannerArray = new Array ("../advertising/graphics/voteyes.gif","../advertising/graphics/employment.gif");
var bannercounter = 0;
// cookiedata and expiration
var expires=new Date();
expires.getTime();
expires.setTime(expires.getTime() + 1000*60*60*24*90); // a three-month old cookie
var expiration=expires.toGMTString();

window.onload = function () {
	fixFrames();
	changeBanner(); setInterval("changeBanner()",60000);
}

if (!readCookie("AFNnewsboxes") ) {
	top.topFrame.boxes = unshuffledboxes;
} else {
	var temp = eval("new Array(" + readCookie("AFNnewsboxes") +")" );
	var cookiedate = eval('new Date("' + temp[0] + '")' );
	if (cookiedate > lastupDate) {
		top.topFrame.boxes = temp.slice(1,temp.length);
	} else  {
		top.topFrame.boxes = unshuffledboxes;
	}
}

setDateVars(thedate);
determineScreenWidth(); readWideScreen();

function readCookie(which) { // a generic cookie reader
	if (document.cookie.indexOf(which) != -1) {
		var start = document.cookie.indexOf(which);
		var end = document.cookie.indexOf(";", start);
		if (end == -1) {
			end = document.cookie.length;
		}
		return ( unescape( document.cookie.substring(start+which.length+1,end) ) );
	} else {
		return (null);
	}
}
function writeCookie(which, data, remove) { // a generic cookie writer
	if (remove) {
		var quickdeath = thedate.toGMTString();
		document.cookie = which + "=" + escape(data) + "; expires=" + quickdeath + "; path=/;";
	} else {
		document.cookie = which + "=" + escape(data) + "; expires=" + expiration + "; path=/;";
	}
}

function readFavoriteCookies () {
	if (readCookie("AFNfavoriteslabels")) {
		var resetlabels = ""; var reseturls = "";
		var labels = readCookie("AFNfavoriteslabels");
		var urls = readCookie("AFNfavoritesurls");
		
		eval( "var labelarray = new Array(" + labels + ")");
		eval( "var urlarray = new Array(" + urls + ")");
		
		var favorites = "<p class=\"StoryAnchor\">Your favorite forums:<br />";
		for (var x = 0; x < labelarray.length; x++) {
			favorites += "\&nbsp;\&nbsp;<a href=\"" + urlarray[x] + '" target="_parent">' + labelarray[x] + "</a><br />";	
			resetlabels = resetlabels + "'" + labelarray[x] + "',"; reseturls = reseturls + "'" + urlarray[x] + "',";
		}
		favorites += "</p>";
		writeCookie("AFNfavoriteslabels", resetlabels.slice(0,resetlabels.length-1)); writeCookie("AFNfavoritesurls", reseturls.slice(0,reseturls.length-1));
		return (favorites);
	} else {
		favorites = '<p class="StoryAnchor">Your favorite forums:<br />';
		favorites += '<p class="StoryAnchor">\&nbsp;\&nbsp;You can set your forum favorites by clicking <a href="../navigation/settings.html" target="_parent">my Settings.</a></p>';
		return (favorites);
	}
}

function printCheck () {
	if (!readCookie("AFNrealitycheck")) {
		document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href="/support/realitycheck.html">Reality Check</a></strong>');
	}
}

function determineScreenWidth() {
	if (self.innerHeight) { // all except Explorer
			windowwidth = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowwidth = document.documentElement.clientWidth;
	}
	else if (document.body) { // other Explorers
			windowwidth = document.body.clientWidth;
	}
}

function readWideScreen () { // called from head of mainFrame.shtml
	if ( !readCookie("AFNwidescreen") ) {
		writeCookie("AFNwidescreen", "0")
		top.topFrame.widescreen=0;
	} else {
		top.topFrame.widescreen = readCookie("AFNwidescreen");
	}
}

function printUserInfo(which) {
	if (document.cookie.indexOf(which) == -1) {
		return("\&nbsp;\&nbsp;Please login or register and enter your profile to create your AfterNet.<br />");
	} else {
		return ( "<iframe id=\"messages\" style=\"height:25px\" src=\"http://www.theAfterNet.net/Scripts/yabb/YaBBlite.pl\" allowtransparency=\"true\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" width=\"100%\" scrolling=\"no\"></iframe>");
	}
}

// write frames
function shuffle (direction,which) {
	var temp = top.topFrame.boxes.splice( which*2,2 );
	if (direction == "up") {
		top.topFrame.boxes = temp.concat(top.topFrame.boxes);
	} else {
		top.topFrame.boxes = top.topFrame.boxes.concat(temp);
	}
	var data = '"' + thedate + '","';
	for (var x = 0; x < top.topFrame.boxes.length; x++) {
		if (x == (top.topFrame.boxes.length-1) ) {
			data += top.topFrame.boxes[x] + '"';
		} else 	{
			data += top.topFrame.boxes[x] + '","';
		}
	}
	writeCookie("AFNnewsboxes",data);
	location.href = location.pathname;
}
function minimize(ranking,direction) {
	if (direction == "C") {
		top.topFrame.boxes[(ranking*2)+1] = "C"
	} else {
		top.topFrame.boxes[(ranking*2)+1] = "E"
	}
	var data = '"' + thedate + '","';
	for (var x = 0; x < top.topFrame.boxes.length; x++) {
		if (x == (top.topFrame.boxes.length-1) ) {
			data += top.topFrame.boxes[x] + '"';
		} else 	{
			data += top.topFrame.boxes[x] + '","';
		}
	}
	writeCookie("AFNnewsboxes",data);
	location.href = location.pathname;
}
function setOrder () {
	for (var x = 0; x < top.topFrame.boxes.length; x+=2) {
		eval( 'top.mainFrame.' + top.topFrame.boxes[x] + '.ranking = x/2');
	}
}
function writeAllNewsBoxes() {
	for (var x = 0; x < top.topFrame.boxes.length; x+=2) {
		writeNewsBoxes(top.topFrame.boxes[boxesindex],top.topFrame.boxes[boxesindex+1]);
	}
	setTimeout("setOrder()",1000);
}
function writeNewsBoxes (which,direction) {
	var filename;
	if (direction == "C") {
		filename = "C" + which;
	} else {
		filename = which;
	}
	document.write('<p><iframe src="../news/' + filename + 'box.shtml" class="newsboxes" width="500" height="30" frameborder="0" name="' + which + '" scrolling="no" id="' + which + '" allowtransparency="yes"></iframe></p>');
	boxesindex += 2;
}
function setDateVars (which) {
		which.getMonth() < 10 ? themonth = "0" + (which.getMonth() + 1): themonth = which.getMonth() + 1; 
		which.getDate() < 10 ? theday = "0" + which.getDate(): theday = which.getDate();
		theyear = which.getFullYear();
}
function bigEnough (which, column) {
	var path; var framewidth = 500;
	if (which == "tdih") {
		path = "../tdih/" + themonth + "/" + theday + "/" + "index.html";
	} else  if (which == "settings") {
		path = "../support/settings.html";
	} else  if (which == "whatshot") {
		path = "../news/whatshot.html";
	} else  if (which == "blogworthy") {
		path = "../blogs/blogworthy.html";
	}
	if (column == 1) {
		if ( windowwidth < justwideenough || top.topFrame.widescreen == 0) {
			document.write('<p><iframe frameborder="0" src="' + path +'" scrolling="no" height="30" width="' + framewidth + 'px" name="' + which + '" id="' + which + '"></iframe></p>');
		}
	} else {
		
		if ( windowwidth > justwideenough && top.topFrame.widescreen == 1) {
			framewidth =  windowwidth - 670;
			document.write('<p><iframe frameborder="0" src="' + path +'" scrolling="no" height="30" width="' + framewidth + 'px" name="' + which + '" id="' + which + '"></iframe></p>');
		}
	}	
}

// miscellaneous
function changeBanner() {
	document.getElementById("banner").src = bannerArray[bannercounter];
	bannercounter++;
	if (bannercounter > bannerArray.length-1) {
		bannercounter = 0;
	}
}