var IsSoundManager = Class.extend({

	init: function() {
		this.vVolume = 60;
		this.sounds = 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);
		}
	},
	
	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();
	},
	
	resume: function(url) {
		this.pause();
		this.sounds[url].resume();
	},
	
	pause: function() {
		soundManager.pauseAll();
	},
	
	stop: function() {
		soundManager.stopAll();
	}

});


var IsPlayerCake = IsSoundManager.extend({
		
	init: function(prop) {
		this.cakes = new Array();
		this.links = new Array();
		
		this.prop = prop;

		this._super();
	},
	
	addLink: function(element) {
		var that = this;
		var $element = $(element);
		var href = $element.attr('href');

		$element.wrapInner('<div class="isPlayerOrigContent"></div>');

		var canvas = $(document.createElement('canvas'))
						.attr('width', this.prop.radius*2)
						.attr('height', this.prop.radius*2)
						.attr('id', 'isPlayer_' + this.sounds.length);
		$element.append(canvas);

		this.cakes[href] = this.cakes[href] || new this.cake(canvas[0], this.prop);
		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;
		});
	},
	
	fPlayEffect: function () {
		var l = this.bytesLoaded / this.bytesTotal;
		isSoundManager.cakes[isSoundManager.current].setPos(l, {
			color: isSoundManager.prop.loadColor,
			radius: isSoundManager.prop.innerRadius - isSoundManager.prop.lineWidth + 1});
		
		isSoundManager.cakes[isSoundManager.current].paused = 0;
		var p = this.position / this.durationEstimate;
		isSoundManager.cakes[isSoundManager.current].setPos(p, {
			color: isSoundManager.prop.color,
			radius: isSoundManager.prop.innerRadius});
		$(isSoundManager.links[isSoundManager.current]).addClass('isPlay').removeClass('isPause');

		//Update play-time
		var tsec = this.position / 1000;
		var min = Math.floor(tsec / 60);
		var sec = Math.floor(tsec % 60);
		$('.playerTime').html(min + ':' + ((sec >= 10) ? sec : '0' + sec));
	},
	
	fLoadEffect: function() {
		var p = this.bytesLoaded / this.bytesTotal;
/*
		isSoundManager.cakes[isSoundManager.current].setPos(p, {
			color: isSoundManager.prop.loadColor,
			radius: isSoundManager.prop.innerRadius - isSoundManager.prop.lineWidth + 1});
*/
		$(isSoundManager.links[isSoundManager.current]).addClass('isLoad');
	},
	
	fLoadedEffect: function() {
		$(isSoundManager.links[isSoundManager.current]).removeClass('isLoad');
	},
	
	fPauseEffect: function() {
		isSoundManager.cakes[isSoundManager.current].paused = 1;
		isSoundManager.cakes[isSoundManager.current].pauseOn = 0;
		isSoundManager.cakes[isSoundManager.current].runPause(this.position / this.durationEstimate, isSoundManager.prop.pauseColor);
		$(isSoundManager.links[isSoundManager.current]).addClass('isPause');
	},
	
	fResumeEffect: function() {
		isSoundManager.cakes[isSoundManager.current].clear();
		var p = this.position / this.durationEstimate;
		isSoundManager.cakes[isSoundManager.current].setPos(p);
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPause');
	},
	
	fStopEffect: function() {
		isSoundManager.cakes[isSoundManager.current].paused = 0;
		isSoundManager.cakes[isSoundManager.current].clear();
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPlay').removeClass('isPause');
	},

	fFinishEffect: function() {
		isSoundManager.stop();
		isSoundManager.cakes[isSoundManager.current].paused = 0;
		isSoundManager.cakes[isSoundManager.current].clear();
		$(isSoundManager.links[isSoundManager.current]).removeClass('isPlay').removeClass('isPause');
	},
	
	cake: Class.extend({

		init: function(canvas, prop) {
			this.vCanvas = canvas;
			this.currentPos = 0;
			
			this.prop = prop;
			this.prop.innerRadius = this.prop.radius - this.prop.lineWidth;

			if (typeof G_vmlCanvasManager != "undefined") G_vmlCanvasManager.initElement(canvas);
			
			this.ctx = canvas.getContext('2d');
			this.prop.center = [this.prop.radius, this.prop.radius];
			this.clear();
		},

		canvas: function(canvas) {
			if (canvas) this.vCanvas = canvas;
			return this.vCanvas;
		},
		
		color: function(color) {
			if (color) this.prop.color = color;
			return this.prop.color;
		},
		
		setPos: function(pos, prop) {
			var cake = this;
			var ctx = cake.ctx;
			prop = prop || {};

			ctx.strokeStyle = cake.prop.color;
			ctx.lineWidth = cake.prop.lineWidth || 1;
			ctx.beginPath();
			ctx.moveTo(cake.prop.center[0], cake.prop.center[1]);
			ctx.arc(
					cake.prop.center[0],
					cake.prop.center[1],
					prop.radius || cake.prop.innerRadius,
					-Math.PI * 0.5,
					Math.PI * pos * 2 - 0.5 * Math.PI,
					false
			);

			ctx.lineTo(cake.prop.center[0], cake.prop.center[1]);
			ctx.closePath();
			ctx.fillStyle = prop.color || cake.prop.color;
			ctx.fill();
			cake.currentPos = pos;
		},

		clear: function() {
			var cake = this;
			var color = cake.prop.color;

			cake.ctx.clearRect(0, 0, cake.prop.radius*2, cake.prop.radius*2);
			cake.ctx.strokeStyle = cake.prop.color;
			cake.ctx.lineWidth = cake.prop.lineWidth || 1;
			cake.ctx.beginPath();
			cake.ctx.arc(cake.prop.center[0], cake.prop.center[1], cake.prop.innerRadius, 0, Math.PI*2, false);
			cake.ctx.stroke();
		},

		runPause: function(p, color) {
			var cake = this;
			if (!cake.paused) return;

//			cake.clear();
		}
		
	})

});

var NewsPlayerPrePost = Class.extend({
	init: function() { return this; },	
	pre: function(interaction) { return this; },
	post: function(interaction) {
		if (interaction.element.id != 'play') return;
		$('#' + interaction.id).find('.isPlayLink').click();
		return this;
	}
});

/*Coverscroll*/
var initScroller = function(id) {
	if ($('#' + id + '_pane').length <= 0) $('#' + id).wrapInner('<div id="' + id + '_pane"></div>');
	var items = $('.scrollItem').length + 1;
	var iWidth = $('.scrollItem').width();
	$('#' + id + '_pane').css({position: 'absolute', width: items*iWidth });
	
	updateScroller(id, iWidth);
}
var updateScroller = function(id, iWidth) {
	var items = $('.scrollItem').length;
	
	$('#' + id + '_left').unbind('click');
	if (parseInt($('#' + id + '_pane').css('marginLeft')) >= 0) {
		$('#' + id + '_left').removeClass('enabled');
	} else {
		$('#' + id + '_left').addClass('enabled');
		$('#' + id + '_left').click(function() { scrollPrev(id, iWidth) });
	}
	
	$('#' + id + '_right').unbind('click');
	if (parseInt($('#' + id + '_pane').css('marginLeft')) + iWidth * (items-1) <= parseInt($('#' + id).width())) { 
		$('#' + id + '_right').removeClass('enabled');
	} else {
		$('#' + id + '_right').addClass('enabled');
		$('#' + id + '_right').click(function() { scrollNext(id, iWidth) })
	}
	
};
var scrollNext = function(id, iWidth) {
	$('#' + id + '_pane').animate({marginLeft: '-=' + iWidth + 'px' }, 300, 'easeOutQuint', function() {
		updateScroller(id, iWidth);
	});
};
var scrollPrev = function(id, iWidth) {
	$('#' + id + '_pane').animate({marginLeft: '+=' + iWidth + 'px' }, 300, 'easeOutQuint', function() {
		updateScroller(id, iWidth);
	});
};

$(document).ready(function() {
	soundManager.url = '/';
	$('.isPlayLink').each(function() {
		isSoundManager.addLink(this);
	});

	$('.playerVolume .mute').click(function() {
		if (isSoundManager.muted) {
			soundManager.unmute();
			isSoundManager.muted = 0;
			$('.playerVolume .mute').removeClass('on');
		} else {
			soundManager.mute();
			isSoundManager.muted = 1;
			$('.playerVolume .mute').addClass('on');
		}
	});
	$('.playerVolume .volume').click(function(e) {
		var thisX = $(this).offset().left;
		var mouseX = e.pageX;
		var percent = parseInt((mouseX - thisX) / $(this).width() * 100);
		$('.playerVolume .visible').css('width', percent + '%');
		$('.playerVolume .invisible').css('width', (100-percent) + '%');
		isSoundManager.volume(percent);
	});
	$('.shadow').wrap('<div class="_shad1"><div class="_shad2"><div class="_shad3"><div class="_shad4"></div></div></div></div>');

	initScroller('coverScroll');
});
