	PINGWIRE.Class.create("PINGWIRE.Controls.Poller", "PINGWIRE.Controls.Control", 
		function(config) {
			var thisContext = this;
			PINGWIRE.Controls.Control.call(this, config);
			$(document).ready(function(){
			    thisContext.initialize();
			});
		}, 
		{
			currentThumbIndex: 5,
			currentRowIndex: 0,
			firstPoll: true,
			paused: false,
			seconds: 82,
			secondsDelay: 1.8,
			thumbsPerRow: 5,
			thumbsUrl: "/image.results",
			pendingThumbsArray: [],

			getNewRowHtml: function() {
				return "<div class='Clear'></div>";
			},

			getThumbs: function() {
				var thisObj = this;
				$.getJSON(this.thumbsUrl, function(data) {
					thisObj.responseThumbs(data)
				});
			},
			
			getThumbHtml: function(currentThumbArray) {
				var html = 	"<a href='" + currentThumbArray['image_url'] + "' class='Thumb' id='" + currentThumbArray['image_id'] + "' target='_blank'>";
				html += 	"<img src='" + currentThumbArray['thumb_url'] + "'>";
				//html += 	"<img src='images/testThumb.gif'>";
				html +=		"</a>";
				return html;
			},
			
			getThumbUrlArray: function(text) {
				var re = new RegExp("(?:http://twitpic.com/)([A-Za-z0-9_]*)");
				var results = (re.exec(text));
				return results;
			},
			
			initialize: function() {
				this.setThumbsPerRow(true);
				this.setResize();
				this.getThumbs();
				this.poll();
			},
			
			isValidUrl: function(currentThumbUrlArray) {
				if (currentThumbUrlArray != null) {
					return true;
				} 
				return false;
			},
			
			pendingThumbsInterval: function() {
				setInterval(PINGWIRE.Delegate.create(this, this.writeThumb), (this.secondsDelay * 1000));
			},
			
			prependRow: function() {
				$("#Thumbs").prepend(this.getNewRowHtml());
			},
			
			poll: function() {
				this.publicInterval = setInterval(PINGWIRE.Delegate.create(this, this.getThumbs), (this.seconds * 1000));
			},

			removeClears: function() {
				$(".Clear").remove();
			},
			
			resize: function() {
				this.removeClears();
				this.setThumbsPerRow(false);
			},
			
			responseThumbs: function(response) {
				this.updateThumbs(response);
			},
			
			setResize: function() {
				var thisContext = this;
				$(window).bind("resize", function() {
					thisContext.resize();
				});
			},
			
			setThumbsPerRow: function(isInit) {
				var windowWidth = $(window).width();
				var panelWidth = 300;
				var sideMargin = 40;
				var availableWidth = windowWidth - panelWidth - (sideMargin*2.5);
				var thumbWidth = 151;
				var thumbsPerRow = (Math.floor(availableWidth/thumbWidth));
				this.thumbsPerRow = thumbsPerRow;
				$("#Thumbs").width(availableWidth);
				if (isInit == true) {
					this.currentThumbIndex = this.thumbsPerRow;
				}
			},
			
			setRow: function() {
				if (this.currentThumbIndex < (this.thumbsPerRow-1)) {
					this.currentThumbIndex++;
				} else {
					this.currentRowIndex++;
					this.currentThumbIndex = 0;
					this.prependRow();
				}
			},
			
			setSecondsDelay: function() {
				this.secondsDelay = (this.seconds/this.pendingThumbsArray.length) + .1;
			},

			updateThumbs: function(thumbs) {
				this.updatePendingThumbs(thumbs);
				if (this.firstPoll == true) {
					this.firstPoll = false;
					this.pendingThumbsInterval();
				}
			},
			
			updatePendingThumbs: function(thumbs) {
				for(var i=0; i< thumbs.length; i++) {
					this.pendingThumbsArray.push(thumbs[i]);
				}
				this.setSecondsDelay();
			},
			
			writeThumb: function() {
				if (this.paused == false) {
					if (this.pendingThumbsArray.length > 0) {
						var currentThumb = this.pendingThumbsArray.shift();
						if (currentThumb) {
							this.setRow();
							$("#Thumbs").prepend(this.getThumbHtml(currentThumb));
							$("#" + currentThumb['image_id']).show("fast");
						}
					}
				}
			}
			
		}
	);