﻿/****************************************************
    Turns a list into a list that can be expanded using plus minus images
    Requires:
        Eduserv.CookieManager
        jquery core
*****************************************************/
function PartialList(params){
    this.listId = params.listId; 
    
    this.showAllText = params.showAllText || this.showAllText;
    this.displayValue = params.displayValue || this.displayValue;       
    this.defaultNumberVisible = params.defaultNumberVisible || this.defaultNumberVisible;
    
    this.list = $(params.list);
    this.parent = $(this.list.parent().get(0));
}

PartialList.prototype.list = ""; //the ID of the list
PartialList.prototype.displayValue = "list-item"; //the value to set the display css value to: default: list-item
PartialList.prototype.defaultNumberVisible = 5; //the default number of visible items
PartialList.prototype.parent = null;
PartialList.prototype.listItems = null;
PartialList.prototype.init = PartialList_RenderList;
PartialList.prototype.plusFunction = "";
PartialList.prototype.minusFunction = "";
PartialList.prototype.showAllText= "Show All";
PartialList.prototype.showAllButton = null;
PartialList.prototype.showAll = null;

function PartialList_RenderList(){


    this.listItems = this.list.find("li");   
    
    if(this.listItems.length == 0) return;
    if(this.listItems.length < this.defaultNumberVisible)return ;
    if(this.listItems.length > 1){
       
    
    }
    var cc = this;
    this.minusFunction = function(){
            PartialList_removeItem(cc);
        };
        this.plusFunction = function(){
            PartialList_addItem(cc);
        }; 
        this.showAll = function(){
            PartialList_showAllClick(cc);
        };
        
        
        
    //setup the default visible items
    var visibleStart = 0;
    visibleStart = this.defaultNumberVisible;
    
    var toHide = this.listItems.length - (visibleStart);    
    this.showAllButton = $("<li class='showAllButton'><a href='#'>"+this.showAllText+"</a></li>");  
    this.showAllButton.appendTo(this.list);
    this.showAllButton.click(this.showAll);
        
    while(toHide > 0){
        this.minusFunction();
        toHide--;  
         
    }   
    this.showAllButton.css("display",this.displayValue);
   
        
       
 
}
function PartialList_showAllClick(obj){
     for(var i =0; i <obj.listItems.length;i++){
        if($(obj.listItems[i]).css("display") == "none"){
            $(obj.listItems[i]).css("display",obj.displayValue);                     
        }
     }
       obj.showAllButton.css("display","none");  
}



function PartialList_removeItem(obj){

    for(var i = obj.listItems.length-1; i >-1;i--){                
        if($(obj.listItems[i]).css("display") == obj.displayValue){
            $(obj.listItems[i]).css("display","none");           
            
            i=-1;
        }
    }      
}

