function resize()
{
	var bh = document.viewport.getHeight();
	bh = parseInt(bh) - 145 - 40 - 5;

	if( bh > 30 )
	{
		$('text').style.height = bh - 30 + 'px';
		$('meniu').style.height = bh - 7 + 'px';

		var th = $('text').getHeight();
		var mh = $('meniu').getHeight();

		if( th > mh )
		{
			$('meniu').style.height = th - 7 + 'px';
		}
		else
		{
			$('text').style.height = mh - 30 + 'px';
		}

		var c = $('logo').viewportOffset();
		$('language').style.left = c[0] + 10 + 'px';
	}
}

function animate()
{
	var isIE	= document.all;

	if( !isIE )
	{
		$('text-inner').setOpacity(0);
		Effect.Appear('text-inner', { duration: 1.0 });
	}
}

function changeText( link )
{
	$('meniu').childElements().each(
		function(obj)
		{
			if( obj.readAttribute("href") == link )
			{
				obj.addClassName("h1");
				obj.setOpacity(0);
				Effect.Appear(obj, { duration: 1.0 });
//				dhtmlHistory.add(link, 'a');

				$('text-inner').innerHTML = '<img src="media/loading.gif" alt="..." class="loading" />';

				new Ajax.Request('/ajax.html', { 
					method: 'get',
					parameters: 'link='+link,
					onSuccess: function(transport, json){
						document.title = 'Transventa.lt - ' + json['title'];
						$('text-inner').innerHTML = json['content'];
						animate();
					},
					onFailure: function(error)
					{
						$('text-inner').innerHTML = error;
						animate();
					}
				});
			}
			else
			{
				obj.removeClassName("h1");
				obj.setOpacity(1);
			}
		}
	);
}

function meniu()
{
	$('meniu').childElements().each(
	
	function(obj)
	{
		if(obj.tagName.toUpperCase() == 'H1')
		{
			obj.replace('<a href="'+obj.readAttribute('title')+'" class="h1">'+obj.innerHTML+'</a>');
		}
	}

	);

	$('meniu').childElements().each(
	
	function(obj)
	{
		Event.observe(obj, "click",
			function(e){ 
				var element = Event.element(e);
				//changeText(element.readAttribute('href'));

				//Event.stop(e);
			} 
		);
	}

	);
}

function onLoad()
{
	animate();
	meniu();

	//dhtmlHistory.initialize();
	//dhtmlHistory.addListener(yourListener);

	if( location.hash != '' )
	{
//		changeText(location.hash.substr(1));
	}
}

var yourListener = function(newLocation, historyData) {
	changeText(newLocation);
}

/*
window.dhtmlHistory.create({
	toJSON: function(o) {
		return Object.toJSON(o);
	},
	fromJSON: function(s) {
		return s.evalJSON();
	}
});
*/

document.observe("dom:loaded", resize);
document.observe("dom:loaded", onLoad);
Event.observe(window, "resize", resize);