var enlargePhotos = function() {
	this.parentArea = null;
	this.area = null;
	this.areaTarget = null;
	this.closeButton = null;
	this.imageLoad = null;
	this.description = null;

	this.cssFile = URL + '/js/enlargePhotos/css/enlargePhotos.css';
	addCss(this.cssFile);

	this.urlSmall = '/p/';
	this.urlBig = '/g/';

};

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) {
	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 = _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 = _target.getElementsByTagName("img");
	var o = this;
	for(var i = 0; i<c.length ; i++) {
		c[i].onclick = function() {
			o.enlargeThis(this);
			return false;
		}
	}
};

enlargePhotos.prototype.toEnlarge = function(caller, _target) {
	var o = this;
	caller.onclick = function() {
		o.enlargeThis(_target);
		return false;
	}
}
