var enlargePhotos = function(/*toEnlarge, urlSmall, urlBig*/) {
	this.parentArea = null;
	this.area = null;
	this.areaTarget = null;
	this.closeButton = null;
	this.imageLoad = null;
	this.description = null;

	this.cssFile = URL + 'application/js/enlargePhotos/css/enlargePhotos.css';
	addCss(this.cssFile);

	this.urlSmall = '/s/';
	this.urlBig = '/b/';

	var a = arguments;

	if(a[0])
	{
		if(a[1])  this.urlSmall = a[1];
		if(a[2]) this.urlBig = a[2];
		if( a[0].tagName )
		{
			if(a[0].tagName.toLowerCase()=='img')
				return this.toEnlarge(a[0], a[0]);
			else if(a[0].tagName.toLowerCase()=='a') {
				var tg = $t('img', a[0])[0];
				if(!tg) tg = a[0];
				return this.toEnlarge(a[0], tg);
			}
		}

		this.childsToEnlarge(a[0]);
	}

};

enlargePhotos.prototype.setCssFile = function(_src) { this.cssFile = _src; }

enlargePhotos.prototype.createArea = function(_target) {
	if(this.area) return;
	var _target = isset(_target) ? _target : document.body;

	var ua = usefullArea();
	var h = document.body.offsetHeight>ua['y']  ? document.body.offsetHeight : ua['y'];

	this.parentArea =  cE("div", _target, 'parentImageLoadArea');
	this.parentArea.style.width = document.body.offsetWidth + 'px';
	this.parentArea.style.height = h + 'px';

	this.area =  cE("div", _target, 'imageLoadArea');

	this.closeButton = cE('a', this.area, 'closeImageLoadArea');
	this.closeButton.innerHTML = "fechar";
	this.closeButton.setAttribute("href", "javascript:;");
	var o = this;
	this.closeButton.onclick = this.parentArea.onclick = function() {
		o.area.parentNode.removeChild(o.area);
		o.parentArea.parentNode.removeChild(o.parentArea);
		o.area=null;
	};

	centralizeElement(this.area);


	this.areaTarget = cE('div', this.area, 'imageLoadAreaTarget');

	this.imageLoad = cE('img', this.areaTarget, 'imageLoad');
	this.showHideImage(false);

	this.description = cE('p', this.areaTarget, 'descriptionTextArea');
};

enlargePhotos.prototype.showHideImage = function(sh) {
	if(sh) this.imageLoad.style.display = 'block';
	else this.imageLoad.style.display = 'none';
};

enlargePhotos.prototype.formatSrc = function(objImg) {
	if(typeof objImg=='string') return objImg;
	var _src = objImg.getAttribute("src");
	_src = (!_src) ? objImg.getAttribute("href") : _src ;
	if(!_src) return false;
	var newSrc = _src.replace(this.urlSmall, this.urlBig);
	return newSrc;
};

enlargePhotos.prototype.enlargeThis = function(_target) {
	var description = (typeof _target!='string') ? _target.getAttribute("title") : '';

	this.createArea();

	this.showHideImage(false);
	this.area.className = 'loading';

	var _img = new Image();
	var o = this;
	_img.onload = function() {
		o.imageLoad.src = this.src;
		o.area.className = '';
		o.showHideImage(true);
		o.area.style.width = this.width + 'px';
		if(description)
			o.description.innerHTML = description;
		centralizeElement(o.area);
		o.area.style.visibility = 'visible';
	}
	_img.src = this.formatSrc(_target);
};

enlargePhotos.prototype.childsToEnlarge = function(_target)
{
	var c = $t("a", _target);
	var o = this;
	c.forEach(
		function(e) {
			var _img = $t('img', e)[0];
			if(_img)
			{
				e.onclick = function(){ o.enlargeThis($t('img', this)[0]); return false; };
			}
		}
	);
};

enlargePhotos.prototype.toEnlarge = function(caller, _target) {
	var o = this;
	caller.onclick = function() {
		o.enlargeThis(_target);
		return false;
	}
}