function QuestionAssociation(){
    this.sQuestionType = 'ASSOCIATION';
    this.TEXT = 0;
    this.IMAGE = 1;
    
    this.NONE = 0;
    this.LETTERS = 1;
    this.NUMBERS = 2;
    
    //System settings
    this.sListIdPrefix = 'association';
    this.sIdDelimiter = '_';
    this.iListHeightAdjust = -17;
    this.sLabelSuffix = ')&nbsp;&nbsp;';
    this.iListItemPadding = 10;
    
    //Quiz settings
    this.oMaster = null;
    this.sServerSaveURL = '';
    this.bSaveParticipation = false;
    
    //User settings
    this.iChoiceWidth = 200;
    this.iChoiceMaxHeight = 250;
    this.iLabelType = this.NONE;
    
    //Variables
    this.sQuestionText = '';
    this.sTitle = null;
    this.sImageFileName = null;
    this.oVideoInfo = null;
    this.sAudioFileName = null;
    this.sChoicesA = new Array();
    this.sChoicesB = new Array();
    this.iChoicesPos = new Array();
    this.sSaveValue = '';
    this.sImgsToPreload = new Array();
    this.iQuestionID = 0;
    this.oList = null;
    this.condition = new Condition();
    this.sCommentTitle = null;
    this.sCommentText = '';
    
    //Mandatory functions
    this.display = function(oParentElement,iWidthAvail){
        //Create p
        var oP = ns_buildHTMLElement('p',{style:'display:inline;'});
        oP.className = 'QuestionText';
        oP.innerHTML = this.sQuestionText;
        oParentElement.appendChild(oP);
        
        if(this.sQuestionText || this.sQuestionText.length > 0){
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/spacer.gif', width:'5', height:'19'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/question_line.gif', width:iWidthAvail, height:'1'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/spacer.gif', width:'5', height:'19'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
        }
        
        //Lists
        var oListsWrapper = ns_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
        var oCurrRow = oListsWrapper.insertRow(0);
        var oLabelsWrapper = oCurrRow.insertCell(0);
        var oChoicesWrapper = oCurrRow.insertCell(1);
        
        oChoicesWrapper.align = 'left';
        oLabelsWrapper.margin = '0';
        
        this.oList = this.buildAssociationSortable();
        this.oList.id = this.sListIdPrefix + this.sIdDelimiter + this.iQuestionID
        oChoicesWrapper.appendChild(this.oList);
        
        var oLabelsList = this.buildAssociationLabels();
        oLabelsList.id = this.sListIdPrefix  + this.sIdDelimiter + 'labels' + this.sIdDelimiter + this.iQuestionID
        oLabelsWrapper.appendChild(oLabelsList);
        
        oParentElement.appendChild(oListsWrapper);
        
        Sortable.create(this.oList.id);
        this.balanceLists(this.oList,oLabelsList);
        
        oParentElement.appendChild(this.oMaster.buildCommentLayout(this));
    }
    
    this.save = function(){
        this.sSaveValue = '';
        var iIndex = null;
        var sId = null;
        var iPos = 0;
        
        for(var i = 0;i < this.oList.childNodes.length;i++){
            if(this.oList.childNodes[i].tagName == 'LI'){
                sId = this.oList.childNodes[i].id;
                iIndex = sId.substring(sId.lastIndexOf(this.sIdDelimiter) + 1);
                this.iChoicesPos[iIndex] = iPos;
                
                this.sSaveValue += iIndex + ',';
                iPos++;
            }
        }
        
        if(this.sSaveValue.length > 0){
            this.sSaveValue = this.sSaveValue.substr(0,this.sSaveValue.length - 1);
        }
        
        //Comment
        if(this.sCommentTitle){
            this.sCommentText = $(this.oMaster.sTxtCommentPrefix + this.iQuestionID).value;
        }
    }
    
    this.submit = function(){
        if(this.bSaveParticipation){   
            this.oMaster.saveParticipation(this.iQuestionID,0,this.sSaveValue);
            
            //Comment
            if(this.sCommentTitle){
                this.oMaster.saveComment(this.iQuestionID,this.sCommentText);
            }
        }
    }
    
    this.isAnswered = function() {
        return true;
    }
    
    this.hide = function(){
        Sortable.destroy(this.oList.id);
        delete this.oList;
    }
    
    this.setQuestionText = function(s){
        this.sQuestionText = s;
    }
    
    this.setTitle = function(s){
        this.sTitle = s;
    }
    this.setImage = function(s){
        this.sImageFileName = s;
        this.sImgsToPreload[this.sImgsToPreload.length] = s;
    }
    this.setLabelType = function(i){
        this.iLabelType = i;
    }
    this.setVideo = function(s,iWidth,iHeight){
        this.oVideoInfo = {sFileName:'',iWidth:0,iHeight:0};
        this.oVideoInfo.sFileName = s;
        this.oVideoInfo.iWidth = iWidth;
        this.oVideoInfo.iHeight = iHeight;   
    }
    this.setAudio = function(s){
        this.sAudioFileName = s;
    }
    //Question specific functions
    this.addChoice = function(sChoiceA,iChoiceAType,sChoiceB,iChoiceBType){
        this.sChoicesA[this.sChoicesA.length] = [sChoiceA,iChoiceAType];
        this.sChoicesB[this.sChoicesB.length] = [sChoiceB,iChoiceBType];
        this.iChoicesPos[this.iChoicesPos.length] = this.iChoicesPos.length;
        
        if(iChoiceAType == this.IMAGE){
            this.sImgsToPreload[this.sImgsToPreload.length] = sChoiceA;
        }
        if(iChoiceBType == this.IMAGE){
            this.sImgsToPreload[this.sImgsToPreload.length] = sChoiceB;
        }
        
    }
    this.buildAssociationSortable = function(){
        var oListItem = null;
        var iCurrIndex = null;
        var oList = ns_buildHTMLElement('ul',{});
        oList.style.listStyleType = 'none';
        oList.style.padding = '0';
        oList.style.marginLeft = '0';
        
        for(var i = 0;i < this.sChoicesB.length;i++){
            oListItem = ns_buildHTMLElement('li',{});
            
            oListItem.style.marginBottom = '20px';
            oListItem.style.border = 'solid 1px gray';
            oListItem.style.width = this.iChoiceWidth + 'px';
            oListItem.style.padding = this.iListItemPadding + 'px';
            oListItem.style.cursor = 'move';
            
            for(var j = 0;j < this.iChoicesPos.length;j++){
                if(this.iChoicesPos[j] == i){
                    iCurrIndex = j;
                    break;
                }
            }
            
            if(this.sChoicesB[iCurrIndex][1] == this.TEXT){
                oListItem.innerHTML = this.sChoicesB[iCurrIndex][0];
            }else{
                oListItem.appendChild(ns_buildImageObject(this.sChoicesB[iCurrIndex][0],this.iChoiceWidth,this.iChoiceWidth,this.oMaster.sMediasFolder));
            }
            
            oListItem.id = this.sListIdPrefix + this.sIdDelimiter + this.iQuestionID + this.sIdDelimiter + iCurrIndex;
            
            oList.appendChild(oListItem);
        }
        
        return oList;
    }
    this.buildAssociationLabels = function(){
        var oListItem = null;
        var iCurrIndex = null;
        var oList = ns_buildHTMLElement('ul',{});
        var sLabel = '';
        var oImg = null;
        oList.style.listStyleType = 'none';
        
        oList.style.padding = '0';
        oList.style.marginLeft = '0';
        
        for(var i = 0;i < this.sChoicesA.length;i++){
            oListItem = ns_buildHTMLElement('li',{});
            
            oListItem.style.marginBottom = '20px';
            oListItem.style.width = this.iChoiceWidth + 'px';
            oListItem.style.padding = this.iListItemPadding + 'px';
            
            if(this.iLabelType == this.LETTERS){
                sLabel = ns_getLetter(i + 1) + this.sLabelSuffix;
                oListItem.style.width = (this.iChoiceWidth + 20) + 'px';
            }else if(this.iLabelType == this.NUMBERS){
                sLabel = (i + 1) + this.sLabelSuffix;
                oListItem.style.width = (this.iChoiceWidth + 20) + 'px';
            }
            oListItem.innerHTML = sLabel;
            
            if(this.sChoicesA[i][1] == this.TEXT){
                oListItem.innerHTML += this.sChoicesA[i][0];
            }else{
                oImg = ns_buildImageObject(this.sChoicesA[i][0],this.iChoiceWidth,this.iChoiceMaxHeight,this.oMaster.sMediasFolder);
                oImg.align = 'absmiddle';
                oListItem.appendChild(oImg);
            }
            
            oList.appendChild(oListItem);
        }
        
        return oList;
    }
    this.balanceLists = function(oListA,oListB){
        var iMaxHeight = 0;
        
        for(var i = 0;i < oListA.childNodes.length;i++){
            if(oListA.childNodes[i].tagName == 'LI'){
                iMaxHeight = Math.max(iMaxHeight,getElementPos(oListA.childNodes[i]).height);
                
            }
        }
        
        for(var i = 0;i < oListB.childNodes.length;i++){
            if(oListB.childNodes[i].tagName == 'LI'){
                iMaxHeight = Math.max(iMaxHeight,getElementPos(oListB.childNodes[i]).height);
            }
        }
        
        iMaxHeight += this.iListHeightAdjust;
        
        for(var i = 0;i < oListA.childNodes.length;i++){
            if(oListA.childNodes[i].tagName == 'LI'){
                oListA.childNodes[i].style.height = iMaxHeight + 'px';
                
                
            }
        }
        
        for(var i = 0;i < oListB.childNodes.length;i++){
            if(oListB.childNodes[i].tagName == 'LI'){
                oListB.childNodes[i].style.height = (iMaxHeight + 2) + 'px';
            }
        }
    }
    this.shuffle = function(){
        this.iChoicesPos = ns_getShuffledOrder(this.iChoicesPos.length);
    }
}