/**
 * @author kurokawamasahiro
 */

	function emslideshow(imgList,nArray){
		
		this.list=imgList;
		this.images=[];//image objectList
		this.count=0;
		this.space=$(nArray["gallaryId"]);//gallaryspace
		this.changeSec=nArray["fadeIn"];//表示スピード
		this.intervalSec=nArray["loop"];//切り替えスピード
		this.emptySec=nArray["fadeOut"];//消去スピード
		this.max=0;//スペース内の画像数
			
		this.loadImages=function(){
			var self=this;
			for(var i=0;i<this.list.length;i++){
				var img = new Image();
				img.src = this.list[i];
				img.onload=function(){
					self.loadEnd(this);
				}
			}
		}
		
		this.loadEnd = function(myImage){
			this.images[this.count]=this.imageSizeChecker(myImage);
			this.count++;
			if (this.count == this.list.length) {
				this.setImages();
			}
		}
		//img size
		this.imageSizeChecker=function(img){
			var rate=this.space.height()/img.height;
			return {"src":img.src,"height":Math.floor(img.height*rate),"width":Math.floor(img.width*rate)};
		}
		
		//
		this.showInterval=function(){
			
			var self=this;
			var myInterval=setInterval(function(){
				self.emptyImages();
			},this.intervalSec);
		}
		
		//ギャラリー消去		
		this.emptyImages=function(){
			
			for(var i=0; i<this.max;i++){
				$("img#gImage"+i).fadeOut(this.emptySec);
				$("a.crop").fadeOut(this.emptySec);
			}
			
			var self=this;
			var lp=setTimeout(function(){
				self.setImages();
			},1000);
		
		}
		//ギャラリー開始
		this.setImages=function(){
			
			var self=this;
			
			//ランダム			
			imgPick=[];			
			var lng1=this.images.length;
			var lng2=this.images.length;
			tmp=[];
			tmp2=[];
			for(var i=0;i<lng1;i++){
				tmp[i]=i;
			}
			for(var i=0;i<lng1;i++){
				tmp2.push(tmp.splice(Math.floor(Math.random()*lng2--),1));
			}
			
			//imgタグの挿入		
			var n=0;
			var myGallary="";			
			var myWidth=0;
			var spaceWidth=this.space.width();
			this.space.empty();
			
			var iv=setInterval(function(){
				var num = tmp2[n];
				if (num) {
					myWidth += self.images[num]["width"];
					$("#chk").text(myWidth + "/" + spaceWidth);
					if (spaceWidth < myWidth) {
						self.max = n;
						myGallary = "<a class='crop'><img width='" + self.images[num].width + "' height='" + self.images[num].height + "'></a>";
						var cropWidth = self.images[num].width - (myWidth - spaceWidth);
						self.space.append(myGallary);
						$("a.crop").css({
							"line-height": self.images[num].height + "px",
							"width": cropWidth + "px",
							"height": self.images[num].height + "px"
						});
						
						$("a.crop img").css({
							"opacity": 0.1
						}).attr('src', self.images[num].src).fadeTo(self.changeSec, 1);
						
						clearInterval(iv);
					}
					else {
						myGallary = "<img id='gImage" + n + "' class='gImage' width='" + self.images[num].width + "' height='" + self.images[num].height + "'>";
						self.space.append(myGallary);
						$("#gImage" + n).css({
							"opacity": 0.1
						}).attr('src', self.images[num].src).fadeTo(self.changeSec, 1);
						n++;
					}
				}
			},this.changeSec);
		}
	}