//ギャラリーオブジェクトコンストラクタ
var Gallery = function(initObject){

	try {
		//初期化
		that = this;
	
		//変数代入
		this.id = initObject.id;
		this.width = initObject.width;
		this.height = initObject.height;
		this.delay = initObject.delay || 4000; //デフォルト切り替え時間 4秒
	
		//images 初期化
		var images = initObject.images.split(",");
		this.images = [];
		for (var i = 0; i < images.length; i ++) {
			if(images[i] != '') {
				var img = new Image();
				img.src = images[i];
				//this.images[i] = {src: images[i]};
				this.images[i] = img;
			}
		}
	
		jQuery(document).ready(function(){
			that.start();
		});
	
		return this;
	
	} catch(err) {
		//console.log(err);
	}

}

//ギャラリー開始
Gallery.prototype.start = function(){

	if(typeof this.galleryTimer === "undefined") {
		//各種初期化
		var that = this;
		this.count = 0;
		this.frontPhoto = jQuery(this.id);
		this.initGallery(); //ギャラリーの初期化
	
		//jQuery.each(this.images,function(i) {
		//	jQuery('body').append('<div style="display: none;"><img id="loader_'+i+'" src="'+this.src+'" /></div>');
		//	this.image = document.getElementById("loader_"+i);
		//});
	
		this.checkLoadFinTimer = setInterval(function(){that.checkLoadFin();}, 200);
		
	} else {
		this._start();
	}

}

//ギャラリー初期化メソッド
Gallery.prototype.initGallery = function() {
	this.frontPhoto.css({'position': 'absolute', top: 0, left: 0, width: this.width, height: this.height}).attr('alt', 'photo').parent().css({'position':'relative'}).append('<img id="back_photo" alt="photo" />');
	this.backPhoto = jQuery('#back_photo');

	this.backPhoto.css({width: this.width, height: this.height}).attr("src", this.images[0].src);

}

//ギャラリー画像読み込みチェックメソッド
Gallery.prototype.checkLoadFin = function() {
	var num = 0;
	var taht = this;
	
	jQuery.each(this.images,function(i) {
		//if(this.image != undefined && this.image.complete == true) { num++ };
		if(this.complete == true) { num++ };
	});

	if(num == this.images.length){
		clearInterval(this.checkLoadFinTimer);
		//this.galleryTimer = setInterval(function(){taht.slidePhoto();}, this.delay);
		this._start();
		return;
	}

}

//ギャラリー自動差し替え開始メソッド
Gallery.prototype._start = function() {
	var that = this;
	clearInterval(this.galleryTimer);
	this.galleryTimer = setInterval(function(){that.slidePhoto();}, this.delay);
}

//ギャラリー画像差し替えメソッド
Gallery.prototype.slidePhoto = function() {
	var _front = this.frontPhoto;
	var _back = this.backPhoto;

	var _frontImg = this.images[this.count].src ;
	var _backImg = (this.count == this.images.length - 1)? this.images[0].src : this.images[this.count+1].src ;

	if(_front.css('opacity') < .5 ) {
		_front.attr("src", _frontImg).animate({opacity: 0}, 100,function(){_back.attr("src", _backImg); _front.css({opacity: 1}).animate({opacity: 0},2000);});
	} else {
		_front.attr("src", _frontImg).animate({opacity: 0},2000);
		_back.attr("src", _backImg);
	}

	this.count = (this.count == this.images.length-1)? 0 : this.count + 1;
}

//ギャラリー停止
Gallery.prototype.stop = function() {
	clearInterval(this.galleryTimer);	
}


//ギャラリー作成
//var gallery = new Gallery('#front_photo',477,307,"/assets/templates/hatchery/img/slide/slide_01.jpg,/assets/templates/hatchery/img/slide/slide_02.jpg,/assets/templates/hatchery/img/slide/slide_03.jpg,/assets/templates/hatchery/img/slide/slide_04.jpg");
