var OnTheBBC = {
	followers_json: '/followers.txt',
	followers: function(callback) {
		$.get(this.followers_json, function(data, status) {
			var json = '[' +data.replace(/,$/, '')+ ']'
			var stats = eval(json);
			if(callback) { callback(stats); }
		});
	},
	usage_chart: function(container) {
		// generates a google charts image and places it into container
		this.followers(function(stats) {
			console.log(stats)
			var followers = [], dates = [], max = -1, min = 1.0e100;
			for (var i = 0; i < stats.length; i++) {
				var s = stats[i];
				dates.push(s.date);
				var total = 0;
				for (chan in s.followers) {
					var n  = s.followers[chan];
					total += n;
				}
				if(total > max) { max = total; }
				if(total < min) { min = total; }
				followers.push(total)
			};
			// max += 100;
			var date_labels = [];
			for (var i = 0; i < dates.length; i++) {
				if((i === 0)||(i === (dates.length-1))) {
					var dd = dates[i], date = new Date();
					var y = parseInt(dd.substr(0,4), 10), m = parseInt(dd.substr(4,2), 10), d = parseInt(dd.substr(6,2), 10);
					date.setUTCFullYear(y,m,d)
					var p = date.toDateString().split(/ /);
					date_labels[i] = p[2] + ' ' + p[1] + ' ' + p[3];
				} else {
					date_labels[i] = '';
				}
			};
			console.log(min, max, followers, followers.length)
			var params = {
				cht: 'bvs',
				chbh:'a,3',
				chxt: 'y,x',
				chxl: '1:|'+date_labels.join('|'),
				chxr: '0,0,'+max,
				chco: '79b24e',
				chf: 'bg,s,EFEFEF00',
				chs: '400x200',
				chds: '0,'+max,
				chd: 't:' + followers.join(',')
			}
			var url = 'http://chart.apis.google.com/chart?' + $.param(params);
			console.log(url)
			$(container).append('<img src="'+url+'" />')
		});
	}
};


var Section = function(link) {
	this.link = $(link);
	this.init();
};
$.extend(Section, {
	index: [],
	active: null,
	watching: true,
	create: function(link){
		this.push(new Section(link));
	},
	push: function(s){
		this.index.push(s);
	},
	reset: function() {
		for (var i = this.index.length - 1; i >= 0; i--){
			this.index[i].hide();
		};
	},
	watch_scroll: function(attribute){
		this.watching = true;
	},
	unwatch_scroll: function(attribute){
		this.watching = false;
	},
	scroll: function(){
		if(!this.watching) { return; }
		var min = $(document).height()*2, c;
		for (var i = this.index.length - 1; i >= 0; i--){
			var d = this.index[i].distance();
			if(d !== false && d < min) { min = d; c = this.index[i]; }
		};
		if (this.active) { this.active.hide(); }
		this.active = c;
		c.highlight();
	},
	log: function(m){
		if(console && console.log) { console.log(m); }
	}
});

Section.prototype = {
	init: function(){
		if(this.internal_link()) {
			this.active = true;
			var c = (function(s){
				return function() {
					return s.show();
				};
			})(this);
			this.link.click(c);
			var m = /^.*(#[\w-]+)/.exec(this.link.attr('href'));
			if (m) {
				this.id = m[1];
				this.content = $(this.id);
				this.offset = Math.max(0, this.content.offset().top - 38);
			}
			if(window.location.hash === this.id) {
				this.show();
			}
		}
	},
	internal_link: function(){
		return (location.pathname.replace(/^\//,'') == this.link[0].pathname.replace(/^\//,''));
	},
	show: function(){
		Section.reset();
		Section.unwatch_scroll();
		var cb = (function(s) {
			return function() {
				Section.watch_scroll();
				Section.active = s;
			};
		})(this);
		$('html,body').animate({scrollTop:this.offset}, 500, 'easeOutCirc', cb)
		this.highlight();
		return !$.browser.msie;
	},
	highlight: function(){
		this.link.parent('li').addClass('active');
	},
	hide: function(){
		if(this.active) {this.link.parent('li').removeClass('active');}
	},
	distance: function() {
		if(!this.active) { return false; }
		return Math.abs($(window).scrollTop() - this.offset)
	},
	log: function(m){
		Section.log(m);
	}
};

$(document).ready(function() {
	$('.navigation ul li a').each(function() {
		Section.create(this);
	});
	// ie8 doesn't react well to me grabbing the scroll event
	if(!($.browser.msie && parseInt($.browser.version, 10) === 8)) {
		$(window).scroll(function() {
			Section.scroll();
			return true;
		});
	}
	$('.site-preview a').fancybox({hideOnContentClick: true, imageScale:true});
	$('.site-thumbs a').fancybox({hideOnContentClick: true, imageScale:true});
});

