var Ep = {
	currentrequest: '',
	pagecache: '',
	tplsection: 'main',
	currentpos: false,
	currentactive: false,
	pagehidden: false,
	hideDelay: false,
	getCacheContainer: function()
	{
		if (!(c = $(this.pagecache)))
			c = $(document.body.appendChild(new Element('div', {'id': this.pagecache})));
		
		return c;
	},
	getCached: function(page)
	{
		return (this.getCacheContainer().retrieve(page, false));
	},
	getUrl: function()
	{
		var url = location.href.split("#");
		return (url[1] || null);
	},
	divertLinks: function(container)
	{
		$(container).select('a.internal-link').each(function(el) {
			el.observe('click',  Ep.onLinkClick);
		});
	},
	initPage: function()
	{		
		this.divertLinks('content');
		this.divertLinks('menu');
		this.moveToPosition();
		
		new PeriodicalExecuter(this.observeBackButton.bind(this), 2);
	},
	handleRequestPageReply: function(response, cache)
	{
		if (cache != false)
			this.setCached(this.currentrequest, response);
		
		this.setUrl(this.currentrequest);
		
		this.currentactive = this.currentrequest;
		this.currentrequest = '';

		$('content').update(response.content);
		$('menu').update(response.menu);
		
		this.divertLinks('content');
		this.divertLinks('menu');
	},
	moveToPosition: function()
	{
		new Effect.Parallel([
			new Effect.Tween(null, 0, 235, {sync: true}, function(p) {
				$('content-wrap').setStyle({'left': p+'px'});
			}),
			new Effect.Tween(null, 204, 165, {sync: true}, function(p) {
				$('content-wrap').setStyle({'top': p+'px'});
			}),
			new Effect.Appear('content-wrap', {sync:true})
		], {duration: 0.2});

		new Effect.Parallel([
			new Effect.Tween(null, 205, 115, {sync: true}, function(p) {
				$('logo').setStyle({'left': p+'px'});
			}),
			new Effect.Tween(null, 204, 60, {sync: true}, function(p) {
				$('logo').setStyle({'top': p+'px'});
			}),
			new Effect.Appear('logo', {sync:true})
		], {duration: 0.5, queue: 'end'});

		new Effect.Parallel([
			new Effect.Tween(null, 320, 325, {sync: true}, function(p) {
				$('extras').setStyle({'left': p+'px'});
			}),
			new Effect.Tween(null, 204, 120, {sync: true}, function(p) {
				$('extras').setStyle({'top': p+'px'});
			}),
			new Effect.Appear('extras', {sync:true})
		], {duration: 0.5, queue: 'end'});

	},
	observeBackButton: function()
	{
		if (this.currentactive && this.getUrl() != this.currentactive)
			this.requestPage(this.getUrl());
	},
	onLinkClick: function(event)
	{
		event.stop();
		var p = this.href.gsub(new RegExp(".+"+Page.options.root), '').gsub(".html", '');
		Ep.requestPage(p);
	},
	preLoad: function()
	{
		if (page = this.getUrl()) {
			location.href = Page.options.root+page+'.html';
			return;
		}
			
		document.observe('dom:loaded', Ep.initPage.bindAsEventListener(Ep));
	},
	requestPage: function(page)
	{
		var page = page.sub(/[\w]{2}\//, '');
		
		this.currentrequest = page;
		
		if (cached = this.getCached(page))
			return this.handleRequestPageReply(cached, false);
			
		Page.get(page, {
			sections: ['content','menu'],
			onComplete: this.handleRequestPageReply.bind(this)
		});
	},
	setCached: function(page, content)
	{
		this.getCacheContainer().store(page, content);
	},
	setUrl: function(url)
	{
		location.href = '#' + url;
	}
};
Ep.pagecache = 'page-cache-'+Date();

var RoundedDiv = {
	drawPaddingElement: function(element, r, lineheight, i, leftorright)
	{
		width = (i==0 ? r : r - Math.sqrt(Math.pow(r, 2) - Math.pow((r - lineheight*i),2)));
		var d = new Element('div').setStyle({
			//outline: '1px solid #ff0000',
			cssFloat: leftorright || 'left',
			clear: leftorright || 'left',
			width: width+'px',
			height: lineheight+'px'
		});
		element.insertBefore(d, element.down());
	}
};
RoundedDiv.Methods = {
	toCircle: function(element, options)
	{
		var options = Object.extend({
			lineheight: 10,
			semicircle: false,
			semicircleorientation: 'right'
		}, options || {});
		var element = $(element);
		var r = Math.floor((element.getWidth()-element.getStyle('border-left-width').sub('px', '')*2-element.getStyle('padding-left').sub('px', '')*1-element.getStyle('padding-right').sub('px', '')*1)/2);
		var c = Math.floor(r/options.lineheight);
		var increment = Math.round(r/c, 2);
		for (i=0; i<c; i++) {
			if (options.semicircle && options.semicircleorientation == 'left' || !options.semicircle)
				RoundedDiv.drawPaddingElement(element, r, options.lineheight, i);
			if (options.semicircle && options.semicircleorientation == 'right' || !options.semicircle)
				RoundedDiv.drawPaddingElement(element, r, options.lineheight, i, 'right');
		}
		for (i=c; i>0; i--) {
			if (options.semicircle && options.semicircleorientation == 'left' || !options.semicircle)
				RoundedDiv.drawPaddingElement(element, r, options.lineheight, i);
			if (options.semicircle && options.semicircleorientation == 'right' || !options.semicircle)
				RoundedDiv.drawPaddingElement(element, r, options.lineheight, i, 'right');
		}
	}
};
Element.addMethods(RoundedDiv.Methods);

