/**
 * @param Model model
 * @param Controller controller
 */
function MainView(model, controller){
	this.model = model;
	this.controller = controller;
	this.modeMenu = null;
	this.feedMenu = null;
	this.dateView = null;
	this.scheduleLoadingView = null;
	
	if(this.model.getAppState()==AppStateTypes.DOCUMENT_READY){
		this.createChildren();
	}else{
		this.model.addEventListener("appStateChange", BoundMethod.create(this, this.appStateChangeHandler));
	}
}

MainView.prototype.createChildren = function(){
	this.modeMenu = $('#modeMenu');
	this.feedMenu = $('#feedMenu');
	this.dateView = $('#dateView');
	this.scheduleLoadingView = $('#scheduleLoadingView');
	
	document.title = this.model.getDictionary().documentTitle;
	
	this.updateMode();
	this.model.addEventListener("modeChange", BoundMethod.create(this, this.modeChangeHandler));
	
	this.updateDateView();
	this.model.addEventListener("dateChange", BoundMethod.create(this, this.dateChangeHandler));
	
	//Feeds combo//
	var feeds = this.model.getNetworkFeeds();
	var feedCode = this.model.getFeed()
	
	var cbo = '<select name="feed">';
	for(var i=0; i<feeds.length; i++){
		cbo += '<option value="'+feeds[i].code+'"'+(feeds[i].code==feedCode ? ' selected="selected"' : '')+'>'+feeds[i].label+'</option>';
	}
	cbo += '</select>';
	
	this.feedMenu.append(cbo);
	
	$('#feedMenu select').change(function() {
		  MainApp.getInstance().controller.changeFeed(this.value);
	});
	//--//
}

MainView.prototype.updateMode = function(){
	var dic = this.model.getDictionary();
	this.modeMenu.empty();
	
	if(this.model.getMode()==Model.MODE_DAY){
		this.modeMenu.append('<a href="javascript:MainApp.getInstance().controller.changeMode(Model.MODE_DAY)"><strong>'+dic.dayModeLabel+'</strong></a> | <a href="javascript:MainApp.getInstance().controller.changeMode(Model.MODE_WEEK)">'+dic.weekModeLabel+'</a>');
	}else{
		this.modeMenu.append('<a href="javascript:MainApp.getInstance().controller.changeMode(Model.MODE_DAY)">'+dic.dayModeLabel+'</a> | <a href="javascript:MainApp.getInstance().controller.changeMode(Model.MODE_WEEK)"><strong>'+dic.weekModeLabel+'</strong></a>');
	}
}

MainView.prototype.updateDateView = function(){
	this.dateView.empty();
	this.dateView.append('<p>'+$.format.date(this.model.getDate(), this.model.getDictionary().dateFormat)+'</p>');
}

MainView.prototype.updateScheduleLoadingView = function(){
	if(this.model.getAppState()==AppStateTypes.SCHEDULE_LOADING){
		this.scheduleLoadingView.empty();
		this.scheduleLoadingView.append('<div class="loader"></div>');
	}else
	if(this.model.getAppState()==AppStateTypes.SCHEDULE_SUCCESS){
		this.scheduleLoadingView.empty();
	}else
	if(this.model.getAppState()==AppStateTypes.SCHEDULE_ERROR){
		this.scheduleLoadingView.empty();
		this.scheduleLoadingView.append('<p>'+this.model.getDictionary().connError+'</p>');
	}
}

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

MainView.prototype.dateChangeHandler = function(event){
	this.updateDateView();
}

MainView.prototype.appStateChangeHandler = function(event){
	if(this.model.getAppState()==AppStateTypes.DOCUMENT_READY){
		this.createChildren();
	}else{
		this.updateScheduleLoadingView();
	}
	
}
