window.addEvent('domready', function(){
	new caixadeluz();
});

caixadeluz = new Class({
	initialize:function(){
		$$('a.caixadeluz').each(function(el){
			el.setStyle('cursor', 'pointer');
			el.store('url', el.get('href'));
			el.set('href', 'javascript:;');
			el.addEvent('click', function(ev){
				this.current = el;
				this.spinner = new Asset.image('http://portal.colegiofreinet.com.br/imagens/spinner_48px.gif', {'id':'caixadeluz_spinner', 'styles':{'margin':'auto'}});
				this.iniciar();
			}.bind(this));
		}.bind(this));
	},
	iniciar:function(){
		if ($('caixadeluz_base')){
			return false;
		}
		document.addEvent('keyup', function(ev){
			if (ev.key == 'esc'){
				this.finalizar();
			}
		}.bind(this));
		new Element('div', {
			'id':'caixadeluz_base',
			'styles':{
				'position':'fixed',
				'width':'100%',
				'height':'100%',
				'top':'0',
				'left':'0',
				'z-index':'100',
				'background-color':'#000',
				'opacity':'0'
			},
			'events':{
				'click':function(){
					this.finalizar();
				}.bind(this)
			}
		}).inject($$('body')[0], 'bottom');
		$('caixadeluz_base').set('tween', {property:'opacity', duration:200}).get('tween').start(0.9).chain(function(){
			new Element('div', {
				'id':'caixadeluz_canvas',
				'styles':{
					'position':'fixed',
					'width':'48px',
					'height':'48px',
					'top':'50%',
					'left':'50%',
					'margin-top':'-24px',
					'margin-left':'-24px',
					'overflow':'hidden',
					'background-color':'#fff',
					'padding':'10px',
					'z-index':'101'
				}
			}).adopt(this.spinner).inject('caixadeluz_base', 'before');
			if (navigator.appVersion.test('MSIE 6')){
				ds = document.getSize();
				sc = document.getScroll();
				ss = document.getScrollSize();
				$('caixadeluz_base').setStyles({
					'position':'absolute',
					'width':ds.x,
					'height':ss.y,
					'top':0
				});
				$('caixadeluz_canvas').setStyles({
					'position':'absolute',
					'top':(sc.y.toInt() + 350)
				});
			}
			preimg = new Asset.image(this.current.retrieve('url'), {
				'styles':{
					'position':'absolute',
					'margin':'auto',
					'visibility':'hidden'
				},
				onload:function(){
					this.centralizar(preimg);
				}.bind(this)
			});
		}.bind(this));
	},
	fit:function(imgres){
		res = document.getSize();
		margem = 50;
		maxres = {x:(res.x - margem), y:(res.y - margem)}
		this.newres = {x:0, y:0};
		if (imgres.x > maxres.x){
			rate = (((maxres.x * 100) / imgres.x) / 100);
			this.newres.x = (imgres.x * rate).toInt();
			this.newres.y = (imgres.y * rate).toInt();
		}
		if (imgres.y > maxres.y){
			rate = (((maxres.y * 100) / imgres.y) / 100);
			this.newres.y = (imgres.y * rate).toInt();
			this.newres.x = (imgres.x * rate).toInt();
		}
	},
	centralizar:function(img){
		$$('#caixadeluz_canvas img#caixadeluz_spinner').dispose();
		img.inject($$('body')[0], 'bottom');
		size = img.getSize();
		this.fit(size);
		size = {x:(this.newres.x === 0 ? size.x : this.newres.x), y:(this.newres.y === 0 ? size.y : this.newres.y)}
		img.dispose();
		$('caixadeluz_canvas').adopt(
			new Element('img', {
				'id':'caixadeluz_image',
				'src':img.get('src'),
				'width':size.x,
				'height':size.y,
				'styles':{
					'margin-bottom':10,
					'opacity':0,
					'border':'1px solid #eee'
				}
			}),
			new Element('div', {
				'id':'caixadeluz_toolbar',
				'styles':{
					'height':30,
					'display':'none'
				}
			}).adopt(
				new Element('span', {
					'id':'caixadeluz_description',
					'html':this.current.get('title'),
					'styles':{
						'position':'absolute',
						'float':'left',
						'margin-right':'75px',
						'font-size':'10px',
						'font-weight':'bold',
						'color':'#666'
					}
				}),
				new Element('img', {
					'src':'http://www.sisfacil.com.br/imagens/closelabel.gif',
					'styles':{
						'float':'right',
						'cursor':'pointer'
					},
					'events':{
						'click':function(){
							this.finalizar();
						}.bind(this)
					}
				})
			)
		);
		//$('caixadeluz_toolbar').set('slide', {duration:750}).slide('hide');
		$('caixadeluz_canvas').set('morph', {duration:500}).get('morph').start({
			'width':size.x,
			'height':size.y + 30,
			'margin-top':('-' + ((size.y + 10 + 30) / 2).toInt() + 'px'),
			'margin-left':('-' + ((size.x + 10) / 2).toInt() + 'px')
		}).chain(function(){
			$('caixadeluz_image').tween('opacity', 1).get('tween').chain(function(){
				$('caixadeluz_toolbar').reveal();
			}.bind(this));
		}.bind(this));
	},
	finalizar:function(){
		this['finalEfeito' + $random(1,2)]();
	},
	overlaykill:function(){
		$('caixadeluz_base').get('tween').start(0).chain(function(){
			$('caixadeluz_base').dispose();
		});
	},
	finalEfeito1:function(){
		props = ['width','height'];
		prop1 = props.getRandom();
		props.erase(prop1);
		prop2 = props[0];
		$('caixadeluz_canvas').get('tween', {property:prop1, duration:300}).start(0).chain(
			function(){
				$('caixadeluz_canvas').get('tween', {property:prop2, duration:300}).start(0).chain(
					function(){
						$$('#caixadeluz_canvas').dispose();
					this.overlaykill();
					}.bind(this)
				);
			}.bind(this)
		);
	},
	finalEfeito2:function(){
		if ($chk($('caixadeluz_canvas')) && $chk($('caixadeluz_canvas').nix)){
			$('caixadeluz_canvas').nix({
				onHide:function(){
					this.overlaykill();
				}.bind(this)
			});
		} else {
			this.finalEfeito1();
		}
	}
});
