/*
	Copyright (c) 2006-2007, ePages GmbH
	All Rights Reserved.

	epages.cartridges.de_epages.content.widget.ToolNewPage $Revision: 1.14 $

*/
dojo.provide("epages.cartridges.de_epages.content.widget.ToolMovePage");
dojo.require("epages.widget.LocalizedWidget");

dojo.declare(
	"epages.cartridges.de_epages.content.widget.ToolMovePage",
	[epages.widget.LocalizedWidget],
	{
		/**
		 * public properties
		 */
		uri           : '?',
		objectTreeId  : 'contentExplorerObjectTree',
		classStoreId  : 'contentClassStore',
		objectStoreId : 'objectStore',
		imagePath     : epages.themeUrl('images'),
		horizontalMovement: true,//is indent and outdent allowed, feature dependent

		/**
		 * private properties
		 */
		 _classStore  : undefined,
		 _objectStore : undefined,
		 _objectTree  : undefined,

		_upClickConnect: undefined,
		_downClickConnect: undefined,
		_leftClickConnect: undefined,
		_rightClickConnect: undefined,

		/**
		 * widget properties
		 */
		templatePath:    dojo.moduleUrl('epages.cartridges.de_epages.content.widget', 'templates/ToolMovePage.html'),
		translationName: dojo.moduleUrl('epages.cartridges.de_epages.content.widget', 'templates/translation'),

		postCreate: function() {
			this.inherited("postCreate", arguments);
			dojo.subscribe(this.objectTreeId+'/select', this, '_onSelectObject');
			this._classStore = dojo.getObject(this.classStoreId);
			this._objectStore = dojo.getObject(this.objectStoreId);
			this._objectTree = $$(this.objectTreeId);
			if(this.horizontalMovement == false){
				dojo.addClass(this.indentNode, 'HideElement');
				dojo.addClass(this.outdentNode, 'HideElement');
			}
		},

		objectTree: function () {
			if (!this._objectTree) {
				this._objectTree = $$(this.objectTreeId);
				if (!this._objectTree){
					console.warn('%s is not a tree', this.objectTreeId);
				}
			}
			return this._objectTree;
		},

		_onMoveDown: function () { this.objectTree().moveDownCurrent();this._onSelectObject(this.objectTree().lastFocused);},
		_onMoveUp:   function () { this.objectTree().moveUpCurrent();  this._onSelectObject(this.objectTree().lastFocused);},
		_onOutdent:  function () { this.objectTree().outdentCurrent(); this._onSelectObject(this.objectTree().lastFocused); delete this._objectStore.indentNode;},
		_onIndent:   function () { this.objectTree().indentCurrent();  this._onSelectObject(this.objectTree().lastFocused); delete this._objectStore.indentNode;},

		_onSelectObject:function(node){
			// move down
			if (this._objectStore.canMoveItem(1, node.item)&&this.canMoveItem(1,node.item)) {
				if (this._downClickConnect === undefined) {
					dojo.removeClass(this.moveDownNode.parentNode,'DisabledButton');
					this._downClickConnect = this.connect(this.moveDownNode.parentNode,'click', '_onMoveDown')[0];
				}
			} else {
				if (this._downClickConnect !== undefined) {
					dojo.addClass(this.moveDownNode.parentNode,'DisabledButton');
					dojo.disconnect(this._downClickConnect);
					this._downClickConnect = undefined;
				}
			}
			// move up
			if (this._objectStore.canMoveItem(-1, node.item) && this.canMoveItem(-1,node.item)) {
				if (this._upClickConnect === undefined) {
					dojo.removeClass(this.moveUpNode.parentNode,'DisabledButton');
					this._upClickConnect = this.connect(this.moveUpNode.parentNode,'click', '_onMoveUp')[0];
				}
			} else {
				if (this._upClickConnect !== undefined) {
					dojo.addClass(this.moveUpNode.parentNode,'DisabledButton');
					dojo.disconnect(this._upClickConnect);
					this._upClickConnect = undefined;
				}
			}
			// outdent
			if (this._objectStore.canOutdentItem(node.item) && this.canOutdentItem(node.item)) {
				if (this._outdentClickConnect === undefined) {
					dojo.removeClass(this.outdentNode.parentNode,'DisabledButton');
					this._outdentClickConnect = this.connect(this.outdentNode.parentNode,'click', '_onOutdent')[0];
				}
			} else {
				if (this._outdentClickConnect !== undefined) {
					dojo.addClass(this.outdentNode.parentNode,'DisabledButton');
					dojo.disconnect(this._outdentClickConnect);
					this._outdentClickConnect = undefined;
				}
			}
			// indent
			if (this._objectStore.canIndentItem(node.item) && this.canIdentItem(node.item)) {
				if (this._indentClickConnect === undefined) {
					dojo.removeClass(this.indentNode.parentNode,'DisabledButton');
					this._indentClickConnect = this.connect(this.indentNode.parentNode,'click', '_onIndent')[0];
				}
			} else {
				if (this._indentClickConnect !== undefined) {
					dojo.addClass(this.indentNode.parentNode,'DisabledButton');
					dojo.disconnect(this._indentClickConnect);
					this._indentClickConnect = undefined;
				}
			}
		},

		canIdentItem: function(item) {
			var upper = this._findCloseByVisibleItem(item,-1);
			if(upper == undefined){
				return null;
			}
			var upperClass = this._classStore.fetchItemByIdentity(upper.classId);

			return null != $A(upperClass.contentChildClasses).find(item.classId);
		},

		canOutdentItem: function(item) {
			var parent = this._objectStore.fetchItemByIdentity(item.parentId);
			var parentClass = this._classStore.fetchItemByIdentity(parent.classId);

			return null != $A(parentClass.contentChildClasses).find(item.classId);
		},

		_findCloseByVisibleItem: function(item,step){
			//summary: find item that is close to the given one
			//step is the movment defines if the item cames before or after the current item
			var parent = this._objectStore.fetchItemByIdentity(item.parentId);
			var childrenIds = parent.children;
			var foundItem = $A(childrenIds).find(item.objectId);
			var upper = this._objectStore.fetchItemByIdentity(childrenIds[foundItem+step]);
			if(upper == undefined){
				return undefined;
			}
			if(upper.isDeleted == false){
				return upper;
			}
			else{
				return this._findCloseByVisibleItem(upper);
			}
		},

		canMoveItem:function(step,item){
			var upper = this._findCloseByVisibleItem(item,step);
			if(upper == undefined){
				return false;
			}
			else{
				return true;
			}
		}
	}
);