// -------- trida pro praci s tagy ---------
function tagBuilder(iCreator, iContainer) {
	this.tagString = "";
	this.tagList = new Array();
	this.tagIndex = new Array(); // indexovani poctu tagu

	this.tagListCont = iContainer; // div zobrazujici mnozinu tagu
	this.creator = iCreator;

	this.clearTags = function() {
        this.tagIndex = new Array();
		this.tagList  = new Array();
	}
	
	this.getTagArray = function(iTags) {
        var tArray = new Array();

		if (iTags != "") {
			var pos = 0;
			var lastWord = "";
			var aktChar = "";

			while (pos <= iTags.length) {
				aktChar = (pos == iTags.length ? " " : iTags.substr(pos, 1));

				switch (aktChar) {
                    case "!" :
                    case "?" :
                    case ";" :
                    case "$" :
                    case "#" :
                    case "*" :
                    case "," :
                    case "<" :
                    case ">" :
                        break;

                    case "-" :
                    case "_" :
                    case "." :
					case " " :
						if (lastWord != "") {
                            var founded = false;
                            var subPos = 0;
                            
                            while (subPos < tArray.length && !founded)
                                if (tArray[subPos] == lastWord) founded = true;
                                else subPos++;
                                
                            if (!founded) tArray[tArray.length] = lastWord;
							lastWord = "";
						}
						break;

					default :
						lastWord += aktChar;
						break;
				}
				pos++;
			} // while
		}
		
		return tArray;
    }

	this.updateTags = function(iTags) {
		this.clearTags();
		this.tagString = iTags;
		this.tagList = this.getTagArray(iTags);
	}

    this.tagIndex = function(iTag) {
		var founded = false;
		var pos     = 0;

		while (pos < this.tagList.length && !founded)
			if (this.tagList[pos].toLowerCase() == iTag) founded = true;
			else pos++;

		return founded ? pos : -1;
    }
    
    this.remTag = function(iIndex) {    
        var sf = 0;
        
        this.tagIndex[this.tagList[iIndex]] = 0;

        for (sf = iIndex; sf < this.tagList.length - 1; sf++)
            this.tagList[sf] = this.tagList[sf + 1];
        return this.tagList.pop();
    }

    this.addTags = function(iTagList) { return this.addTagsNum(iTagList, 1); }
    this.remTags = function(iTagList) { return this.remTagsNum(iTagList, 1); }

	this.addTagsNum = function(iTagList, iNum) {
		// vlozi dalsi mnozinu tagu
		var pos = 0;
		var f;
		var changed = false;

		for (f = 0; f < iTagList.length; f++) {
            pos = this.tagIndex(iTagList[f]);

			if (pos == -1) {
				this.tagList[this.tagList.length] = iTagList[f];
				this.tagIndex[iTagList[f]] = iNum;
                changed = true;
			} else {
                this.tagIndex[iTagList[f]] += iNum;
            }
		}

		return changed;
	}

	this.remTagsNum = function(iTagList, iNum) {
        // odebere mnozinu tagu
        var pos = 0;
        var founded = false;
        var sf = 0;
        var changed = false;

        for (var f = 0; f < iTagList.length; f++) {
            pos = this.tagIndex(iTagList[f]);

            if (pos != -1) {
                this.tagIndex[iTagList[f]] += - iNum;
                if (this.tagIndex[iTagList[f]] <= 0) {
                    // vymaz
                    this.remTag(pos);
                    changed = true;
                }
            } // if
        } // for

        return changed;
    }

	this.updateTagListCont = function(iHrefMethod, iReadOnly) {
		var toPut = "";
		if (this.tagList.length > 0) {
			toPut += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left : 6px;margin-bottom : 10px\">";
			for (var f = 0; f < this.tagList.length; f++) {
				toPut += "<tr>";
				toPut += "<td><img src=\"images/photo/icon_tagsTiny.gif\" alt=\"\" border=\"0\"/></td>";
				toPut += "<td style=\"padding-left : 7px;padding-right : 7px\">" + this.tagList[f] + "</td>";
				if (!iReadOnly) {
    				toPut += "<td>";
    				switch (iHrefMethod) {
    					case "dialog" :
    						toPut += "<a href=\"javascript:dialogs.getDialog('" + this.creator.identify + "').remSpecificTag(" + f + ")\" class=\"tinyCommentGray\">(x)</a>";
    						break;
    				}
    				toPut += "</td>";
				}
                toPut += "</tr>";
			}
			toPut += "</table>";
		} else toPut = "<div style=\"text-align : center\" class=\"tinyCommentGray\">Fotografie namá zatím žádné tagy.</div>";
		this.tagListCont.innerHTML = toPut;
	}
}

