/**************
 * Por Jose conde
 * jose.conde@gmail.com
 */

function hideSubElements(div){
	for(var i=0;i<div.childNodes.length;i++){
		if(div.childNodes[i].style&&!(div.childNodes[i].nodeName=="IMG"||div.childNodes[i].nodeName=="img"))
			div.childNodes[i].style.display="none";
	}
}

function showSubElements(div,render){
	if(!render)render="block";
	for(var i=0;i<div.childNodes.length;i++){
		if(div.childNodes[i].style)
			div.childNodes[i].style.display=render;
	}
}


function imageResize(){
	this.modal=new modalPopup();
	this.images=new Array();
	this.div=document.createElement("DIV");
	this.div.id="imgDiv";
	this.div.style.zIndex=1000;
	this.div.style.display="none";
	this.div.className="popup";
	this.div.style.top=parseInt((parseInt(screen.availHeight)/2)+parseInt(document.documentElement.scrollTop))+"px";
	this.div.style.left=parseInt(parseInt(document.body.clientWidth)/2)+"px";
	this.div.style.height="0px";
	this.div.style.width="0px";
	document.body.appendChild(this.div);
	that=this;

	this.addImage=function(id,src,h,w){
		var img=new Image();
		img.src=src;
		var proporcion=h/w;
		if(h>w){
			if(h>(parseInt(screen.availHeight)-50)){
				h=parseInt(screen.availHeight-50);
				w=parseInt(h/proporcion);
			}
		}else{
			if(w>(parseInt(document.body.clientWidth)-50)){
				w=parseInt(document.body.clientWidth)-50;
				h=parseInt(w*proporcion);
			}
		}
		img.height=h;
		img.width=w;
		this.images.push(new Array(id,img,false,h,w));
		}

	this.getImage=function(id){
		for(var i=0;i<this.images.length;i++){
			if(id==this.images[i][0]){
				return this.images[i];
			}
		}
		return null;
	}

	this.start=function(id,speed){
		var arrImg=that.getImage(id);
		if(!arrImg[2]){
			var divX;
			if(!document.getElementById("X")){
				divX=document.createElement("DIV");
				divX.is="X";
				divX.className="X";
				divX.innerHTML="&nbsp;X&nbsp;";
			}else{
				divX=document.getElementById("X");
			}
			divX.onclick=function(){that.close(id,speed)};
			that.div.appendChild(divX);
			arrImg[1].height=0;
			arrImg[1].width=0;
			that.div.appendChild(arrImg[1]);
			hideSubElements(that.div);
			that.modal.showModal(that.div.id);
			arrImg[2]=true;
		}
		var i=parseInt(that.div.style.height);
		var j=parseInt(that.div.style.width);
		//this.expandX(id,speed);
		if(i<arrImg[3]){
			i=i+(speed*2);
			if(i>arrImg[3])i=arrImg[3];
			that.div.style.height=i+"px";
			that.div.style.top=(parseInt(that.div.style.top)-speed)+"px";
			arrImg[1].height=i;
			setTimeout("that.start('"+id+"',"+speed+")",0);
		}else if(j<arrImg[4]){
			j=j+(speed*2);
			if(j>arrImg[4])j=arrImg[4];
			that.div.style.width=j+"px";
			that.div.style.left=(parseInt(that.div.style.left)-speed)+"px";
			arrImg[1].width=j;
			setTimeout("that.start('"+id+"',"+speed+")",0);
		}else{
			showSubElements(that.div);
			arrImg[2]=false;
		}
	}

	this.close=function(id,speed){
		var arrImg=that.getImage(id);
		var i=parseInt(that.div.style.height);
		var j=parseInt(that.div.style.width);
		hideSubElements(this.div);
		if(i>0){
			i=i-(speed*2);
			if(i<0)i=0;
			that.div.style.height=i+"px";
			that.div.style.top=(parseInt(that.div.style.top)+speed)+"px";
			arrImg[1].height=i;
			setTimeout("that.close('"+id+"',"+speed+")",10);
		}else if(j>0){
			j=j-(speed*2);
			if(j<0)j=0;
			that.div.style.width=j+"px";
			that.div.style.left=(parseInt(that.div.style.left)+speed)+"px";
			arrImg[1].width=j;
			setTimeout("that.close('"+id+"',"+speed+")",10);
		}else{
			that.modal.hideModal(that.div.id);
		}
	}
	
	this.expandX=function(id,speed){
		var arrImg=that.getImage(id);
		var j=parseInt(that.div.style.width);
		if(j<arrImg[4]){
			j=j+(speed*2);
			if(j>arrImg[4])j=arrImg[4];
			that.div.style.width=j+"px";
			that.div.style.left=(parseInt(that.div.style.left)-speed)+"px";
			arrImg[1].width=j;
			setTimeout("that.expandX('"+id+"',"+speed+")",0);
		}else{
			return;
		}
	}
	
	this.expandY=function(id,speed){
		var arrImg=that.getImage(id);
		var i=parseInt(that.div.style.height);
		if(i<arrImg[3]){
			i=i+(speed*2);
			if(i>arrImg[3])i=arrImg[3];
			that.div.style.height=i+"px";
			that.div.style.top=(parseInt(that.div.style.top)-speed)+"px";
			arrImg[1].height=i;
			setTimeout("that.start('"+id+"',"+speed+")",0);
		}else{
			return;
		}
	}
}
