soundManager.url = '/';
soundManager.allowPolling = true;
soundManager.useHighPerformance = true;
soundManager.waitForWindowLoad = true;
soundManager.consoleOnly = true;
soundManager.debugMode = false;

window.addEvent('domready',function() {
	soundManager.onload = function() {
		try {
			new AmbientRecordings();
		} catch(e) {
			console.dir ? console.dir(e) : console.log(e);
		}
	}
});

var AmbientRecordings = new Class({
	initialize: function() {
		var loadmsg = new Element('h3').set('html','LOADING&hellip;');
		$('ambientrecordings').adopt(loadmsg);
		
		this.recordings = new Hash();
		this.counter = new Counter();
		
		this.ol = new Element('ol').addClass('recordinglist');		
		$('ambientrecordings').adopt(this.ol);
		
		tumblr_api_read.posts.each(function(post) {
			var string = post['audio-caption'].replace(/&#160;/,' ');
			var count = post['audio-plays'].toInt();				
			var file = /audio_file=(.+)&/.exec(post['audio-player'])[1];

			var split = /#([0-9]+) - (.*) \((.+)\)/.exec(string);
			if(split) {
				var number = split[1];
				var about = split[2];
				var date = split[3];

				var rec = new Recording(this,number,file,about,date,count);							
				this.recordings.set(number,rec);
			}
		},this);
		
		if(tumblr_api_read.posts.length == 1) {
			this.ol.adopt(
				new Element('li').addClass('singlepost').adopt(
					new Element('a',{
						'href': '/',
						'html': '&lt;&lt; home'
					})
				)
			);
		}
		
		this.ol.adopt(new Element('li',{
			'class': 'copyright',
			'html': '©2009 all rights reserved'
		}));
		
		$(document.body).adopt(
			new Element('iframe',{
				'id': 'tumblr_controls',
				'src': 'http://www.tumblr.com/dashboard/iframe?src=http%3A%2F%2Fambientrecordings.tumblr.com%2F',
				'border': '0',
				'scrolling': 'no',
				'allowtransparency': 'true',
				'frameborder': '0'
			})
		);
		
		loadmsg.destroy();
	},
	stopAllExcept: function(number) {
		this.recordings.each(function(rec) {
			if(number != rec.number) {
				rec.sound.stop();
				if(!rec.sound.loaded) {
					rec.sound.unload();
				}
				
				rec.listing.removeClass('selected');
				rec.statusspan.setStyle('visibility','hidden');
				rec.statusspan.set('html','');
			}
		});
	}
});

var Recording = new Class({
	initialize: function(AR,number,file,about,date,count) {
		this.AR = AR;
		this.number = number;
		this.file = file;
		this.about = about;
		this.date = date;
		this.count = count;
		
		var statusupdate = function(bytesLoaded,bytesTotal) {
			this.statusspan.set('html','loading&hellip; ' + ((bytesLoaded/bytesTotal) * 100).floor() + '%');
			if(bytesLoaded == bytesTotal) {				
				this.statusspan.fade('out');
			}
		}.bind(this)
		
		this.sound = soundManager.createSound({
			id: this.number,
			url: this.file,
			whileplaying: function() {
				AR.counter.set(this.position);
			},
			whileloading: function() {
				statusupdate(this.bytesLoaded,this.bytesTotal);
			}
		});

		this.mainspan = new Element('span',{
			'html': (this.number.toInt() < 10 ? '#0' : '#') + this.number + ' - ' + this.about,
			'class': 'item',
			'events': {
				click: function(event) {
					if(event.target.tagName != 'A') {
						this.AR.counter.slideUp();
						this.AR.stopAllExcept(this.number);
						this.sound.togglePause();
						this.listing.addClass('selected');
						this.statusspan.set('html','loading&hellip;');
						this.statusspan.setStyle('visibility','visible');	
					}
				}.bind(this)
			}
		});
		
		this.datespan = new Element('span',{
			'html': this.date,
			'class': 'date'
		});
		this.mainspan.adopt(this.datespan);
		
		this.statusspan = new Element('span').addClass('status');

		this.listing = new Element('li');		
		this.listing.adopt(this.mainspan);			
		this.listing.adopt(this.statusspan);		
		this.AR.ol.adopt(this.listing);				
	}
});

var Counter = new Class({
	initialize: function() {
		this.container = new Element('div').setProperty('id','counter');	
		
		if(Browser.Engine.webkit) {
			this.safarihack = new Element('div').setStyle('visibility','hidden');
			$(document.body).adopt(this.safarihack);
		}
		
		this.digits = new Hash({
			colon: new Element('div').addClass('digit').addClass('colon'),
			seconds_ones: new Element('div').addClass('digit').addClass('seconds_ones'),
			seconds_tens: new Element('div').addClass('digit').addClass('seconds_tens').adopt(new Element('div').addClass('overlay')),
			minutes_ones: new Element('div').addClass('digit').addClass('minutes_ones').adopt(new Element('div').addClass('overlay'))
		});
		
		this.digits.each(function(d) {
			this.container.adopt(d);
		},this);
		
		this.digits.seconds_ones.set('tween',{
			duration: 800,
			fps: 60,
			transition: Fx.Transitions.Quint.easeOut
		});
		
		$(document.body).adopt(this.container);
	},
	slideUp: function() {
		this.digits.seconds_ones.tween('margin-top',0);
	},
	set: function(ms) {
		var offset = -1 * (ms / 10).floor();
		
		var so_top = offset;
		var so_height = 1600 - offset;
		
		var st_mtop = ms < 9000 ? 900 + offset : 0;		
		var co_mtop = ms < 10000 ? 1000 + offset : 0;
		var mo_mtop = ms < 59000 ? 5900 + offset : 0;	
		
		var mod = ms % 10000;
		var multiplier = (ms / 10000).floor();
		var st_height = 1600 + (multiplier * 100);
		var st_top = mod >= 9000 ? (-1 * (mod / 10).floor()) + 900 - (multiplier * 100) : multiplier * -100;
		
		var mod = ms % 60000;
		var multiplier = (ms / 60000).floor();
		var mo_top = mod >= 59000 ? (-1 * (mod / 10).floor()) + 5900 - (multiplier * 100) : multiplier * -100;

		this.digits.seconds_tens.setStyle('height',st_height);
		this.digits.seconds_ones.setStyle('height',so_height);				

		this.digits.colon.setStyle('margin-top',co_mtop);
		this.digits.minutes_ones.setStyle('margin-top',mo_mtop);
		this.digits.seconds_tens.setStyle('margin-top',st_mtop);

		this.digits.minutes_ones.setStyle('top',mo_top);
		this.digits.seconds_tens.setStyle('top',st_top);
		this.digits.seconds_ones.setStyle('top',so_top);

		if(this.safarihack) {
			this.safarihack.set('text',0);
		}
	}
});