// replace $ function
var SAJ = jQuery.noConflict();

function Highlight(postingId, promoId, alt)
{
    SetClassName(postingId, alt == '1' ? 'highlightAltRow' : 'highlightRow');
    SetClassName(promoId, alt == '1' ? 'highlightAltPromoRow' : 'highlightPromoRow');
}

function UnHighlight(postingId, promoId, alt)
{
    SetClassName(postingId, alt == '1' ? 'altRow' : '');
    SetClassName(promoId, alt == '1' ? 'altRowPromo' : '');
}

function SetClassName(id, name)
{
    document.getElementById(id).className = name;
}

function setWFHhover(theRow)
{
    var defaultClass = theRow.className;
    theRow.className = defaultClass + " highlightRow";
}

function removeWFHhover(theRow)
{
    var currentClass = theRow.className;
    var newClass = currentClass.slice(0, currentClass.indexOf(" highlightRow"));
    theRow.className = newClass;
}

// randomize company results
var originalList = "";

function randomizeResults(resultID, minResults) 
{
    var listContainer = "div[id$='" + resultID + "']";

    if(SAJ(listContainer + " .item-list").length > minResults) 
    { // if there are enough results
        // add class of 'random' to container
        SAJ(listContainer).addClass("random");
    
        // get the original list
        originalList = SAJ(listContainer).html();
        
        // make an array of the list items
        var randomList = new Array();
        
        for(var x = 0; x < (minResults + 1); x++) // pull random items from original array, and put into new one
        { 
            var random = Math.floor(Math.random() * SAJ(listContainer).find(".item-list").length);
            
            randomList.push('<span class="item-list">' + SAJ(listContainer).find(".item-list:eq(" + random + ")").html() + '</span>');
            
            SAJ(listContainer).find(".item-list:eq(" + random + ")").remove();
        }
        
        // sort list
        randomList.sort(function(itemOne, itemTwo) 
        {
            var titleOne = SAJ(itemOne).text();
            var titleTwo = SAJ(itemTwo).text();
            
            if(titleOne < titleTwo) 
            {
                return -1;
            }
            else if(titleOne > titleTwo) 
            {
                return 1;
            }
            else 
            {
                return 0;
            }
        });
        
        // put new list inside container
        SAJ(listContainer).html(randomList.join("<br />"));
    }
}


// facet toggling
function collapseFacet(facetID, numToShow, moreText, lessText) 
{
    var $theFacet = SAJ("div[id$='" + facetID + "']");
    
    if($theFacet.find(".item-list").size() > numToShow) 
    {
        var $theLinks = $theFacet.find(".item-list:gt(" + (numToShow - 1) + "), br:gt(" + (numToShow - 2) + ")");
        var more = moreText + " &raquo;";
        var less = "&laquo; " + lessText;
        var collapsed = true;
    
        $theLinks.hide();
        
        SAJ('<a href="#" class="more">' + more + '</a>').click(function() 
        {
            if(collapsed == true) 
            {
                if($theFacet.hasClass("random")) // for randomized results
                { 
                    $theFacet.html(originalList).removeClass("random");
                    $theLinks = $theFacet.find(".item-list:gt(" + (numToShow - 1) + "), br:gt(" + (numToShow - 2) + ")");
                }
                else 
                {
                    $theLinks.show();
                }
                
                SAJ(this).html(less);
                
                collapsed = false;
            }
            else 
            {
                $theLinks.hide();
                SAJ(this).html(more);
                collapsed = true;
            }
            
            return false;
            
        }).insertAfter($theFacet);
    }
}

function hideJBESearchControl(clientId)
{
   document.getElementById(clientId).style.display = "";
}