// defining Page.HOST, Page.GFX based on host page's inclusion path
Page.HOST = false;
var scripts = document.getElementsByTagName('SCRIPT');
for(var i = 0; i < scripts.length; i++){
	if(scripts[i].src.indexOf('/global/js/page.js') > -1){
		Page.HOST = scripts[i].src.substring(0, scripts[i].src.indexOf('/global/js/page.js'));
	}
}
if(!Page.HOST){alert("Fejl i inkluderingen af page.js")};

Page.JS    = Page.HOST + '/global/js/';
Page.ICONS = Page.HOST + '/global/gfx/icons/';
Page.GFX   = Page.HOST + '/layout/formel3/gfx/';
Page.CSS   = Page.HOST + '/layout/formel3/css/';

// check for menu extension
if(typeof(menuextension) != "undefined"){
	document.write('<script type="text/javascript" src="'+Page.JS+'menu_extender.js"></script>');
}

// include favorite icons
document.write('<link rel="icon" href="'+SystemFaviconUrl+'" type="image/x-icon" >');
document.write('<link rel="shortcut icon" href="'+SystemFaviconUrl+'" type="image/x-icon">');

// We might want to consider including these within the page object

// include stylesheets
if(parent.window.frames.length == 0 || window.name == "mainframe"){ // force css correction
	document.write('<link rel="stylesheet" href="'+Page.CSS+'print.css" media="print" type="text/css">');
	document.write('<style type="text/css">@import url('+Page.CSS+'basics.css);</style>');
	document.write('<style type="text/css">@import url('+Page.CSS+'menu.css);</style>');
	Page.NO_FORCE = true;
}

// include additional scripts
document.write('<script type="text/javascript" src="'+Page.JS+'util.js"></script>');				// include utilities. Essential!
document.write('<script type="text/javascript" src="'+Page.JS+'menu.js"></script>');				// containing top menu functionality


// page layout types (see documentation for further specifications)
Page.FRONTPAGE = 1;
Page.SUB_FRONTPAGE = 8;

Page.ARTICLE = 2;

Page.PLATFORM = 5;
Page.PLATFORM_FRONTPAGE = 6;

Page.SYSTEM = 3;
Page.SYSTEM_LOGIN = 9;
Page.POPUP = 4;

Page.FORCE_CSS = 7;


// bodytypes
var FULLBODY = 0;				// content is full width
var STANDARD = 1;				// content is 4/5 wide, with menu on the left
var NO_LEFT = 2;				// content is 4/5 wide, with right column, no left menu
var NO_RIGHT = 3;				// content is 4/5 wide, with menu on the left, no right column
var PLATFORM = 4;				// content is 4/5 wide, with special platform menu on the left
var SYSTEM = 5;				// content is full width
var ARTICLE = 6;				// content is 3/5 wide, with menu on the left and right column for external content
var SYSTEM_LOGIN = 7;		// content is full width, header is full width


Page.Vars = new Object();
// page identification handlers
Page.Vars.trail = new Array();		// containing trail names
Page.Vars.trailURL = new Array();		// containing trail url's
Page.Vars.trailID = new Array();		// containing trail dogtags
Page.Vars.trailSuccess = false;		// is the page identified


function Page() {
	var _username = "";					// page username (otherwise set in menu.aspx)
	var _title = "";						// page title (otherwise set by chosen menu item)
	var _browsertitle = false;	 	// browser title (otherwise set by title)
	var _dogtag = ""; 					// page identification
	var _status = false;					// status information
	var _help = false;					// related help page

	var _layout = false;					// setLayout required
	var _colorScheme = false;


	var _tillid_logo = false;			// flag used to check which logo

	// body layout types
	var _bodytype = -1;


	this.setBodyType = function(type){_bodytype = type;}				// set bodytype
	this.getBodyType = function(){										// get bodytype, STARDARD if not set
		if(_bodytype == -1){
			_bodytype = STANDARD;
		}
		return _bodytype;
	}
	this.setUsername = function(string){_username = string;}			// set username in hostpage, optional
	this.setTitle = function(str){_title = str;};						// set title in hostpage, optional
	this.setHelp = function(str){_help = str;};							// set help id for generel page help, optional
	this.setStatus = function(str){_status = (_status ? _status + str : str) + "\n";};						// set status for printout in buttom aligned statusbar, optional
	this.setDogtag = function(str){_dogtag = str;};						// set dogtag, NOT optional, identifies this page to secure writeout of correct menu
	this.setLayout = function(i, ext){									// set page layout, default is set to ARTICLE
		_layout = i;
		// define body type
		ext = (ext == undefined) ? STANDARD : ext;						// decide body type based on layout or extention parameter
		
		// only include page.css for NON-SYSTEM pages (modify, when system pages are converted)
		document.write('<style type="text/css">@import url('+Page.CSS+'page.css);</style>');
		
		if(this.getLayout() == Page.PLATFORM){
			document.write('<script type="text/javascript" src="'+Page.JS+'menu_icons.js"></script>');
			document.write('<style type="text/css">@import url('+Page.CSS+'moederum.css);</style>');
			this.setBodyType((ext == STANDARD ? PLATFORM : ext));
		
		}else if(this.getLayout() == Page.PLATFORM_FRONTPAGE){
			document.write('<script type="text/javascript" src="'+Page.JS+'menu_icons.js"></script>');
			document.write('<style type="text/css">@import url('+Page.CSS+'moederum.css);</style>');
			this.setBodyType(FULLBODY);
		
		}else{
			this.setBodyType(ext);
		}
		
		if(_colorScheme){
			document.write('<style type="text/css">@import url('+Page.CSS+_colorScheme+'.css);</style>');
		}
	}
	// set colorScheme
	this.setColorScheme = function(scheme){
		// if called after setLayout, include css directly
		if(_layout){
			document.write('<style type="text/css">@import url('+Page.CSS+scheme+'.css);</style>');
		}
		_colorScheme = scheme;
	}

	this.setBrowserTitle = function(str){_browsertitle = str;};			// set browser title, called from header function

	this.showWait = function(){// show wait layer, and hide pageWrap
/*		if(document.getElementById('waitWrap')){
			document.getElementById('pageWrap').style.display = "none";
			document.getElementById('waitWrap').style.display = "block";
		}*/
	}

	this.getUsername = function(){										// getting username
		if(_username != ""){													// if set by hostpage
			return _username;
		}else{
			try{
				if(menu_username != undefined){							// get username from menu.aspx
					return menu_username;
				}else{
					return "";
				}
			}catch(e){
				return "";
			}
		}
	}
	this.getRealname = function(){										// getting real name
		if(typeof(menu_realname) != "undefined" && menu_realname != ""){
			return menu_realname;
		}else{
			return this.getUsername();
		}
	}
	this.getTitle = function(){											// getting page title
		if(_title != ''){												// if set by hostpage
			return _title;
		}else if(Page.Vars.trailSuccess){											// else if found through dogtag
			return Page.Vars.trail[Page.Vars.trail.length-1];
		}else{
			return "";
		}
	}
	this.getTitleUrl = function(){											// getting page title
		if(Page.Vars.trailSuccess){											// else if found through dogtag
			return Page.Vars.trailURL[Page.Vars.trail.length-1];
		}else{
			return false;
		}
	}
	this.getMenuTitle = function(){										// getting page title
		if(Page.Vars.trailSuccess && Page.Vars.trail.length > 1){							// if page is identified
			if(Page.Vars.trailID[1] == "p-g"){									// if item is within "Mine Grupper", skip a level and return level 3
				return Page.Vars.trail[2];
			}else{														// otherwise return level 2
				return Page.Vars.trail[1];
			}
		}else{
			return "";
		}
	}
	this.getMenuTitleUrl = function(){										// getting page title
		if(Page.Vars.trailSuccess && Page.Vars.trail.length > 1){							// if page is identified
			if(Page.Vars.trailID[1] == "p-g"){									// if item is within "Mine Grupper", skip a level and return level 3
				return Page.Vars.trailURL[2];
			}else{														// otherwise return level 2
				return Page.Vars.trailURL[1];
			}
		}else{
			return false;
		}
	}
	this.getStatus = function(){return _status;};	// get page status value, or false if no value is set
	this.getDogtag = function(){return _dogtag;};	// returns dogtag
	this.getLayout = function(){return _layout;};	// returns layout
	this.getHelp = function(){return _help;};		// returns help id
	this.getBrowserTitle = function(){				// return browsertitle if set, or page title
		return (_browsertitle) ? _browsertitle : this.getTitle();
	}

	// identifying the current page in menucontent based on dogtag
	this.identifyPage = function(){
		// iterator ofr iterating through all menu threads
		this.iterate = function(content){
			// loop through content
			for(var i = 0; i < content.length; i++){
				// pre add item to trail
				Page.Vars.trail[Page.Vars.trail.length] = content[i].name;
				Page.Vars.trailID[Page.Vars.trailID.length] = content[i].dogtag;
				Page.Vars.trailURL[Page.Vars.trailURL.length] = content[i].url;
				// if page is found, return true
				if(content[i].dogtag && content[i].dogtag != undefined && content[i].dogtag != "" && content[i].dogtag == _dogtag){
					return true;
				// continue traversing menu
				}else if(content[i].childs){
					// start new thread, return true if it leads to identification
					if(this.iterate(content[i].childs)){
						return true;
					// still unidentified, remove pre added entry
					}else{
						Page.Vars.trail = Page.Vars.trail.popLastElement();
						Page.Vars.trailURL = Page.Vars.trailURL.popLastElement();
						Page.Vars.trailID = Page.Vars.trailID.popLastElement();
					}
				// still unidentified, remove pre added entry
				}else{
					Page.Vars.trail = Page.Vars.trail.popLastElement();
					Page.Vars.trailURL = Page.Vars.trailURL.popLastElement();
					Page.Vars.trailID = Page.Vars.trailID.popLastElement();
				}
			}
			// loop ended, page still unidentified
			return false;
		}
		// if menucontent (menu.aspx) exists, try to identify page
		if(typeof(menucontent) != "undefined"){
			if(menucontent.childs){
				Page.Vars.trailSuccess = this.iterate(menucontent.childs);

				// if page is unidentifiable (NOT FRONTPAGE), send error message
				if(!Page.Vars.trailSuccess && page.getLayout() != Page.PLATFORM_FRONTPAGE){
					alert("Siden kan ikke identificeres!\nKontroll\u00e9r sidens dogtag!");
				}
			}
		}else{	// error in menu.aspx
			menucontent = false;
			alert("Fejl i inkluderingen af menu indhold!");
		}
	}

	// creating page wrap div
	this.getPageWrap = function(){
		var html = '<div id="pageWrap">';
		return html;
	}
	this.endPageWrap = function(){
		var html = '</div>';
		return html;
	}

	// creating page wrap div
	this.waitWrap = function(){
		var html = '<div id="waitWrap">Vent mens data hentes ...</div>';
		return html;
	}

	// creating page header div, NOT finished (mn, 20-09-2004)
	this.getHeader = function(){
		var help = (typeof(menu_help) != "undefined" ? (menu_help.text ? (menu_help.url ? menu_help : false) : false) : false);
		if(!help && typeof(menu_helpurl) != "undefined"){
			menu_help = {text:"Hj&aelig;lp",url:menu_helpurl};
			help = {text:"Hj&aelig;lp",url:menu_helpurl};
		}

		var html = '<div id="headerWrap">';
		html += '<table cellspacing="0"><tr>';
		html += '<td><a href="' + ((typeof(menu_rooturl) != "undefined") ? menu_rooturl : '/') + '"><img src="' + SystemLogoUrl + '"></a></td>';


		// search element
		html += '<td id="search">';
		if(Page.userLoggedIn) {
			html += '<button type="button" style="width:auto" onclick="location.href=\'/login.php?logout=on\'">'+lang['navigation_user-logout']+'</button>';
			if(LoggedInUserCanAccessBackend) {
				if(LoggedInUserCanAccessBackendState) {
					html += '<button type="button" style="width:auto" onclick="location.href=\'/?setBackendStatus=off\'">Backend OFF</button>';
				} else {
					html += '<button type="button" style="width:auto" onclick="location.href=\'/?setBackendStatus=on\'">Backend</button>';
				}
			}
		}
		if(SystemAboutUrl) {
			html += '<div><a href="'+SystemAboutUrl+'" target="_BLANK">'+lang['navigation_about']+'</a>';
		} else {
			html += '<div><a href="/misc/about.php">'+lang['navigation_about']+'</a>';
		}
		html += '|<a href="/misc/privacy.php">'+lang['navigation_privacy']+'</a>';
		html += '|<a href="/misc/terms.php">'+lang['navigation_terms']+'</a>';
		if(SystemFeatureHelp) {
			html += '|<a href="/help/">'+lang['navigation_help']+'</a>';
		}

		html += '</div></td>';

		html += '</tr></table>';
		html += '</div>';
		return html;
	}
	
	// creating topmenu
	this.getMenu = function(){
		// initialize menu object
		var html = '';
		html += '<div id="menuWrap"><div id="topmenu">' + Page.Menu.createTop() + '</div>';
		// frontpages only have toplevel menu
		html += '<div id="submenu">';
		// decide which menu to use
		if(Page.userLoggedIn){
			html += Page.Menu.createSubPlatform();
		}else{
			html += Page.Menu.createSubSimple();
		}
		html += '</div>';
		html += '</div>';
		return html;
	}

	// creating trail line
	this.getTrail = function(){
		var html = '<div id="trailWrap"><table><tr>';
		html += '<td><a href="/"><img src="'+Page.ICONS+'icon_home_16.gif" alt="Til forsiden"></a></td>';

		if(Page.Vars.trailSuccess){
			
			// calculate possible average string length
			var cutLength = (140 - (Page.Vars.trail.length*4))/Page.Vars.trail.length;
			
			for(var i = 1; i < Page.Vars.trail.length; i++){
				// if item has a url, include link
				if(Page.Vars.trailURL[i]){
					html += '<td>&nbsp;/&nbsp;<a href="' + Page.Vars.trailURL[i] + '">' + Page.Vars.trail[i].cutString(cutLength).entityify() + '</a></td>';
				}else{
					html += '<td>&nbsp;/&nbsp;' + Page.Vars.trail[i].cutString(cutLength).entityify() + '</td>';
				}
				// adjust average string length
				if(Page.Vars.trail[i].length < cutLength){
					cutLength += (cutLength - Page.Vars.trail[i].length)/(Page.Vars.trail.length-(i+1));
				}
			}
		}
		html += '</tr></table></div>';
		return html;
	}

	// creating left menu
	this.getLeftMenu = function(){
		var html = '';
		// if submenu is selected, create related left menu
		if(Page.Menu._selectedSUB != undefined){
			// create menu with icons
			html += '<div class="chromeBox"><div class="chromeBoxHeader">' + this.getMenuTitle().cutString(22).entityify() + '</div></div>';
			if(this.getLayout() == Page.PLATFORM){
				html += '<div id="leftMenu">' + Page.Menu.createLeftMenuPlatform(menucontent.childs[Page.Menu._selected].childs[Page.Menu._selectedSUB].childs) + '</div><div class="space">&nbsp;</div>';
			// create simple left menu
			}else{
				html += '<div id="leftMenu">' + Page.Menu.createLeftMenu(menucontent.childs[Page.Menu._selected].childs[Page.Menu._selectedSUB].childs) + '</div><div class="space">&nbsp;</div>';
			}
		}
		return html;
	}

	// create the content body
	this.getBody = function(){
		// insert table to adjust column height
		var html = '<table cellspacing="0" id="pageTable"><tr>';
		// insert menuColumn for left menu 
		if(this.getBodyType() == PLATFORM || this.getBodyType() == NO_RIGHT){
			html += '<td id="menuColumn">' + this.getLeftMenu() + '</td>';
		}
		// insert contentColumn
		html += '<td id="contentColumn"><div id="content">';
		html += '<div class="chromeBox"><div class="chromeBoxHeader"><div class="title">' + this.getTitle().cutString(70 - this.getRealname().length).entityify() + '</div><div class="username">' + this.getRealname() + '</div></div></div>';
		return html;
	}
	this.endBody = function(){
		var html = '';
		html += '</div></td>';
		if(this.getBodyType() != NO_RIGHT){
			html += '\n<td id="rightColumn"></td>';
		}
		var curdate = new Date();
		html += '</tr></table><div id="footerWrap">&copy; 2003-' + curdate.getFullYear() + ' <a href="http://peytz.dk" title="Peytz &amp; Co">Peytz &amp; Co</a>.</div>';

		if(typeof(footer_info) != "undefined" && footer_info.text != ""){
			html += '<div id="footerWrap">' + footer_info.text;
			if(footer_info.email && footer_info.email != ""){
				html += ' • Email: <a href="mailto:' + footer_info.email + '">' + footer_info.email + '</a>';
			}
			if(footer_info.url && footer_info.url != ""){
				html += ' • <a href="' + footer_info.url + '">' + footer_info.url + '</a>';
			}
			html += '</div>';
		}
		return html;
	}
	
	// create the status line (only available for SYSTEM)
	this.getStatusLine = function(){
		var html = '<div style="height:20px;">Status: <span style="font-style:italic">' + (this.getStatus() ? this.getStatus() : '&nbsp;') + '</span></div>';
		return html;
	}
	
	
	// creating header based on layout
	this.header = function(){
		if(this.getLayout() != Page.POPUP){this.identifyPage();}	// identify page, unless it is a popup
		document.title = this.getBrowserTitle();					// set browser title
		
		// initiate html creation based on layout types
		var html = '';
		html += this.getPageWrap();
		html += this.getHeader();
		html += this.getMenu();
		html += this.getTrail();
		html += this.getBody();
		document.write(html);
	}

	// creating footer
	this.footer = function(){
		
		// create footer html
		var html = '';
		if(this.getLayout() == Page.PLATFORM){
			html += this.endBody();
			html += this.endPageWrap();
			html += this.waitWrap();
		}else if(this.getLayout() == Page.PLATFORM_FRONTPAGE){
			html += this.endBody();
			html += this.endPageWrap();
			html += this.waitWrap();
		}else{
			return;
		}
		document.write(html);
		// global initiation of button and input effects
		Page.OL.addOnloadAction('awakeInterface()')

		document.write('<script type="text/javascript" src="'+Page.JS+'util_stats.js"></script>');

		// check for existance of Page.RightColumn object
		if(typeof(Page.RightColumn) != 'undefined'){

			// insert content if rightcontent elements exist
			if(document.getElementById("rightColumn")){
				document.getElementById("rightColumn").innerHTML = Page.RightColumn;
			}
		}

	}
}
var page = new Page();
