/*
 * util-bbox.js - BIGGER_BOX - JavaScript LightBox window like
 * util.js and util-effects.js is needed
 * Copyright (c) 2006-2007 Lucas Ferreira (www.lucasferreira.com)
 *
 * Version: 0.7.3
 */

var BIGGER_BOX = function()
{
	var bbox;
	var overlay;
	var img;
	var footer;
	var loading;
	var legend;
	var close;
	var imgPreloader;
	
	var oldResize;
	var marginTop = 0;
	var _links = new Array();
	
	var ACTIVE_ALT = "";
	var BBOX_WIDTH = 250;
	var BBOX_HEIGHT = 250;
	var BORDER_WIDTH = 4;
	var CLOSE_TEXT = "[x] fechar";
	
	return {
	
		init: function()
		{
			$("a[rel!=null]").run(function(){
				if( this.getAttribute("rel") && this.getAttribute("rel").indexOf("bbox") > -1 )
				{
					_links.push( this );
				}
			});
			
			_links.apply("e", "click", this.clickHandler);
			
			overlay = $e("DIV", {"id": "overlay"}).css({"display": "none"});
			
			bbox = $e("DIV", {"id": "bbox"}).css({"display": "none"});			
			
			footer = $e("DIV", {"class": "footer clearfix"});
			loading = $e("DIV", {"class": "loading"});
			legend = $e("DIV", {"class": "legend"});
			close = $e("A", {"class": "close"});
			
			imgPreloader = new Image();			
			
			document.body.append(overlay);
			document.body.append(bbox);
		},
		
		getCenteredXY: function(pgSize, oSize)
		{	
			return {
				x: ( (pgSize[0]/2) - (oSize[0]/2) ),
				y: ( (pgSize[1]/2) - (oSize[1]/2) )
			}
		},
		
		resizeOverlay: function(pgSize)
		{
			pgSize = (pgSize || getPageSize());
			
			overlay.setHeight(pgSize[1]);
		},
		
		start: function(a)
		{
			var pgSize = getPageSize();
			
			//overlay...
			Alpha.set(overlay, 1);
			this.resizeOverlay(pgSize);
			overlay.onclick = _d( this, this.end );
			overlay.toggle();
			
		 	overlay.opacity(60, null, 300);
			
			//bbox...
			bbox.innerHTML = "";
			bbox.setSize(BBOX_WIDTH, BBOX_HEIGHT);
			bbox.toggle();
			
			//loading..
			bbox.append(loading);

			bbox.css({
				"marginLeft": ((bbox.getWidth()/2)*-1) + "px",
				"marginTop": ((bbox.getHeight()/2)*-1) + "px"
			});
			
			//image preloader...
			imgPreloader.onload = _d( this, this.onLoadComplete );
			imgPreloader.src = a.getAttribute("href");

			//footer legend...
			ACTIVE_ALT = (a.title || "&nbsp;");

			//image...
			img = $e("IMG");
			img.addClass("image");
			img.css({"visibility": "hidden"});
			bbox.append(img);
			
			//legend and close...
			close.title = close.innerHTML = CLOSE_TEXT;
			close.href = "#";
			close.onclick = _d( this, this.end );
								
			footer.append(legend);
			footer.append(close);
			
			//resize overlay event...
			oldResize = (window.onresize || null);
			window.onresize = _d(this, function(){
				if(oldResize != null)
				{
					oldResize();
				}
				
				this.resizeOverlay();
			});
		},
		
		onLoadComplete: function()
		{
			var _comp = function()
			{
				img.src = imgPreloader.src;
				legend.innerHTML = img.alt = ACTIVE_ALT.length < 1 ? "&nbsp;" : ACTIVE_ALT;
				img.css({"visibility": "visible"});
				img.opacity(100);

				footer.setTop(imgPreloader.height);
				footer.setWidth(imgPreloader.width);
				this.append(footer);
			
				this.fx({
					"height": (this.getHeight() + legend.getHeight() - (BORDER_WIDTH*2)),
					"margin-top": (marginTop - (legend.getHeight()/2) - (BORDER_WIDTH*2))
				});
			};
			
			marginTop = ((imgPreloader.height/2)*-1);
			
			bbox.fx({
				"margin-left": ((imgPreloader.width/2)*-1),
				"margin-top": marginTop,
				"width": imgPreloader.width,
				"height": imgPreloader.height
			}, _comp);
		},
		
		end: function(e)
		{
			overlay.toggle();
			bbox.toggle();
			
			try
			{
				//cancel de onresize event...
				if(oldResize != null)
				{
					window.onresize = oldResize;
				}
				else
				{
					window.onresize = function(){};				
				}
				
				Event.cancel(e);
				return false;
			}
			catch(e) {}
		},
		
		clickHandler: function(e)
		{
			BIGGER_BOX.start(this);
			
			Event.cancel(e);
			return false;
		}
	
	};
}();

//from LightBox(c) - http://www.huddletogether.com
function getPageScroll()
{
	return (self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
}

function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY)
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

	return [pageWidth, pageHeight, windowWidth, windowHeight];
}

_c( _d( BIGGER_BOX, BIGGER_BOX.init ) );

