Wall={
	
	thumbs:[],
	found:{},
	running:true,
	loadingImg:null,
	counter:0,
	first:true,
	page:10,
	paused:true,
	term:'twitpic',
	onFilter:false,

	init:function() {
		Wall.setTerm();
		Wall.getSearches();
		setTimeout("Wall.cleanup()", 20000);

		Event.observe(document, 'keypress', function(e){
			if(Wall.onFilter) return;
			var code;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			var character = String.fromCharCode(code);
			if(character=="s" || character == "S") Wall.running = !(Wall.running);
			if(character=="c") alert("Found: "+Wall.thumbs.length+"\nCounter: "+Wall.counter);
		});
	},

	setTerm:function() {
		var frag = window.location.toString().split('?')[1];
		if(frag != undefined && frag != "") {
			Wall.term = 'twitpic '+frag;
			// likely we will only have a few so lets start with MOST recent
			Wall.page = 1;
		}

		$('q').observe('focus', function() {
			Wall.onFilter = true;
		});
		$('q').observe('blur', function() {
			Wall.onFilter = false;
		});

		$('filterForm').observe('submit', function(e) {
			Event.stop(e);
			window.location='/?'+$F('q');
		});
		$('filterForm').observe('mouseover', function() {
			$('filterForm').setOpacity(0.95);
		});
		$('filterForm').observe('mouseout', function() {
			$('filterForm').setOpacity(0.45);
		});
		$('filterForm').setOpacity(0.45);
	},
		
	getSearches:function() {
		if(Wall.page < 0) Wall.page = 10;

		var s = document.createElement('script');
		s.type = 'text/javascript';
		s.src = 'http://search.twitter.com/search.json?q='+Wall.term+'&callback=Wall.load&rpp=100&page='+Wall.page;
		document.getElementsByTagName('head')[0].appendChild(s);

		if(Wall.first) {
			Event.observe($(s), 'load', Wall.show);	
			Wall.first = false;		
		}

		Wall.page--;

		setTimeout("Wall.getSearches();",5000);
	},
	
	load:function(data) {
		if(data.results == undefined) return;

		for(var i=0; i < data.results.length; i++) {
			matches = data.results[i].text.match(/twitpic.com\/[0-9a-zA-Z]*/g);
			$A(matches).each(function(matchurl) {
				twitpicId = matchurl.split("/")[1];
				if(!Wall.found[twitpicId]) {
					Wall.thumbs[Wall.thumbs.length] = twitpicId;
					Wall.found[twitpicId] = data.results[i];
				}	
			});
			
		}
	},
	
	show:function() {			
			if(!Wall.running) {
				setTimeout('Wall.show();',1500);
				return;		
			}

			if(Wall.counter >= Wall.thumbs.length) {
				setTimeout('Wall.show();',1500);
				return;
			}

			var twitpicId = Wall.thumbs[Wall.counter];
			img = document.createElement('img');
			img.src = 'http://twitpic.com/show/mini/'+twitpicId;

			alink =document.createElement("a");
			alink.href = 'http://twitpic.com/'+twitpicId;
			alink.title = Wall.found[twitpicId]['from_user'] + " - " + Wall.found[twitpicId]['text'];
			alink.appendChild(img);
			alink.rel = twitpicId;

			$(alink).observe('click', function(e) {
				Event.stop(e);
				Wall.showLarge(this.rel);
			}.bindAsEventListener(alink));

			// document.body.appendChild(a);
			var loadImg = new Image();
			loadImg.src = 'http://twitpic.com/show/mini/'+twitpicId;
			$(loadImg).observe('load', function() {
				if($$('#thumbs a').length==0) {
					$('thumbs').appendChild(alink);	
				} else {
					$('thumbs').insertBefore(alink,$$('#thumbs a')[0]);
				}
				$(this).hide();

				new Effect.Appear($(this), {duration:0.5});						
			}.bindAsEventListener(alink));

			Wall.counter++;
			
			setTimeout('Wall.show();',1500);
	
	},

	showLarge:function(twitpicId) {
		var sizes = Wall.viewport();
		var post = Wall.found[twitpicId];
		// console.log(twitpicId);
		$('largeImage').src = 'http://twitpic.com/show/large/'+twitpicId;
		$('profilePic').href = 'http://twitter.com/'+post['from_user'];
		$$('#profilePic img')[0].src = post['profile_image_url'];
		$$('#large h1 a')[0].href = 'http://twitter.com/'+post['from_user'];
		$$('#large h1 a')[0].innerHTML = post['from_user'];
		$$('#large h2 a')[0].href = 'http://twitpic.com/'+twitpicId;
		$$('#large blockquote a')[0].href = 'http://twitter.com/'+post['from_user']+'/status/'+post['id'];
		$$('#large blockquote a')[0].innerHTML = post['text'];

		Wall.paused = Wall.running;
		Wall.running = false;
		Wall.overlay();

		$('large').setStyle({
			left: ((sizes[0]/2)-365)+"px"
		});
		$('large').removeClassName('hide');
	},

	hideLarge:function() {
		Wall.running = Wall.paused;
		$('overlay').remove();
		$('large').addClassName('hide');
		$('largeImage').src = '/images/loader.gif';
		$$('#profilePic img')[0].src = '/images/loader.gif';
	},

	overlay:function() {
		if($('overlay') != null) $('overlay').remove();

		var sizes = Wall.viewport();
		var div = $(document.createElement('div'));
		div.id = 'overlay';
		div.setOpacity(0.4);
		div.setStyle({
			width: sizes[0]+"px",
			height: sizes[1]+"px",
			top: 0,
			left: 0
		});

		div.observe('click', function(e) {
			Event.stop(e);
			Wall.hideLarge();
		});

		document.body.appendChild(div);
	},

	viewport:function() {
		var viewportwidth;
		var viewportheight;
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined') {
			viewportwidth = window.innerWidth;
			viewportheight = window.innerHeight;
		} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
			viewportwidth = document.documentElement.clientWidth;
			viewportheight = document.documentElement.clientHeight;
		} else {
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
			viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		}

		return [viewportwidth, viewportheight];
	},

	cleanup:function() {
		var i;
		$$('#thumbs a').each(function(t) {
			if(i > 500) {
				t.remove();
			}
			i++;
		});

		setTimeout("Wall.cleanup()", 20000);
	}

}

var kkeys = [];
var konami = "38,38,40,40,37,39,37,39,98,97";
Event.observe(document, 'keypress', function(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	kkeys.push(code);
	if (kkeys.toString().indexOf(konami.toString()) >= 0 ) {
		Wall.running = false;
		KonamiImages();
		KonamiBackground();

		var klogo = document.createElement("img");
		klogo.src = '/images/konami-logo.jpg';
		klogo.id = 'konamiLogo';
		document.body.appendChild(klogo);

		kkeys = [];
	}
});

function KonamiImages(){
	var x = 0;
	$$('#thumbs a').each(function(t) {
		if(x > 100) {
			t.remove();
		}
		x++;
	});

	var s = '{var R=0, x1=.1, y1=.05, x2=.25, y2=.24, x3=1.6, y3=.24, x4=300, ';
	s += 'y4=200, x5=300, y5=200, I=$("thumbs").getElementsByTagName("img"), ';
	s += 'L=I.length; setInterval(function (){for(var i=0; i-L; i++){var S=I[ i ].style; S.position=';
	s += '\'absolute\'; S.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; S.top=(Math.cos';
	s += '(R*y1+i*y2+y3)*y4+y5)+"px"}R++},5); void(0)}';
	eval(s);
}

var kb = 1;
function KonamiBackground() {
	if(kb > 8) kb = 1;
	var bi = new Image();
	bi.src = "/images/konami"+kb+".jpg";
	$(bi).observe('load', function() {
		document.body.style.backgroundImage = "url(/images/konami"+kb+".jpg)";
		kb++;
		setTimeout("KonamiBackground();",5000);
	});
}

Event.observe(window, 'load', function() {
	Wall.init();
});
