﻿/// <reference path="../../client/js/jquery-1.2.6.js" />
/*
* VisualTagEditor.js
* 
* Developer: Chris Webb, www.thegoodnamesweregone.com
*
* License: It's free for use commerical or otherwise. The only thing you can't do is
* claim it as your own work. Feel free to adapt. If you've got any suggestions for 
* improvements, let me know on thegoodnamesweregone.com.
*
* A link back would be nice ;-)
*
*/

var VteDelimiter = ","; // Stores tag delimiter used by VisualTagEditor. Defaults to comma.

// Finds and initialises all VisualTagEditor controls on the page
function InitVisualTagEditors(delimiter, maxTagLength) 
{
    VteDelimiter = delimiter; // set delimiter
    
    // Get all elements with "VisualTagEditor" class name
    var tagControls = $(".VisualTagEditor");


    // Setup each Tag
    for (i = 0; i < tagControls.length; i++) {

        $(tagControls[i]).data("MaxTagLength", maxTagLength); // set MaxTagLength
        $(tagControls[i]).data("LastInputLength", 0);         // set LastInputLength

        var inputEl = $(tagControls[i]).find("input:text");  // Find Text Field to attach events ...

        $(inputEl).keyup(
            function(event) {
                inputEl = event.target;
                divEl = $(inputEl).parent().get(0);

                lastInputLen = $(divEl).data("LastInputLength");
                maxTagLength = $(divEl).data("MaxTagLength");

                if (event.keyCode == 188) { //Check for comma
                    RegisterVisualEditorTag(inputEl, inputEl.value);
                } else if (event.keyCode == 8 && inputEl.value == "" && lastInputLen == 0) { // Check for deletion
                    RemoveLastVisualEditorTag(divEl);
                } else {
                    if (inputEl.value.length > maxTagLength) { // Check tag length
                        alert("Tags cannot be longer than " + maxTagLength + " characters");
                        inputEl.value = inputEl.value.substring(0, maxTagLength);
                    }
                }

                $(inputEl).data("LastInputLength", inputEl.value.length);
            }
        );
        
        $(inputEl).blur(
            function(event){
                inputEl = event.target;
                RegisterVisualEditorTag(inputEl, inputEl.value);
            }
        );

        $(tagControls[i]).click(
            function(event) {
                if (event.target.tagName == "DIV") {
                    $(event.target).find("input:text").focus();
                }
            }
        );
        
        
        // Load Pre-defined tags
        var tags = $(tagControls[i]).find("input:hidden").get(0).value;
                
        if (tags.length > 0) {
            arrTags = tags.split(delimiter); //split tags
            $(tagControls[i]).find("input:hidden").get(0).value = ""; // Clear input (it will be repopulated)
            
            ClearVisualEditorTags($(tagControls[i]));
            
            for (j = 0; j < arrTags.length; j++) //loop, add
            { 
               RegisterVisualEditorTag($(inputEl), arrTags[j]);
            }
            
        }
        
    }

}


// Checks conditions and adds new tag entered by user to the UI and underlying <input type=hidden />
function RegisterVisualEditorTag(inputEl, tagText) 
{
    var divEl = $(inputEl).parent().get(0);
    
    var hiddenInput = $(divEl).find("input:hidden");

    if (tagText.length > 0) { //Add Tag
        inputEl.value = ""; //clear textbox
        
        tagText = tagText.replace(",", "").replace(/^\s+/, '').replace(/\s+$/, ''); // Clean

        if (tagText == "" || tagText == " ") return; // No empty tags thanks

        /*        
        var AutoCapRefundef;
        if (AutoCapRef != AutoCapRefundef) {
            if (this.AutoCapRef.checked && !this.DisableCaseParsing) {
                tagText = tagText.charAt(0).toUpperCase() + tagText.substring(1);
            }
        }
        */

        var tags = $(divEl).find(".Tag");
        
        if (tags != undefined) 
        {
            var debug = "";
            
            for (i = 0; i < tags.length; i++) {
                if ($(tags[i]).text() == tagText) {
                    return; // don't add existing tags
                }
            }

            if (tags.length == $(divEl).data("MaxTagLength")) {
                alert("You cannot add any more tags");
                return;
            }
        }
        
        // Update Data
        if (hiddenInput.get(0).value != "") hiddenInput.get(0).value += ",";
        hiddenInput.get(0).value += tagText;
        
        // Update UI
        $(inputEl).before("<a class=\"Tag\" href=\"javascript:\" title=\"Click to delete\" onclick=\"RemoveVisualEditorTag(this);\" >" + tagText + "</a>")
    }
    
}

// Removes a Tag from the UI and data field
function RemoveVisualEditorTag(aEl)
{
    var deletingTag = $(aEl).text();

    var hiddenInput = $(aEl).parent().find("input:hidden");

    arrTags = hiddenInput.get(0).value.split(VteDelimiter); //split tags

    var tagFound = false;
    for (i = 0; i < arrTags.length; i++) {
        if (arrTags[i] == deletingTag) {
            tagFound = true;
        }
        if (tagFound && i != arrTags.length-1) {
            arrTags[i] = arrTags[i + 1];
        }
    }
    arrTags.pop();

    hiddenInput.get(0).value = arrTags.join(VteDelimiter);
    
    $(aEl).remove();
}

// Removes last Tag <a> from the UI
function RemoveLastVisualEditorTag(divEl) 
{
    RemoveVisualEditorTag($(divEl).find("a:last"));
}

// Removes all Tags (<a>) from the UI
function ClearVisualEditorTags(divEl)
{
    var tags = $(divEl).find("a.Tag");

    if (tags != undefined) {
        for (i = 0; i < tags.length; i++) {
            RemoveVisualEditorTag(tags[i]);
        }
    }
    
}