/* Author: David Jakobik, EtherCycle

*/

window.addEvent('domready', function() {
	var preloadWrapper = new Element('div',{'id':'preload'})
	preloadWrapper.inject(document.body,'bottom');
	
	if ($('gallery')) {
		var veil = new Element('div',{'id':'veil'});
		veil.inject($('gallery'),'bottom').fade('hide');
		var veilFx = new Fx.Tween(veil);
		
		
		var lightBox = new Element('div',{'id':'lightBox'});
		lightBox.inject($('gallery'),'bottom').fade('hide');
		var lightBoxFx = new Fx.Tween(lightBox);

		var lightBoxImg = new Element('img');
		lightBoxImg.inject($('lightBox'),'top');

		var lightBoxClose = new Element('a',{'class':'btnClose','href':'#','html':'Close'});
		lightBoxClose.inject($('lightBox'),'bottom');

		lightBoxClose.addEvent('click',function(e){
			e.stop();
			lightBoxFx.start('opacity',1,0).chain(function(){
				veilFx.start('opacity',0.75,0);
			});
		})

		$$('#gallery a').each(function(galleryLink){
			
			if (!(galleryLink.hasClass('btnClose'))) {
				
				var galleryPreload = new Element('img',{'src':galleryLink.href});
				galleryPreload.inject(preloadWrapper);

			
				galleryLink.addEvent('click', function(e){
					e.stop();
					lightBoxImg.set('src',this.href);
					
					veilFx.start('opacity',0,0.75).chain(function(){
						lightBoxFx.start('opacity',0,1);
						var lightBoxVertPos = (veil.getSize().y/2)-(lightBoxImg.getSize().y/2);
						// alert(lightBoxVertPos+', '+(veil.getSize().y/2)+', '+(lightBoxImg.getSize().y));
						lightBoxImg.setStyle('margin-top',lightBoxVertPos);
					});
				})
			}
		})
		

	}
	
	if ($('twitter')) {
		var twitterRemote = new Element('script',{'src':'http://twitter.com/statuses/user_timeline/amphorahome.json?callback=twitterCallback2&count=3'});
		twitterRemote.inject(document.body,'bottom');
	}

});


function twitterCallback2(twitters) {
	var statusHTML = [];
	for (var i=0; i<twitters.length; i++){
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			return '<a href="'+url+'">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://www.twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		});

		statusHTML.push('<li>'+status+'</li>\n');
	}
	document.getElementById('tweets').innerHTML = statusHTML.join('');
}

