﻿//KeyCodes upArrow: 38 ; downArrow: 40
//=====================================

//      put scrollbar position at focused suggestion

//Fires when Key is down
function intelliSearch(dataString) {
    this.dbString = dataString;
    this.suggestbox = document.getElementById("suggestbox");
    this.searchfield = document.getElementById("ctl00_cphContent_searchfield"); //formerly searchfield
   
    this.ArrowPointer;
    //Normal Style init
    this.listStyle = "style='font-size:90%; list-style:none;'";
    if (keyValidation() == 5){
            if ((searchfield.value == "") || (searchfield.value.length < 2)) {
                suggestbox.style.display = "none";
            }
            else {
                suggestbox.style.display = "block";
                this.ArrowPointer = Infinity;
                fillsuggest();
            }
     }
     //Arrows up or down
     else if ((keyValidation() == 3) || (keyValidation() == 2)) {
     clearStyleAll();
             //Suggest not yet focused
             if (ArrowPointer == Infinity) {
                 //Arrow up
                 if (keyValidation() == 3) {
                     ArrowPointer = aInputArray.length - 1;
                     listStyleOver(ArrowPointer);
                     
                 }
                 //Arrow down
                 if (keyValidation() == 2) {
                     ArrowPointer = 0;
                     listStyleOver(ArrowPointer);
                     
                 }
             }
             //Suggest focused, continue at Pointer position
             else {
                 //Arrow up
                 if (keyValidation() == 3) {
                     if (ArrowPointer != 0) {
                         listStyleOut(ArrowPointer);
                         
                         ArrowPointer = ArrowPointer - 1;
                         listStyleOver(ArrowPointer);
                         
                     }
                     else {
                         listStyleOut(ArrowPointer);
                         ArrowPointer = Infinity;
                     }
                 }
                 //Arrow down
                 if (keyValidation() == 2) {
                     if (ArrowPointer != aInputArray.length - 1) {  
                         listStyleOut(ArrowPointer);
                         
                         ArrowPointer = ArrowPointer + 1;

                         listStyleOver(ArrowPointer);
                         
                     }
                     else {
                         listStyleOut(ArrowPointer);
                         ArrowPointer = Infinity;
                     }
                 }
             }
               
     }
     return searchfield.value;
}

//Fill suggestbox
function fillsuggest() {
    suggestbox.innerHTML = "<ul class='suggestUl'>";
    getCharsSearch();
    this.q = 0;
    this.aInputArray = new Array();
    this.avatarArray = new Array();
    //Var to check whether suggestbox is in use or not
    this.suggestBoxInNeed = false;
            //Compare entry with database to create suggestion
            for (var s = 0; s < aSearchArray.length; s++){
                if (suggestString[s].toLowerCase() == searchfield.value.toLowerCase()) {
                        suggestBoxInNeed = true;
                        //Mirror positive hits in new Array 
                        aInputArray[q] = aSearchArray[s];
                        avatarArray[q] = aAvatarArray[s];
                        q++;
                    }
                }
                //Undisplay suggestbox in case of not having positive hits
                if (suggestBoxInNeed == false) {
                    suggestbox.style.display = "none";
                }
                //Create suggestion string for each result
                this.boldPart = new Array();
                this.boldPartTemp = new Array();
                this.normalPart = new Array();
                this.normalPartTemp = new Array();
                
                //Button to close suggestionbox
                suggestbox.innerHTML += "<p class='close' id='close' onclick='javascript:closeSuggestBox();'><span style='visibility: hidden;'>X</span></p>";
                closeButtonStyle();
                
                for (var k = 0; k < aInputArray.length; k++) {
                    normalPartTemp[k] = "";

                    boldPartTemp[k] = aInputArray[k].split("", searchfield.value.length);
                    boldPart[k] = "";
                    for (y = 0; y < searchfield.value.length; y++) {
                        boldPart[k] += boldPartTemp[k][y];
                    }
                    //put Array parts in Temp-vars to allow LowerCase search
                    this.inputTemp = aInputArray[k];
                    this.searchValueTemp = searchfield.value;
                    inputTemp.toString();
                    searchValueTemp = searchValueTemp.toLowerCase();
                    inputTemp = inputTemp.toLowerCase();
                    boldPart[k].toString();
                    
                    normalPart[k] = inputTemp.replace(searchValueTemp, ""); 
                    
                        normalPartTemp[k] += normalPart[k];

                        suggestbox.innerHTML += "<li class='mouseli' id='" + k + "' onmouseover='javascript:clearStyleAll();listStyleOver(" + k + ");' onclick='javascript:mouseclickli(" + k + ");' onmouseout='javascript:listStyleOut(" + k + ");' " + listStyle + "><IMG src='" + getWebrootPath() + "Images/avatars/" + avatarArray[k] + ".jpg' width='30px' height='30px' style='margin-left: 5px; padding-top: 2px;' /><span id='bold' style='font-weight: bold; margin-left: 4px;'>" + boldPart[k] + "</span>" + normalPartTemp[k] + "</li>";
                        try {
                            document.getElementById("fields").value = avatarArray[k].toString();
                        }
                        catch(ex){}
                }

     
}
//Create Chars out of Database entries to compare
function getCharsSearch() {
    //Save results in Array
    //splitt dbString
    dbString = dbString.replace(/!7/gi, "o");
    dbString = dbString.replace(/&=/gi, "@");
    dbString = dbString.replace(/%66/gi, "a");
    dbString = dbString.replace(/%20/gi, ".de");
    dbString = dbString.replace(/%46/gi, ".com");
    dbString = dbString.replace(/#77/gi, "u");
    dbString = dbString.replace(/&7895=/gi, "i");
    dbString = dbString.replace(/!54/gi, "e");
    dbString = dbString.replace(/%245/gi, "r");
    dbString = dbString.replace(/#96/gi, "t");
    dbString = dbString.replace(/!652/gi, "m");
    dbString = dbString.replace(/&10347/gi, "n");
    dbString = dbString.replace(/#98_/gi, " ");
    
    this.dbContent = dbString.split(";");
    this.dbUser = new Array();
    for (var p = 0; p < dbContent.length - 1; p++) {
        dbUser[p] = dbContent[p].split("|");
    }
    this.aSearchArray = new Array();
    this.aAvatarArray = new Array(); 
    for (var t = 0; t < dbUser.length; t++) {
        aSearchArray[t] = dbUser[t][1];
        aAvatarArray[t] = dbUser[t][0];
    }
   //splitt results in Chars
    this.propose = new Array();
    this.suggestString = new Array();
    for(var i=0; i < aSearchArray.length; i++){
        propose[i] = aSearchArray[i].split("", searchfield.value.length);
        suggestString[i] = "";
        for (var z = 0; z < searchfield.value.length; z++) {
            suggestString[i] += propose[i][z];
            
        }
        
        suggestString[i].toString();
    }
}

//Key validation
function keyValidation() {
    var event = window.event;
   //Chars and Numbers
   if ((event.keyCode > 47) && (event.keyCode < 91) || (event.keyCode == 46) || (event.keyCode == 8)) {
        return 5;
    }
    //up Arrow
    else if (event.keyCode == 38){
        return 3;
    }
     //down Arrow
    else if (event.keyCode == 40){
        return 2;
    }
    else {
        return 0;
    }
}


//Mouse click <li>
function mouseclickli(param) {
    this.mouseli = document.getElementById(param);
    searchfield.value = mouseli.innerText;
    suggestbox.style.display = "none";
}

//Style - on Mouse over, Focus
function listStyleOver(param) {
    this.listStylingOver = document.getElementById(param);
    listStylingOver.style.backgroundColor = "#B7E4FC";
    listStylingOver.style.border = "1px solid";
    listStylingOver.style.borderColor = "#8DD2F6";
    listStylingOver.style.color = "black";
    searchfield.value = listStylingOver.innerText;
   
}

//Style - on Mouse out
function listStyleOut(param) {
    this.listStylingOut = document.getElementById(param);
    listStylingOut.style.backgroundColor = "white";
    listStylingOut.style.color = "black";
    listStylingOut.style.border = "0";
}

//Clear all Focus
function clearStyleAll() {
    for (var p = 0; p < aInputArray.length; p++) {
        listStyleOut(p);
    }
}
//Close Button Style
function closeButtonStyle() {
    this.closeButton = document.getElementById("close");
    closeButton.style.fontFamily = "Arial Black";
    closeButton.style.width = "15px";
    closeButton.style.float = "right";
    closeButton.style.height = "15px";
    closeButton.style.fontWeight = "bold";
    closeButton.style.background = "url('" + getWebrootPath() + "Images/close.gif') top left no-repeat";
    closeButton.style.fontSize = "60%";
    closeButton.style.color = "white";
    closeButton.style.cursor = "pointer";
    closeButton.style.padding = "2px;"
    closeButton.style.margin = "0";
    

}

//Close Suggest
function closeSuggestBox() {
    suggestbox.style.display = "none";
}

