//Core.js: stuff that the average user shouldn't be playing with
//NO TOUCHY
//(unless you're reall sure of what you're doing of course)

//Standard date info gettage
var time=new Date()
var day=time.getDay()
var date=time.getDate()
var month=time.getMonth()
var year=time.getFullYear()

/*Meta data for the comics. Filled by zInfo*/
var days=new Array()
var dates=new Array()
var months=new Array()
var years=new Array()
var titles=new Array()
var images=new Array() //filename, not path. The comics go in site+cDir
var xs=new Array() //widths.
var ys=new Array() //heights.
var notes=new Array() //notes that go at the bottom of the page

//Represents the total number of comics on the site. Counts from 0
var totalPages=-1

var isArchive=false //states if we're on the full archives or not
var isIndex=false //are we on the index page, or somewhere else?


//Any library type functions go here.

function intToDay(day)
{
var r;
if(day==0) r="Sunday"
else if(day==1) r="Monday"
else if(day==2) r="Tuesday"
else if(day==3) r="Wednesday"
else if(day==4) r="Thursday"
else if(day==5) r="Friday"
else if(day==6) r="Saturday"
else r=0
return r
}

function intToMonth(month)
{
var r
if(month==0) r="January"
else if(month==1) r="February"
else if(month==2) r="March"
else if(month==3) r="April"
else if(month==4) r="May"
else if(month==5) r="June"
else if(month==6) r="July"
else if(month==7) r="August"
else if(month==8) r="September"
else if(month==9) r="October"
else if(month==10) r="November"
else if(month==11) r="December"
return r
}

function daysInMonth(m)
{
m++; if(m>12) m=1;
if(m==2) if(year%4 != 0) return 28; else return 29; //correct for leap years during february
else if(m==4 || m==6 || m==9 || m==11) return 30
else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) return 31
else {document.write("!!AHH!! The month is " +m+ " instead of a normal month!!<br>"); return 0;}
}

function monthNameShort(mont){
result="";
if(mont>11) mont=0;
if(mont==0) result="JAN";
else if(mont==1) result="FEB";
else if(mont==2) result="MAR";
else if(mont==3) result="APR";
else if(mont==4) result="MAY";
else if(mont==5) result="JUN";
else if(mont==6) result="JUL";
else if(mont==7) result="AUG";
else if(mont==8) result="SEP";
else if(mont==9) result="OCT";
else if(mont==10) result="NOV";
else if(mont==11) result="DEC";
return result;}

//Turns 1 into "1st", 32 into 32nd, 53 into 53rd, and 76 into 76th, for example.
function numSuffix(n)
{
var suffix = "th"
if(n<11 || n>20)
 {
 if(n%10==1) suffix = "st"
 else if(n%10==2) suffix = "nd"
 else if(n%10==3) suffix = "rd"
 }
return suffix
}


//Adds a number of digits in front of a string until it is a specified length. Good for fixing times that would be chopped.
function addZeroes(n,digit,num)
{
var re=n+""
var len=re.length+1
for(i=0;i<=num-len;i++)
	{
	re=digit+re
	}
return re;
}


function putCookie(name,value,daysToExpiry,path,domain,secure)
{
/*This is the full list of cookie fields:
Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure*/
putCookie_time=new Date()
y=putCookie_time.getFullYear()
m=putCookie_time.getMonth()
d=putCookie_time.getDate()+daysToExpiry //plus any # of days to set it ahead by that much. Yay, advancement code.
while(d>daysInMonth(m))
	{
	d-=daysInMonth(m++);
	if(m==12) {m=0; y++}
	}
var putCookie_cookieVal=name+"="+value+"; expires="+d+"-"+monthNameShort(m)+"-"+y+" 05:00:00 GMT"
if(path) putCookie_cookieVal+="; path="+path;
if(domain) putCookie_cookieVal+="; domain="+domain;
if(secure) putCookie_cookieVal+="; secure";
if(document.cookie == document.cookie) document.cookie=putCookie_cookieVal
}


function getCookie(name)
{
if(document.cookie)
	{
	index = document.cookie.indexOf(name);
	if(index!=-1)
		{
		namestart = (document.cookie.indexOf("=", index) + 1);
		nameend = document.cookie.indexOf(";", index);
		if (nameend == -1) {nameend = document.cookie.length;}
		return document.cookie.substring(namestart, nameend);
		}
	}
}


/*zInfo stuff*/
var dayOfTheWeek = new Array(0,0,0,0,0,0,0)
var dayOfTheMonth=new Array(0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0)
var everyXDays=0;

//It might be useful to add a "default  to" feature, where you specify what you want to defualt to and then switch the x/y vars around to accomodate this. Like what I used to do
function addComicFull(day,date,month,year,title,image,x,y,note)
{
month--
//which way is faster?
d=new Date(year,month,date)
if(d.getTime()>time.getTime()) return;
totalPages++;
days[totalPages]=day
dates[totalPages]=date
months[totalPages]=month
years[totalPages]=year
titles[totalPages]=title
images[totalPages]=image
xs[totalPages]=x
ys[totalPages]=y
notes[totalPages]=note
}



function addComic(title,image,x,y,note)
{
//get the date on the previous comic
day=days[totalPages]
date=dates[totalPages]
month=months[totalPages]
year=years[totalPages]
matchFound=false; //flag to hold whether we've blindly bumbled over the correct date yet.

daysCount=0; //how many days in the future the day is

//search matches in days of the week
if(!matchFound) for(daysCount=0; daysCount<7;)
	{
	daysCount++
	n=day+daysCount
	if(n>=7) n-=7
	if(dayOfTheWeek[n])
		{
		matchFound=true;
		break;
		}
	}

dIM=daysInMonth(month);
if(!matchFound) for(daysCount=0; daysCount<dIM;)
	{
	daysCount++
	n=date+daysCount
	if(n>=dIM) n-=dIM
	if(dayOfTheMonth[n])
		{
		matchFound=true;
		break;
		}
	}

if(!matchFound) if(everyXDays!=0)
	{
	daysCount=everyXDays;
	matchFound=true;
	}

day+=daysCount;
date+=daysCount;

//calculate real dates, not just "daysCount days from now"
while(day>=7) day-=7;
while(date>daysInMonth(month))
	{
	date-=daysInMonth(month++);
	while(month>11)
		{month-=12;
		year++;}
	}

addComicFull(day,date,month+1,year,title,image,x,y,note) //+1 is for jan=1
}



//Miscellaneous functions

//turns the "authors" array into a string
function authorString()
{var re=""
for(ti=0;ti<authors.length;ti++)
	{re+=authors[ti];
	if(ti!=authors.length-1) re+=", "
	if(ti==authors.length-2) re+="and "}
return re;}

//takes the index of a comic and gives it's date in the format that keenspace uses in their title bars
function dateString(n) 
{return intToDay(days[n])+ ", " +intToMonth(months[n])+ " the " +dates[n]+numSuffix(dates[n])+ ", "+years[n]}

//pgNess(): toggle the pg status of the site. Optional. Requires a bunch of extra work to be any good
//so you may just want to delete pgNess(). Uses cookies
function pgNess()
{status=true
if(pg) status=false
putCookie("pgness",status,3,'/')
location.reload();}

//Extract the vars from the GET data using args.js
var page=1;
var pg=false;
pgTmp=getCookie("pgness")
if(pgTmp=="true") pg=true
var args=new argv()
pageTmp=args.getVal("p")
page=pageTmp/1 //Make the variable numeric