// image slideshow
function imgShow($el,option){
	shw.path=option.path;
	shw.img=option.img;
	shw.delay=option.delay;
	shw.trans=option.trans;
	shw.trans2=option.trans2;
	shw.speed=option.speed;
	shw.transDual=option.transDual;
	if($el.length>0) shw.init($el);
};

var shw = {
	timer	:	null,
	ct		:	0,
	num		:	0,
	root	:	null,
	owidth	:	0,
	oheight	:	0,
	dir		:	'next',
	nwidth	:	0,
	nheight	:	0,
	nextImg	:	new Image,
	getImg	:	function(path){
					this.nextImg.src = this.path+path.src;
					this.nwidth = this.nextImg.width;
					this.nheight = this.nextImg.height;
				},
	preload	:	function(){
					for(var i = 0; i<this.img.length; i++){
						$("<img>").attr("src", this.path+this.img[i]['src']);
					}
				},
	init	:	function(img){
					$this = this;
					$img = $(img);
					$this.preload();
					// setup vars
					$this.num = $this.img.length;
					$this.owidth = $img.width();
					$this.oheight = $img.height();
					$this.ct = 1; // assuming the image that starts the show is the first in the array
					// build show
					$img.after("<div class='shwWrap'><div class='shwRoot'></div></div>");
					$img.next().find(".shwRoot").append($img);
					$this.root = $root = $img.parent(".shwRoot");
					$root.css({
						overflow:"hidden",
						width: $this.owidth+"px",
						height: $this.oheight+"px"
					});
					$img.css({
						float:"left"
					});
					var delayDif = this.delay-3000;
					if(delayDif>=4000){$this.loadimg('next');}
					else{setTimeout(function(){$this.loadimg('next')}, ((delayDif>0)?delayDif:3000)); /*delay for Preloading*/}
				},
	loadimg:	function(){
					// load next/last img
					this.getImg(this.img[this.ct]);
					this.play();
				},
	play:		function(){
					$this = this;
					$this.timer = setTimeout(function(){$this.imgGo()}, $this.delay);
				},
	imgGo:		function(){
					var $img = this.img[this.ct];
					// add next img
					this.root
						.append("<img alt='"+$img.title+"' src='"+this.path+$img.src+"' width='"+this.nwidth+"' height='"+this.nheight+"' />")
						.find("img[src='"+this.path+$img.src+"']")
						.css({float:"left",margin:'0px',padding:'0px'});
					
					this.changeImg();
					
					if(this.dir != 'last') this.dir='next';
					if(this.dir=='next'){
						if(this.ct<this.num-1) this.ct++;
						else{
							this.ct = 0;
						}
					}else{
						if(this.ct>0) this.ct--;
						else this.ct = this.num;
					}
					this.loadimg();
				},
	changeImg:	function(){
					var $root = this.root;
					var $img = this.img[this.ct];
					var $nimg = $root.find("img[src='"+this.path+$img.src+"']");
					var $oimg = $root.find("img:first");
					
					if(this.transDual){eval("$nimg.hide()."+this.trans2+"("+this.speed+"); $oimg."+this.trans+"("+this.speed+", function(){$oimg.remove();})");}
					else{eval("$nimg.hide(); $oimg."+this.trans+"("+this.speed+", function(){$nimg."+this.trans2+"("+this.speed+"); $oimg.remove();})");}
				}
};
jQuery(document).ready(function(){
	imgShow(
		$("img.slideshow"),
		{
			path	: 'photos/homes/gallery/',
			img		: eval("["+stringVar($("img.slideshow").attr("class"),'imgs')+"]"),
			delay	: 5000,
			trans	: 'fadeOut',
			trans2	: 'fadeIn',
			speed	: 500,
			transDual: false
		}
	);
});



