var Flicker = Class.create({

	initialize:function(el, opt){
		this.el = $(el);
		this.el.setOpacity(0);
		this.opt = Object.extend({
			"random":true,
			"timer": false
		}, opt || {});
		this.run_effect();
	},

	run_effect: function(){

		setTimeout(function(){
			this.el.morph('opacity:0.6', {duration:0.4,  queue: {position: 'end', scope: 'flickerq'}});
			Effect.Queues.get('flickerq').interval = 30;
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:1', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:1', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0.8', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
		}.bind(this), 1000);
		setTimeout(function(){
			this.el.morph('opacity:0.8', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0.7', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
		}.bind(this), 2000);
		setTimeout(function(){
			this.el.morph('opacity:0.8', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0.7', {duration:0.2,  queue: {position: 'end', scope: 'flickerq'}});
			this.el.morph('opacity:0', {duration:0,  queue: {position: 'end', scope: 'flickerq'}});
		}.bind(this), 2500);
// 		this.el.morph('opacity:0.1;', {'duration': 0.2});
		setTimeout(function(){
			this.el.morph('opacity:1;', {'duration': 1.5});
		}.bind(this), 2800);
// 		setTimeout(function(){
// 			new Effect.Opacity(this.el, { from: 0.8, to: 0.1, duration: 0});
// 		}.bind(this), 16000);

	}

});

var html5_audiotypes={
	"mp3": "audio/mpeg",
	"mp4": "audio/mp4",
	"ogg": "audio/ogg",
	"wav": "audio/wav"
}

function createsoundbite(sound){
	var html5audio=document.createElement('audio')
	if (html5audio.canPlayType){ //check support for HTML5 audio
		for (var i=0; i<arguments.length; i++){
			var sourceel=document.createElement('source')
			sourceel.setAttribute('src', arguments[i])
			if (arguments[i].match(/\.(\w+)$/i))
				sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
				html5audio.appendChild(sourceel)
		}
		html5audio.load()
// 		console.log(html5audio);
		html5audio.playclip=function(){
			html5audio.pause()
// 			html5audio.currentTime=0
			html5audio.play()
		}
		return html5audio
	}
	else{
		return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
	}
}

document.observe("dom:loaded", function(){
	new Flicker("site-description");
// 	var n = 1;
	var link_elements = $("menu-main").select('li.menu-item a');
	link_elements.each(function(el, n){
		el.observe("mouseover", function(){
			var sound = createsoundbite("/piano_"+(n+1)+".mp3", "/piano_"+(n+1)+".ogg");
			sound.playclip()
		});
// 		n++
	});
// 	setTimeout(function(){
// 		$('content').select('h1.entry-title').each(function(el){
// 			new Effect.Move(el, {x:760, duration:0.8, transition: Effect.Transitions.spring});
// 		});
// 	}, 800);

})
