var IsSoundManager = Class.extend({

	init: function() {
		this.vVolume = 60;
		this.sounds = new Array();
		this.links = new Array();
		this.no = 0;

		soundManager.defaultOptions.whileplaying = this.fPlayEffect;
		soundManager.defaultOptions.onstop = this.fStopEffect;
		soundManager.defaultOptions.onpause = this.fPauseEffect;
		soundManager.defaultOptions.onresume = this.fResumeEffect;
		soundManager.defaultOptions.whileloading = this.fLoadEffect;
		soundManager.defaultOptions.onload = this.fLoadedEffect;
		soundManager.defaultOptions.onfinish = this.fFinishEffect;

		soundManager.debugMode = false;
	},
	
	volume: function(vol) {
		this.vVolume = vol;
		for(var i in this.sounds) {
			soundManager.setVolume(i, vol);
		}
	},
	
	addLink: function(element) {
		var that = this;
		var $element = $(element);
		var href = $element.attr('href');

		this.links[href] = element;

		$element.click(function(e) {
			if (that.current == href && that.sounds[href] && !that.sounds[href].paused) {
				isSoundManager.pause();
			} else if (that.sounds[href] && that.sounds[href].paused) {
				that.current = href;
				that.resume(href);
			} else {
				that.stop();
				that.current = href;
				that.addSound(href);
				that.play(href);
			}
			e.stopPropagation();
			return false;
		});
		
		if (this.addExtension) this.addExtension(element);
	},
	
	addSound: function(url) {
		var that = this;

		this.sounds[url] = soundManager.createSound({
			id: url,
			url: url,
			volume: that.vVolume
		});
		this.sounds[url].no = this.no++;
	},

	play: function(url) {
		if (!this.sounds[url]) addSound(url);
		this.sounds[url].play();
		if (this.playClickExtension != undefined) this.playClickExtension(url);
	},
	
	resume: function(url) {
		this.pause();
		this.sounds[url].resume();
	},
	
	pause: function() {
		soundManager.pauseAll();
	},
	
	stop: function() {
		soundManager.stopAll();
	},
	
	fPlayEffect: function () {
		var l = this.bytesLoaded / this.bytesTotal;
		var p = this.position / this.durationEstimate;
		$(isSoundManager.links[isSoundManager.current]).addClass('isPlay').removeClass('isPause');

		//Update play-time
		var tsec = this.position / 1000;
	
		if (isSoundManager.playExtension != undefined) isSoundManager.playExtension(p, tsec);
	},
	
	fLoadEffect: function() {
		var p = this.bytesLoaded / this.bytesTotal;
		$(isSoundManager.links[isSoundManager.current]).addClass('isLoad');
		
		if (isSoundManager.loadExtension != undefined) isSoundManager.loadExtension(p);
	},
	
	fLoadedEffect: function() {
		$(isSoundManager.links[isSoundManager.current]).removeClass('isLoad');
		
		if (isSoundManager.loadedExtension != undefined) isSoundManager.loadedExtension();
	},
	
	fPauseEffect: function() {
		$(isSoundManager.links[isSoundManager.current]).addClass('isPause');
		
		if (isSoundManager.pauseExtension != undefined) isSoundManager.pauseExtension();
	},
	
	fResumeEffect: function() {
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPause');

		if (isSoundManager.resumeExtension != undefined) isSoundManager.resumeExtension();
	},
	
	fStopEffect: function() {
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPlay').removeClass('isPause');
		
		if (isSoundManager.stopExtension != undefined) isSoundManager.stopExtension();
	},

	fFinishEffect: function() {
		isSoundManager.stop();
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPlay').removeClass('isPause');
		
		if (isSoundManager.finishExtension != undefined) isSoundManager.finishExtension();
	}

});

var IsPlayerBar = IsSoundManager.extend({

	init: function() {
		this.bar = new Array();
		return this._super();
	},
	
	addExtension: function(element) {
		var $soundElement = $($(element).parents('.isSoundElement')[0]);
		if ($soundElement.length <= 0) return;
		
		var $iDiv = $soundElement.find('.isSoundInteractor');
		if ($iDiv.length <= 0) return;
		
		var url = $(element).attr('href');
		
		if ($iDiv.find('.isFullBar').length <= 0) 
			$iDiv.append('<div class="isBarContainer"><div class="isFullBar"></div><div class="isLoadBar"></div><div class="isPlayBar"></div></div>');

		this.bar[url] = this.bar[url] || new Array(4);
		this.bar[url].full = $iDiv.find('.isFullBar');
		this.bar[url].load = $iDiv.find('.isLoadBar');
		this.bar[url].play = $iDiv.find('.isPlayBar');
		this.bar[url].time = $soundElement.find('.playerTime');

		$iDiv.find('.isBarContainer').css('position', 'relative');
		
		$(this.bar[url].time).html('0:00');
		
/*
		$(this.bar[url].full).click(function(e) {
				var thisX = $(this).offset().left;
				var mouseX = e.pageX;
				var decimal = (mouseX - thisX) / $(this).width();
				var position = isSoundManager.sounds[url].duration * decimal;
				isSoundManager.sounds[url].setPosition(position);
		});
*/
	},
		
	playExtension: function(p, sec) {
		//Show time-label
		var min = Math.floor(sec / 60);
		var sec = Math.floor(sec % 60);
		$(isSoundManager.bar[isSoundManager.current].time).html(min + ':' + ((sec >= 10) ? sec : '0' + sec));
		
		//Convert to pixels
		var $fullBar = $(isSoundManager.bar[isSoundManager.current].full); 
		var px = $fullBar.width() * p;

		//Set play-bar width
		$(isSoundManager.bar[isSoundManager.current].play).css('width', px + 'px').addClass('isPlaying');

		//

	},

	playClickExtension: function(url) {
		$(isSoundManager.links[url]).addClass('isPlay');
		$(".entry.nytt.artist:has(.isSound:not(.isPlay))").each(function() {
			$(this).find('.playerBackground,.playerTime,.isSoundInteractor div').fadeOut('fast');
		});

	},

	stopExtension: function() {
		//Reset time-label
		$(isSoundManager.bar[isSoundManager.current].time).html("0:00");
		
		//Set play-bar width
		$(isSoundManager.bar[isSoundManager.current].play).css('width', '0px').removeClass('isPlaying');	
	},

	finishExtension: function() {
		//Reset time-label
		$(isSoundManager.bar[isSoundManager.current].time).html("0:00");
		
		//Set play-bar width
		$(isSoundManager.bar[isSoundManager.current].play).css('width', '0px').removeClass('isPlaying');
	}		
});

/* Start bygdalarm */
$(document).ready(function() {

	is.prepare();
	
	$(".entry.nytt.artist").hover(function() {
		var $sound = $(".isSound:not(.isPlay)", this);
		if ($sound.length > 0)
			$(this).find('.playerBackground,.playerTime,.isSoundInteractor div').fadeIn('fast');
	}, function() {
		var $sound = $(".isSound:not(.isPlay)", this);
		if ($sound.length > 0)
			$(this).find('.playerBackground,.playerTime,.isSoundInteractor div').fadeOut('fast');
	});
});

$(window).load(function() {
	$('.isSound').each(function() {
		isSoundManager.addLink(this);
	});
});
