var arrTags = new Array();
var address = "/ws/Metadataplus.asmx/GetItemsBy4Metadata1Wildcard?sitekit_SiteID=2598&metadata_field1=Dbase.Published&metadata_value1=Published&metadata_field2=&metadata_value2=&metadata_field3=&metadata_value3=&metadata_field4=&metadata_value4=&wildmeta_field=Dbase.Keywords&orderby=ID&sitekit_GroupID=&wildmeta_value=";
var numberOfSizes = 6;
var arrTagsOnly;
var debugVar;

/*main function to populate the tags*/
function tagCloud() {
    if (typeof (tagCloudType) != 'undefined') {
        if (tagCloudType == "libraryPage") {
            //setTimeout('tagsLibraryPage(resourceViewTags)', 200);
            if (typeof (resourceViewTags) != 'undefined') {
                tagsLibraryPage(resourceViewTags);
            }
        }
        if (tagCloudType == "defaultPage") {
            selectedTags();
        }
        if (tagCloudType == "getAllTags") {
            getAllTags();
        }
    }
}

/*Functions to populate the tags cloud on the library page*/
function tagsLibraryPage(rawTags) {
    //split the tags string into seperate tags and store them in an array
    rawTags = rawTags.replace("undefined", "")
    arrTagsOnly = rawTags.split(",");

    arrTagsOnly = arrTagsOnly.stripWhiteSpace();
    arrTagsOnly = arrTagsOnly.unique();
    arrTagsOnly = arrTagsOnly.clean('');
    arrTagsOnly = arrTagsOnly.clean(' ');
    
    //turn the arrTags into an Array of Arrays to hold both the tag name and number
    //of occurances
    var tagLimit = arrTagsOnly.length;
    if (tagLimit > 25) {
        tagLimit = 25;
    }
    
    for (i = 0; i < tagLimit; i++) {
        if (i < 25) {
            if (!((arrTagsOnly[i] == ' ') && (arrTagsOnly[i] == ''))) {
                //add to the array of arrays
                arrTags[i] = new Array(4);
                arrTags[i][0] = arrTagsOnly[i];

                var thisTag = arrTagsOnly[i];
                //populate the array of arrays
                arrTags[i][1] = countTagOccurances(thisTag);
            }
        }
        
    }

    //get the maximum number of tags
    var highestTagCount = 1;
    for (i = 0; i < arrTags.length; i++) {
        
        if (arrTags[i][1] > highestTagCount) {
            if (!(arrTags[i][0] == ' ')) {
                highestTagCount = arrTags[i][1];
            }
        }
    }
    //calculate the relative size of each tag and assign it a value
    for (i = 0; i < arrTags.length; i++) {
        var place = 0;

        place = arrTags[i][1] * (numberOfSizes / highestTagCount);

        if (place > 5) {
            arrTags[i][2] = "CommonTag1";
        }
        if (place > 4) {
            arrTags[i][2] = "CommonTag2";
        }
        else if (place > 3) {
            arrTags[i][2] = "CommonTag3";
        }
        else if (place > 2) {
            arrTags[i][2] = "CommonTag4";
        }
        else if (place > 1) {
            arrTags[i][2] = "CommonTag5";
        }
        else {
            arrTags[i][2] = "CommonTag6";
        }
    }

    //generate the HMTL
    var tagCloudHTML;
    tagCloudHTML = '<div class="CommonTagCloud">';
    for (i = 0; i < arrTags.length; i++) {
        arrTags[i][0] = arrTags[i][0].replace(/^\s+|\s+$/g, '');
        tagCloudHTML += '<span class="' + arrTags[i][2] + '">';
        //tagCloudHTML += '<a href="/search-results.htm?orderby=ID&metadata_field2=&metadata_value2=&metadata_field3=&metadata_value3=&metadata_field4=&metadata_value4=&wildmeta_field=Dbase.Keywords&wildmeta_value=' + arrTags[i][0] + '" rel="tag"> ';
        tagCloudHTML += '<a href="/by-tag.htm?metadata_field=Dbase.Keywords&metadata_value=' + arrTags[i][0] + '" rel="tag"> ';
        tagCloudHTML += arrTags[i][0];
        tagCloudHTML += ' </a>';
        tagCloudHTML += '</span>';
    }
    tagCloudHTML += '</div>';
    tagCloudHTML = tagCloudHTML.replace("undefined", "")
    if (tagCloudHTML.length > 0) {
        document.getElementById("cccTagCloud").innerHTML = tagCloudHTML;
    }
    else {
        selectedTags();
    }

}

function selectedTags() {
    //get the maximum number of tags
    var highestTagCount = 1;
    for (i = 0; i < SelectedTags.length; i++) {
        if (SelectedTags[i][1] > highestTagCount) {
            highestTagCount = SelectedTags[i][1];
        }
    }
    //calculate the relative size of each tag and assign it a value
    for (i = 0; i < SelectedTags.length; i++) {
        var place = 0;

        place = SelectedTags[i][1] * (numberOfSizes / highestTagCount);

        if (place > 5) {
            SelectedTags[i][2] = "CommonTag1";
        }
        if (place > 4) {
            SelectedTags[i][2] = "CommonTag2";
        }
        else if (place > 3) {
            SelectedTags[i][2] = "CommonTag3";
        }
        else if (place > 2) {
            SelectedTags[i][2] = "CommonTag4";
        }
        else if (place > 1) {
            SelectedTags[i][2] = "CommonTag5";
        }
        else {
            SelectedTags[i][2] = "CommonTag6";
        }
    }

    //generate the HMTL
    var tagCloudHTML;
    tagCloudHTML = '<div class="CommonTagCloud">';
    for (i = 0; i < SelectedTags.length; i++) {
        tagCloudHTML += '<span class="' + SelectedTags[i][2] + '">';
        tagCloudHTML += '<a href="/by-tag.htm?metadata_field=Dbase.Keywords&metadata_value=' + SelectedTags[i][0] + '" rel="tag"> ';
        tagCloudHTML += SelectedTags[i][0];
        tagCloudHTML += ' </a>';
        tagCloudHTML += '</span>';
    }
    tagCloudHTML += '</div>';
    tagCloudHTML = tagCloudHTML.replace("undefined", "")

    document.getElementById("cccTagCloud").innerHTML = tagCloudHTML;
    document.getElementById("divTagsTitle").innerHTML = "Selected Tags";
}

function getAllTags() {
    document.getElementById("cccTagCloud").innerHTML = resourceViewTags;
}
//OUTSIDE FUNCTIONS

//perform an XML request
/*
function countTagOccurances(tag) {
var count;
//get an xml doc of all occurances of that tag
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var xmlDocAddress = address + arrTagsOnly[i];
xmlhttp.open("GET", address + arrTagsOnly[i], false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

count = xmlDoc.getElementsByTagName("tblDownloads").length;
return count;
}
*/
/*
function rand(l, u) // lower bound and upper bound
{
return Math.floor((Math.random() * (u - l + 1)) + l);
}
function countTagOccurances(tag) {
return rand(1, 6);
}
*/
function countTagOccurances(tag) {
    var substrings = tagsDbase.split(tag);
    //alert("tag:" + tag + ": " + substrings.length);
    return substrings.length;

}

//Make an Array Unique
/*
Array.prototype.unique = function() {
    var r = new Array();
    o: for (var i = 0, n = this.length; i < n; i++) {
        for (var x = 0, y = r.length; x < y; x++) {
            if (r[x] == this[i]) {
                continue o;
            }
        }
        r[r.length] = this[i];
    }
    return r;
}
*/

Array.prototype.unique = function() {
    var newArray = new Array();
    label: for (var i = 0; i < this.length; i++) {
        for (var j = 0; j < newArray.length; j++) {
            if (newArray[j] == this[i])
                continue label;
        }
        newArray[newArray.length] = this[i];
    }
    return newArray;
}



Array.prototype.stripWhiteSpace = function() {
    for (var i = 0; i < this.length; i++) {
        this[i] = this[i].replace(/^\s+|\s+$/g, '');
    }
    return this;
}




//remove empty items from an array
Array.prototype.clean = function(deleteValue) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == deleteValue) {
            this.splice(i, 1);
            i--;
        }
    }
    return this;
};

