/* Copyright (c) 2006-2007, Apple Inc. All rights reserved. */
function prepare(inAlwaysRun) {
	if (window.unitTestHandler && (!inAlwaysRun)) return true;
	server();
	serverui();
	gPoofManager = new PoofManager();
	gNotifier = new Notifier();
	gPopupManager = new PopupManager();
	gToolbar = new Toolbar();
	gTagPageController = new TagPageController();
	gSearchPopup = new SearchPopup();
}

function documentShouldUnload() {
	if (window.gTagPageController && window.gTagPageController.mEditMode) {
		return Loc.unloadConfirm;
	}
}

var TagPageController = Class.create();
TagPageController.prototype = {
	initialize: function() {
		bindEventListeners(this, [
			'handleTagClick', 'handleTagFieldChange',
			'handleEditButtonClick', 'handleDoneButtonClick', 'handleCancelButtonClick'
		]);
		this.mElement = $('alltags');
		this.mAllTags = {};
		this.mAllTags = $A(this.mElement.getElementsByTagName('a')).each(function(a) {
			this.mAllTags[Element.firstNodeValue(a)] = a;
		}.bind(this));
		this.addToolbarItems();
		Object.extend($('editing_document_title'), {value:Loc.tags_edit_tags, readOnly:true, disabled:true});
		gToolbar.mChildren.edit.setCallback(this.handleEditButtonClick);
		gToolbar.mChildren.add.setEnabled(false);
		gToolbar.mChildren.remove.setEnabled(false);		
	},
	addToolbarItems: function() {
		var tb = gToolbar.mEditToolbar;
		// add cancel and done buttons
		var ul = gToolbar.addButtonsToToolbar(tb, ['done_button','cancel_button']);
		ul.className = 'tbactions';
		// hook up buttons
		gToolbar.setToolbarButtonCallback('done_button', this.handleDoneButtonClick);
		gToolbar.setToolbarButtonCallback('cancel_button', this.handleCancelButtonClick);
	},
	draw: function() {
		var elm = Builder.node('ul');
		newTags = {};
		$H(this.mTags).each(function(tag) {
			newTags[tag.key] = Builder.node('a', {href:CollabUID.sharedInstance().mBaseLocation+'search/?tag='+tag.key}, [tag.key]);
			newTags[tag.key].onclick = this.handleTagClick;
			elm.appendChild(Builder.node('li', [newTags[tag.key]]));
		}.bind(this));
		replaceElementContents(this.mElement, elm);
	},
	switchToEditor: function() {
		this.mEditMode = true;
		this.mChanges = [];
		this.mTags = {};
		$A(this.mElement.getElementsByTagName('a')).each(function(a) {
			a.onclick = this.handleTagClick;
			this.mTags[Element.firstNodeValue(a)] = a;
		}.bind(this));
		this.mOrigTags = Object.extend({}, this.mTags);
		gToolbar.expandToolbar();
	},
	switchToDisplay: function() {
		this.mEditMode = false;
		$A(this.mElement.getElementsByTagName('a')).each(function(a) {
			a.onclick = '';
		});
		gToolbar.collapseToolbar();
	},
	handleTagClick: function(inEvent) {
		var a = Event.findElement(inEvent, 'a');
		this.showRenameDialog(Element.firstNodeValue(a), a);
		return false;
	},
	handleEditButtonClick: function() {
		serverui().ensureLogin(this.switchToEditor.bind(this), 'admin');
		return false;
	},
	handleDoneButtonClick: function() {
		if (this.mChanges.length > 0) {
			var callback = function() {
				dialogManager().hide();
				this.switchToDisplay();
				gNotifier.print('tags_saved_message');
			}
			dialogManager().showProgressMessage('tags_save_progress');
			server().settings.manageTags(callback.bind(this), uid().mBasePath, this.mChanges);
		}
		else {
			this.switchToDisplay();
		}
		return false;
	},
	handleCancelButtonClick: function() {
		this.mTags = this.mOrigTags;
		this.draw();
		this.switchToDisplay();
		return false;
	},
	handleTagFieldChange: function() {
		$('tags_rename_dialog_ok').value = Loc[($F('tags_rename_dialog_name') == '' ? 'tags_rename_dialog_delete' : 'tags_rename_dialog_ok')];
	},
	showRenameDialog: function(inTagName, inOverElement) {
		if (!this.mRenameDialog) {
			this.mRenameDialog = dialogManager().drawDialog('tags_rename_dialog', [
				{label:'tags_rename_dialog_name', contents:'<input type="text" id="tags_rename_dialog_name" />'}
			], 'tags_rename_dialog_ok');
			this.mTagSearchField = new TagSearchField('tags_rename_dialog_name');
			observeEvents(this, 'tags_rename_dialog_name', {change:'handleTagFieldChange', keyup:'handleTagFieldChange'});
		}
		$('tags_rename_dialog_name').value = inTagName;
		var okCallback = function() {
			var newName = $F('tags_rename_dialog_name');
			// bail if no change
			if (newName == inTagName) {
				return true;
			}
			// add change to list
			this.mChanges.push([inTagName, newName]);
			// deleted tag?
			if (newName == '') {
				gPoofManager.showOverElement(this.mTags[inTagName], this.draw.bind(this));
				delete this.mTags[inTagName];
			}
			// merge or rename
			else {
				this.mTags[newName] = this.mTags[inTagName];
				delete this.mTags[inTagName];
				this.draw();
			}
		}
		dialogManager().show(this.mRenameDialog, null, okCallback.bind(this));
		targetedDialogManager().show(this.mRenameDialog, null, okCallback.bind(this), inOverElement);
	}
}

// use tag-specific strings
Loc.tooltips.edit_button = Loc.tooltips.edit_button_tags;

if (window.loaded) loaded('tags.js');