/**
 * Extends EventDispatcher
 * Event names are defined by convention: propName+Change, e.g: for the <code>appState<code> property update the event name is <code>appStateChange<code>
 */
function ScheduleWeek(){
}

ScheduleWeek.prototype = new EventDispatcher();
ScheduleWeek.prototype.constructor = ScheduleWeek;

/**
 * @param from Date
 * @param to Date
 * @param shows {ShowDTO}[]
 * @return
 */
ScheduleWeek.prototype.setWeekData = function(from, to, shows){
	this.from = from;
	this.to = to;
	
	/*
	 "shows": [
        {
          "id": 3412882,
          "title": "JUSTICIA A CUALQUIER PRECIO",
          "originalTitle": "THE FLOCK",
          "genreList": "Crime,Drama,Horror,Mystery",
          "airDateTime": "2010-08-17T00:15:00"
        },
	 */
	this.shows = shows;

	this.days = this.createDays(from, to);
	
	/*
	 [
	 	{hour:"", shows: Array(7)<Show> //show puede ser null  }
	 ]
	 */
	var hoursMap = {};
	for(var i=0; i<shows.length; i++){
		var show = shows[i];
		var tmp = show.airDateTime.split("T");
		var showDay = tmp[0];
		var showHour = tmp[1];
		
		if(!hoursMap[showHour]){
			hoursMap[showHour] = this.createWeekArray();
		}
		
		var showDate = ParseHelper.stringToDate(show.airDateTime);
		hoursMap[showHour][this.getDayPos(showDate)] = show;
	}
	
	var showsGrid = [];
	for(var key in hoursMap){
		showsGrid.push({hour:key, shows:hoursMap[key]});
	}
	
	showsGrid.sort(
			function(a,b){
				if(a.hour>b.hour)
					return 1;
				else if(a.hour<b.hour)
					return -1;
				else
					return 0;
			});
	
	this.showsGrid = showsGrid;
	this.dispatchEvent({type:"showsGridChange"});
}

/**
 * @private
 */
ScheduleWeek.prototype.createWeekArray = function(){
//	var week = new Array(7);
//	for(var i=0; i<7; i++){
//		week[i] = null;
//	}
//	return week;
	return [null, null, null, null, null, null, null];
}


/**
 * Creates week array (7 elements) calculating days between from and to params
 * @private
 * @param from Date
 * @param to Date
 * @return Array(7)
 */
ScheduleWeek.prototype.createDays = function(from, to){
	var days = [from];
	
	for(var i=1; i<7; i++){
		var day = new Date(days[i-1]);
		day.setDate(day.getDate()+1);
		days.push(day);
	}
	//days.push(to);
	
	return days;
}

/**
 * @private
 * @param date Date
 * @return int
 */
ScheduleWeek.prototype.getDayPos = function(date){
	var pos = -1;
	
	for(var i=0; i<this.days.length; i++){
		var day = this.days[i];
		if(date.getDate()==day.getDate()){
			return i;
		}
	}
	
	return pos;
}


/**
 * @return Date
 */
ScheduleWeek.prototype.getFrom = function(){
	return this.from;
}

/**
 * @return Date
 */
ScheduleWeek.prototype.getTo = function(){
	return this.to;
}

/**
 * @return ShowDTO[]
 */
ScheduleWeek.prototype.getShows = function(){
	return this.shows;
}

/**
 * @return Date[]
 */
ScheduleWeek.prototype.getDays = function(){
	return this.days;
}

/**
 * @return Map<String, Object>[]
 */
ScheduleWeek.prototype.getShowsGrid = function(){
	return this.showsGrid;
}

/**
 * Gets a show form the showsGrid
 * @param pos {rowIndex, colIndex} 
 * @return ShowDTO
 */
ScheduleWeek.prototype.getShowByPos = function(pos){
	return this.showsGrid[pos.rowIndex].shows[pos.colIndex];
}


