/**
 * @param String container
 * @param Model model
 * @param Controller controller
 */
function WeekView(container, model, controller){
	this.container = container;
	this.model = model;
	this.controller = controller;
	
	this.model.addEventListener("appStateChange", BoundMethod.create(this, this.appStateChangeHandler));
}



WeekView.prototype.createChildren = function(){
	
	function renderHour(item, column){
		if(item.hour){
			return item.hour.substring(0, item.hour.lastIndexOf(":"));
		}
		return "";
	}
	
	function renderShowItem(item, column){
		if(item.shows && item.shows[column.index]){
			return ""+item.shows[column.index].title;
		}
		return "";
	}
	
	var cols = [
        {title:this.model.getDictionary().hourTitle, labelFunction:renderHour},
		{title:"", index:0, labelFunction:renderShowItem},
		{title:"", index:1, labelFunction:renderShowItem},
		{title:"", index:2, labelFunction:renderShowItem},
		{title:"", index:3, labelFunction:renderShowItem},
		{title:"", index:4, labelFunction:renderShowItem},
		{title:"", index:5, labelFunction:renderShowItem},
		{title:"", index:6, labelFunction:renderShowItem}
	]; 
	
	this.grid = new DataGrid(this.container, "gridTable", cols, BoundMethod.create(this, this.itemClickHandler), true);
}


WeekView.prototype.updateWeek = function(){
	if(this.model.getScheduleWeek().getShowsGrid()){
		var dic = this.model.getDictionary();
		var days = this.model.getScheduleWeek().getDays();
		for(var i=0; i<days.length; i++){
			var day = days[i];
			
			var title =	dic.dayNames[day.getDay()] + 
						"<br/>" +
						$.format.date(day, dic.shortDateFormat);
			
			this.grid.setColumnTitle(title, i+1);
		}
		
		this.grid.setData(this.model.getScheduleWeek().getShowsGrid());
	}
}

WeekView.prototype.updateMode = function(){
	if(this.model.getMode()==Model.MODE_WEEK){
		this.updateWeek();
		$("#"+this.container).show();
	}else{
		$("#"+this.container).hide();
	}
}

WeekView.prototype.itemClickHandler = function(point){
	point.colIndex--; //because first col in the DataGrid is used to display the hour
	this.controller.selectShow(point);
}

WeekView.prototype.appStateChangeHandler = function(event){
	if(this.model.getAppState()==AppStateTypes.DOCUMENT_READY){
		this.createChildren();
		this.updateMode();
		this.model.getScheduleWeek().addEventListener("showsGridChange", BoundMethod.create(this, this.scheduleWeekChangeHandler));
		this.model.addEventListener("modeChange", BoundMethod.create(this, this.modeChangeHandler));
	}
}

WeekView.prototype.modeChangeHandler = function(event){
	this.updateMode();
}

WeekView.prototype.scheduleWeekChangeHandler = function(event){
	this.updateWeek();
}


