var AffiliateIconSelector = new Class({

	Implements: [Options],

	options: {
		iconCount: 15,
		linkIcons:'',
		width:'',
		height:'',
		cookie: 'cnetBSIcon'
	},

	initialize: function(icons,container,options){
		this.setOptions(options);
		if (!this.setIcons(icons).icons)return false;
		if (!(this.imageContainer = $(container))) return false;
		this.displayIcons();
	},
	
	setIcons: function(icons){
		if ($type(icons) != 'array'){
			// dbug.log('Passed icons object is not an array type. It is type %s. %o', $type(icons), icons);
		} else {
			this.icons = icons;			
		}
		return this;
	},
	
	getDisplayIcons: function(){
		picked = this.icons;
		var icons = $unlink(this.icons), selected=[], picked,
		 	cookieVal = Cookie.get(this.options.cookie);
		// dbug.log('icons are being selected: %o', this.icons);
		
		while (selected.length < this.options.iconCount && (picked = icons.getRandom())){			
			dbug.log('picked: %o', picked.company);
			
			// only do this cookie check and set if we're in the first loop through
			if(!selected.length && icons.length > 1){
				while(picked.company == cookieVal){
					dbug.log('picked matched %s:', cookieVal);
					picked = icons.getRandom();
					// console.log('New picked: %o', picked.company);
				}
				Cookie.set(this.options.cookie, picked.company);
				// dbug.log('cookie value: %o', cookieVal);
			}
			selected.push(picked);
			icons.erase(picked);
		};	
		return selected;	
	},
	
	displayIcons: function(icons){
		icons = icons || this.getDisplayIcons();
		// dbug.log('Featured Icons %o',icon);
		this.imageContainer.empty();
		
		icons.each(function(icon){
			var imgInjectLoc = this.imageContainer;
			var iconLink;
			
			if(icon.href){
				iconLink = new Element('a',{
					href: icon.href, 
					'class': "iconLink",
					target: icon.target
				}).inject(this.imageContainer);
				imgInjectLoc = iconLink;
			}
			new Element('img', {
				src: icon.imagePath,
				width: icon.width,
				height: icon.height
			}).inject(imgInjectLoc);
		},this);
	}
	
});

