// JavaScript Document

// Script desenvolvido  por WILKER (http://jsdev.blogspot.com/)


var $ = function(string) {
	return document.getElementById(string);
};

var loading = new Image();
loading.src = 'loading.gif';

var toLoad = new Image();

var openImage = function(src) {
	var image = $('showPhoto');
	var imageC = $('fotoContainer');
	var backDiv = $('transparentBackground');
	
	imageC.style.width = '52px';
	imageC.style.height = '52px';
	
	setAlpha(backDiv, 0);
	centerObj(imageC);
	
	image.src = loading.src;
	imageC.style.display = '';
	backDiv.style.display = '';
	
	image.onclick = function() {
		closeImage(backDiv, imageC);
	};
	
	backFade(backDiv, 0, 80);
	
	toLoad.src = src;
	
	verifyImage(image, image, function() {expand.call(this, toLoad, image, imageC);});
};

var closeImage = function(backDiv, imageC) {
	imageC.style.display = 'none';
	backFade(backDiv, 80, 0);
};

var verifyImage = function(image, imageIn, cb) {
	if(image.complete) {
		cb.call(this);
		//imageIn.src = '';
	} else
		setTimeout(function() {verifyImage.call(this, image, imageIn, cb);}, 20);
};

var expand = function(image, imageIn, container) {
	var vel = 10;
	
	var w1 = image.width + 20;
	var h1 = image.height + 20;
	
	var w2 = parseInt(container.style.width);
	var h2 = parseInt(container.style.height);
	
	var dw = w1 - w2;
	var dh = h1 - h2;
	
	var dt = Math.max(dw, dh);
	var steps = Math.ceil(dt / vel);
	
	var wpos = [];
	var hpos = [];
	
	for(var i = 0; i < dt; i += steps) {
		var pct = i / dt;
		wpos[wpos.length] = Math.round(dw * pct) + w2;
		hpos[hpos.length] = Math.round(dh * pct) + h2;
	}
	
	i -= vel;
	
	if(i < dt) {
		wpos[wpos.length] = w1;
		hpos[hpos.length] = h1;
	}
	
	doExpand.call(this, container, image, imageIn, wpos, hpos, 0);
};

var doExpand = function(obj, image, imageIn, wpos, hpos, index) {
	var newpos = centerObj(obj, wpos[index], hpos[index], true);
	
	obj.style.width = wpos[index] + 'px';
	obj.style.height = hpos[index] + 'px';
	obj.style.left = newpos.x + 'px';
	obj.style.top = newpos.y + 'px';
	
	index++;
	
	if(index < wpos.length) {
		setTimeout(function() {doExpand.call(this, obj, image, imageIn, wpos, hpos, index);}, 30);
	} else {
		imageIn.src = image.src;
	}
};

var backFade = function(obj, from, to) {
	if(to > from) {
		from += 22;
		
		if(from > to)
			from = to;
	} else {
		from -= 22;
		
		if(from < to)
			from = to;
	}
	
	setAlpha(obj, from);
	
	if(from == 0)
		obj.style.display = 'none';
	
	if(from != to)
		setTimeout(function() {return backFade.call(this, obj, from, to);}, 30);
};

var getAlpha = function(obj) {
	if(obj.filters)	
		return obj.style.filter.alpha.opacity;
	
	if(obj.style.mozOpacity)
		return obj.style.MozOpacity * 100;
	
	return -1;
};

var setAlpha = function(obj, value) {
	if(value > 100)
		value = 100;
		
	if(value < 0)
		value = 0;
	
	if(obj.filters)	{
		obj.style.filter = 'alpha(opacity=' + value + ')';
		return;
	}
	
	obj.style.MozOpacity = Math.min(0.99, value / 100);
};

var centerObj = function(obj) {
	var getBack = !!(arguments[3]);
	
	var wx = document.body.offsetWidth;
	var wy = document.body.offsetHeight || window.innerHeight;
	
	var w = arguments[1] || parseInt(obj.style.width);
	var h = arguments[2] || parseInt(obj.style.height);
	
	var px = Math.round(wx / 2 - w / 2);
	var py = Math.round(wy / 2 - h / 2);
	
	if(getBack)
		return {x: px, y: py};
	else {
		obj.style.left = px + 'px';
		obj.style.top = py + 'px';
	}
};

