function adjustHeight(old_height, new_height) {
	if (typeof new_height == 'undefined') {
		var content = $(".copyarea:last").find('.content:last');
		var new_height = content.height();
	}
	if (new_height < 338) {
		new_height = 338;
	}
	var bg_height = new_height+110;

	$("#content").height(bg_height+'px');

	if (typeof old_height == 'undefined' || old_height == -1) {
		$(".contentBg_shadow").height(new_height+'px');
		old_height = 0;
	}

	if (new_height > old_height) {
		$(".contentBg_shadow").height(new_height);
		$(".copyarea:last").height(new_height);
		$("#bg2_mid .left").height(new_height+118);
	}
	else {
		$(".copyarea:last").animate({
			'height': new_height+'px'
		}, 500);
		$(".contentBg_shadow").animate({
			'height': new_height+'px'
		}, 1000);
		$("#bg2_mid .left").height(new_height+118);
	}
}

var pg_chg = false;
var cache = {};
$("#nav a").live('click', function() {
	if (!pg_chg) {
		pg_chg = true;

		if (typeof home_slide != 'undefined' && home_slide != '') {
			clearInterval(home_slide);
		}
		var $this = $(this);
		var old_rel = $("#inner_content > div:first").attr('rel');
		var new_rel = $this.attr("rel");
		var href = $this.attr('href').replace('#!/', '');

		$("#nav a").removeClass('active');
		$this.addClass('active');

		if (old_rel != new_rel) {
			if (typeof cache[href] == 'undefined') {
				Shadowbox.open({
					content: '#loading_ani',
					player: 'inline',
					width: 136,
					height: 32
				});

				$.get('php-bin/get_content.php', {
					pg: href
				}, function (data) {
					cache[href] = data;

					if (new_rel > old_rel) {
						$("#inner_content").append("<div id='"+href+"' rel='"+new_rel+"'>"+data+"</div>");

						$("#inner_content > div:first").animate({
							'marginLeft': '-=900px'
						}, 500, function() {
							$(this).remove();
						});
						$("#inner_content > div:last").animate({
							'marginLeft': '0px'
						}, 500, function() {
							$(this).css('marginLeft', '0px');
							adjustHeight();
						});
					}
					else {
						$("#inner_content").prepend("<div id='"+href+"' rel='"+new_rel+"' style='margin-left: -900px;'>"+data+"</div>");

						$("#inner_content > div:last").animate({
							'marginLeft': '900px'
						}, 500, function() {
							$(this).remove();
						});
						$("#inner_content > div:first").animate({
							'marginLeft': '0px'
						}, 500, function() {
							$(this).css('marginLeft', '0px');
							adjustHeight();
						});
					}

					pg_chg = false;
					Shadowbox.close();
				});
			}
			else {
				data = cache[href];
				
				if (new_rel > old_rel) {
					$("#inner_content").append("<div id='"+href+"' rel='"+new_rel+"'>"+data+"</div>");

					$("#inner_content > div:first").animate({
						'marginLeft': '-=900px'
					}, 500, function() {
						$(this).remove();
					});
					$("#inner_content > div:last").animate({
						'marginLeft': '0px'
					}, 500, function() {
						$(this).css('marginLeft', '0px');
						adjustHeight();
					});
				}
				else {
					$("#inner_content").prepend("<div id='"+href+"' rel='"+new_rel+"' style='margin-left: -900px;'>"+data+"</div>");

					$("#inner_content > div:last").animate({
						'marginLeft': '900px'
					}, 500, function() {
						$(this).remove();
					});
					$("#inner_content > div:first").animate({
						'marginLeft': '0px'
					}, 500, function() {
						$(this).css('marginLeft', '0px');
						adjustHeight();
					});
				}

				pg_chg = false;
			}
		}	
	}
});

$(".left_navi a").live('click', function() {
	if (!pg_chg) {
		pg_chg = true;

		var $this = $(this);
		var copyarea = $(".copyarea");
		var old_content = copyarea.find(".content");
		var old_height = old_content.height();
		var pg = $this.attr('href').split('#!/');
		var new_rel = $this.attr('rel');
		var old_rel = old_content.attr('rel').split('_');
		old_rel = old_rel[0];

		copyarea.height(old_height);
		$(".left_navi a").removeClass('active');
		$this.addClass('active');

		if (old_rel != new_rel) {
			if (typeof cache[pg[1]] == 'undefined') {
				Shadowbox.open({
					content: '#loading_ani',
					player: 'inline',
					width: 136,
					height: 32
				});

				$.get('php-bin/get_content.php', {
					pg: pg[1]
				}, function (data) {
					$('html, body').animate({scrollTop: '200px'}, 800);
					cache[pg[1]] = data;

					if (new_rel > old_rel) {
						old_content.after('<div class="content" rel="'+new_rel+'">'+data+'</div>');
						new_content = $(".content:last");
						var new_height = new_content.height();

						old_content.animate({
							'marginTop': '-='+old_height+'px'
						}, 500, function() {
							old_content.remove();
							adjustHeight(old_height);
						});
						new_content.animate({
							'marginTop': '0px'
						}, 500).css('marginTop', '0px');
					}
					else {
						old_content.before('<div class="content" rel="'+new_rel+'">'+data+'</div>');
						new_content = $(".content:first");
						var new_height = new_content.height();

						new_content.css('marginTop', '-'+new_height+'px');

						old_content.animate({
							'marginTop': new_height+'px'
						}, 500, function() {
							old_content.remove();
							adjustHeight(old_height);
						});
						new_content.animate({
							'marginTop': '0px'
						}, 500).css('marginTop', '0px');
					}

					pg_chg = false;
					Shadowbox.close();
				});
			}
			else {
				$('html, body').animate({scrollTop: '200px'}, 800);
				data = cache[pg[1]];

				if (new_rel > old_rel) {
					old_content.after('<div class="content" rel="'+new_rel+'">'+data+'</div>');
					new_content = $(".content:last");
					var new_height = new_content.height();

					old_content.animate({
						'marginTop': '-='+old_height+'px'
					}, 500, function() {
						old_content.remove();
						adjustHeight(old_height);
					});
					new_content.animate({
						'marginTop': '0px'
					}, 500).css('marginTop', '0px');
				}
				else {
					old_content.before('<div class="content" rel="'+new_rel+'">'+data+'</div>');
					new_content = $(".content:first");
					var new_height = new_content.height();

					new_content.css('marginTop', '-'+new_height+'px');

					old_content.animate({
						'marginTop': new_height+'px'
					}, 500, function() {
						old_content.remove();
						adjustHeight(old_height);
					});
					new_content.animate({
						'marginTop': '0px'
					}, 500).css('marginTop', '0px');
				}

				pg_chg = false;
			}
		}
	}
});

$("a.page_navi").live('click', function() {
	if (!pg_chg) {
		pg_chg = true;

		Shadowbox.open({
			content: '#loading_ani',
			player: 'inline',
			width: 136,
			height: 32
		});

		if (typeof home_slide != 'undefined' && home_slide != '') {
			clearInterval(home_slide);
		}

		var $this = $(this);
		var copyarea = $(".copyarea");
		var old_content = copyarea.find(".content");
		var old_height = old_content.height();
		var pg = $this.attr('href').split('#!/');
		var new_rel = $this.attr('rel');
		var old_rel = $("#inner_content > div:first").attr('rel');

		var href = $this.attr('href').replace('#!/', '');
		var master_href = href.split('/');
		master_href = master_href[0];

		if (new_rel != old_rel) {
			$.get('php-bin/get_content.php', {
				pg: href,
				subpg: true
			}, function (data) {
				pg_chg = false;
				if (new_rel > old_rel) {
					$("#inner_content").append("<div id='"+href+"' rel='"+new_rel+"'>"+data+"</div>");

					$("#inner_content > div:first").animate({
						'marginLeft': '-=900px'
					}, 500, function() {
						$(this).remove();
					});
					$("#inner_content > div:last").animate({
						'marginLeft': '0px'
					}, 500, function() {
						$(this).css('marginLeft', '0px');
						adjustHeight();
					});
				}
				else {
					$("#inner_content").prepend("<div id='"+href+"' rel='"+new_rel+"' style='margin-left: -900px;'>"+data+"</div>");

					$("#inner_content > div:last").animate({
						'marginLeft': '900px'
					}, 500, function() {
						$(this).remove();
					});
					$("#inner_content > div:first").animate({
						'marginLeft': '0px'
					}, 500, function() {
						$(this).css('marginLeft', '0px');
						adjustHeight();
					});
				}

				$("#nav a").removeClass('active').each(function() {
					if ($(this).attr('href') == '#!/'+master_href) {
						$(this).addClass('active');
					}
				});

				pg_chg = false;
				Shadowbox.close();
			});
		}
	}
});

$(".inner_navi a").live('click', function() {
	if (!pg_chg) {
		pg_chg = true;

		Shadowbox.open({
			content: '#loading_ani',
			player: 'inline',
			width: 136,
			height: 32
		});

		var $this = $(this);
		var copyarea = $(".copyarea");
		var old_content = copyarea.find(".content");
		var old_height = old_content.height();
		var pg = $this.attr('href').split('#!/');
		var rel = $this.attr('rel');
		var new_rel = rel.split('_');
		new_rel = new_rel[new_rel.length-1];
		var old_rel = old_content.attr('rel');
		if (old_rel.indexOf('_') == -1) {
			old_rel = old_rel+'_0';
		}
		old_rel = old_rel.split('_');
		old_rel = old_rel[old_rel.length-1];

		if (old_rel != new_rel) {
			$.get('php-bin/get_content.php', {
				pg: pg[1]
			}, function (data) {
				$('html, body').animate({scrollTop: '200px'}, 800);
				if (new_rel > old_rel) {
					old_content.after('<div class="content" rel="'+rel+'">'+data+'</div>');
					new_content = $(".content:last");
					var new_height = new_content.height();

					old_content.css('float', 'left').css('opacity', '1');
					new_content.css('float', 'left').css('opacity', '0');

					old_content.animate({
						'marginLeft': '-=590px',
						'opacity': '0'
					}, 500, function() {
						old_content.remove();
						adjustHeight();
					});
					new_content.animate({
						'marginLeft': '0px',
						'opacity': '1'
					}, 500);
				}
				else {
					old_content.before('<div class="content" rel="'+new_rel+'">'+data+'</div>');
					new_content = $(".content:first");
					var new_height = new_content.height();

					old_content.css('float', 'left').css('marginLeft', '0px').css('opacity', '1');
					new_content.css('float', 'left').css('marginLeft', '-590px').css('opacity', '0');

					old_content.animate({
						'marginLeft': '590px',
						'opacity': '0'
					}, 500, function() {
						old_content.remove();
						adjustHeight();
					});
					new_content.animate({
						'marginLeft': '0px',
						'opacity': '1'
					}, 500);
				}

				pg_chg = false;
				Shadowbox.close();
			})
		}
	}
});
