////
// sorting
// This function is the constructior of the object "sorting".
//

function sorting(){
  this.nodeId     =  new Array();

  //Methods of this object:
    this.addTopic   =  addTopic;
    this.addId      =  addId;
    this.sortRank   =  sortRank;
    this.drawRating =  drawRating;
  ////

}



////
// addTopic
// This function adds an new topic to the nodeId-array.
//

function addTopic(id){
  i = this.nodeId.length;
  x = new Array(); 
  x[0]=id;
  this.nodeId[i] = x;
}



////
// addId
// This function adds an ID to the last topic in the nodeId-array.
//
function addId(id){
  this.nodeId[this.nodeId.length - 1].push(id);
}



////
// sortRank
// This function builds an array with the ranking of a certain user or usergroup.
//

function sortRank(user){
    for (k=0;k<this.nodeId.length;k++){
        akt = this.nodeId[k];
        id = akt.shift();
        var ranking = new Array();

        if (akt.length>1){
            var xmlHttp = false;
            var xmlDatei;

            //Mozilla, Opera, Safari sowie Internet Explorer 7
                if (typeof XMLHttpRequest != 'undefined'){
                    xmlHttp = new XMLHttpRequest();
                    //alert ("XMLHTttpRequest abgesetzt (Fall 1)!");
                }
                if (!xmlHttp){
                    //Internet Explorer 6 und älter
                        try{
                            xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
                            //alert ("XMLHTttpRequest abgesetzt (Fall 2)!");
                        } 
                        catch(e){
                            try {
                                xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
                                //alert ("XMLHTttpRequest abgesetzt (Fall 3)!");
                            }
                            catch(e){
                                xmlHttp  = false;
                                //alert ("XMLHTttpRequest nicht abgesetzt!");
                            }
                        }
                    ////
                }
            ////
        
            if (xmlHttp) {

                for (i = 0; i < akt.length; i++){
                    //alert ("in for1");
                    var value=0;
                    xmlHttp.open('GET', '/rating/cgi-bin/xmlpoll.py/xmlpoll?userid='+user+'&type='+id+'&subid='+akt[i],false);
                    xmlHttp.onreadystatechange = function (){
                        //alert ("part1");
                        if (xmlHttp.readyState == 4) {
                            //alert ("part2");
                            //alert(xmlHttp.responseText);
                            xmlDatei = xmlHttp.responseXML;
                            value = xmlDatei.getElementsByTagName("value")[0].firstChild.data;
                        }
                    };
                    xmlHttp.send(null);
                    ranking[i] = value;
                    //this.drawRating(id,akt[i]);
                }

                //1. Copying the ranking array:
                    sortCopy = new Array();
                    //sortCopy = ranking;
                    for (i=0; i<ranking.length;i++){
                        sortCopy[i]=ranking[i];
                    }
                ////

                //2. Sorting the sort array:
                    sortCopy.sort(Numsort);
                    sortCopy.reverse();
                ////

                //3. Writing the sorted nodes in the HTML-View:
                    for (i=0;i<sortCopy.length;i++){
                        for (j=0;j<ranking.length;j++){
                            if (sortCopy[i]==ranking[j]){
                                ranking[j]=-1;
                                var a=j+1;
                                node=document.getElementById(id+"_"+a).cloneNode(true);
                                document.getElementById("sort_"+id).removeChild(document.getElementById(id+"_"+a));
                                document.getElementById("sort_"+id).appendChild(node);
                                x=this.drawRating(id,akt[j]);
                            }
                        }
                    }
                ////
      
            }
        }
        else{
            x=this.drawRating(id,akt[0]);
        }
    }
}


////
// drawRating
// This function calls the "blendquickrating" Funktion of the rating-class, witch draws the rating-stars on the HTML-page.
//

function drawRating(type,id){
    blendquickrating(type.toString(),id.toString());
    return true;
}



////
// Numsort
// This function is a helping function for the sorting. Without, the raking-array would be sorted lexikographically.
//

function Numsort (a, b) {
  return a - b;
}
