$(document).ready(function() {
	var available = $(window).height()-$('#top').height()-20;
	if($('#level0').height() < available){
		//$('#level0').css('min-height', available);
		$('#level1').css('min-height', available);
		//$('#level2').css('min-height', available);
	}
	var pathname = $(location).attr('href');
	$('#nav').find('a').each(function(){
		if($(this).attr('href') == "./?Page=<?=$_GET['Page']?>" ||($(this).attr('href') == "./" && pathname == "http://shotokai.org/"))
			$(this).addClass("highlight");
	});
	var $page = $(this).attr("href");
	$('a#page').click(function(e){
		var $page = $(this).attr('title');
		$.get("http://shotokai.org/include/Dynamic_Page_C_L.php", { name : $page }, function(data) {
			$('#level2').fadeOut('fast');
			$('#level2').html(data).fadeIn();
			var loading = $('<img style="margin-left:250px; margin-top: 100px" width="200px" src="http://shotokai.org/Images/kdsspin.svg">').appendTo('#level2');
			$('#main').hide();
			$('#rightBar0').hide();
			var interval = setInterval(function(){
				var images = $('#main').find('img');
				var images2 = $('#rightBar0').find('img');
				var completed = 0;
				// Counts number of images that are succesfully loaded
				images.each(function(){
					if (this.complete) completed++;	
				});
				images2.each(function(){
					if (this.complete) completed++;	
				});
				if (completed == images.length+images2.length){
					clearInterval(interval);
					// Timeout added to fix problem with Chrome
					setTimeout(function(){
						loading.fadeOut('slow', function(){
							$('#main').fadeIn(500);
							$('#rightBar0').fadeIn(500);
						});
					}, 700);
				}
			}, 100);
		});
		//return false;
	});
	$('a').click(function(e){            
		if($(this).attr("name")=='edit'){
		$.get($(this).attr("href"), function(data) {
			$('#rightBar0').slideUp('slow');
			$('#main').slideUp('slow');
			$('#main').html(data).hide();
			$('#rightBar0').empty();
			$('div').each(function(){
				if($(this).attr('id')=='1'){
					$('#rightBar0').append($(this)).hide();
				}
			});
			
			var loading = $('<div class="loading"><b>Loading...</b></div>').appendTo('#level2').slideDown('fast');
			var interval = setInterval(function(){
				var images = $('#main').find('img');
				var images2 = $('#rightBar0').find('img');
				var completed = 0;
				// Counts number of images that are succesfully loaded
				images.each(function(){
					if (this.complete) completed++;	
				});
				images2.each(function(){
					if (this.complete) completed++;	
				});
				if (completed == images.length+images2.length){
					clearInterval(interval);
					// Timeout added to fix problem with Chrome
					setTimeout(function(){
						loading.fadeOut('slow', function(){
							$('#main').slideDown(500);
							$('#rightBar0').slideDown(500);
						});
					}, 1000);
				}
			}, 100);
		});
		return false;
		}
	});
	$('#menu').hide();
	var flowOrig = $('#flower').css("left");
	$('#flower').animate({left: "+=900px"}, 500);
	$('#menu').show(500);
	$('#flower').click(function(){
		var pos = $('#flower').css("left");
		if (pos == flowOrig){
			$('#flower').animate({left: "+=900px"}, 500);
		}else{
			$('#flower').animate({left: "-25px"}, 500);
		}
		$('#menu').toggle('slow');
	});
});
