/* Release Model- Responsible for loading releases */
function ReleaseModel(settings, player) {
	this.WebService= player.WebService;
	this.StartIndex= 1;
	this.ItemsPerPage= settings.itemsPerPage;
	this.TotalCount= 500;
	this.CurrentCategory= null;
	this.CurrentRelease= null;
	this.ReleaseView= null;
	this.Player= player;
	this.OnLoadComplete= null;
	this.SetView= function(x) { this.ReleaseView= x; };
	this.ResetTotalCount= function() { this.TotalCount= 500; };
	this.SetCategory= function(x, onLoadComplete) { 
		this.SearchField= null; 
		this.StartIndex= 1; 
		this.ResetTotalCount();
		this.CurrentCategory= x;
		this.OnLoadComplete= onLoadComplete;
		this.Load(); 
	};
	this.Search= function(x) { this.ResetTotalCount(); this.SearchField= x; this.StartIndex= 1; this.Load(); };
	this.ClearSearch= function() { this.SearchField= null; this.ResetTotalCount(); this.Load(); };
	this.SetCurrentRelease= function(x) { 
		this.CurrentRelease= x; 
		this.Player.SelectClip(x.URL); 
		this.Player.ClipView.SetRelease(x);
		if(this.ReleaseView != null) this.ReleaseView.SyncToCurrentRelease(this); // won't work on the carousel version FIXME
		if(this.Player.ReviewModel != null) { this.Player.ReviewModel.SetShowCode(x.EpisodeCode); }
		if(this.Player.DiscussionModel != null) { this.Player.DiscussionModel.SetShowCode(x.EpisodeCode); }
	};
	this.SearchField= null;
	this.Releases= [];
	this.SetSingleEpisodeShowCode= function(showCode) { 
		this.ResetTotalCount(); 
		this.SingleEpisodeReleaseListShowCode= showCode;	
		this.StartIndex= 1; 
		this.Load(); 
	};
	this.SingleEpisodeReleaseListShowCode= null;
	this.EndIndex= function() {
		var result= this.StartIndex+this.ItemsPerPage -1;
		if(result>this.TotalCount) { result= this.TotalCount; }
		return result;
	};
	this.SetStartIndex= function(si) {
		this.StartIndex= si;
		if(this.StartIndex>this.TotalCount) { this.StartIndex= this.TotalCount; } // shouldn't happen;
		this.Load();
	};
	this.NextPage= function() {
		this.StartIndex+= this.ItemsPerPage;
		if(this.StartIndex>this.TotalCount) { this.StartIndex= this.TotalCount; } // shouldn't happen;
		this.Load();
	};
	this.PrevPage= function() {
		this.StartIndex-= this.ItemsPerPage;
		if(this.StartIndex<1) { this.StartIndex= 1; }
		this.Load();
	};
	this.PrevPageAvailable= function() {
		return (this.StartIndex>1);
	};
	this.NextPageAvailable= function() {
		return (this.StartIndex+this.ItemsPerPage<=this.TotalCount);
	};
	this.Load= function() {
		// it's possible to not have a view (tabs category list)
		if(this.ReleaseView) { this.ReleaseView.DisplayLoading(this); }

		if(this.SingleEpisodeReleaseListShowCode != null)
		{
			this.WebService.GetSingleEpisodeReleaseList(
				this.StartIndex, this.EndIndex(),
				this.SingleEpisodeReleaseListShowCode, this.ParseReleaseList, this);		
		}
		else if(this.SearchField != null)
		{
			this.WebService.GetSearchReleaseList(this.StartIndex, this.EndIndex(),
				this.SearchField, this.ParseReleaseList, this);		
		} else {
			this.WebService.GetCategoryReleaseList(this.StartIndex, this.EndIndex(),
				this.CurrentCategory, this.ParseReleaseList, this);		
		}
	};
	this.ParseReleaseList= function(statusCode, jsonObject, rm) {
		rm.Releases= jsonObject.items;
		rm.TotalCount= jsonObject.listInfo.totalCount;
		rm.SortFields= jsonObject.sortFields;
		if(rm.ReleaseView) {
			rm.ReleaseView.Refresh(rm);
		}
		if(rm.OnLoadComplete) {
			rm.OnLoadComplete(rm);
			rm.OnLoadComplete= null; // only works on the first time..
		}
	};
	
	this.DurationString= function(ms) {
		var ts= ms/1000;
		var hours= Math.floor(ts/3600);
		var minutes= Math.floor((ts-(hours*3600))/60);
		var seconds= Math.floor((ts-(hours*3600)-(minutes*60)));
		var result= "";
		
		if(hours>0) {
			result= hours+':';
			if(minutes<10) {
				result += '0';
			}
		}
		
		result += minutes+':';
		if(seconds<10) {
			result+='0';
		}
		result+= seconds;
		
		return result;
	};
}