/**
 * All intellectual property rights in this Software throughout the world belong to UK Radioplayer, 
 * rights in the Software are licensed (not sold) to subscriber stations, and subscriber stations 
 * have no rights in, or to, the Software other than the right to use it in accordance with the 
 * Terms and Conditions at www.radioplayer.co.uk/terms. You shall not produce any derivate works 
 * based on whole or part of the Software, including the source code, for any other purpose other 
 * than for usage associated with connecting to the UK Radioplayer Service in accordance with these 
 * Terms and Conditions, and you shall not convey nor sublicense the Software, including the source 
 * code, for any other purpose or to any third party, without the prior written consent of UK Radioplayer.
 *
 * @name StationRow
 * @description creates a station preset row object
 *
 * @param position : {Number} row number
 *
 * @author Mike Colley <michael.colley@bbc.co.uk>
 */

radioplayer.models.StationRow = function( position ) {
	this._position = position;
	this._presets = [];
	this._lastplayed = [];
	this._nodeList = false;
	this._nodeFactory = new radioplayer.models.StationIconFactory();
};

radioplayer.models.StationRow.prototype = {	
	addPresets: function( presets ) {
		this._presets = this._presets.concat(presets);
	},	
	
	addPreset: function( preset ) {
		this._presets.push(preset);
	},
	
	addLastPlayed: function( lastplayed ) {
		this._lastplayed.push(lastplayed);
	},
	
	getNumberOfEmptySpaces: function() {
		return (4-this._presets.length);
	},
	
	addEmptyPlaceholders: function() {
		var emptySpacesToFill,
			i;
		
		//getNumberOfEmptySpaces -1 to account for lastplayed being the 1st placeholder
		(this._lastplayed.length > 0) ? emptySpacesToFill = (this.getNumberOfEmptySpaces()) -1 : emptySpacesToFill = this.getNumberOfEmptySpaces();
		
		for ( i=0; i<emptySpacesToFill; i++) {
			this.addPreset(this._nodeFactory.createEmptyPlaceholder());
		}
		return (i < 2);		
	},
	
	findFirstEmptyPlaceholder: function() {
		var presetLength = this._presets.length,
			i;
		for ( i = 0 ; i < presetLength ; i++) {	
			 if (!this._presets[i].getPid()) {
				return this._presets[i]; 
			 }
		}
		return false;
	},
	
	getNodeList: function() {
		var presetLength = this._presets.length,
			i;

		if (!this._nodeList) {
			this._nodeList = glow.dom.create('<div id="row_'+this._position+'" class="preset-row">');
			this._nodeList.addClass('station-row');
		
			if (this._lastplayed.length > 0) {
				this.getStationNode(this._lastplayed[0], true);
			}
			
			for ( i=0 ; i < presetLength ; i++) {	
				this.getStationNode(this._presets[i], false);				
			}
		}
		return this._nodeList;
	},
	
	getStationNode: function (station, addLastPlayedCSSClass) {
		var containerNode,
			dropTargetNode;
		
		containerNode = glow.dom.create("<div class='station-container'></div>");
		dropTargetNode =  glow.dom.create("<div class='drop-target'></div>");
		
		if (addLastPlayedCSSClass) {
			containerNode.addClass('last-played');
			dropTargetNode.attr('id', 'last-played');
		}
		
		dropTargetNode.append(station);
		containerNode.append(dropTargetNode);
		this._nodeList.append(containerNode);
	}            
		
};

