var currentPage = 0;

document.observe("dom:loaded", function() {
	//pageTracker._trackPageview('aloitus');
	new Effect.Appear($('content_left'), { queue: 'end', duration: 0.5 });

	// Set y-positions
	var root = $$('#nav>li')[0].cumulativeOffset().top;
	$$('#nav ul').each(function(i) {
		var pos = root - i.cumulativeOffset().top;
		i.setStyle({ marginTop: pos + 'px'});
		// Element.clonePosition(i, $('nav'), 'setTop');
	});
	
	updateNavi(false);
});

function toggler(obj, onoff) {
	var el = document.getElementById(obj);
	if (onoff == 'on') {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
}


var changeTimeout = null;
function changePage(id) {
	currentPage = id;
	updateNavi($('page_' + id));
	slideout('top_middle');
	clearTimeout(changeTimeout);
	changeTimeout = setTimeout('changePageRequest("'+id+'")',600); // wait slideout
}

function nextPage() {
	if (currentPage) {
		for (var i = 0; i < ROOT_PAGES.length; i++) {
			if (ROOT_PAGES[i] == currentPage) {
				if (i + 1 >= ROOT_PAGES.length) {
					var index = 0;
				} else {
					var index = i + 1;
				}
				changePage(ROOT_PAGES[index]);
				return;
			}
		}
	} else {
		changePage(ROOT_PAGES[0]);
	}
}

function prevPage() {
	if (currentPage) {
		for (var i = 0; i < ROOT_PAGES.length; i++) {
			if (ROOT_PAGES[i] == currentPage) {
				if (i - 1 < 0) {
					var index = ROOT_PAGES.length - 1;
				} else {
					var index = i - 1;
				}
				changePage(ROOT_PAGES[index]);
				return;
			}
		}
	} else {
		changePage(ROOT_PAGES[ROOT_PAGES.length - 1]);
	}
}

function changePageRequest (id) {
	var url = '?page_id='+id;
	new Ajax.Request(url, {
  	method: 'get',
  	onSuccess: function(transport) {
			$('top_middle').update(transport.responseText);
			slidein('top_middle');
		}
	});
}

function slideout (div) {
	//new Effect.SlideLeftOut(div, { queue: 'end', duration: 0.5 });
	new Effect.Fade(div, { queue: 'end', duration: 0.5 });
/*  Effect.Appear, Effect.Fade
    * Effect.Puff
    * Effect.DropOut
    * Effect.Shake
    * Effect.SwitchOff
    * Effect.BlindDown, Effect.BlindUp
    * Effect.SlideDown, Effect.SlideUp
    * Effect.Pulsate
    * Effect.Squish
    * Effect.Fold
    * Effect.Grow
    * Effect.Shrink
*/
}

function slidein (div) {
	new Effect.Appear(div, { queue: 'end', duration: 0.5 });
}

function popupWindow(content) {
	$('PopupContent').update(content);
	$('Popup').style.display = 'block';
}

function ajaxPopupWindow(url) {
	new Ajax.Request(url, {
  		method: 'get',
  		onSuccess: function(transport) { popupWindow(transport.responseText) }
	});
}

function updateNavi(elem) {
	if (!currentPage || ROOT_PAGES.find(function(i) { return i == currentPage })) {
		$('navleft').show();
		$('navright').show();
	} else {
		$('navleft').hide();
		$('navright').hide();
	}

	// Clear all styles
	$$('#nav a').each(function(i) {
		i.className = '';
	});
	
	// Get root level elements
	if (elem) {
		visible = new Array();
		
		// Get element ancestors & ancestor siblings
		var ancestors = elem.ancestors().grep(new Selector('li'));
		visible = visible.concat(ancestors);
		ancestors.each(function(i) {
			visible = visible.concat(i.siblings());
			i.down('a').className = 'selected';
		});
		
		// Get element children
		var child = elem.up(0).down('ul');
		if (child) {
			visible = visible.concat(child.childElements());
		}
		
	} else {
		var visible = $$('#nav>li');
	}
	
	var all = $$('#nav li');
	$$('#nav li').each(function(i) {
		var found = false;
		visible.each(function(j) {
			if (i == j) {
				found = true;
				$break;
			}
		});
		if (found) {
			i.show();
		} else {
			i.hide();
		}
	});
}
