﻿/** -------------------------
* favorites.js
* description: Handles functionality related to favoriting. If user is logged in, a request is made for any favoriting data. Also sets the click functionality to toggle favorite.
*/

var Vevo = window.Vevo || {};

Vevo.Favorites = (function() {
	var SERVICE = Vevo.Proxy.Favorites;

	function getFavorites($nodes) {
		var favorites = {
			video: [],
			artist: [],
			playlist: [],
			channel: []
		};

		$nodes.each(function(i,nodeObj) {
			var rel = $(nodeObj).attr('rel').split(':'), fav = favorites[rel[0]];
			/* console.log(fav, favorites, rel[0], rel[1], $(nodeObj), nodeObj) */
			if (typeof fav !== 'undefined') {
				var idx = fav.length, id = rel[1];
				if ($.inArray(id, fav) < 0) { fav.push(id); }
			}
		});

		if (favorites.video.length > 0) { SERVICE.getFavorites(favorites.video, 'video'); }
		if (favorites.artist.length > 0) { SERVICE.getFavorites(favorites.artist, 'artist'); }
		if (favorites.playlist.length > 0) { SERVICE.getFavorites(favorites.playlist, 'playlist'); }
		if (favorites.channel.length > 0) { SERVICE.getFavorites(favorites.channel, 'channel'); }
	} // getFavorites;

	function hoverOn(ev) {
		var t = $(this);
		t.find('.favorData').addClass('ui-hide');
		if (t.hasClass('un-favoritable')) {
			t.find('.favorClear').removeClass('ui-hide');
		}
		else { t.find('.favorTip').removeClass('ui-hide'); }
	} // hoverOn(ev)

	function hoverOff(ev) {
		var t = $(this);
		t.find('.favorData').removeClass('ui-hide');
		t.find('.favorTip, .favorClear').addClass('ui-hide');
	} // hoverOff(ev)

	return {
		initFav: function(scope){
			var $favoritable = $('.favoritable',scope).hover(hoverOn, hoverOff);

			$favoritable.find('.favorData',scope).each(function() {
				$(this).text(Vevo.Util.formatNum($(this).text(), true));
			});
			if (Vevo.user.isLoggedIn) {
				//console.info('Initializing Favorites');
				getFavorites($favoritable);

			}
		},
		updateFavoriteHandler: function (e, obj){
			var $this = obj.$node;
			/* update favorite count display & state display
				must be in a loop because there might be the same playlist/video/etc on the same page!!!! (--petey)
			*/
			$this.each(function(i,t){
				var $t = $(t);
				var fav;
				if (obj.isFav) {
					fav=$t.hasClass('un-favoritable')?0:1;
					$t.addClass('un-favoritable').attr('title', $t.attr('title').replace('favor', 'unfavor'));
				}
				else {
					fav=!$t.hasClass('un-favoritable')?0:-1;
					$t.removeClass('un-favoritable').attr('title', $t.attr('title').replace('unfavor', 'favor'));
				}

				var $favData = $t.find('.favorData').removeClass('ui-hide');
				$t.find('.favorTip, .favorClear').addClass('ui-hide');
				$favData.text(Vevo.Util.formatNum(Math.max(0, Number(Vevo.Util.formatNum($favData.text(), false)) + fav), true));
			});
		},
		init: function(e, user) {
			Vevo.Favorites.initFav();
			$(Vevo).bind('updatedFavorite', Vevo.Favorites.updateFavoriteHandler);
			$('.favoritable').live('click', function(ev) {
				ev.preventDefault();
				ev.stopPropagation();
				if (Vevo.user.isLoggedIn) { SERVICE.toggleFavorite(this); }
				else { Vevo.messageButNotLoggedIn.show("Favorites and Recommendations", "You will need to be signed in to favorite or get recommendations."); }
			});
		} // Favorites.init
	}; 
})();
/* Init is now called from global.js: Vevo.recommendations.finished.execChec */
