
var CaseList = {
    debug: 0,
    provider: new CaseAjaxProvider(),
    
    options: {
		byId: 0,
		byTypeId: '',
		caseId: 0,
		userId: 0,
		secondId: 0			// drugi operator
    },
    
    aCase:     Array(),
    aValid:     Array(),
    aInvalid:   Array(),
//	panel: 0,
    
    init: function() {
        var aTmp = $A(document.getElementsByClassName('mediaBorder'));
        aTmp.each(function(media) {
            var id = media.id.replace('media', '');
            CaseList.aCase.push(id);
//            CaseList.aValid = CaseList.aCase;
            CaseList.aInvalid = CaseList.aCase;
		});
        
        if (CaseList.debug) {
            bod = document.getElementsByTagName('body')[0];
            debug = document.createElement('div');
            valid = document.createElement('div');
            valid.id = 'validItems';
            invalid = document.createElement('div');
            invalid.id = 'invalidItems';
        }
    },
    
    narrowResults: function(options) {
		
		if (obj = document.getElementById('evidenceLoading1')) obj.style.display='';
		if (obj = document.getElementById('evidenceLoading2')) obj.style.display='';
		
        if (!options) {
            //  we want to clear any filter
            this.options.byTypeId = null;
            $('filerByType').selectedIndex = 0;
        } else {
            if (typeof options.byId != "undefined") {
                this.options.byId = options.byId;
            }
            if (typeof options.byTypeId != "undefined") {
                this.options.byTypeId = options.byTypeId;
            }
            if (typeof options.caseId != "undefined") {
                this.options.caseId = options.caseId;
			}
            if (typeof options.secondId != "undefined") {
                this.options.secondId = options.secondId;
			}
            if (typeof options.userId != "undefined") {
                this.options.userId = options.userId;

				if (obj = document.getElementById('ajaxMessage')) obj.style.display='none';
//				if (obj = document.getElementById('errorMessage')) obj.style.display='none';

				if (obj = document.getElementById('tab5'+'_'+this.options.userId)) 	 obj.className="";
				if (obj = document.getElementById('tab8'+'_'+this.options.userId)) 	 obj.className="";
				if (obj = document.getElementById('tab10'+'_'+this.options.userId))  obj.className="";
				if (obj = document.getElementById('tab11'+'_'+this.options.userId))  obj.className="";
				if (obj = document.getElementById('tab12'+'_'+this.options.userId))  obj.className="";
				if (obj = document.getElementById('tab4'+'_'+this.options.userId))   obj.className="";

				if (obj = document.getElementById('tab5'+'_'+this.options.secondId))  obj.className="";
				if (obj = document.getElementById('tab8'+'_'+this.options.secondId))  obj.className="";
				if (obj = document.getElementById('tab10'+'_'+this.options.secondId)) obj.className="";
				if (obj = document.getElementById('tab11'+'_'+this.options.secondId)) obj.className="";
				if (obj = document.getElementById('tab12'+'_'+this.options.secondId)) obj.className="";
				if (obj = document.getElementById('tab4'+'_'+this.options.secondId))  obj.className="";

				if (obj = document.getElementById('mediaContent'+this.options.userId)) {
					obj.style.backgroundColor='#ffffff';
				}
				if (obj = document.getElementById('mediaContent'+this.options.secondId)) {
					obj.style.backgroundColor='#DBE2EF';
				}

/*
				if (obj = document.getElementById('mediaBox'+this.options.userId)) {
					if ( obj.style.display=='none') {
						var toShow = this.options.userId;
						var toHide = this.options.secondId;
					} else {
						var toShow = 0;
						var toHide = 0;
					}
				}
				if (obj = document.getElementById('mediaBox'+this.options.secondId)) {
					if ( obj.style.display=='none') {
						var toShow = this.options.secondId;
						var toHide = this.options.userId;
					} else {
						var toShow = 0;
						var toHide = 0;
					}
				}
				alert(this.options.userId);
				alert(toHide);
*/
//				if ( this.panel != toShow && toShow > 0 && toHide > 0) {
//					new Effect.Appear('mediaBox'+this.options.userId, {duration: 0.6,from:0,to:1});
//					new Effect.BlindUp('mediaBox'+toHide, {duration: 0.6});
//				}
//				this.panel = this.options.userId;
				
				document.getElementById('tab'+this.options.byId+'_'+this.options.userId).className="current";
			}
        }

		var aMatching = CaseList.getValidCase(this.options);
        CaseList.updateValidCase(aMatching);

		if (obj = document.getElementById('evidenceLoading1')) obj.style.display='none';
		if (obj = document.getElementById('evidenceLoading2')) obj.style.display='none';

        return false;
    },
    
    getValidCase: function(options) {
        var HW = new CaseAjaxProvider();
        var aValidIds = HW.getValidIds(options);

        return aValidIds;
    },
   
    updateValidCase: function(aValidItems) {
        //  make a copy of former valid media
        var aOldValid = CaseList.aValid;
        var aToHide = new Array();
        var aToShow = new Array();

        aOldValid.each( function(caseId) {
            if (aValidItems.indexOf(caseId) == -1) {
                aToHide.push(caseId);
            }
        });
        
        aValidItems.each( function(caseId) {
//            alert('id ' +caseId +' indexOf aOldValid ' +aOldValid.indexOf(caseId));
            if (aOldValid.indexOf(caseId) == -1) {
                aToShow.push(caseId);
            }
        });
        
        CaseList.aValid = aValidItems;
        
        if (this.debug) {
            CaseList.aOldValid = aOldValid;
            CaseList.aValidItems = aValidItems;
            CaseList.aToHide = aToHide;
            CaseList.aToShow = aToShow;
            CaseList._debug();
        }
        
        CaseList.refreshScreen(aToHide, aToShow);
    },
    
    deleteMediaById: function(caseId) {
        if (confirmDelete('media')) {
            var ok = this.provider.deleteMediaById(caseId);
            if(ok.messageType == "info") {
                // and remove it from screen
                this.hideItem(caseId);
            }
            Element.update('ajaxMessage', '<div class="' +ok.messageType +'Message">' +ok.message +'</div>');
        }
        return false;
    },

// VOTE !!!!!!!!!!!!!!!!
    vote: function(caseId,userId) {
		var ok = this.provider.vote(caseId,userId);
		var test = ok ? ok.messageType : '';
		if ( test != "") {
			Element.update('voteMessage', '<div class="' +ok.messageType +'Message">' +ok.message +'</div>');
		}
        return false;
    },

    refreshScreen: function(aToHide, aToShow) {
        aToHide.each(function(caseId) {
             CaseList.hideItem(caseId);
        });
        
        aToShow.each(function(caseId) {
             CaseList.showItem(caseId);
        });
    },
    
    hideItem: function(itemId) {
        //Effect.Shrink($('media' +itemId));
        Element.hide($('media' +itemId));
    },
    
    showItem: function(itemId) {
        //Effect.Grow($('media' +itemId));
        Element.show($('media' +itemId));
    },
    
    _debug: function() {
        confirm('Options : \n' +
              'By type : ' +this.options.byTypeId +
              '\n' +
              'Anciens valid : \n' +CaseList.aOldValid +
              '\n' +
              'Valid : \n' +CaseList.aValidItems +
              '\n\n' +
              'To hide : \n' + CaseList.aToHide +
              '\n' +
              'To show : \n' + CaseList.aToShow
              );
    }
    
}

