function trim(str) {
  return str.replace(/^\s*|\s*$/g, '');
}

function sortKeyword(a, b) {
  var x = a.kwd;
  var y = b.kwd;
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function randOrd(){
	return (Math.round(Math.random())-0.5); 
} 

//
// Parameters
// ----------
// -keywordList : ^ delimited list of words listed in descending order of popularity.
// -urlList     : ^ delimited list of the URLs corresponding to the words listed in keywordList.
// -mode        : initialized to 'init' the first time generateCloud is being called.
// -basefontsize: initialized to the value of the reference font size (unit is px) from which the calculated font size of each tag is calculated from
// -fontsizediff: initialized to the multiplication factor the calculated font size is based on: the bigger the value, the greater the difference in 
//                font sizes between two consecutive popular words.  Note: this assumes that the list of popular words provided to the function,
//                keywordList is sorted in descending order of popularity. 
//
function generateCloud(keywordList, urlList, mode, basefontsize, fontsizediff) {
    var keywords = new Array;
    var urls     = new Array;
   
    keywords = keywordList.split("^");
    urls     = urlList.split    ("^");
    
    var sortedkeywords = new Array;
    
    // We need to store the original order of the keywords in the list to use as the popularity ranking
    for (var k = 0; k < keywords.length; ++k) {
        sortedkeywords[k] = { kwd:trim(keywords[k])
                            , order:k
                            , url:urls[k]
                            };
    }
    
    var cloudHTML = '';
    
    //sortedkeywords.sort(sortKeyword);
    sortedkeywords.sort(randOrd);
    
    for (var k = 0 ; k < sortedkeywords.length ; k++) {
        var fontsize = basefontsize - parseInt(sortedkeywords[k]['order'] * fontsizediff);
        if ( fontsize < 11 ) {
          fontsize = 11;
        }
        
        cloudHTML += '<a href="'+ sortedkeywords[k]['url'] + '"'
                   + ' title="search for ' + sortedkeywords[k]['kwd']
                   + '" style="font-size:' + fontsize + 'px'+ ';line-height:' + fontsize + 'px">'
                   + sortedkeywords[k]['kwd']
                   + '</a>&nbsp;&nbsp;&nbsp;\n';
    }
    document.getElementById('hotsearches').innerHTML = cloudHTML;

    if (mode != 'init') {
        updatePreviewColors();
        updatePreviewFonts();
    }
}

function sli_suggestions_flow(flow, resultsPerPage) {
  var display_value = (flow == 'list') ? 'inline' : 'block';

  for ( var i = 1 ; i < (resultsPerPage + 1) ; i++ ) {
    if ( (suggestionsParent = document.getElementById('sli_search_suggestions_' + i)) ) {
      suggestionsParent = suggestionsParent.firstChild;

      for ( var j = 0 ; j < suggestionsParent.childNodes.length ; j++ ) {
        if ( suggestionsParent.childNodes[j].className == 'sli_suggestion' ) {
          //suggestionsParent.childNodes[j].setAttribute('style', 'display: ' + display_value);
          suggestionsParent.childNodes[j].style.display = display_value;
        }
      }
    }
  }
}
