var subnav = function() {
	$each($(document.body).getElement('div.sub-nav').getElements('a'), function(item) {
		item.addEvent('click', function(e) {
			e.stop();
			new Request.HTML({url: '/sync'+this.get('href'), onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				// Focus
				$(document.body).getElement('div.sub-nav').getElement('a.focus').removeClass('focus');
				item.addClass('focus');
				// JSON
				if (responseHTML.split('***').length>1) {
					var json = JSON.decode(responseHTML.split('***')[0]);
					if ($chk(json.image)) $(document.body).getElement('div.content').getElement('img').set('src', '/images/elements/banner/'+json.image+'.png');
					if ($chk(json.right)) $(document.body).getElement('div.rightside').getElement('img').set('src', '/images/elements/rightside/'+json.right+'.png');
					responseHTML = responseHTML.split('***')[1];
				}
				// HTML
				var content = $(document.body).getElement('div.sub-nav').getNext('div');
				content.empty().set('html', responseHTML);
				// Header
				var h2_image = item.get('href').split('/')[item.get('href').split('/').length-2];
				var h2_name = item.get('html').replace(/\n/, '').replace(/\s/g, ' ');
				//while(h2_name.search('  ')) h2_name = h2_name.replace('  ', ' ');
				h2 = new Element('h2', {'html': h2_name, 'styles': {'background-image': 'url(/images/elements/header/'+h2_image+'.png)'}}).inject(content, 'top');
			}}).send();
		});
	});
}

window.addEvent('domready', function() { subnav.run(); });