var resp = '';
function showContent()
	{
	document.getElementById('content').innerHTML = resp;
	chgLinks();
	initSlimBox();
	setTimeout(function() {fade('content',100);}, 100);
	}

function fade(id, dest,after)
	{
	var e = document.getElementById(id);
	if(e.opa == undefined)
		e.opa = 100;
	if(e.opa != dest && !navigator.userAgent.match(/\bMSIE [0-8]\b/))
		{
		if(!dest)
			e.opa -= 10;
		else
			e.opa += 10;
		e.style.opacity = ''+(e.opa/100)+'';
		setTimeout(function() {fade(id,dest,after)}, 30);
		}
	else
		{
		if(after != null)
			after();
		}
	}

function chkHash()
	{
	if(window.location.hash != window.location.prevHash)
		{
		var pars = parseArgs(window.location.hash)
		if(pars[0] == 'fotografia' || pars[0] == 'grafika')
			chgMain(pars[0]);
		fetch(pars);
		window.location.prevHash = window.location.hash;
		}
	setTimeout(chkHash,100);
	}
	
function initFetch()
	{
	window.location.prevHash = '';
	setTimeout(chkHash,0);
	chgLinks();
	}

function parseArgs(parStr)
	{
	var end;
	var pars = [];
	var strEnd = 0;
	while(!strEnd)
		{
		if((end = parStr.indexOf('&')) <= 0)
			{
			strEnd = 1;
			end = parStr.length;
			}
		pars[pars.length] = parStr.substring(parStr.indexOf('=')+1,end);
		parStr = parStr.substr(end);
		}
	return pars;
	}
function chgMain(el)
	{
	el = document.getElementById(el);
  el.className = 'currMain';
	if(el.id == 'grafika')
		document.getElementById('fotografia').className = '';
	else
		document.getElementById('grafika').className = '';
	}
function chgLinks()
	{
	var m = document.getElementById('site');
	if(m)
		{
		var l = m.getElementsByTagName('a');
		var d = l.length;
		var i;
		for(i = 0; i != d; ++i)
			if(l[i].d == undefined && l[i].rel != 'lightbox[]' && l[i].href.indexOf('mailto:') != 0)
				{
				if(l[i].href.indexOf('?') == -1)
					l[i].href += '#';
				l[i].d = parseArgs(l[i].href);
				l[i].href = l[i].href.replace('?','#');
				}
		}
	}

function fetch(pars)
	{
	var getStr = '';
	if(pars.length != undefined)
		this.d = pars;
	if(this.d)
		{
		var i,d = this.d.length;
		getStr = '?';
		for(i = 0; i != d; ++i)
			{
			getStr+='d'+i+'='+this.d[i];
			if(i != d-1)
				getStr+='&';
			}
		}
	var httpReq;
	if(window.XMLHttpRequest)
		httpReq = new XMLHttpRequest();
	else if(window.ActiveXObject)
		httpReq = new ActiveXObject('Microsoft.XMLHTTP');
	httpReq.onreadystatechange = function()
		{
		if(httpReq.readyState == 4) //loading complete
			{
			if(httpReq.status == 200) //ok
				{
				resp = httpReq.responseText;
				fade('content',0,showContent);
				}
			}
		};
	httpReq.open('GET','fetchContent.php'+getStr,true);
	httpReq.send(null);
	}
	
addToLaunch(initFetch);
