var PressRetailersManager = new Class({

	name: 'PressRetailersManager',
	platforms: {},
	
	initialize: function(form, list, items, icons) {
		this.form = $(form);
		this.list = $(list);
		this.items = (items ? items : {});
		this.icons = (icons ? icons : {});
		this.initFilter();	
		this.populateList();
	},
	
	initFilter: function() {
		this.filter = this.form.getElement("select[name=platform]");
		this.items.platforms.each(function(item) {
			this.filter.options[this.filter.options.length] = new Option(item.platform, item.platform);
			this.platforms[item.platform] = item;
		}, this);
		this.filter.addEvent('change', function(event) {
			this.populateList();
		}.bind(this));
		
		// set the default list view if platform query param is defined
		var uri = new URI(location.href);
		if (uri.getData('platform')) {
			this.filter.value = uri.getData('platform');
			this.populateList();
		}
	},
	
	populateList: function() {
		var tbody = this.list.getElement('tbody');
		tbody.getChildren().destroy();
		var listHeader = this.list.getElement('#list-header');
		listHeader.set('html', (this.filter.value ? this.filter.value : '&nbsp;'));
		if (this.platforms[this.filter.value]) {
			this.platforms[this.filter.value].items.each(function(item, index) {
				var tr = new Element('tr', {'class': ((index % 2) ? 'even' : '')});
				tr.adopt(new Element('td', {'class': 'press_items', html: item.product}));
				tr.adopt(this.createDownloadCell(item.all, 'all'));
				tr.adopt(this.createDownloadCell(item.datasheet, 'download'));
				if (item.media || true) {	// TODO: only display if media items exist
					var link = this.form.detailurl.value + "?platform="+this.filter.value+"&product="+item.product;
					tr.adopt(this.createDownloadCell(link, 'downloadmore'));
				} else {
					tr.adopt(this.createDownloadCell(false, 'downloadmore'));					
				}
				tr.adopt(this.createDownloadCell(item.logo, 'download'));
				tr.adopt(this.createDownloadCell(item.pressrelease, 'download'));
				tbody.adopt(tr);
			}, this);
		}
	},
		
	createDownloadCell: function(link, downloadType) {
		var td = new Element('td', {'class': 'press_download'});
		if (link) {
			var a;
			if (downloadType == 'downloadmore') {
				a = new Element('a', { href: link });
			} else {
				a = new Element('a', { href: link, target: '_blank' });				
			}
			td.adopt(a.adopt(this.getDownloadIcon(downloadType, 'div')));
		} else {
			td.adopt(this.getDownloadIcon('x', 'div'));
		}
		return td;
	},
		
	getDownloadIcon: function(downloadType, elementType) {
		var text = 'x';
		switch(downloadType) {
			case 'all' : text = 'all'; break;
			case 'download' : text = 'download'; break;
			case 'downloadmore' : text = 'download...'; break;
		}
		if (this.icons.x) {
			var icon = this.icons.x;
			switch(downloadType) {
				case 'all' : icon = this.icons.all; break;
				case 'download' : icon = this.icons.download; break;
				case 'downloadmore' : icon = this.icons.downloadmore; break;
			}
			if (elementType == 'div') {
				var cssClass = 'icon-'+downloadType;
				return new Element('div', { 'class': cssClass, html: text });				
			} else {
				return new Element('img', { src : icon.src, width: icon.width, height: icon.height, align: icon.align, alt: text, border: 'none' });
			}
		}
		return new Element('span', { html: text });
	}
});

var PressRetailersUtils = {
	setHeader : function(text) {
		var header = $(document.body).getElement('#masthead h1');
		if (header) {
			header.set('html', text);
		}
	}
};
