//****************************************************************************
// Copyright (C) thePlatform for Media, Inc. All Rights Reserved.
//****************************************************************************

// a reliable global variable that holds the widget
var tpFavoritesWidget;

// A constant that holds the name of the "Favorites" category in the CategoryList control.
var tpFavoritesCategoryName = "Favorites";

if (window.addEventListener){
	window.addEventListener("load", setupFavoriteWidget, true);
} else if (window.attachEvent){
	window.attachEvent("onload", setupFavoriteWidget );
}

function checkFavorites(evt) {
	tpFavoritesWidget.handleMediaChange(evt);
}
function showFavorites(evt){
	tpFavoritesWidget.handleCategoryChange(evt);
}
function clearFavorites(evt){
	tpFavoritesWidget.handleReleaseChange(evt);
}
function setupFavoriteWidget() {
	
	// Set up widget and register it with the widget manager.
	tpFavoritesWidget = new CommunityManagerFavorite( tpCommunityWidgetManager.getCommunityManager() );
	tpCommunityWidgetManager.registerWidget( tpCommunityWidgetManager.favoritesWidget);
	
	// Load it up with data.
	tpFavoritesWidget.activate();
	
	// Register for changes to other state on the page.
	tpCommunityWidgetManager.getCommunityManager().registerForSignIn(tpFavoritesWidget.handleSignIn.bindTo(tpFavoritesWidget));
	tpCommunityWidgetManager.getCommunityManager().registerForSignOut(tpFavoritesWidget.handleSignOut.bindTo(tpFavoritesWidget));
	
	// Register for PDK player events.
	tpController.addEventListener("OnMediaStart", checkFavorites);
	tpController.addEventListener("OnCategorySelected", showFavorites);
	tpController.addEventListener("OnReleaseStart", clearFavorites);
}

/* FAVORITES WIDGET OBJECT */
CommunityManagerFavorite = function() {
	this._initialize.apply(this, arguments);
}

CommunityManagerFavorite.prototype = {
	
	_initialize: function( communityManagerInstance ) {
		this.ratingsContainer = j$(tpCommunityWidgetManager.getWidgetDiv( tpCommunityWidgetManager.ratingWidgetTypeName ))[0];
		this.cmInstance = communityManagerInstance;
	},
	/*
	* Puts the favorites icon into the Release widget. Loads the UserList for this user.
	*/
	activate: function() {
		this.favoriteURIs = new Array();
		this.cmInstance.getUserList("tp_favorites", "1.0", tpUserListsDataServicePrefix, this.receiveUserList.bindTo( this ));
	},
	/* 
	* Process the data service response
	*/
	receiveUserList: function( userListObject ) {
		
		if(typeof userListObject.isException != 'undefined' && Boolean(userListObject) == true) {
			this.cmInstance.errorHandler(userListObject);
			return false;
		}
		if ( userListObject.items.length > 0) {
			this.hasFavoriteList = true;
			this.favoriteListURI = userListObject.items[0].ID;
			this.favoriteURIs = userListObject.items[0].itemIDs;
		} else {
			this.hasFavoriteList = false;
			this.favoriteURIs = new Array();
		}
		// Detect if we've arleady loaded, if so, update the UI.
		if ( j$("#communityManagerFavoriteIcon").length > 0 )
			this.setFavoritesIcon(j$("#communityManagerFavoriteIcon")[0].name);
		
		if ( this.currentCategory == tpFavoritesCategoryName ) {
			this.updateReleaseModel();
		}
	},
	handleReleaseChange: function(evt) {
		if( j$("#communityManagerFavoriteIcon").length > 0) {
			var contentIndex = this.cmInstance.getContentIndex(evt);
			if (contentIndex >= 0) {
				this.clip = event.data.clips[contentIndex];
				var mediaID = tpMediaDataServicePrefix + tpMediaDataServicePath + "/" + evt.data.clips[contentIndex].baseClip.contentID;
				this.setFavoritesIcon(mediaID);
				j$("#communityManagerFavoriteIcon").show();
			} else {
				j$("#communityManagerFavoriteIcon").hide();
			}	
		}
	},
	handleSignIn: function(evt) {
		this.cmInstance.getUserList("tp_favorites", "1.0", tpUserListsDataServicePrefix, this.receiveUserList.bindTo( this ));
	},
	handleSignOut: function(evt) {
		this.cmInstance.getUserList("tp_favorites", "1.0", tpUserListsDataServicePrefix, this.receiveUserList.bindTo( this ));
	},
	/*
	* As the Media that is playing back changes we'll need to update the favorites icon to be a plus or a minus.
	*/
	handleMediaChange: function(evt) {
		if (evt.data.baseClip.isAd) {
			// Ad logic
			//j$("#communityManagerFavoriteIcon").hide();
		} else {
			this.clip = evt.data;
			var mediaID = tpMediaDataServicePrefix + tpMediaDataServicePath + "/" + evt.data.baseClip.contentID;
			this.setFavoritesIcon(mediaID);
			j$("#communityManagerFavoriteIcon").show();			
		}
	},
	/*
	* Tracks the state of the categories widget.
	*/
	handleCategoryChange: function(evt) {
		var oldCategory = this.currentCategory;
		this.currentCategory = evt.data;
		
		if (this.currentCategory == tpFavoritesCategoryName ) {
			this.updateReleaseModel();
		}
	},
	handleAddFavorite: function (evt) {
		var self = evt.data;
		var target = evt.currentTarget||evt.target;
		tpFavoritesWidget.addMediaToFavorites( target.name );
		tpFavoritesWidget.setFavoritesIcon(target.name);
		if ( tpFavoritesWidget.currentCategory == tpFavoritesCategoryName ) {
			tpFavoritesWidget.updateReleaseModel();
		}
	},
	handleRemoveFavorite: function (evt) {
		var self = evt.data;
		var target = evt.currentTarget||evt.target;
		tpFavoritesWidget.removeMediaFromFavorites( target.name );
		tpFavoritesWidget.setFavoritesIcon(target.name);
		if ( tpFavoritesWidget.currentCategory == tpFavoritesCategoryName ) {
			tpFavoritesWidget.updateReleaseModel();
		}
	},
	handleCreateFavoriteListResponse: function( evt ) {
	
	},
	/*
	* If the Favorites Category is currently showing, update the underlying release model, otherwise noop.
	*/
	updateReleaseModel: function() {
		var IDArray = this.getFavoritesAsMediaIDs();
		var query = "query=ContentIDs|";
		if ( IDArray.length > 0 ) {
			for ( var i = 0; i < IDArray.length; i++) {
				query = query + IDArray[i].substring(IDArray[i].lastIndexOf("/")+1) + ",";
			}
			// Remove the extra comma at the end.
			query = query.substring(0, query.length - 1);
		} else {
			query = query + "0";
		}
		tpController.refreshReleaseModel("", "", null, null, null, [query]);
	},
	/*
	* Updates the icon for whatever is appropriate given URI passed in.
	*/
	setFavoritesIcon: function( objectURI ) {
		var inFavorites = this.getFavoritesAsURIs( objectURI ).length != 0;
		if ( inFavorites ) {
			favoritesIcon = DomFragment("div", {
				title: "Remove from Favorites",
				id: "communityManagerFavoriteIcon",
				name: objectURI,
				className: "removeFromFavorites"
			});
		} else {
			favoritesIcon = DomFragment("div", {
				title: "Add to Favorites",
				id: "communityManagerFavoriteIcon",
				name: objectURI,
				className: "addToFavorites"
			});
		}
		if( j$("#communityManagerFavoriteIcon").length > 0) {
			j$("#communityManagerFavoriteIcon").replaceWith(favoritesIcon);
		} else {
			this.ratingsContainer.appendChild( favoritesIcon );
		}
		
		if ( inFavorites ) {
			j$("#communityManagerFavoriteIcon").bind( "click", this, this.handleRemoveFavorite );
		} else {
			j$("#communityManagerFavoriteIcon").bind( "click", this, this.handleAddFavorite );
		}
	},
	/*
	* Looks for the URI in the current Favorites model and removes it in memory and then sends the call to the data service to delete the item.
	*/
	removeMediaFromFavorites: function( objectURI ) {
		this.favoriteURIs = jQuery.grep(this.favoriteURIs, function (a) { return a.indexOf(objectURI) == -1; });
		// update persistence.
		if ( this.hasFavoriteList ) {
			this.cmInstance.updateUserList( this.favoriteListURI, "Favorites","tp_favorites", this.favoriteURIs, "1.0", tpUserListsDataServicePrefix, null)
		    tpController.dispatchEvent("OnDeleteFavorite", {clip: this.clip});
		}
	},
	/*
	* Looks for the URI in the current Favorites model and adds it in memory and then sends the call to the data service to add the item to the UserList.
	*/
	addMediaToFavorites: function( objectURI ) {
		this.favoriteURIs = [ objectURI ].concat( this.favoriteURIs );
		// update persistence.
		if ( this.hasFavoriteList ) {
			this.cmInstance.updateUserList( this.favoriteListURI, "Favorites","tp_favorites", this.favoriteURIs, "1.0", tpUserListsDataServicePrefix, null)
		} else {
			this.cmInstance.addUserList( "Favorites", "tp_favorites", this.favoriteURIs,"1.0", tpUserListsDataServicePrefix, this.handleCreateFavoriteListResponse)
		}
		tpController.dispatchEvent("OnAddFavorite", {clip: this.clip});
	},
	/*
	* Return back an  array of object URIs. Returns all the URIs if the filterPrefix param is null.
	*/
	getFavoritesAsURIs: function( filterPrefix ) {
		if ( filterPrefix != null && this.favoriteURIs && this.favoriteURIs.length > 0) {
			var returnArray = jQuery.grep(this.favoriteURIs, function (a) { return a.indexOf(filterPrefix) == 0; });
			return returnArray;
		} 
		return this.favoriteURIs;
	},
	/*
	* Return back an  array of content IDs for Media objects in the currently loaded favorites list.
	*/
	getFavoritesAsMediaIDs: function() {
		return this.getFavoritesAsURIs( tpMediaDataServicePrefix + tpMediaDataServicePath );
	}
}
	
	
	
	
	