// JavaScript Document for MONA

IsMSIE   = navigator.appVersion.indexOf('MSIE') != -1;
IsMSIE7  = navigator.appVersion.indexOf('MSIE 7') != -1;
IsMSIE8  = navigator.appVersion.indexOf('MSIE 8') != -1;
IsChrome = navigator.appVersion.indexOf('Chrome') != -1;

// Onload functions for MONA
function onLoad_MONA() {
	
	// populate elements
	document.getElementById('MONAELMNT_portfolio').innerHTML = MONA_PortfolioList;
	
	// Position and show elements
	positionMONAELMNTs();
	showMONAELMNTs();
	
	// Load homepage
	loadPage('home');
	
	/*
	// Set title font size for shIEte
	if (IsMSIE) {
		with (document.getElementById('MONAELMNT_header').style) {
			width      = '365px';
			fontSize   = '35px';
			fontWeight = 'bold';
		}
	}
	*/
}

// Returns X center of browser
function getXcenter() {
	return Math.floor(document.body.clientWidth/2);
}

// Positions elements of page for MONA
function positionMONAELMNTs() {
	xCenter = getXcenter();
	
	with (document) {
		getElementById('MONAELMNT_header').style.marginLeft    = (xCenter - 40) + 'px';
		getElementById('MONAELMNT_body').style.marginLeft      = (xCenter - 80) + 'px';
		getElementById('MONAELMNT_portfolio').style.marginLeft = (xCenter - 455) + 'px';
		getElementById('MONAELMNT_footer').style.marginLeft    = (xCenter - 315) + 'px';
	}
}

// Show MONA page elements (initially hidden so that they aren't randomly all over the screen before they are positioned)
function showMONAELMNTs() {

	// loop through applicable elements to set display
	AllDivs = document.getElementsByTagName('div');
	for (var i in AllDivs) {
		if (typeof(AllDivs[i].id) == undefined || AllDivs[i].id == 'undefined' || AllDivs[i].id == undefined || AllDivs[i].id == '')
			continue;
		
		if (AllDivs[i].id.indexOf('MONAELMNT') != -1)
			AllDivs[i].style.visibility = 'visible';
	}
}

// Loads a page
function loadPage(page, ext) {

	if (!ext)
		ext = 'html';
	
	// Indicate loading...
	document.getElementById('bodycontent').innerHTML = '<div id="loading">[ loading ]</div>';
	
	// Load the page via AJAX
	AJAX_sendRequest(true, '/pages/' + page + '.' + ext, "loadPageAction(response, '" + page + "', '" + ext + "');");
}

function loadPageAction(output, page, extra) {
	PortfolioPage = (extra)?extra.substr(extra.lastIndexOf('=')+1):page;
	
	// Set output
	document.getElementById('bodycontent').innerHTML = convertSpecialCharsForAJAX(output);
	
	// reset all nav elements
	allAnchors = document.getElementsByTagName('a');
	for (var i in allAnchors) {
	
		// just ignore some obscure error I can't pinpoint
		try {
			
			// only change portfolio pages
// 			if (allAnchors[i].innerHTML.indexOf('portfolio') == -1)
// 				continue;
		
			// bottom nav (doesn't change) or otherwise irrelevant
			if (allAnchors[i] == 'undefined' || typeof(allAnchors[i].className) == 'undefined' || typeof(allAnchors[i].innerHTML) == 'undefined' || allAnchors[i].className == 'navbot')
				continue;
			
			// THIS page
			TestPage = allAnchors[i].innerHTML.replace(/ /g, '_').toLowerCase();
			if (TestPage == page || TestPage == PortfolioPage.replace(/^portfolio_/, ''))
				allAnchors[i].className = allAnchors[i].className.replace(/off$/i, 'on');
			else
				allAnchors[i].className = allAnchors[i].className.replace(/on$/i, 'off');
		}
	
		catch (e) {
			// screw it
		}
	}
	
	// Loading portfolio page, preload the images
	if (page == 'portfolio') {
		
		// yo dawg, IE's too stupid to handle ajax within ajax
		if (IsMSIE)
			setTimeout('preloadPortfolioImgs(PortfolioPage);', 2000);
		else
			preloadPortfolioImgs(PortfolioPage);
	}
}
