//Good Old Fashioned script
var gof = {
	ready : function(){
	var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('margin-top').replace(/auto/, 0));
	console.log('top'+ top);
    $(window).scroll(function (event) {
      // what the y position of the scroll is
      var y = $(this).scrollTop();
		console.log(y);
      // whether that's below the form
      if (y >= top + 108) {
        // if so, ad the fixed class
        $('#sidebar').addClass('fixed');
      } else {
        // otherwise remove it
        $('#sidebar').removeClass('fixed');
      }
    });
		var about= $('#about'),
			mission = $('#mission'),
			blog = $('#blog'),
			topbar = $('#topbar'),
			ms = $('#missionStatement'),
			cb = $('#contactBox'),
			posts = $('.post'),
			jsSnips = $('pre.js'),
			contact = $('#contact');
			jsSnips.snippet('javascript',{style:'vim',showNum:true});
		//console.log('rtg');
		topbar.click(function(){ location.href = '/'; });
		blog.click(function(){
			//$('.post').not('.template').show('slow');
			cb.hide('fast');
			ms.hide('fast');
			gof.getPosts();
		});
		mission.click(function(){
			$('.post').hide('fast');
			cb.hide('fast');
			ms.show('fast');
		});
		contact.click(function(){
			$('.post').hide('fast');
			ms.hide('fast');
			cb.show('fast');
		});
		gof.getPosts(3);
	},
	getPosts: function(lim){
		var url = '/blog/?rm=lp';
		if(lim>0){
			url = url + '&lim=' + lim;
		}
		$.getJSON(url, function(data){
				console.log('size'+ data.length);
				$('#posts').html('');
				for(var i=0;i<data.length;i++){
						console.log('foo'+ i);
						$('.template span.blogContent').html(data[i].message);
						var post = $('.template').clone().removeClass('template').appendTo('#posts');
						post.children('.titlePost').html( data[i].title);
						post.children('table tbody tr td div.text span').html(data[i].message);
						post.show('blind','slow');
				}
		});
	}

}

$(gof.ready);

