$(document).ready(function (){

	$('#portfolio-nav-top a').live('click', function(eve){
		eve.preventDefault();		
	
		// a url plugin is used here to get the second url segment of the clicked link
		// ex. markhynes.ca/portfolio/4
		var url = $(this).url();
		var segment = url.segment(2);

		// the url segment is sent to the controller/method
		// a json object (called data) is returned from the controller
		// the json object contains:
		// - the html view to be rendered (named data.content)
		// - the prev and next link #s (named data.prev and data.next)
		$.get('/portfolio/get_ajax_view/'+segment,
			function(data){				

				// insert requested project
		        $("#project-section").fadeTo(70, 0, 'swing', function () {
		        	$("#project-section").empty().append(data.content).fadeTo(80, 1, 'swing');
		        });
        
				// replace the prev and next links
				$('.button-previous-project').attr("href", "/portfolio/"+data.prev);
				$('.button-next-project').attr("href", "/portfolio/"+data.next);						

			}, "json");
	});


	$('#portfolio-nav-bottom a').live('click', function(eve){
		eve.preventDefault();		
	
		// a url plugin is used here to get the second url segment of the clicked link
		// ex. markhynes.ca/portfolio/4
		var url = $(this).url();
		var segment = url.segment(2);

		// the url segment is sent to the controller/method
		// a json object (called data) is returned from the controller
		// the json object contains:
		// - the html view to be rendered (named data.content)
		// - the prev and next link #s (named data.prev and data.next)
		$.get('/portfolio/get_ajax_view/'+segment,
			function(data){
			
      			// scroll the page 
      			$('html, body').animate({scrollTop: $("body").offset().top}, 200, 'swing', function() {
					// insert requested project
	        		$("#project-section").delay(50).fadeTo(70, 0, 'swing', function () {
	        			$("#project-section").empty().append(data.content).fadeTo(80, 1, 'swing');
	        		});  			
      			});
        
				// replace the prev and next links
				$('.button-previous-project').attr("href", "/portfolio/"+data.prev);
				$('.button-next-project').attr("href", "/portfolio/"+data.next);						

			}, "json");
	});

});
