// JavaScript Document
$(document).ready(function(){
	
	// set Lazy Load for images
	$("img").lazyload({event:'nearby'});
	
	// Initialize Canvas
	$('#canvas').draggable({ 
			addClasses: false, cancel: 'input, option, p, h1, h2, dd, dl, dt, cite, object, embed, img, .foot-notes li',
			scroll: false
	});
	
	$("#canvas").bind('dragstop',function(event, ui){
		$("img").filter(function() {
			//calculate distance of image from window
			var x1 = $(this).position().top;
			var y1 = $(this).position().left;
			targetParent = $(this).offsetParent();
			while (targetParent.attr("id") != 'canvas' ) {
				x1 += targetParent.attr("offsetTop");	
				y1 += targetParent.attr("offsetLeft");
				targetParent = targetParent.offsetParent();
			}
			var x2 = $("#canvas").position().top * -1;
			var y2 = $("#canvas").position().left * -1;
			var distance = Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
			//return true if the image is one window width distance away from the window
			if (distance <= $(window).width() )
				return true;
			else
				return false;
		}).trigger('nearby');							
	});
	
	
	//Center on the "home" content
	$('#canvas').animate({
				top: ( $("#main").attr("offsetTop") )* -1,
				left: ( $("#main").attr("offsetLeft") ) * -1
	},0);
			
	//Nav Scroll functionality
	$('a.nav').click(function(event){
		var target = $(this).attr("href");
		var targetParent = $(target).offsetParent();
		var topOffset = $(target).attr("offsetTop");
		var leftOffset = $(target).attr("offsetLeft");
		while (targetParent.attr("id") != 'canvas' ) {
			topOffset += targetParent.attr("offsetTop");	
			leftOffset += targetParent.attr("offsetLeft");
			targetParent = targetParent.offsetParent();
		}
		topOffset *= -1;
		leftOffset *= -1;
		
		$("img").filter(function() {
			//calculate distance of image from window
			var x1 = $(this).position().top;
			var y1 = $(this).position().left;
			targetParent = $(this).offsetParent();
			while (targetParent.attr("id") != 'canvas' ) {
				x1 += targetParent.attr("offsetTop");	
				y1 += targetParent.attr("offsetLeft");
				targetParent = targetParent.offsetParent();
			}
			var x2 = topOffset * -1;
			var y2 = leftOffset * -1;
			var distance = Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
			//return true if the image is one window width distance away from the window
			if (distance <= $(window).width() )
				return true;
			else
				return false;
		}).trigger('nearby');
		
		
		$('#canvas').animate({
				   top: topOffset, 
				   left: leftOffset
	    }, 1000);
		event.preventDefault();
	});					   
		 	 
	//View switch functionality
	$('#x-ray-switcher').click(function(event){
		$('body').toggleClass("x-ray-view");
		event.preventDefault();
	});
	
	//Go Down functionality
	$('#down').click(function(event){
		topOffset = $('#canvas').css('top').replace('px','') - 400;
		leftOffset = $('#canvas').css('left').replace('px','');
		
		$("img").filter(function() {
			//calculate distance of image from window
			var x1 = $(this).position().top;
			var y1 = $(this).position().left;
			targetParent = $(this).offsetParent();
			while (targetParent.attr("id") != 'canvas' ) {
				x1 += targetParent.attr("offsetTop");	
				y1 += targetParent.attr("offsetLeft");
				targetParent = targetParent.offsetParent();
			}
			var x2 = topOffset * -1;
			var y2 = leftOffset * -1;
			var distance = Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
			//return true if the image is one window width distance away from the window
			if (distance <= $(window).width() )
				return true;
			else
				return false;
		}).trigger('nearby');
		
		$('#canvas').animate({
			top: topOffset
		});
		
		event.preventDefault();
	});
			 
});
