$(document).ready(function() {
	
	var hash = window.location.hash;
	if ($(hash).length > 0){
		$('html,body').animate({scrollTop: $(hash).offset().top},'slow');
	}
	

	$("#image img.homelogo").hide();

	sizeScroll();
	resizeLogo();
	resizeMap();
	resizeSite();
	$(window).resize(function(){
		sizeScroll();
		resizeLogo();
		resizeMap();
		resizeSite();		
	});
	$("div.homenavi a").hide();
	$("div.homenavi").not(".visited").find(".shape").hide();
	
	$("div.homenavi").not(".visited").hover(
		function(){
			$(this).find(".shape").show();
		},
		function(){
			$(this).find(".shape").hide();
		}
	);
	

	$(".share").click(function(){
		var el = $(this).parent().parent().find(".sharebuttons");
		slideToggle(el);
		return false;
	})
	$(".printmap").click(function(){
		var url = $(this).attr("href");
		var hash = window.location.hash;
		url = url+hash;
		window.open(url, 'print', "width=700,height=700,left=0,top=0,scrollbars=1,resizable=1,status=1").focus();
		return false;
	})
		
	$("div.caption").each(function(){
		var h = $(this).height();
		var padding = h/16 * 5;
		$(this).css("padding-bottom",padding+"px");
	})


	$("div.clk").click(function(){
		var current = $(this).find(".ci").html();
		var id = $(this).attr("id").replace("c","");
		var thisdiv = $(this);
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/getimage.php",
			dataType: "html",
			data: "id="+id+"&c="+current,
			success: function(resultdata) {
				$(thisdiv).html(resultdata);
			}
		});
	});


	$("div.homenavi").click(function(){
		var href = $(this).find("a").attr("href");
		window.location = href;
	});
	$("div.pagenavi").click(function(){
		var href = $(this).find("a").attr("href");
		window.location = href;
	});
	$("div.homenavi").each(function(){
		$(this).mousemove(function(e){
			$(this).find("a").show();
			var offset = $(this).offset();
			$(this).find("a").css({
				top: (e.pageY + 0 - offset.top) + "px",
				left: (e.pageX + 13 - offset.left) + "px"
			});
		});
	});
	$("div.homenavi").mouseout(function(e){
		$(this).find("a").hide();
    });
    
    $('.scroll').jScrollPane();
    
    
});




function slideToggle(el, bShow){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

	var svg = $el.parent().find(".share");
  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 250});
    var color = svg.find("rect").css("fill");
    svg.find("rect").css("fill","white");
    svg.find("text").css("fill",color);
    svg.find("polyline").css("stroke",color);
    
  } else {
    $el.animate({height: 0}, {duration: 250, complete:function (){
        $el.hide();
	    var color = svg.find("polyline").css("stroke");
	    svg.find("rect").css("fill",color);
	     svg.find("text").css("fill","black");
	    svg.find("polyline").css("stroke","white");
      }
    });
  }
}


function sizeScroll(){
	$(".scroll").each(function(){
		var th = $(this).height();
		var offset = $(this).offset();
		var wh = $(window).height();
		var nh = (Math.floor((wh-offset.top)/21)-1)*21;
		if (nh < 252){
			nh = 252;
		}
		var dragh = $(".jspDrag").height();
		var newdragh = Math.round(dragh * nh/th);
		
		$(this).height(nh);
		$(".jspContainer, .jspTrack").height(nh);
		$(".jspDrag").height(newdragh);
	});
}

function resizeSite(){
	var wh = $(window).height()-48;
	var siteh = $("#site").height();
	if (wh > siteh){
		$("#site").height(wh);
	}
}

function resizeLogo(){
	var wh = $(window).height();	
	var imgh = wh-320;
	if (imgh < 320)
		imgh = 320;
	$("#image img.homelogo").height(imgh).fadeIn(200);
}
function resizeMap(){
	var wh = $(window).height();
	var maph = wh-106-20;
	if (maph < 486)
		maph = 486;
/*
	var ww = $(window).width();	
	var mapw = ww-300-30;
	if (mapw < 512)
		mapw = 512;
*/
	$("#map_canvas").height(maph);
}
