﻿/****************************************************
    Turns a list into a list that can be expanded using plus minus images
    Requires:
        Eduserv.CookieManager
        jquery core
        
    ME- this has been modified from the original 27-01-2009
*****************************************************/
function ExpandableList(params){
    this.listId = params.listId;
    this.enabledPlusImg = params.enabledPlusImg;
    this.enabledMinusImg = params.enabledMinusImg;
    this.disabledPlusImg = params.disabledPlusImg;
    this.disabledMinusImg = params.disabledMinusImg;   
    
    this.displayValue = params.displayValue || this.displayValue;   
    this.saveToCookie = params.saveToCookie || this.saveToCookie;
    this.defaultNumberVisible = params.defaultNumberVisible || this.defaultNumberVisible;
    
    
    this.list = $("#"+this.listId); 
    
    this.parent = $(this.list.parent().get(0));
}

ExpandableList.prototype.listId = ""; //the ID of the list
ExpandableList.prototype.enabledPlusImg = ""; //the image the user clicks on to add an item
ExpandableList.prototype.enabledMinusImg = ""; //the image the user clicks on to remove and item
ExpandableList.prototype.disabledPlusImg = ""; //the image displayed when an item can't be added
ExpandableList.prototype.disabledMinusImg = ""; //the image displayed when an item can't be removed
ExpandableList.prototype.displayValue = "list-item"; //the value to set the display css value to: default: list-item
ExpandableList.prototype.saveToCookie = false; //saves the changes to a cookie to be recalled later
ExpandableList.prototype.defaultNumberVisible = 5; //the default number of visible items
ExpandableList.prototype.plus = null;
ExpandableList.prototype.minus = null;
ExpandableList.prototype.list = null;
ExpandableList.prototype.parent = null;
ExpandableList.prototype.listItems = null;
ExpandableList.prototype.renderList = ExpandableList_RenderList;
ExpandableList.prototype.renderButtons = ExpandableList_buttons;
ExpandableList.prototype.enablePlus = ExpandableList_enable_plus;
ExpandableList.prototype.enableMinus = ExpandableList_enable_minus;
ExpandableList.prototype.disablePlus = ExpandableList_disable_plus;
ExpandableList.prototype.disableMinus = ExpandableList_disable_minus;
ExpandableList.prototype.saveCookie = ExpandableList_SaveCookie;
ExpandableList.prototype.plusFunction = "";
ExpandableList.prototype.minusFunction = "";


function ExpandableList_RenderList(){
    if(this.list.html() == null)
     return;
    $('#'+this.listId+'buttons').remove();
    this.listItems = this.list.find("li");   
    
    if(this.listItems.length == 0) return;
    
    if(this.listItems.length > 1){
        //add enabled button      
            this.parent.prepend(
                "<div id='"+this.listId+"buttons' class='listControls'>"
                +"<img class='plus' src='"+this.enabledPlusImg+"' />"
                +"<img class='minus' src='"+this.enabledMinusImg+"' />"        
                +"</div>"        
            );
            
        this.plus = this.parent.find(".plus");
        this.minus = this.parent.find(".minus");   
        //setup functions
        var cc = this;  
        this.minusFunction = function(){
           
            ExpandableList_removeItem(cc);
        };
        this.plusFunction = function(){
            ExpandableList_addItem(cc);
        };
    
    }
    //setup the default visible items
    var visibleStart = 0;
    
    if(this.saveToCookie){ 
        var cookieMan = new CookieManager();        
        visibleStart = parseInt(cookieMan.read(this.listId)) || this.defaultNumberVisible;             
        
    }
    else{
   
        visibleStart = this.defaultNumberVisible;
    }   
    var toHide = this.listItems.length - (visibleStart);    
    while(toHide > 0){
    
        this.minusFunction();
        toHide--;        
    }    
         
       
    this.renderButtons();
}


function ExpandableList_addItem(obj){       
            
    for(var i =0; i <obj.listItems.length;i++){
        
        if($(obj.listItems[i]).css("display") == "none"){
            $(obj.listItems[i]).css("display",obj.displayValue);
            i = obj.listItems.length;
            
        }
    }
     obj.renderButtons();
     obj.saveCookie();
}

function ExpandableList_removeItem(obj){
    
    for(var i = obj.listItems.length-1; i >-1;i--){                    
        if($(obj.listItems[i]).css("display") != "none"){
        
            $(obj.listItems[i]).css("display","none");           
         
            i=-1;
        }
    }
       obj.renderButtons();
       obj.saveCookie();
}

function ExpandableList_disable_plus(){
    this.plus.unbind("click",this.plusFunction);    
    this.plus.attr("src",this.disabledPlusImg);
}
function ExpandableList_disable_minus(){
    this.minus.unbind("click",this.minusFunction);    
    this.minus.attr("src", this.disabledMinusImg);
}
function ExpandableList_enable_plus(){
    this.disablePlus();       
    this.plus.bind("click", this.plusFunction);    
    this.plus.attr("src",this.enabledPlusImg);
}

function ExpandableList_enable_minus(){
    this.disableMinus();
    this.minus.bind("click",this.minusFunction);
    this.minus.attr("src",this.enabledMinusImg);
}

function ExpandableList_buttons(){
  
    if(this.minus == null || this.plus == null){  return;}
    
     if(this.listItems.length < 2){
         this.disablePlus();
         this.disableMinus();            
     }
     else{  
     
        var hiddenStart = -1;             
        for(var i = 0; i < this.listItems.length; i++){          
            if($(this.listItems[i]).css("display") == "none" && hiddenStart == -1){
                hiddenStart = i;     
            }
        }      
        
        if(hiddenStart == 1){
           this.disableMinus();
        }
        else{
        
           this.enableMinus();
        }
        
        if(hiddenStart == -1){
           this.disablePlus();
        }
        else{
            this.enablePlus();
        }
      
     }
}
function ExpandableList_SaveCookie(){
    var num = this.defaultNumberVisible;
    
    for(var i=0; i < this.listItems.length; i++){
        if($(this.listItems[i]).css("display") == "none"){
            var cookieMan = new CookieManager();
            cookieMan.create(this.listId, i, 999);                      
            return;
        }
    }
    var cookieMan = new CookieManager();
    cookieMan.create(this.listId, this.listItems.length, 999);     
}