$(document).ready(function() {    
    $('#analysis-fav').click(function () {
        var t = 'a';
        if ($(this).hasClass('active')) {
            t = 'r';
            $(this).removeClass('active');
            $(this).attr('title', 'Als Favorit speichern');
        } else {
            t = 'a';
            $(this).addClass('active');
            $(this).attr('title', 'Aus Favoriten entfernen');
        }

        $.ajax({
            type: "POST",
            url: "/request/user/favoriteWebsite.php",
            data: "w="+$.data($(this)[0], 'w')+"&t="+t
        });
        return false;
    });
});

function showDiv (id) {
    $('#'+id).get(0).style.display = 'block';
    $('#'+id).get(0).style.visibility = 'visible';
}

function hideDiv (id) {
    $('#'+id).get(0).style.display = 'none';
    $('#'+id).get(0).style.visibility = 'hidden';
}

//function $ (id) {
//    var e = document.getElementById(id);
//    return e ? e : false;
//}

function redirect (url) {
    window.location.href = url;
}

function parseCSVToArray(str) {
    var arr = str.split("\n");
    var result = new Array();
    for (i = 0; i < arr.length; i++) {
        result = result.concat(arr[i].split(","));
    }
    var result2 = array_unique(result);
    result2.sort();
    return result2;
}

function arrayToLower(arr) {
    var str = arr.join("###;;###");
    str = str.toLowerCase();
    return str.split("###;;###");
}

function showPhraseList(inputId, maxNumKeywords) {
    var input = $('#'+inputId).val();
    
    var output = $('#phrases-add-list').get(0);
    var phrases = parseCSVToArray(input);
    phrases = arrayToLower(phrases);
    var str = '';
    var count = 0;
    for (i in phrases) {
        if (phrases[i].length > 0) {
            str += '<li>'+phrases[i]+'</li>';
            count++;
        }
        if (count == 50)
            break;
    }
    output.innerHTML = str;

    var rem = $('#phrases-remaining').get(0);
    rem.style.fontWeight = 'bold';

    var maxNum = maxNumKeywords ? maxNumKeywords : 50;
    var remaining = maxNum-count;
    if (remaining == 0) {
        rem.style.color = 'red';
    } else if (remaining <= 5) {
        rem.style.textDecoration = 'underline';
        rem.style.color = '';
    } else {
        rem.style.color = '';
    }
    rem.innerHTML = (maxNum-count)+' verbleibend';
}

function reportEditDeletePhrase (phraseId) {
    $('#phrasesDelete').get(0).value += phraseId+',';
    $('#phraseList-'+phraseId+'-keywords').get(0).style.textDecoration = 'line-through';
    $('#phraseList-'+phraseId+'-action').get(0).onclick = function() {
        reportEditDeletePhraseRemove (phraseId);
    };
    $('#phraseList-'+phraseId+'-action').get(0).firstChild.src = '/images/silk/arrow_refresh_small.png';
    $('#phraseList-'+phraseId+'-action').get(0).title = 'Keyword nicht aus Bericht entfernen';
}

function reportEditDeletePhraseRemove (phraseId) {
    var arr = $('#phrasesDelete').value.split(',');
    var res = new Array();
    for (index in arr) {
        if (arr[index] != phraseId && arr[index] != '')
            res.push(arr[index]);
    }
    $('#phraseList-'+phraseId+'-keywords').style.textDecoration = 'none';
    $('#phraseList-'+phraseId+'-action').onclick = function() {
        reportEditDeletePhrase (phraseId);
    };
    $('#phraseList-'+phraseId+'-action').firstChild.src = '/images/silk/cross.png';
    $('#phraseList-'+phraseId+'-action').title = 'Keyword aus Bericht entfernen';
    $('#phrasesDelete').value = res.join(',')+',';
}





function array_unique (array) {
    // Removes duplicate values from array
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/array_unique
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +      input by: duncan
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nate
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael Grier
    // %          note 1: the second argument, sort_flags is not implemented
    // *     example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
    // *     returns 1: ['Kevin','van','Zonneveld']
    // *     example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
    // *     returns 2: {'a': 'green', 0: 'red', 1: 'blue'}

    var key = '', tmp_arr1 = new Array(), tmp_arr2 = new Array();
    var val = '';
    tmp_arr1 = array;

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if ((haystack[fkey] + '') === (needle + '')) {
                return fkey;
            }
        }
        return false;
    };

    for (key in tmp_arr1) {
        val = tmp_arr1[key];
        if (false === __array_search(val, tmp_arr2)) {
            tmp_arr2[key] = val;
        }

        delete tmp_arr1[key];
    }

    return tmp_arr2;
}

