﻿window.onresize = resize;
var curPage;
var height = 0;


function windowHeight() {
	var ie = /*@cc_on!@*/false;
	return (ie ? document.documentElement.clientHeight
					: window.innerHeight);
}
function resize()
{
	try {
		height = windowHeight() - 271; //246;

		var breadcrumbs = document.getElementById('breadcrumbs').innerHTML.trim();
		if (!breadcrumbs)
			height += 12;
		
		document.getElementById('content').style.minHeight = height + 'px';
	}
	catch (e) { }
}


function loadContent(page) {
	try
	{
		page = page.toLowerCase();
		if (page == curPage)
			return false;
		curPage = page;
		
		var req = (window.XMLHttpRequest) ? new XMLHttpRequest() // IE7+, FF, Chrome, Opera, Safari
				: ((window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') // IE6, IE5
				: null);//no XMLHttp support
				
		if (!req) return true;
		window.location.hash = curPage;


		req.onreadystatechange = function()
		{
			if (req.readyState == 4 && req.status == 200)
			{
				if (req.responseText && req.responseText != '')
				{
					var respText = req.responseText;
					var breadcrumbs = respText.substring(0, respText.indexOf('@>'));
					respText = respText.substring(respText.indexOf('@>') + 2);
					document.getElementById('content').innerHTML = respText;
					document.getElementById('breadcrumbs').innerHTML = breadcrumbs;
					if (!breadcrumbs.trim())
						resize();
				}
			}
		}
		req.open('GET', 'FileHandler.aspx?page=' + page, true);
		req.send(null);
		return false;
	}
	catch (e) {return true;}
}

/**/
function checkHash() {
	var hash = window.location.hash.substr(1).trim();
	if (hash === '') hash = curPage;
	if (hash != curPage)
		loadContent(hash);
}

function load(p) {
	if (!window.location.hash)
		window.location.hash = curPage = p;
	resize();
	setInterval('checkHash()', 500);
}



String.prototype.trim = function()
{
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function validate(a)
{
	if (!curPage)
		return;
	
	var uri = ((a.id == 'cssA') ? 'http://jigsaw.w3.org/css-validator/validator?uri='
								: 'http://validator.w3.org/check?uri=') + 'www.xchorses.se/?page=' + curPage;
	a.target = '_blank';
	a.href = uri;
}




var posX;
var posY;

function showBigImage(sender) {
	var bigImg = document.getElementById('bigImg');
	var src = sender.src;
	src = src.substr(src.indexOf('#') + 1);
	bigImg.src = src;
	bigImg.style.display = 'block';
}

function hideBigImage() {
	var bigImg = document.getElementById('bigImg');
	bigImg.src = '';
	bigImg.style.display = 'none';
}

function moveBigImage() {
	var bigImg = document.getElementById('bigImg');
	var windHeight = windowHeight();
	var y = 0;
	if (windHeight > bigImg.height)
		y = (posY + bigImg.height > windHeight) ? windHeight - bigImg.height : posY;
	bigImg.style.left = posX + 'px';
	bigImg.style.top = y + 'px';// (y - document.documentElement.scrollTop) + 'px';
}
function mouseMove(e) {
	posX = (e.pageX || e.x) + 10;
	posY = (e.pageY || e.y) - (document.body.scrollTop || document.documentElement.scrollTop) + 10;
}
