PictureThis.Lightbox = function()
{
	
	var YUE = YAHOO.util.Event;
	var YUD = YAHOO.util.Dom;
	
	var colour = '000000';
	
	return {
		
		init: function(root_id)
		{
			this.root 			= document.getElementById(root_id);
			this.pagePhoto 	= this.root.getElementsByTagName('img')[0];
			
			this.setup();
			this.attachEvents();
		},
		
		setup: function()
		{
			var width = YUD.getDocumentWidth();
			var height = YUD.getDocumentHeight() + 175; // lol wut


			this.maskPanel = document.createElement('div');
			this.maskPanel.id = 'lightbox_mask_panel';
			this.maskPanel.style.margin = '0 auto';
			
			this.close = document.createElement('span');
			this.close.id = 'lightbox_close';
			
			this.closeimg = document.createElement('img');
			this.closeimg.src = '/images/button_close.gif';
			this.closeimg.width = 20;
			this.closeimg.height = 19;
			
			var txt = document.createTextNode('Close');
			this.close.appendChild(txt);
			this.close.appendChild(this.closeimg);
			this.maskPanel.appendChild(this.close);
			
			this.photo = this.pagePhoto.cloneNode(false);
			this.maskPanel.appendChild(this.photo);
			
			this.controls = this.root.getElementsByTagName('div')[0].cloneNode(true);
			this.controls.style.display = 'block';
			this.maskPanel.appendChild(this.controls);
			
			YUD.insertBefore(this.maskPanel, document.getElementsByTagName('body')[0].getElementsByTagName('div')[0]);
			
			this.sizecontrol = this.controls.getElementsByTagName('div')[0];
			this.colourcontrol = this.controls.getElementsByTagName('div')[1];
			
			this.maskBG = document.createElement('div');
			this.maskBG.id = 'lightbox_mask_bg';
			this.maskBG.style.width = width + 'px';
			this.maskBG.style.height = height + 'px';
			
			YUD.insertBefore(this.maskBG, this.maskPanel);
			

		},
		
		attachEvents: function()
		{
			YUE.on(this.colourcontrol, 'click', this.changeColour, this);
			YUE.on(this.pagePhoto, 'click', this.show, this);
			YUE.on(this.close, 'click', this.hide, this);
			YUE.on(this.sizecontrol, 'click', this.changeImage, this);
			YUE.on(this.maskBG, 'click', this.hide, this);
		},
		
		changeColour: function(e, that)
		{
			if(colour == '000000')
			{
				// need to set the initial colours via JS to keep IE happy, obviously
				that.maskBG.style.backgroundColor = '#000000';
				that.maskBG.style.color = '#ffffff';
				that.maskPanel.style.backgroundColor = '#000000';
				that.maskPanel.style.color = '#ffffff';
				
				that.colourcontrol.innerHTML = '<a href="#" onclick"return false;">View on black</a>';
				that.closeimg.src = that.closeimg.src.replace(/close.gif/,'close_fff.gif');

				var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {backgroundColor: { to: '#ffffff' }, color: { to: '#000000'} }); 
				var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {backgroundColor: { to: '#ffffff' }, color: { to: '#000000'} }); 
				anim1.animate();
				anim2.animate();	
				
				colour = 'ffffff';
			}
			else
			{
				that.colourcontrol.innerHTML = '<a href="#" onclick"return false;">View on white</a>';
				that.closeimg.src = that.closeimg.src.replace(/close_fff.gif/,'close.gif');
				
				var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {backgroundColor: { to: '#000000' }, color: { to: '#ffffff'} }); 
				var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {backgroundColor: { to: '#000000' }, color: { to: '#ffffff'} }); 
				anim1.animate();
				anim2.animate();

				colour = '000000';
			}
			
			YUE.preventDefault(e);
		},
		
		changeImage: function(e, that)
		{
			var target = YUE.getTarget(e);
			
			while(target.nodeName != 'A')
			{
				target = target.parentNode;
			}
			
			if(!target)
			{
				return;
			}

			var src = target.getAttribute('href');
			var dims = target.getAttribute('title').split('x');
			
			that.photo.width = dims[0];
			that.maskPanel.style.width = dims[0] + 'px';
			that.photo.height = dims[1];

			that.positionPanel();
			
			that.photo.src = target.getAttribute('href');

			YUE.preventDefault(e);
		},
		
		positionPanel: function()
		{
			var panel_padding = parseInt(YUD.getStyle(this.maskPanel, 'padding-left'))*2;
			var panel_width = parseInt(YUD.getStyle(this.maskPanel, 'width')) + panel_padding;
			var left = Math.round((YUD.getViewportWidth() / 2) - (panel_width / 2)) + 'px';
			var top = YUD.getDocumentScrollTop() + 50 + 'px';

			this.maskPanel.style.left = left;
			this.maskPanel.style.top  = top;
		},
		
		show: function(e, that)
		{
			that.maskBG.style.display = 'block';
			that.maskPanel.style.display = 'block';
			that.positionPanel();
			
			var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {opacity: { to: 0.95 } }); 
			var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {opacity: { to: 1 } }); 
			anim1.animate();
			anim2.animate();
			
			YUE.preventDefault(e);
		},
		
		hide: function(e, that)
		{
			var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {opacity: { to: 0 } }); 
			var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {opacity: { to: 0 } });
			anim1.onComplete.subscribe(function(){that.maskBG.style.display = 'none'});
			anim1.onComplete.subscribe(function(){that.maskPanel.style.display = 'none'});
			anim1.animate();
			anim2.animate();
		}
		
	};
}();

YAHOO.util.Event.onAvailable('footer', PictureThis.Lightbox.init, 'photobox', PictureThis.Lightbox);