var selected = null;
var lng = 'en';
var newed = null;

$(document).ready(function() {
	//$('.title').corners('top-left transparent');
	//$('.rounded').corners('top-left bottom-right transparent');
	
	$('div .content').hide();
	
	$('.title').css({'cursor': 'pointer'}).click(function() {
		var target = $(this).attr('class').split(' ')[1];
		$.history.add('/' + lng + '/' + target);
	});
	
	var toggle = function(hash) {
		if (selected != null && selected != hash) {
			$('div .' + selected + ':not(div)').animate({backgroundColor: "rapture", color: "bliss", paddingLeft: '10px'}, 300);
			$('div .' + selected + ':not(h3)').slideUp(300);
		}
		selected = hash;
		$('div .' + selected + ':not(div)').animate({backgroundColor: "disillusion", color: "martini", paddingLeft: '20px'}, 300);
		$('div .' + selected + ':not(h3)').slideDown(300);
		
		$('#langs a').click(function() {
			if (lng != $(this).attr('href').substr(1)) {
				$('.' + lng + '_selected').removeClass(lng + '_selected').addClass(lng);
				lng = $(this).attr('href').substr(1);
				$(this).addClass(lng);
				$(this).addClass(lng + '_selected');
				if (selected != null) {
				  $('div .' + selected + ':not(div)').animate({backgroundColor: "rapture", color: "bliss", paddingLeft: '10px'}, 300);
					$('div .' + selected + ':not(h3)').slideUp(300, function() {
						$.post('/core/data.json', {lng: lng, part: newed ? newed : selected}, process, "json");
					});
				} else {
					$.post('/core/data.json', {lng: lng, part: newed ? newed : selected}, process, "json");
				}
			}
			return false;
		});
	};
	
	var process = function(data) {
		$.each(data.data, function() {
		  if (this.name == 'title') {
		    document.title = this.content;
		  } else if (this.className != undefined) {
				$(this.name).attr('class', '');
				$(this.name).addClass(this.className);
			} else {
				$(this.name).html(this.content);
			}
		});
		$('h3').animeText({step: 40, chars: ['[','\\','*','#','$',')',';','+','~','&','^'], colors: ['#236071','#4ba8a2','#e2e9e6'], lastChar: '_', lastColor: '#262c2a'});
		selected = data.part;
		$.history.add('/' + lng + '/' + selected);
		$('div .' + selected + ':net(h3)').slideDown(300);
		$('div .' + selected + ':not(div)').animate({backgroundColor: "disillusion", color: "martini", paddingLeft: '20px'}, 300);
	};
	
	$(window).history(function(e, currentHash) {
		if (currentHash == '') currentHash = '/' + lng + '/info';
		var parts = currentHash.substr(1).split('/');
		if (parts[1] == 'null') parts[1] = 'info';
		
		if (parts[0] != lng) {
			if (selected != parts[1]) newed = parts[1];
			$('#langs .' + parts[0]).click();
		} else {
			toggle(parts[1]);
		}
	});
	$(window).historyadd(function(e, currentHash, previousHash) {
		if (currentHash == '') currentHash = '/' + lng + '/info';
		var parts = currentHash.substr(1).split('/');
		if (parts[1] == 'null') parts[1] = 'info';
		
		if (parts[0] != lng) {
			if (selected != parts[1]) newed = parts[1];
			$('#langs .' + parts[0]).click();
		} else {
			toggle(parts[1]);
		}
	});
	
	$.history.init();
	
	$('h3').animeText({step: 20, chars: ['[','\\','*','#','$',')',';','+','~','&','^'], colors: ['#236071','#4ba8a2','#e2e9e6'], lastChar: '_', lastColor: '#262c2a'});
});