
/**
 * sortBy
 * 
 */

var netcastTemplateEngine = new TemplateEngine('/netcast/tpl/');

function sortBy(list, by){
	this.list = list;
	this.INDEX=0;
	this.BITRATE=1;
	this.NAME=2;
	
	if (this.list != '') {
		switch(by){
		case this.INDEX:
			this.list.sort(function(a, b){
 				return a.index - b.index
			})
			break;

		case this.BITRATE:
			this.list.sort(function(a, b){
 				return b.br - a.br
			})
			break;

		case this.NAME:
			this.list.sort(function(a, b){
				var nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase()
				if (nameA < nameB) //sort string ascending
					return -1
				if (nameA > nameB)
					return 1
				return 0 //default return value (no sorting)
			})
			break;

		default:
			alert('[Netcast sortBy] wrong List type!');
		};
	}
	return this.list;
};

/**
 * setSelected(bitrate)
 * 
 */

function setSelected(bitrate){
	document.getElementById('bitrateNetcast').value = parseInt(bitrate);
}

/**
 * NetcastList
 * 
 */

function NetcastList(xml){
	this.xmlitems = getNamedChildren(xml, 'e2netcastlist', 'e2netcast');
	this.sort = parseInt(getNodeContent(xml, 'e2netcastmenuSortBy'));
	this.netcastlist = [];
	
	this.getArray = function(){
		if(this.netcastlist.length == 0){
			var len = this.xmlitems.length;
			for(var i=0;i<len;i++){
				var netcast = new Netcast(this.xmlitems.item(i),i+1).toJSON();
				this.netcastlist.push(netcast);
			}
		}
		debug("[NetcastList].sort " + this.sort);
		return sortBy(this.netcastlist, this.sort);
	};
};

/**
 * Netcast
 * 
 */

function Netcast(xml, index){
	this.index = index;
	this.name = getNodeContent(xml, 'e2name');
	this.url = getNodeContent(xml, 'e2url','');
	this.id = getNodeContent(xml, 'e2id','');
	this.br = getNodeContent(xml, 'e2bitrate','');
	this.genre = getNodeContent(xml, 'e2genre','').replace( /,(\S)/g, ', $1');
	this.base = getNodeContent(xml, 'e2base','');
	this.urlprefix = this.url == '' ? '' : 'Url: ';
	this.bitratetext = this.br == '' ? '' : this.br+' kbit/s ';
	this.genreprefix = this.genre == '' ? '' : 'Genre: ';
	this.indextext = netcastcore.currentNetcastList < 2 ? '('+this.index+') ' : '';
	this.editfavo = netcastcore.currentNetcastList > 0 ? '1' : '0';

	this.json = {
			'index' : this.index,
			'name' : this.name,
			'url' : this.url,
			'id' : this.id,
			'br' : this.br,
			'genre' : this.genre,
			'base' : this.base,
			'urlprefix' : this.urlprefix,
			'bitratetext' : this.bitratetext,
			'genreprefix' : this.genreprefix,
			'indextext' : this.indextext,
			'editfavo' : this.editfavo
	};

	this.toJSON = function(){
		return this.json;
	};
	
};

/**
 * NetcastPluginList
 * 
 */

var NetcastPluginList = Class.create({
	initialize: function(xml){
		this.anyGui = false;
		this.list = this.parse(xml);
	},
	
	parse: function(xml){
		var items = xml.getElementsByTagName('e2netcastplugin');
		var list = [];
		var len = items.length;
		for(var i = 0; i < len; i++){
			var external = new NetcastPlugin(items[i]);
			if(external.hasGui)
				this.anyGui = true;
			list.push(external);
		}

		return list;
	},
	
	getArray: function(){
		return this.list;
	}
});

/**
 * NetcastPlugin
 * 
 */

function NetcastPlugin(xml){
	this.path = getNodeContent(xml, 'e2path');
	this.name = getNodeContent(xml, 'e2name');
	this.hasGui = getNodeContent(xml, 'e2hasgui') == 'True';
	this.target = getNodeContent(xml, 'e2target') == 'True' ? 'target="_blank"' : '';
	this.title = getNodeContent(xml, 'e2title') == '' ? '' : 'title="' +  getNodeContent(xml, 'e2title') +'"';

	this.json = {
			'path' : this.path,
			'name' : this.name,
			'hasGui' : this.hasGui,
			'target' : this.target,
			'title' : this.title,
	};

	this.toJSON = function(){
		return this.json;
	};
	
};

/**
 *NetcastMenu
 * 
 */

function NetcastMenu(xml){
	
	this.sortby = getNodeContent(xml, 'e2netcastmenuSortBy');
	
	this.Sorter = new Array(getNodeContent(xml, 'e2netcastmenuIndex'),
			getNodeContent(xml, 'e2netcastmenuBitrate'),
			getNodeContent(xml, 'e2netcastmenuName')
		);
			
	this.sort_name = this.Sorter[this.sortby];
	this.sort_txt = getNodeContent(xml, 'e2netcastmenuSortbyTitle');
	this.index = getNodeContent(xml, 'e2netcastmenuIndex');
	this.name = getNodeContent(xml, 'e2netcastmenuName');
	this.bitrate = getNodeContent(xml, 'e2netcastmenuBitrate');
	this.genre = getNodeContent(xml, 'e2netcastmenuGenre');
	this.addfavo = getNodeContent(xml, 'e2netcastmenuAdd');
	this.delfavo = getNodeContent(xml, 'e2netcastmenuDel');
	this.info = getNodeContent(xml, 'e2netcastmenuInfo');
	this.menu = getNodeContent(xml, 'e2netcastmenuMenu');
	this.toplist = getNodeContent(xml, 'e2netcastmenuTop');
	this.genrelist = getNodeContent(xml, 'e2netcastmenuGenreL');
	this.favorites = getNodeContent(xml, 'e2netcastmenuFavo');
	this.genre = getNodeContent(xml, 'e2netcastmenuGenre');
	this.search = getNodeContent(xml, 'e2netcastmenuSearch');
	this.setup = getNodeContent(xml, 'e2netcastmenuSetup');
	this.stations = getNodeContent(xml, 'e2netcastmenuStations');
};

/**
 *NetcastSettings
 * 
 */

function NetcastSettings(xml){
	this.backgroundmvi = getNodeContent(xml,'e2netcastmvi');
	this.only_mpeg = getNodeContent(xml,'e2netcastonlympeg') == 0 ? '' : 'checked';
	this.only_mpeg_txt = getNodeContent(xml,'e2netcastonlympegtxt');
	this.sr_const_stream_txt = getNodeContent(xml,'e2netcastconststreamtxt');
	this.bitrate_txt = getNodeContent(xml,'e2netcastbitratetxt');
	this.backgroundmvi_txt = getNodeContent(xml,'e2netcastmvitxt');
	this.btn_save = getNodeContent(xml,'e2netcastbtnsavetxt');
	this.bitrate = getNodeContent(xml,'e2netcastbitrate');
};

/**
 * iNetcast
 * 
 */

function iNetcast(element) {
	this.id = element.readAttribute('data-id');
	this.url = element.readAttribute('data-url');
	this.base = element.readAttribute('data-base');
	this.index = element.readAttribute('data-index');
	this.name = element.readAttribute('data-name');
	this.bitrate = element.readAttribute('data-bitratetext');
};

/**
 * Netcastinfo
 * 
 */

function Netcastinfo(xml) {
	this.bitrate = !netcastcore.currentNetcast.bitrate ? 'N/A' : netcastcore.currentNetcast.bitrate;
	this.title = getNodeContent(xml, 'e2netcastTitle');
	this.title_txt = getNodeContent(xml, 'e2netcastTitletxt');
	this.location = getNodeContent(xml, 'e2netcastLocation');
	this.codec = getNodeContent(xml, 'e2netcastCodec');
	this.station = getNodeContent(xml, 'e2netcastOrganisation');
	this.info = getNodeContent(xml, 'e2netcastmenuInfo');
	this.bitrate_txt = getNodeContent(xml,'e2netcastbitratetxt');
};
