// ---------- dialog pro pridavani / odebirani tagu -----------
function photoDialog_tags(iIdentify, iReadOnly) {
	this.inheritFrom = photoDialog;
	this.inheritFrom();

	this.tags = null; // tag builder
	this.readOnly = iReadOnly;
	
	this.tagBox = null;
	this.tagList = null;
	
	this.identify = iIdentify;
	this.lastSelectionCount = 0;
	this.lastTagIndex = 0;

	// ---------- override -----------
	this.inicialize = function() {
		this.beforeState = "dialog_state1";
		
		this.tagBox  = document.getElementById(this.identify + "dialog_tagBox");
		this.tagList = document.getElementById(this.identify + "dialog_tagList");
		
		this.tags = new tagBuilder( this, this.tagList );
	}
	
	this.execute = function() {
		this.commandName = "photo_tags";
		this.additionalParams = "\"" + this.tagBox.value + "\"";
		var cmd = this.composeCommand();
		
		if (exec == null) {
			alert("Chyba!");
			this.toSelect();
		} else this.execCmd(cmd);
	}
	
	this.finish = function(success) {
		if (this.finishFlag) {
            //if (success) {
                if (this.tags.addTagsNum( this.tags.getTagArray(this.tagBox.value), this.lastSelectionCount ))
                    this.tags.updateTagListCont("dialog", this.readOnly);
            //}

			this.tagBox.value = "";
			this.finishFlag   = false;
		}
	}

	this.finishQuiet = function(success) {
		if (this.finishFlag) {
            if (success) {
    		    this.tags.remTag(this.lastTagIndex);
    		    this.tags.updateTagListCont("dialog", this.readOnly);
    		    this.lastTagIndex = 0;
		    }

			this.finishFlag = false;
		}
	}

	this.updateSelection = function(iRef) {
		this.lastSelectionCount = dialogs.collection.length;
		if (this.lastSelectionCount == 0) {
			this.setState("dialog_state1");
		} else {
			this.setState("dialog_state2");
		}

		this.tagsUpdate(iRef, this.lastSelectionCount <= 1);
	}
	// -------------------------------

    this.addTags = function(iTags) {
        this.tags.addTags( iTags );
        this.tags.updateTagListCont("dialog", this.readOnly);
    }

	this.tagsUpdate = function(iRef, forceChange) {
        if (iRef != null) {
            var changed = false;

    		if (!iRef.selected) changed = this.tags.addTags( iRef.tags.tagList );
    		else changed = this.tags.remTags( iRef.tags.tagList );

            if (changed || forceChange)
    		    this.tags.updateTagListCont("dialog", this.readOnly);
		}
	}
	
	this.remSpecificTag = function(tagIndex) {
		this.commandName = "photo_tagsRem";
		this.additionalParams = "\"" + this.tags.tagList[tagIndex] + "\"";
		var cmd = this.composeCommand();

		if (exec == null) alert("Chyba!");
		else {
            this.execCmd(cmd);
            this.lastTagIndex = tagIndex;
        }
	}
}

