var Favorites = {
	initialize: function () {
		this.favorites = new Array ();
		this.element = $('favorites_head');
		this.element.onclick = this.show.bindAsEventListener(this);
		dnd.registerDrop (this);
		this.alertObject = new ElementHintFading ( {upwards:true} );
		this.element.appendChild(this.alertObject.element);
	},

	show: function () {
		document.title = "My Favorite Videos | iDesktop.tv";
		rc.showFavorites = true;
		rc.loadResults({total:this.favorites.length, results: this.favorites});
	},

	addVideo: function (jsonObj, showMessage) {
		if (arguments.length == 1) var showMessage = true;
		if (!user_name) {
			showLoginOrRegisterForm();
			return;
		}
		for (var i = 0; i < this.favorites.length; i ++)
			if (this.favorites[i].id == jsonObj.id) {
				alert ('You have already added this video to your favorites!');
				return;
			}
		for (var i = this.favorites.length; i > 0; i --) {
			this.favorites[i] = this.favorites[i-1];
			this.favorites[i].ord = i;
		}
		jsonObj.ord = 0;// this.favorites.length;
		jsonObj.t = "v";
		this.favorites[0] = jsonObj;
		urchinTracker('favorties/add/' + jsonObj.id);
		new Ajax.Request ('favorites_add.php', {
			method:'post',
			parameters: "jsonObj=" + escape(VideoToJSONString(jsonObj)) + "&id=" + jsonObj.id + "&ispr=" + (jsonObj.isprivate?'1':'0')
		});
		this.showCount();
		if (showMessage) {
			//alert ('The video was added to your favorites');
			this.alertObject.show('The video has been added to your favorites');
		}
		if (rc.showFavorites) {
			rc.loadResults({total:this.favorites.length, results: this.favorites});
		}
	},

	removeVideo: function (jsonObj) {
		this.favorites = this.favorites.without(jsonObj);
		urchinTracker('favorties/remove/' + jsonObj.id);
		new Ajax.Request('favorites_remove.php', {
			method:'post',
			parameters: "id=" + jsonObj.id
		})
		this.showCount();
	},

	userHasLoggedIn: function () {
		this.element.style.display = 'inline';
		this.reloadFavorites();

	},

	userHasLoggedIn_handler: function (ajaxResponse) {

	},

	userHasLoggedOut: function () {
		this.element.style.display = 'none';
		this.favorites.length = 0;
		this.showCount();
	},

	clearFavorites: function () {
		if (!user_name) {
			alert ('You have to log in first!');
			return;
		}
		if (confirm ('Are you sure you want to delete all your favorite videos?')) {
			new Ajax.Request('clear_favorites.php', {});
			this.favorites.length = 0;
			this.showCount();
		}
	},

	reloadFavorites: function () {
		this.favorites.length = 0;
		this.showCount();
		this.loadPage(0);
	},
	loadPage: function (page) {
		new Ajax.Request ('favorites_get.php?page=' + page, {
			method:'post',
			onSuccess: this.loadPage_handler.bind(this)
		});
	},

	loadPage_handler: function (ajaxResponse) {
		var t = ajaxResponse.responseText;
		var jsonObj = null;
		eval ('jsonObj = ' + t);
		if (jsonObj != null) {
			//we should sort them by ord, but the trick is that order by ord is similar to order by id desc, which is done
			// in the query;)
			for (var i = 0; i < jsonObj.results.length; i ++) {
					jsonObj.results[i].t = "v";
					this.favorites.push(jsonObj.results[i]);
			}
			//this.showCount();
		}
		if (!jsonObj.isLastPage)
			this.loadPage(jsonObj.page + 1);
		else
			this.showCount();
	},

	dragHover: function (event, draggedObject) {
		dnd.setText('add to favorites');
	},

	dragUnhover: function() {

	},

	recieveDrag: function (draggedObject) {
		this.addVideo(draggedObject.jsonObj);
	},
	showCount: function () {
		$('favorites_count').innerHTML = this.favorites.length;
	}
}