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

	, "text!../../templates/footer/layout.html"
]
, function (
_
, $
, Backbone
, Bootstrap

, tmplFooterLayout
) {
	"use strict";

	var Footer = Backbone.View.extend({
		events:			{
			"click button[data-target='about']":		"__eventButtonAboutClick"
			, "click button[data-target='sidebar']":	"__eventToggleButtonSideBar"
			, "click button[data-target='random']":		"__eventToggleButtonRandom"
			, "click button[data-target='repeat']":		"__eventToggleButtonRepeat"
		}

		, _stateRepeat:		false

		, _stateRandom:		false

		, initialize:		function (options) {
			_.bindAll(this
				, "__eventToggleButtonRandom"
				, "__eventToggleButtonRepeat"
				, "__eventToggleButtonSideBar"
				, "__eventButtonAboutClick"
			);

			var that	= this
			;

			Footer.__super__.initialize.apply(that, arguments);

			that.$el.html(tmplFooterLayout);

			return that;
		}

		, __eventToggleButtonRandom:	function (event) {
			var that		= this
			,	$target		= $(event.target)
			;

			that._stateRandom	= !that._stateRandom;

			if (event.target.tagName === "I") {
				$target		= $target.parent();
			}

			if (that._stateRandom) {
				$target.addClass("active");
			} else {
				$target.removeClass("active");
			}

			if (!that.__timeoutDebounceRandomButton) {
				that.__timeoutDebounceRandomButton = setTimeout(function () {
					that.trigger("toggle:random", that._stateRandom);

					delete that.__timeoutDebounceRandomButton;
				}, 500);
			}
		}

		, __eventToggleButtonRepeat:	function (event) {
			var that		= this
			,	$target		= $(event.target)
			;

			that._stateRepeat	= !that._stateRepeat;

			if (event.target.tagName === "I") {
				$target		= $target.parent();
			}

			if (that._stateRepeat) {
				$target.addClass("active");
			} else {
				$target.removeClass("active");
			}

			if (!that.__timeoutDebounceRepeatButton) {
				that.__timeoutDebounceRepeatButton = setTimeout(function () {
					that.trigger("toggle:repeat", that._stateRepeat);

					delete that.__timeoutDebounceRepeatButton;
				}, 500);
			}
		}

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

			that.trigger("toggle:sidebar");

			var $target		= $(event.target);
			if (event.target.tagName === "BUTTON") {
				$target		= $target.find("i");
			}

			$target.toggleClass("icon-chevron-right");
			$target.toggleClass("icon-chevron-left");
		}

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

			Backbone.Router.prototype.navigate("about", true);
		}
	});

	return Footer;
});