if(!Utils) throw('A Gallery oszály betöltéséhez a Utils-ra van szükség');

/**
 * Képgaléria
 *
 * @access public
 * @return void
 **/
var Gallery = function(n){
	this.name=n;
	this.container;
	this.canvas;
	this.thumbCont;
	this.thumbWin;

	this.images=new Array;
	this.subscripts=new Array;

	this.width			= 500;
	this.height			= 300;
	this.innerMargin	= 5;
	this.thumbCols		= 2;
	this.thumbWidth		= 60;
	this.thumbMargin	= 4;
	this.rollWidth		= 0;
	this.rollHeight		= 0;

	this.thBdrClr		= '#B4D337';
	this.thBdrOvrClr	= '#000000';

	this.pos			= 0;
	this.tmppos			= 0;
	this.scrolling		= false;
	this.tmpopac		= 0;
	this.toutopac		= null;

	this.autorot=true;
	this.timeout=null;
	this.nImgLoaded=0;
	this.nThumbLoaded=0;
	this.current=0;
	this.next=-1;

	this.imageDir	= 'images/gallery';
	this.thumbDir	= this.imageDir+'/thumbnails';
	this.thumbPfx	= '';
	this.previewDir	= this.imageDir;

	Utils.addInit(this.name+'.init');
}

Gallery.prototype.init = function(){
	if(!this.container) this.container='gallery_'+this.name;
	if(this.container=document.getElementById(this.container)){
		if(this.images.length){
			this.create();

			if(ImgPreloader){
				ImgPreloader.dir=this.dir;
				ImgPreloader.preLoad(this.images);
			}

			this.loadThumbnails();
			this.loadImages();
		}
	} else throw(this.name+'.container nem letezik!');
}

Gallery.prototype.add = function(img,desc){
	var i=0;
	if( img instanceof Array ) { for(i in img) if(typeof img[i]=='string') this.images.push(img[i]); }
	else if(typeof img=='string') this.images.push(img);
	if( desc instanceof Array ) { for(i in desc) if(typeof desc[i]=='string') this.subscripts.push(desc[i]); }
	else if(typeof desc=='string') this.subscripts.push(desc);
}

Gallery.prototype.set = function(arg){
	for(var a in arg) this[a]=arg[a];
}

Gallery.prototype.create = function(){
	this.rollWidth = parseInt(this.thumbCols*this.thumbWidth+(this.thumbCols+2)*this.thumbMargin);

	var panel	= Ł('div',{
		'id' : this.name+'_panel',
		'class' : 'gallery_panel'
	},{
		'width' 	: parseInt(this.width)+'px',
		'height'	: parseInt(this.height)+'px',
		'position'	: 'relative',
		'border'	: 'solid 1px #fff',
		'background': '#1E791B'
	});

	this.canvas	= Ł('div',{
		'id':this.name+'_canvas'
	},{
		'width'		: parseInt(this.width-this.rollWidth-3*this.innerMargin)+'px',
		'height'	: parseInt(this.height-2*this.innerMargin)+'px',
		'position'	: 'absolute',
		'top'		: parseInt(this.innerMargin)+'px',
		'left'		: parseInt(this.innerMargin)+'px',
		'overflow'	: 'hidden'
	});

	this.thumbWin= Ł('div',{
		'id':this.name+'_thumbWin'
	},{
		'width'		: parseInt(this.rollWidth)+'px',
		'height'	: parseInt(this.height-2*this.innerMargin)+'px',
		'position'	: 'absolute',
		'top'		: parseInt(this.innerMargin)+'px',
		'left'		: parseInt(this.width-this.rollWidth-this.innerMargin)+'px',
		'overflow'	: 'hidden'
	});
	Utils.addListener(this.thumbWin,'mousemove',this.thumbWinMouseMove);
	Utils.setAttributes(this.thumbWin,{'gallery':this.name});

	this.thumbCont= Ł('div',{
		'id':this.name+'_thumbCont'
	},{
		'width'		: parseInt(this.rollWidth)+'px',
		'height'	: '0px',
		'position'	: 'relative',
		'top'		: '0px',
		'left'		: '0px'
	});

	Utils.appendChild(this.thumbWin,this.thumbCont);
	Utils.appendChild(panel,this.canvas,this.thumbWin);
	Utils.appendChild(this.container,panel);
}

Gallery.prototype.loadThumbnails = function(){
	this.thumbDir	= this.imageDir+'/thumbnails';

	var thumb;
	for(var i=0;i<this.images.length;i++){
		thumb=Ł('img',{
			'src'		: this.thumbDir+'/'+this.thumbPfx+this.images[i]+'?r='+Math.random()
		},{
			'border'	: 'solid 1px '+this.thBdrClr,
			'margin'	:parseInt(this.thumbMargin/2)+'px',
			'float'		: 'left',
			'cursor'	: 'pointer',
			'display'	: 'none'
		});

		Utils.setAttributes(thumb,{'gallery':this.name,'idx':i});

		Utils.addListener(thumb,'click',this.thumbZoom);
		Utils.addListener(thumb,'load',this.thumbLoaded);
		Utils.addListener(thumb,'mouseover',this.thumbMouseOver);
		Utils.addListener(thumb,'mouseout',this.thumbMouseOut);

		Utils.appendChild(this.thumbCont,thumb);
	}
}

Gallery.prototype.thumbLoaded=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));

	Utils.setStyle(ed.target,{'display':'block'});

	var iRows=parseInt(myGallery.images.length/myGallery.thumbCols);
	if(myGallery.images.length%myGallery.thumbCols>0) iRows++;
	var h=(iRows*(ed.target.height*myGallery.thumbWidth/ed.target.width))+(iRows+1)*(myGallery.thumbMargin+2);

	Utils.setStyle(ed.target,{'width':parseInt(myGallery.thumbWidth)+'px','height':parseInt(ed.target.height*myGallery.thumbWidth/ed.target.width)+'px'});
	Utils.setStyle(myGallery.thumbCont,{'height':h+'px'});
}

Gallery.prototype.thumbZoom=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));

	myGallery.next=ed.target.getAttribute('idx');
	myGallery.rotate();
}

Gallery.prototype.thumbMouseOver=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));
	Utils.setStyle(ed.target,{'borderColor':myGallery.thBdrOvrClr});
}
Gallery.prototype.thumbMouseOut=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));
	Utils.setStyle(ed.target,{'borderColor':myGallery.thBdrClr});
}
Gallery.prototype.thumbWinMouseMove=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));
	if(!myGallery) return false;

	var tW=myGallery.thumbWin;
	var tC=myGallery.thumbCont;

	if(tC.offsetHeight>tW.offsetHeight){
		var t=((tW.offsetHeight-myGallery.thumbWidth-tC.offsetHeight)/(tW.offsetHeight-myGallery.thumbWidth))*(ed.mouseY-Utils.getPosition(tW).top-myGallery.thumbWidth/2);
		if(t>0) t=0;
		if(t<(tW.offsetHeight-tC.offsetHeight)) t=(tW.offsetHeight-tC.offsetHeight);
		if(!myGallery.scrolling){
			if(Math.abs(myGallery.tmppos-t)>tW.offsetHeight) myGallery.scrollTo(t);
			else {
				Utils.setStyle(myGallery.thumbCont,{'top':parseInt(t)+'px'});
				myGallery.tmppos=parseInt(t);
			}
		}
	}
}

Gallery.prototype.scrollTo=function(y){
	this.tmppos=this.pos;
	this.pos=y;
	this.scrolling=true;
	this.scroll();
}

Gallery.prototype.scroll=function(){
	if(Math.abs(this.tmppos-this.pos)>0.2){
		this.tmppos+=(this.pos-this.tmppos)/10;
		Utils.setStyle(this.thumbCont,{'top':parseInt(this.tmppos)+'px'});
		this.timeout=setTimeout(this.name+'.scroll()',10);
	} else this.scrolling=false;
}

Gallery.prototype.loadImages = function(){
	var image;
	for(var i=0;i<this.images.length;i++){
		image=Ł('img',{
			'id'		: this.name+'_'+i,
			'src'		: this.imageDir+'/'+this.images[i]
		},{
			'position'	: 'absolute',
			'top'		: '0px',
			'left'		: '0px',
			'cursor'	: 'pointer',
			'display'	: 'none'
		});

		Utils.setAttributes(image,{'gallery':this.name,'idx':i});

		Utils.addListener(image,'click',this.imageZoom);
		Utils.addListener(image,'load',this.imageLoaded);
		Utils.addListener(image,'mouseover',this.imageMouseOver);
		Utils.addListener(image,'mouseout',this.imageMouseOut);

		Utils.appendChild(this.canvas,image);
	}
}

Gallery.prototype.imageLoaded=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));
	var img=ed.target;
	var cnv=myGallery.canvas;

	Utils.setStyle(img,{'display':'block','visibility':'hidden'});
	var orig_w=img.width;
	var orig_h=img.height;
	var wmax=cnv.offsetWidth;
	var hmax=cnv.offsetHeight;
	var w,h,t,l;

	if((orig_w>wmax)||(orig_h>hmax)){
		w=wmax;
		h=hmax;

		if (w &&(orig_w<orig_h)) w=(h/orig_h)*orig_w;
		else h=(w/orig_w)*orig_h;
	} else {
		w=orig_w;
		h=orig_h;
	}
	t=parseInt((hmax-h)/2);
	l=parseInt((wmax-w)/2);

	var opac=(img.getAttribute('idx')==myGallery.current)?100:0;
	var dsp =(img.getAttribute('idx')==myGallery.current)?'block':'none';
	Utils.changeOpac(img,opac);
	Utils.setStyle(ed.target,{'display':dsp,'visibility':'visible','width':w+'px','height':h+'px','top':t+'px','left':l+'px'});
}

Gallery.prototype.imageZoom=function(e){
	var ed=Utils.getEventDetails(e);
	var myGallery=eval(ed.target.getAttribute('gallery'));

	if(ImageBox) ImageBox.show(myGallery.imageDir+'/'+myGallery.images[ed.target.getAttribute('idx')],myGallery.subscripts[ed.target.getAttribute('idx')]);
}

Gallery.prototype.imageMouseOver=function(e){
	var ed=Utils.getEventDetails(e);
}

Gallery.prototype.imageMouseOut=function(e){
	var ed=Utils.getEventDetails(e);
}

Gallery.prototype.rotate=function(){
	if(this.next==this.current) return true;

	var nextImg=$(this.name+'_'+this.next);
	Utils.changeOpac(nextImg,this.tmpopac);
	Utils.setStyle(nextImg,{'display':'block'});

	this.fade();
}

Gallery.prototype.fade=function(){
	var curImg=$(this.name+'_'+this.current);
	var nextImg=$(this.name+'_'+this.next);

	if(this.tmpopac<100){
		this.tmpopac+=10;

		Utils.changeOpac(curImg,100-this.tmpopac);
		Utils.changeOpac(nextImg,this.tmpopac);

		this.toutopac=setTimeout(this.name+'.fade()',50);
	} else {
		Utils.setStyle(curImg,{'display':'none'});
		this.current=this.next;
		this.next=-1;
		this.tmpopac=0;
	}
}
