var MovieListMenuItem = new Class({

	initialize: function(options) {
		this.typeId = options.typeId;
		this.mainDivId = 'lmmovieList' + this.typeId;
		this.countSpanPref = 'lmmovieListcntLbl';
		this.countValuePref = 'lmmovieListcntval';
		this.numberInTitle = 'currentMovieListNumber';
	},
	
	setCount: function(count){
		if ($(this.mainDivId)){
			$(this.countSpanPref + this.typeId).setHTML('(' + count + ')');
			$(this.countValuePref + this.typeId).value = count;
		}
		if ($(this.numberInTitle) && glob.typeId && (glob.typeId == this.typeId))
			$(this.numberInTitle).setHTML(count);
	},
	
	changeCount: function(change){
		if ($(this.mainDivId)){
			var count = $(this.countValuePref + this.typeId).getValue().toInt();
			this.setCount(count + change);
		}	
	}
});	


var MovieListManager = new Class({

	initialize: function(options) {
		this.sysMsgId = 'sysMsg';
		this.dragMsgId = 'DragMsg';
		this.movieLineIdPrefix = 'movieline_movieId_';
		this.listeners = [];
	},
	
	addListener: function(listener) {
		if (listener) this.listeners.push(listener);
	},	
	
	removeMovie: function(movieId, typeId){
		RequestProcessor.addOrRemoveMovieFromList(movieId, typeId, 0, false, this.removeMovieCallback.bind(this));
	},

	removeFromAllLists: function(movieId, text){
		if(confirm(text)){
			RequestProcessor.removeMovieFromAllList(movieId, this.removeFromAllListsCallback.bind(this));
			return true;
		}
		return false;
	},
	
	getMovieLine: function(movieId){
		return this.movieLineIdPrefix + movieId;
	},
	
	removeMovieCallback: function(wrapper){
		if ($(this.getMovieLine(wrapper.movieId)))
			$(this.getMovieLine(wrapper.movieId)).remove();
		this.onMovieAddedToList(wrapper);
	},

	removeFromAllListsCallback: function(wrapper){
		if ($(this.getMovieLine(wrapper.movieId)))
			$(this.getMovieLine(wrapper.movieId)).remove();
		this.onMovieRemoveFromLists(wrapper);
	},
	
	onMovieRemoveFromLists: function(wrapper){
		if(wrapper && wrapper.listTypes && wrapper.listTypes.length > 0){
			for(var i = 0; i < wrapper.listTypes.length; i++){
				var mlMenuItem = new MovieListMenuItem({typeId : wrapper.listTypes[i].id});
				mlMenuItem.changeCount(wrapper.change);	
			}
		}

		if (wrapper.message && wrapper.message.length > 0){
		    var tr = $chk($(this.sysMsgId))?$(this.sysMsgId):new Element('div', {'id':this.sysMsgId});
		    tr.setHTML(wrapper.message);    
		    tr.injectTop($('maincontent'));
		    
		    var Change = new Fx.Style($(this.sysMsgId),'background-color', {duration:2500});
		    Change.start('#fed');	
	    }
	}, 

	onMovieAddedToList: function(wrapper){
		var mlMenuItem = new MovieListMenuItem({typeId : wrapper.typeId});
		mlMenuItem.changeCount(wrapper.change);	
		
		if (wrapper.message && wrapper.message.length > 0){
		    var tr = $chk($(this.sysMsgId))?$(this.sysMsgId):new Element('div', {'id':this.sysMsgId});
		    tr.setHTML(wrapper.message);    
		    tr.injectTop($('maincontent'));
		    
		    var Change = new Fx.Style($(this.sysMsgId),'background-color', {duration:2500});
		    Change.start('#fed');	
	    }
	    
	    if (wrapper.change > 0){
	    	this.triggerOnAddEvents(wrapper.movieId, wrapper.typeId);
	    }
	}, 
	
	triggerOnAddEvents: function(movieId, typeId){
		for(var i=0; i<this.listeners.length; i++) {
			if (this.listeners[i].onMovieAdded)
				this.listeners[i].onMovieAdded(movieId, typeId);
		}	
	}
});	