Pennente = {
	pubbl: rootPath + "audio/pennente30.mp3",
	auFiles: [
		['multiaudio3', 'shutter'], 
		['multiaudio4', 'shutter2'], 
		['multiaudio5', 'click4c'], 
		['multiaudio6', 'sound59']
	],
	channel_max: 10 // number of channels
};

function FlashSound(flashId, src, playerId, interval){
	this.flashId = flashId;
	this.interval = interval;
	this.player = playerId && $('#' + playerId);
	this.nosound = false;
	this.src = src;
	
	if (this.player) {
		$('div.play a', this.player).click($.proxy(this.play, this));
		$('div.pause a', this.player).click($.proxy(this.pause, this));
		$('div.stop a', this.player).click($.proxy(this.stop, this));
	}
}

$.extend(FlashSound.prototype, {
	initFlash: function(listenerName){
		var me = this;
		
		var flashvars = {
			listener: listenerName,
			interval: this.interval
		};
		var params = {
			movie: rootPath + "audio/player_mp3_js.swf",
			allowscriptaccess: "always"
		};
		var attributes = {
			id: this.flashId
		};
		swfobject.embedSWF(params.movie, this.flashId, "1", "1", "9.0.0", rootPath + "lib/swfobject/expressInstall.swf", flashvars, params, attributes, function(e){
			if (!e.success) {
				if (me.player) {
					me.player.remove();
					$('#' + me.flashId).css('left', 0);
				}
				me.nosound = true;
			}
			else {
				me.flaObj = e.ref;
			}
		});
	},
	
	onInit: function(){
		this.position = 0;
	},
	
	onUpdate: function(){
		if (!this.player) {
			return;
		}
		var isPlaying = (this.isPlaying == "true");
		$('div.play a', this.player).css('display', (isPlaying) ? "none" : "block");
		$('div.pause a', this.player).css('display', (isPlaying) ? "block" : "none");
		
		var timelineWidth = 72;
		var sliderWidth = 0;
		var sliderPositionMin = 0;
		var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
		var sliderPosition = sliderPositionMin + Math.round((timelineWidth - sliderWidth) * this.position / this.duration);
		
		if (sliderPosition < sliderPositionMin) {
			sliderPosition = sliderPositionMin;
		}
		if (sliderPosition > sliderPositionMax) {
			sliderPosition = sliderPositionMax;
		}
		
		$('div.playerslider', this.player).css('width', sliderPosition + "px");
	},

	play: function(){
		if (this.position === 0) {
			this.flaObj.SetVariable("method:setUrl", this.src);
		}
		this.flaObj.SetVariable("method:play", "");
		this.flaObj.SetVariable("enabled", "true");
	},
	
	pause: function(){
		this.flaObj.SetVariable("method:pause", "");
	},
	
	stop: function(){
		this.flaObj.SetVariable("method:stop", "");
	},
	
	play_event_sound: function(s){
		if (this.nosound) {
			return;
		}
		this.flaObj.SetVariable("method:stop", "");
		if (this.currentAudio != s) {
			this.flaObj.SetVariable("method:setUrl", this.src[s]);
			this.currentAudio = s;
		}
		this.flaObj.SetVariable("method:play", "");
		this.flaObj.SetVariable("enabled", "true");
	}
});

$(function(){
	/******************************
	 *           AUDIO
	 ******************************/
	window.radioSound = new FlashSound('radio', Pennente.pubbl, 'player', 500);
	radioSound.initFlash('radioSound');
	
	var myAudio = document.createElement('audio');
	var ext, i;
	
	if (myAudio) {
		// Currently canPlayType(type) returns: "", "maybe" or "probably"
		if (!!myAudio.canPlayType && myAudio.canPlayType('audio/x-wav') !== "") {
			ext = "wav";
		}
		else 
			if (!!myAudio.canPlayType && myAudio.canPlayType('audio/mpeg') !== "") {
				ext = "mp3";
			}
			else 
				if (!!myAudio.canPlayType && myAudio.canPlayType('audio/ogg') !== "") {
					ext = "ogg";
				}
	}
	if (myAudio && myAudio.canPlayType && ext) { // HTML5
		for (i = 0; i < Pennente.auFiles.length; i++) {
			$('<audio id="' + Pennente.auFiles[i][0] + '" src="' + rootPath + 'audio/' + Pennente.auFiles[i][1] + '.' + ext + '" preload="auto"></audio>').appendTo(document.body);
		}
		
		// globals
		Pennente.audiochannels = [];
		Pennente.soundQue = {};
		
		for (var a = 0; a < Pennente.channel_max; a++) { // prepare the channels
			Pennente.audiochannels[a] = {
				channel: new Audio(), // create a new audio object
				finished: -1 // expected end time for this channel
			};
		}
		
		play_multi_sound = function(s){
			for (var a = 0; a < Pennente.audiochannels.length; a++) {
				var now = new Date(), o = Pennente.audiochannels[a], channel = o.channel, audio = document.getElementById(s);
				if (o.finished < now.getTime()) { // is this channel finished?
					o.finished = now.getTime() + audio.duration * 1000;
					channel.src = audio.src;
					channel.load();
					channel.play();
					break;
				}
			}
		};
		
		play_event_sound = function(s){
			if (!Pennente.soundQue[s]) {
				function cb(){
					delete Pennente.soundQue[s];
					play_multi_sound(s);
				}
				Pennente.soundQue[s] = window.setTimeout(cb, 100);
			}
		};
		
		play_sound = function(s){
			document.getElementById(s).play();
		};
	}
	else {
		// globals
		var audiofiles = {};
		for (i = 0; i < Pennente.auFiles.length; i++) {
			audiofiles[Pennente.auFiles[i][0]] = rootPath + 'audio/' + Pennente.auFiles[i][1] + '.mp3';
		}
		$('<div style="width:0; height:0;" id="mouseevents"></div>').appendTo(document.body);
		
		window.soundEvents = new FlashSound('mouseevents', audiofiles);
		soundEvents.initFlash('soundEvents');
		
		window.play_event_sound = function(s){
			soundEvents.play_event_sound(s);
		};
		
	}
	
	/******************************
	 *          LEFT MENU
	 ******************************/
	$('#leftmenu a[rel]').mouseover(function(){
		var off = $(this).offset();
		$('#' + $(this).attr('rel')).css('display', 'block').css('top', (off.top - 3) + 'px');
	}).mouseout(function(){
		$('#' + $(this).attr('rel')).css('display', 'none');
	}).click(function(e){
		e.preventDefault();
	});
	$('div.sm').mouseover(function(){
		$(this).css('display', 'block');
	}).mouseout(function(){
		$(this).css('display', 'none');
	});
	$('#leftmenu a, div.sm a').mouseover(function(){
		play_event_sound('multiaudio5');
	});
	
	/******************************
	 *          THUMBS
	 ******************************/
	$('ul.thumbs a[rel=group]').mouseover(function(){
		play_event_sound('multiaudio4');
	});
});
