/**
 * Beágyazott slideshow
 *
 **/

var InSlideShow = function(idx,sID,cID){
	this.index				= idx;
	this.slideID			= sID;
	this.contID				= cID;
	this.container			= null;
	this.width				= 0;
	this.height				= 0;

	this.imageBox			= (typeof(ImageBox)!='undefined')?ImageBox:null;
	this.preview			= false;
	this.canvas				= null;
	this.roller				= null;
	this.btnLeft			= null;
	this.btnRight			= null;
	this.images				= new Array;
	this.loaded				= new Array;
	this.imgleft			= new Array;
	this.description		= new Array;
	this.loadThumb			= true;
	this.nLoaded			= 0;
	this.baseDir			= '';
	this.incdir				= 'inc';
	this.imageDir			= this.baseDir+'images/inslideshow';
	this.thumbsDir			= this.imageDir+'/thumbs';
	this.suffix				= '_thumb';
	this.margin				= 5;
	this.rollwidth			= 0;
	this.pos				= 0;
	this.tmppos				= 0;
	this.maxpos				= 0;
	this.shadowColor		= 'gray';
	this.btnWidth           = 25;
	this.borderWidth		= 1;
	this.borderColor		= 'black';
	this.borderOverColor	= 'blue';
	this.contBorder			= 'solid 1px #E6E6E6';
	this.backColor			= '#F2F2F2';
	this.canvasColor		= '#EEEEEE';
	this.current			= -1;
	this.autoTimeout;
	this.autodir			= 1;
	this.autoDelay			= 3000;
	this.doAuto				= true;
	this.timeout;
	this.zIndex				= 0;
	this.isIE				= (navigator.userAgent.toLowerCase().indexOf( 'msie', 0 )>0);
}

InSlideShow.prototype.init=function(){
	this.container = document.getElementById(this.contID);
	this.setBaseDir(SlideShowLoader.baseDir);
}

InSlideShow.prototype.Create=function(){
	if(parseInt(this.width)==0) this.width=this.container.parentNode.offsetWidth;

	Utils.setStyle(this.container,
		{
			'width'    : parseInt(this.width)+'px',
			'height'   : parseInt(this.height)+'px',
			'margin'   : parseInt(this.margin)+'px',
			'border'   : this.contBorder,
			'position' : 'relative',
			'zIndex'   : this.zIndex
		}
	);

	this.btnLeft  = InSlideShowButton.Create('left',this.index);
	this.btnRight = InSlideShowButton.Create('right',this.index);
	this.canvas   = InSlideShowCanvas.Create(this.index);
	this.roller   = InSlideShowRoller.Create(this.index);

	Utils.appendChild(this.canvas,this.roller);
	Utils.appendChild(this.container,this.btnLeft,this.canvas,this.btnRight);

	Utils.position(this.canvas,(parseInt(this.width)-2*parseInt(this.btnWidth))+'px','100%',(parseInt(this.btnWidth)+1)+'px','0px');
	Utils.position(this.btnLeft,parseInt(this.btnWidth)+'px','100%','0px','0px');
	Utils.position(this.btnRight,parseInt(this.btnWidth)+'px','100%',(parseInt(this.width)-parseInt(this.btnWidth))+'px','0px');

	Utils.setStyle(this.roller,
		{
			'height' : parseInt(this.height)+'px',
			'left'   : parseInt(this.pos)+'px'
		}
	);

	this.loadThumbnails();
}

InSlideShow.prototype.setParams=function(parameters){
	for(var p in parameters) eval('this.'+p+'="'+parameters[p]+'"');
}

InSlideShow.prototype.setBaseDir=function(newDir){
	this.baseDir	= newDir;
	this.imageDir	= this.baseDir+'images/inslideshow';
	this.thumbsDir	= this.imageDir+'/thumbs';
}

InSlideShow.prototype.addImages=function(images){
	for(var i=0;i<images.length;i++){
		this.images[i]=images[i].name;
		this.description[i]=images[i].description;
	}
}

InSlideShow.prototype.setSize=function(w,h){
	if(w) this.width=(w!='auto')?w:this.container.parentNode.offsetWidth;
	if(h) this.height=h;
}

InSlideShow.prototype.Scroll=function(direction){
	if(direction=='left') InSlideShowRoller.ScrollLeft(this.index);
	else InSlideShowRoller.ScrollRight(this.index);
}

InSlideShow.prototype.prepareSuffix=function(name,suffix){
	var pos=Utils.strpos(name,'.');
	return name.substring(0,pos)+suffix+name.substring(pos);
}

InSlideShow.prototype.Refresh=function(){
	for(var i=this.roller.childNodes.length; i>=0; i--) this.roller.removeChild(this.roller.childNodes[i]);
	this.loadThubnails();
}

InSlideShow.prototype.pauseAutoScroll=function(){ this.doAuto=false; }
InSlideShow.prototype.resumeAutoScroll=function(){ this.doAuto=true; }

InSlideShow.prototype.loadThumbnails=function(){
	var thumbCont,thumb,src;
	for(var i=0; i<this.images.length; i++){
		thumbCont = Utils.createElement('div');
		Utils.setStyle(thumbCont,
			{
				'margin'     : (parseInt(this.margin)-parseInt(this.borderWidth))+'px',
				'styleFloat' : 'left',
				'cssFloat'   : 'left',
				'zIndex'     : this.zIndex+2
			}
		);

		thumb = Utils.createElement('img');
		Utils.setAttributes(thumb,{'slideIndex':this.index,'imageIndex':i});

		Utils.addListener(thumb,'click',this.imgZoom);
		Utils.addListener(thumb,'load',this.imgLoaded);
		Utils.addListener(thumb,'mouseover',this.imgMouseOver);
		Utils.addListener(thumb,'mouseout',this.imgMouseOut);

		Utils.setStyle(thumb,
			{
				'cursor'   : (this.isIE)?'hand':'pointer',
				'display'  : 'none',
				'position' : 'relative',
				'border'   : 'solid '+this.borderWidth+'px '+this.borderColor,
				'zIndex'   : this.zIndex+4
			}
		);

		Utils.appendChild(thumbCont,thumb);
		Utils.appendChild(this.roller,thumbCont);

		Utils.setAttributes(thumb,{'src':this.thumbsDir+'/'+this.prepareSuffix(this.images[i],this.suffix)});
	}
}

InSlideShow.prototype.imgLoaded=function(e){
	var ed=Utils.getEventDetails(e);
	var slideShow=SlideShowLoader.slideShows[ed.target.getAttribute('slideIndex')].Slide;

	Utils.setStyle(ed.target,{'display':'block'});
	slideShow.rollwidth+=ed.target.width+2*parseInt(slideShow.margin);
	Utils.setStyle(slideShow.roller,{'width':parseInt(slideShow.rollwidth)+'px','height':'100px'});

	slideShow.nLoaded++;
	if(slideShow.nLoaded==slideShow.images.length){
		var shadow,width=0,height=0;
		var arImages=slideShow.container.getElementsByTagName('img');
		for(i=0;i<arImages.length;i++){
			slideShow.imgleft[i]=arImages[i].offsetLeft-slideShow.margin;

			shadow=Utils.createElement('div');
			Utils.setStyle(shadow,
				{
					'position'   : 'absolute',
					'width'      : (arImages[i].offsetWidth)+'px',
					'height'     : ((arImages[i].offsetHeight)?arImages[i].offsetHeight:arImages[i].height)+'px',
					'left'       : (arImages[i].offsetLeft+3)+'px',
					'top'        : (arImages[i].offsetTop+3)+'px',
					'background' : slideShow.shadowColor,
					'zIndex'     : parseInt(slideShow.zIndex)+3
				}
			);

			arImages[i].parentNode.appendChild(shadow);

			width+=arImages[i].parentNode.offsetWidth+2*slideShow.margin;
			if(arImages[i].parentNode.offsetHeight>height) height=arImages[i].parentNode.offsetHeight;
		}

		if(parseInt(slideShow.width)==0){
			slideShow.width=(width<=slideShow.container.parentNode.offsetWidth)?width:slideShow.container.parentNode.offsetWidth;
			slideShow.container.style.width=parseInt(slideShow.width)+'px';
		}

		if(parseInt(slideShow.height)==0){
			slideShow.height=height;
			slideShow.container.style.height=(parseInt(slideShow.height)+2*parseInt(slideShow.margin))+'px';
		}

		slideShow.maxpos=parseInt(slideShow.rollwidth)-slideShow.canvas.offsetWidth+parseInt(slideShow.margin);
		if(slideShow.doAuto&&(slideShow.rollwidth>slideShow.canvas.offsetWidth))
			slideShow.autoTimeout=setTimeout('InSlideShowRoller.autoScroll('+slideShow.index+')',10);

		SlideShowLoader.slideShows[ed.target.getAttribute('slideIndex')].loaded=true;
	}
}

InSlideShow.prototype.imgZoom=function(e){
	var ed=Utils.getEventDetails(e);
	var slideShow=SlideShowLoader.slideShows[ed.target.getAttribute('slideIndex')].Slide;
	var imageIndex=ed.target.getAttribute('imageIndex');

	if(slideShow.imageBox) ImageBox.show(slideShow.imageDir+'/'+slideShow.images[imageIndex],slideShow.description[imageIndex]);
}

InSlideShow.prototype.imgMouseOver=function(e){
	var ed=Utils.getEventDetails(e);
	var slideShow=SlideShowLoader.slideShows[ed.target.getAttribute('slideIndex')].Slide;
	slideShow.pauseAutoScroll();
	Utils.setStyle(ed.target,{'borderColor':slideShow.borderOverColor})
}
InSlideShow.prototype.imgMouseOut=function(e){
	var ed=Utils.getEventDetails(e);
	var slideShow=SlideShowLoader.slideShows[ed.target.getAttribute('slideIndex')].Slide;
	slideShow.resumeAutoScroll();
	Utils.setStyle(ed.target,{'borderColor':slideShow.borderColor})
}
