/*jslint laxbreak:true */
/*jslint laxcomma:true */
/*jslint loopfunc:true */
/*jslint strict:true */
/*jslint browser:true */
/*jslint devel:true */
define([
	"underscore"
	, "jquery"
	, "backbone"

	, "modules/webaudio"

	, "text!../../templates/player/layout.html"
	, "text!../../templates/player/player-status.html"
]
, function (
_
, $
, Backbone

, WebAudio

, tmplPlayerLayout
, tmplPlayerStatus
) {
	"use strict";

	if (!_.isFunction(Array.prototype.shuffle)) {
		Array.prototype.shuffle = function() {
			var len	= this.length
			,	i	= len
			;

			if ( len === 0 ) {
				return false;
			}

			while (i--) {
				var p = parseInt(Math.random()*len, 10);
				var t = this[i];
				this[i] = this[p];
				this[p] = t;
			}
		};
	}

	var

	Player		= Backbone.View.extend({
		elViewport:					null
		, $elViewport:				null

		, playIndex:				[]

		, playCursor:				0

		, webAudio:					null

		, _stateRandom:				null

		, _stateRepeat:				null

		, _stateMute:				null

		, _stateVolume:				null

		, _stateAudio:				null

		, _state:					null

		, _states:				{
			stopped:				0
			, loading:				1
			, playing:				2
			, paused:				3
		}

		, events:				{
			//"click #add-friend":  "showPrompt",
			"click div:first-child > a.buttonPrev":
									"__buttonPrevious"
			, "click div:first-child > a.buttonNext":
									"__buttonNext"
			, "click div:first-child > a.buttonPlay":
									"__buttonPlayPause"
			// Volume Down
			, "click div:first-child > .playerVolumeWrapper > a:first-child":
									"__buttonVolumeDown"
			// Volume Up
			, "click div:first-child > .playerVolumeWrapper > a:last-child":
									"__buttonVolumeUp"
			// Volume click
			, "click div:first-child > .playerVolumeWrapper > input":
									"__buttonVolumeClick"
			// Seek input
			, "click div:last-child > .playerProgressWrapper > progress":
									"__buttonSeek"
		}

		, initialize:				function (options) {
			var that	= this
			;

			_.bindAll(this
// Public
				, "setPlayIndex"
				, "setRandomState"
				, "setRepeatState"
				, "playItem"
// Events
				, "__buttonPlayPause"
				, "__buttonNext"
				, "__buttonPrevious"
				, "__buttonVolumeUp"
				, "__buttonVolumeDown"
				, "__buttonVolumeClick"
				, "__buttonSeek"
// Private
				, "_waCreate"
				, "_waBindEvents"
				, "__waStateChanged"
				, "_playerEnded"
				, "_playerLoading"
				, "_playNext"
				, "_playPrevious"
				, "_playerPause"
				, "_playerStop"
				, "_playerStart"
				, "_setVolume"
				, "_getItemData"
				, "_getItemMediaUrl"
				, "_setViewport"
				, "_updateViewportProgress"
			);

			Player.__super__.initialize.apply(that);

			that.$el.html(tmplPlayerLayout);

			that.elViewport		= document.createElement("div");
			that.$elViewport	= $(that.elViewport);

			that.$elViewport.html(_.template(tmplPlayerStatus)({}));

			that.$el.append(that.elViewport);

			that._waCreate();

			return that;
		}

/**
 * PUBLIC
**/
		, setPlayIndex:				function (index) {
			var that			= this
			;

			that._playIndex		= index;
			that._playCursor	= 0;

			if (that._stateRandom) {
				that.setRandomState(that._stateRandom);
			}

			return that._playIndex;
		}

		, setRandomState:			function (random) {
			var that					= this
			;

			if (random === true) {
				that._playIndex.shuffle();
			}

			// TODO: Set cursor back to the item that was playing

			return that._stateRandom	= random;
		}

		, setRepeatState:			function (repeat) {
			var that	= this
			;

			return that._stateRepeat	= repeat;
		}

		, playItem:					function (item) {

		}
/**
 * EVENTS
**/
		, __buttonPlayPause:		function (event) {
			var that	= this
			;

		}

		, __buttonNext:				function (event) {
			var that	= this
			;

		}

		, __buttonPrevious:			function (event) {
			var that	= this
			;

		}

		, __buttonVolumeUp:			function (event) {
			var that	= this
			;

		}

		, __buttonVolumeDown:		function (event) {
			var that	= this
			;

		}

		, __buttonVolumeClick:		function (event) {
			var that	= this
			;

		}

		, __buttonSeek:				function (event) {
			var that	= this
			;

		}

/**
 * PRIVATE
**/
// Audio Player
		, _waCreate:				function () {
			var that	= this
			;

			return that.webAudio	= new WebAudio();
		}

		, _waBindEvents:			function () {

		}

		, __waStateChanged:			function () {
			var that	= this
			;

		}

// Player Controls
		, _playerEnded:				function () {
			var that	= this
			;

		}

		, _playerLoading:			function () {
			var that	= this
			;

		}

		, _playNext:				function () {
			var that	= this
			;

		}

		, _playPrevious:			function () {
			var that	= this
			;

		}

		, _playerPause:				function () {
			var that	= this
			;

		}

		, _playerStop:				function () {
			var that	= this
			;

		}

		, _playerStart:				function () {
			var that	= this
			;

		}

		, _setVolume:				function (volume) {
			var that	= this
			;

		}

// Viewport methods
		, _getItemData:				function (item) {
			var that	= this
			;

		}

		, _getItemMediaUrl:			function (item) {
			var that	= this
			;

		}

		, _setViewport:				function (item) {
			var that	= this
			;

		}

		, _updateViewportProgress:	function (progress, time) {
			var that	= this
			;

		}
	});

	return Player;
});