// Common functions used by Huddingelänkarna

// start och stopp strängar for undermenyerna
submenuItemStart = '<SPAN class="submenuItem">';
submenuItemEnd = '</A></SPAN>'
submenuAStart = '<A href="';
submenuAEnd = '" target = "_parent">';

// stödrutiner

// datumhantering

// constants for date calculations
msPerDay = 24 * 60 * 60 * 1000 // Number of milliseconds per day

// returns a Date object for the dateString parameter, eg. '2002-03-03'.
function stringToDate(dateString) {
//	 string format YYYY-MM-DD
	if (dateString != "") {
		y = Number(dateString.substr(0,4));
		m = Number(dateString.substr(5,2));
		d = Number(dateString.substr(8,2));
		return new Date(y,m-1,d);
	} else {
		return null;
	}
}

// returnerar en datumsträng, tex 25/3, för angiven dag i samma vecka som date. måndag = 0 ... söndag = 6 
function shortDate(day, date) {
	date.setTime(weekStartDate(date).getTime() + day*24*60*60*1000);
	return date.getDate() + '/' + (date.getMonth() + 1);
}

// returns a Date object for the date parameter with the time component set to zero.
function cleanDate(date) {
	return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

// returns the swedish name for the specified month. 0 = januari, ..., 11 = december.
function nameOfMonth(month) {
	names = ["januari","februari","mars","april","maj","juni","juli","augusti","september","oktober","november","december"];
	return names[month];
}

// returns the date for the monday in the same week as the day specified in the parameter date.
function weekStartDate(date) {
//	 beräkna datum för veckans första dag (måndag)
	weekday = (date.getDay() == 0) ? 6 : date.getDay() - 1;
	weekstartdate = new Date();
	weekstartdate.setTime(date.getTime() - weekday*24*60*60*1000);
	return weekstartdate;
}

function getWeek(date) {
    /* Calculate the day of the year value for the first monday using the rule:
       The first week of the year is the first week that has at least for days
       in the new year, i.e. if January 1 is a Monday, Tuesday, Wednesday or 
	Thursday then it is in week 1 otherwise it is in the previous years 
       last week.
	If January 1 is not in week 1 then its in the last week of the previous year.
	Determine if it is week 52 och week 52 of the previous year by calculating
	the date for the monday in the first week of the previous year and check what
	weeknummer Januari 1 this year gets. */



	// calculate the date for the monday in the first week this year 
	fdty = new Date(date.getFullYear(), 0, 1);
	// change day of week numbering from Sunday = 0 to Monday = 0
	wdty = (fdty.getDay() == 0 ? 6 : fdty.getDay() - 1);
	// calculate the date for the monday in the same week as the new years first day
	dfm = (wdty <= 3 ? new Date(fdty.getTime() - wdty*msPerDay) : new Date(fdty.getTime() + (6-wdty+1)*msPerDay));
	wno = (date.getTime() - dfm.getTime()) / msPerDay / 7;
	if (wno < 0) {
		// check if the first day this year is in week 52 or week 53 of last year
		// calculate the date for the monday in the first week of last year 
		fdly = new Date(date.getFullYear() - 1, 0, 1);
		// change day of week numbering from Sunday = 0 to Monday = 0
		wdly = (fdly.getDay() == 0 ? 6 : fdly.getDay() - 1);
		// calculate the date for the monday in the first week of last year
		dfmly = (wdly <= 3 ? new Date(fdly.getTime() - wdly*msPerDay) : new Date(fdly.getTime() + (6-wdly+1)*msPerDay));
		wno = (dfm.getTime() - dfmly.getTime()) / msPerDay / 7;
	} else {
		wno = Math.floor(wno + 1);
	}		
	return wno;
}

// calculate the week number. week 1 is the first week with 4 or more days
function getYearDay(date) {
   /*	Calculate the number of the day within the year for a given date */
	 // create a date object for the first day of the year
   fd = new Date(date.getFullYear(), 0, 1);
   // remove the time component of todays date
   today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
   // calculate the day number in the year
   yd = ((today.getTime() - fd.getTime()) / msPerDay) + 1;
   return yd;	
}

// Objects
// type, a = public, m = members only, e = external

function Activity(date, time, activity, link, type) {
	this.date = stringToDate(date);
	this.time = time;
	this.activity = activity;
	if (time == '00:00:00' | time == '24:00:00') {
		this.caption = activity;
	}else{
		this.caption = time.substr(0,5) + " " + activity;
        }
	this.link = link;
	this.type = type;
}

function Meal(date, dish, link, type) {
	this.date = stringToDate(date);
	this.caption = dish;
	this.link = link;
	this.type = type;
}


function Notice(noticeNo,date,remove,caption,text,signature,reminder,message,link,type) {
	this.noticeNo = noticeNo;
	this.date = stringToDate(date);
	this.remove = stringToDate(remove);
	this.caption = caption;
	this.text = text;
	this.signature = signature;
	this.reminder = stringToDate(reminder);
	this.message = message;
	this.link = link;
	this.type = type;
}

function Newsitem(id,date,category,caption,message,link,type) {
	this.newsitemNo = id
	this.date = stringToDate(date);
	this.category = category;
	this.caption = caption;
	this.message = message;
	this.link = link;
	this.type = type;
}

function Webpage(date, caption, description, newFlag, link, type) {
	this.date = stringToDate(date);
	this.caption = caption;
	this.description = description;
	this.newFlag = newFlag;
	this.link = link;
	this.type = type;
}

function Weblink(date, caption, description, category, link, type) {
	this.date = stringToDate(date);
	this.caption = caption;
	this.description = description;
	this.category = category;
	this.link = link;
	this.type = type;
}

function SponsorOrder(startdate, company, text, link, logo, smlogo, webpage, position, ordertype) {
	this.date = stringToDate(startdate);
	this.company = company;
	this.text = text;
	this.link = link;
	this.logo = logo;
	this.smlogo = smlogo
	this.webpage = webpage;
	this.position = position;
}

function selectToday(date) {
//	 välj ut poster med angivet datum ur listan. returneras i en array.
	sel = new Array()
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() == date.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}

function selectReminders(date) {
//	 välj ut poster med angivet datum ur listan. returneras i en array.
	sel = new Array();
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.reminder != null) {
                	if (tmp.reminder.getTime() > tmp.date.getTime()) {
				if (date.getTime() == tmp.reminder.getTime()) {
					sel[j] = tmp;
					j = j + 1;
				}
			}
		}
	}
	return sel;
}

function selectLatestNotices(date) {
//	 välj ut poster som anslagits senaste veckan. returneras i en array.
	sel = new Array();
	startdate = new Date(); 
	startdate.setTime(date.getTime() - 13*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() <= date.getTime() && tmp.remove.getTime() >= date.getTime()) {
			if (tmp.date.getTime() >= startdate.getTime()) {
				sel[j] = tmp;
				j = j + 1;
			}
		}
	}
	return sel;
}

function selectLastTwoWeeks(date) {
//	 välj ut nyheter från senaste två veckorna. returneras i en array.
	sel = new Array();
	startdate = new Date(); 
	startdate.setTime(date.getTime() - 13*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}

function selectLastSevenDays(date) {
//	 välj ut nyheter från senaste veckan. returneras i en array.
	sel = new Array();
	startdate = new Date(); 
	startdate.setTime(date.getTime() - 6*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}

function selectThisWeek(date) {
//	 select records for this week
	sel = new Array();
	weekday = (date.getDay() == 0) ? 6 : date.getDay() - 1;
	startdate = new Date();
	startdate.setTime(date.getTime() - weekday*24*60*60*1000);
	enddate = new Date();
	enddate.setTime(startdate.getTime() + 6*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime() && tmp.date.getTime() <= enddate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}

function selectWorkDaysThisWeek(date) {
//	 select records for this week
	sel = new Array();
	weekday = (date.getDay() == 0) ? 6 : date.getDay() - 1;
	startdate = new Date();
	startdate.setTime(date.getTime() - weekday*24*60*60*1000);
	enddate = new Date();
	enddate.setTime(startdate.getTime() + 4*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime() && tmp.date.getTime() <= enddate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}


function selectNextWeek(date) {
//	 select records for this week
	sel = new Array();
	weekday = (date.getDay() == 0) ? 6 : date.getDay() - 1;
	startdate = new Date();
	startdate.setTime(date.getTime() + (7-weekday)*24*60*60*1000);
	enddate = new Date();
	enddate.setTime(startdate.getTime() + 6*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime() && tmp.date.getTime() <= enddate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}

function selectWorkdaysNextWeek(date) {
//	 select records for this week
	sel = new Array();
	weekday = (date.getDay() == 0) ? 6 : date.getDay() - 1;
	startdate = new Date();
	startdate.setTime(date.getTime() + (7-weekday)*24*60*60*1000);
	enddate = new Date();
	enddate.setTime(startdate.getTime() + 4*24*60*60*1000);
	j = 0;
	for (i=0; i<this.list.length; i++) {
		tmp = this.list[i];
		if (tmp.date.getTime() >= startdate.getTime() && tmp.date.getTime() <= enddate.getTime()) {
			sel[j] = tmp;
			j = j + 1;
		}
	}
	return sel;
}


function buildSubmenu(selection) {
	submenu = new List(false, swidth, sheight, bgColor);
	medlemmar = false;
	for (i = 0; i < selection.length; i++) {
		tmp = selection[i]
		if (tmp.type == 'a' | tmp.type == 'A') {
			s = submenuItemStart + submenuAStart + tmp.link + submenuAEnd + tmp.caption + '</A>' + submenuItemEnd;
			submenu.addItem(s);
		} else if (tmp.type == 'm' | tmp.type == 'M') {
			medlemmar = true;
		}
	}
	if (medlemmar) {
		s = submenuItemStart + submenuAStart + "medlemmar.htm" + submenuAEnd + "se även medlemssektionen" + '</A>' + submenuItemEnd;
		submenu.addItem(s);
	}
	return submenu;
}

function buildReminderSubmenu(selection) {
	submenu = new List(false, swidth, sheight, bgColor);
	for (i = 0; i < selection.length; i++) {
		tmp = selection[i]
		s = submenuItemStart + submenuAStart + "anslagstavlan.htm" + submenuAEnd + tmp.message + '</A>' + submenuItemEnd; 
		submenu.addItem(s);
	}
	return submenu;
}

function buildExternalSubmenu(selection) {
	submenu = new List(false, swidth, sheight, bgColor);
	for (i = 0; i < selection.length; i++) {
		tmp = selection[i]
		if (tmp.type == 'e' | tmp.type == 'E') {
			s = submenuItemStart + submenuAStart + tmp.link + submenuAEnd + tmp.caption + '</A>' + submenuItemEnd;
			submenu.addItem(s);
		}
	}
	return submenu;
}

function Dayplan(list) {
	this.list = list;
	Dayplan.prototype.selectToday = selectToday;
	Dayplan.prototype.selectThisWeek = selectThisWeek;
	Dayplan.prototype.selectNextWeek = selectNextWeek;
	Dayplan.prototype.buildSubmenu = buildSubmenu;
	Dayplan.prototype.buildExternalSubmenu = buildExternalSubmenu;
}

function LunchMenu(list) {
	this.list = list;
	LunchMenu.prototype.selectToday = selectToday;
	LunchMenu.prototype.selectWorkdaysThisWeek = selectWorkDaysThisWeek;
	LunchMenu.prototype.buildSubmenu = buildSubmenu;
}

function Pinboard(list) {
	this.list = list;
	Pinboard.prototype.selectReminders = selectReminders;
	Pinboard.prototype.selectLatestNotices = selectLatestNotices;
	Pinboard.prototype.buildSubmenu = buildSubmenu;
	Pinboard.prototype.buildReminderSubmenu = buildReminderSubmenu;
}

function News(list) {
	this.list = list;
	News.prototype.selectLastSevenDays = selectLastSevenDays;
	News.prototype.buildSubmenu = buildSubmenu;
}

function Webpages(list) {
	this.list = list;
	Webpages.prototype.selectLastSevenDays = selectLastSevenDays;
	Webpages.prototype.buildSubmenu = buildSubmenu;
}

function Weblinks(list) {
	this.list = list;
	Weblinks.prototype.selectLastSevenDays = selectLastSevenDays;
	Weblinks.prototype.buildSubmenu = buildSubmenu;
}

function SponsorOrders(list) {
	this.list = list;
	SponsorOrders.prototype.selectToday = selectToday;
}

function popWin(url, title, width, height) {
	features = 'width=' + width + ',' + "height=" + height;
	window.open(url, title, features);
}

function player(plugin, url) {
	// temporary fix for opera
// requires plugins.js that detects the following:
//Flash
//Windows Media Player
//Java
//Director
//RealPlayer
//QuickTime
//Acrobat Reader
//SVG Viewer
	if (pluginlist.indexOf(plugin)!=-1) {
		window.location = url;
	} else {
		downloadPlugIn('RealPlayer');
	}
}
function playerPopWin(plugin, url,title,width,height) {
// requires plugins.js that detects the following plugins:
//Flash
//Windows Media Player
//Java
//Director
//RealPlayer
//QuickTime
//Acrobat Reader
//SVG Viewer
	if (pluginlist.indexOf(plugin)!=-1) {
		popWin(url, title, width, height);
	} else {
		downloadPlugIn('RealPlayer');
	}
}
function downloadPlugIn(plugin) {
	switch (plugin.toLowerCase()) {
		case  'realplayer' :
			window.popWin('downloadRealPlayer.htm','',400,200);
			break;
		case 'quicktime' :
			window.popWin('downloadQuickTime.htm','',400,200);
			break;
		default :
			alert('downloadPlugIn - can not identify the specified plugin');
			break;
	}
}
