/*
	main.js
*/


window.menu = new Object();
var cur_img = new Object();
var num_img = new Object();
var img_list = new Object();
window.TextTable = new Object();

var pageLoadFinished = 0;
var exec_checkBrowserSize_part2 = 0;

//------------------------------------------------------------------------------------------------------

var date_popup = new CalendarPopup("date_picker");
date_popup.setMonthNames("Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");
date_popup.setMonthAbbreviations("Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep","Okt","Nov","Dec");
date_popup.setDayHeaders("S","M","T","O","T","F","L");
date_popup.setWeekStartDay(1); // week is Monday - Sunday
date_popup.setTodayText("Idag");
date_popup.offsetX = -80;
date_popup.offsetY = 24;
date_popup.selectDate = function(element, anchor, on_select)
{
	date_popup_return_func = function(y, m, d) {element.value = y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d); on_select();};
	this.setReturnFunction("date_popup_return_func");
	this.showCalendar(anchor, element.value);
}

//------------------------------------------------------------------------------------------------------

function onLoad()
{
	if (exec_checkBrowserSize_part2) checkBrowserSize_part2();
	
	onResize();
	
	pageLoadFinished = 1;
	onPageLoadFinished();
}

function onPageLoadFinished()
{
	Run.run();
}

function onResize()
{
	BrowserSize = getBrowserSize();
	DocumentSize = getElementSize('OuterTable');
	calculateClientArea();
}

function checkBrowserSize()
{
	var bs = getBrowserSize();
	
	if (typeof(ServersBrowserWidth) == "undefined" || ServersBrowserWidth != bs.w)
	{
		exec_checkBrowserSize_part2 = 1;
		document.write('<div id="div" style="height: 110%;">T</div></body></html>');
		document.close();
	}
}

function checkBrowserSize_part2()
{
	var bs = getBrowserSize();
	var ds = getElementSize('div');
	
	replacePage("main.php?setBrowserSize=1&w=" + bs.w + "&doc_w=" + ds.w);
}

//------------------------------------------------------------------------------------------------------

function slideShow_change(article_id, img_num)
{
	cur_img[article_id] = img_num;
	document.getElementById('slideshow_img_' + article_id).src = 'files/' + img_list[article_id][img_num];
	document.getElementById('slideshow_cur_img_' + article_id).innerHTML = (img_num + 1);
	document.location.href = '#Article_' + article_id;
}

function slideShow_prev(article_id)
{
	if (cur_img[article_id] > 0) cur_img[article_id]--;
	
	slideShow_change(article_id, cur_img[article_id]);
}

function slideShow_next(article_id)
{
	if (cur_img[article_id] < num_img[article_id] - 1) cur_img[article_id]++;
	
	slideShow_change(article_id, cur_img[article_id]);
}

function showNode(id)
{
	document.getElementById(id).style.background='#ffcc99';
}

function hideNode(id)
{
	document.getElementById(id).style.background='#ffffff';
}

function menuToggle(id)
{
	if (window.menu[id] && window.menu[id]['hasChildren'])
	{
		window.menu[id]['open'] = 1 - window.menu[id]['open'];
		
		if (window.menu[id]['open'])
		{
			document.getElementById("menu_icon_" + id).src = "images/menu_close.gif";
			document.getElementById("menu_" + id).style.display = "block";
		}
		else
		{
			document.getElementById("menu_icon_" + id).src = "images/menu_open.gif";
			document.getElementById("menu_" + id).style.display = "none";
		}
	}
}

function menuOpen(id)
{
	if (window.menu[id] && window.menu[id]['hasChildren'])
	{
		window.menu[id]['open'] = 1;
		
		if (window.menu[id]['open'])
		{
			document.getElementById("menu_icon_" + id).src = "images/menu_close.gif";
			document.getElementById("menu_" + id).style.display = "block";
		}
	}
}

function menuSubmit(menuId, articleId)
{
	var e = document.forms.form_menu.elements;
	
	e['form_menu[menuId]'].value = menuId;
	e['form_menu[articleId]'].value = articleId;
	
	// Get and send back current status of the menu tree
	// ONLY report the opened nodes
	
	var s = "";
	for (var i in window.menu)
	{
		if (window.menu[i]['open'])
		{
			s += i + "_";
		}
	}
	e['form_menu[openNodes]'].value = s + "0";
	
	document.forms.form_menu.submit();
}

function print_page()
{
	var ccw = 528;
	if (CurrentContentWidth) ccw = CurrentContentWidth;
	var winW = ccw + 60;
	var a;
	a  = '<html><head>';
	a += '<link rel="stylesheet" href="' + MainFullCssUrl + '" type="text/css">';
	a += '<style>body{padding: 20px;} @media print { body{padding: 0;} }</style>';
	a += '</head>';
	a += '<body style="background: #ffffff;">';
	a += '<div style="width: ' + ccw + 'px; background: #ffffff;">';
	a += document.getElementById('infopart').innerHTML;
	a += '</div></body></html>';
	newwin = window.open("", "printerwin", "height=600, width=" + winW + ", scrollbars, menubar, toolbar, resizable")
	newwin.document.write(a);
	newwin.document.close();
}

function menutreePopupSwitch()
{
	var e = document.getElementById('menutreePopup');
	if (e)
	{
		if (e.style.display == 'none' || e.style.display == '')
		{
			e.style.display = 'block';
		}
		else
		{
			e.style.display = 'none';
		}
	}
}

function YMD2DM(date)
{
	var d = date.substr(8, 2);
	var m = date.substr(5, 2);
	if (d.substr(0, 1) == '0') d = d.substr(1, 1);
	if (m.substr(0, 1) == '0') m = m.substr(1, 1);
	
	return d + '/' + m;
}