var ProgressBar = new Class({
	Implements: [Options],
	options: {
		container: $$('body')[0],
		boxID:'',
		percentageID:'',
		displayID:'',
		startPercentage: 0,
		displayText: true,
		speed:10
	},
	initialize: function(options) {
		this.setOptions(options);
		this.createElements();
	},
	createElements: function() {
		var box = new Element('div', { id:this.options.boxID });
		box.inject(this.options.container);
		if(this.options.displayText) { 
			var text = new Element('div', { id:this.options.displayID });
			text.inject(box);
		}
		var perc = new Element('div', { id:this.options.percentageID, 'style':'width:0px;' });
		perc.inject(box);
		this.set(this.options.startPercentage);
	},
	calculate: function(percentage) {
		return ($(this.options.boxID).getStyle('width').replace('px','') * (percentage / 100)).toInt();
	},
	animate: function(to) {
		$(this.options.percentageID).set('morph', { duration: this.options.speed, link:'cancel' }).morph({width:this.calculate(to.toInt())});
		if(this.options.displayText) { 
			$(this.options.displayID).set('text', to.toInt() + '%'); 
		}
	},
	set: function(to) {
		this.animate(to);
	}
	
});
function intro(){
	$('box').addClass('dn');
	$('splash').fade('hide');
	$('splash').removeClass('dn');
	$('splash').fade('in');
	var i = 0;
	$$('img.pre').each(function(e){
		if(e.hasClass('pa')){	
			e.set('tween', {duration: 'short'});
			var eT = e.getStyle('top').toInt();				
			e.fade('hide');
			e.addEvent('click', function(ev){
				e.fade(0.5);
			}); 		
			e.addEvent('mouseover', function(ev){
				e.tween('top',  (eT-7));				
			}); 
			e.addEvent('mouseout', function(ev){
				e.tween('top',  (eT));			
			}); 
			(function(){this.fade('in');}).delay((130*i), e);
			i++;
		}
	});
}
function refreshBuzz(){
	var req = new Request({
		method: 'get',
		url: '/user/splashBuzz/',
		onRequest: function() { 
			$('buzzStatus').set('html', '( updating ... )');
			$clear(buzzCountDown);
		},
		onComplete: function(response) {
			$('buzzItem').set('html',response);			
			$('buzzStatus').set('html', '( next update in <span id="buzzCount">20</span> secs )');
			buzzCountDown = countdownBuzz.periodical(1000);
		}
	}).send();
}
function countdownBuzz(){
	if($('buzzCount')){
		var cd = parseInt($('buzzCount').get('text'));
		$('buzzCount').set('text',cd-1);
		if(cd==1){
			refreshBuzz();
		}
	}
}
var buzzCountDown;
window.addEvent('domready', function() {
	buzzCountDown = countdownBuzz.periodical(1000);
	$('splash').addClass('dn');
	/* Progress Bar */
	var progressBar = new ProgressBar({
		container: $('splashContainer'),
		startPercentage: 0,
		speed: 500,
		boxID: 'box',
		displayID: 'tperc',
		percentageID: 'perc'
	});
	var images = [];
	var i=0;
	$$('img.pre').each(function(e){
		images[i] = e.src;
		i++;
	});
	var loader = new Asset.images(images, {
		onProgress: function(counter,index) {
			progressBar.set((counter + 1) * (100 / images.length));
		},
		onComplete: function() {
			var timeoutID = intro.delay(500);
		}
	});
});