//->from slideshow.js
$(function(){
	$(".slideshow").each(function(){
		var slideShowModule = new SlideShowModule(this)
	});
	$(".slideshow_sp").each(function(){
		var slideShowModule = new SlideShowSpModule(this)
	});
	$(".slideshow_link").each(function(){
		var slideShowModule = new SlideShowLinkModule(this)
	});
})

var SlideShowModule = function(){
	this.num_images = 0;
	this.image_container = null;
	this.initialize.apply(this, arguments);
}
SlideShowModule.prototype = {
	initialize: function(container) {
		$(container).prepend('<div class="img_container"></div>');
		this.image_container = $(".img_container", $(container));
		
		this.intervalMs = 5000;
		if($(container).attr("interval")) this.intervalMs = $(container).attr("interval");
		
		var ref_image_container = this.image_container;
		var ref = this;
		$("img",$(container)).each(function(){
			$(this).appendTo($(ref_image_container));
			ref.num_images ++;
		})
		this.init();
    },
	init: function() {
		$("img",$(this.image_container)).each(function(){
			$(this).css({"position":"absolute", "display":"none"});
			$(this).fadeTo(0,0);
		})
		if(this.num_images == 1){
			var newObj = $("img:first",$(this.image_container));
			newObj.fadeTo(1000,1);
			newObj.appendTo($(this.image_container))
		}else{
			this.start();
		}
    },
	start: function() {
		this.setNewImage(0);
		var ref = this;	
		setInterval(function(){ref.setNewImage(1000);}, this.intervalMs);
	},
	setNewImage: function(t) {
		var prevObj = $("img:last",$(this.image_container));
		var newObj = $("img:first",$(this.image_container));
		newObj.fadeTo(t,1, function(){prevObj.fadeOut(0);});
		newObj.appendTo($(this.image_container))
	}
};


var SlideShowSpModule = function(){
	this.num_images = 0;
	this.container = null;
	this.image_container = null;
	this.initialize.apply(this, arguments);
}
SlideShowSpModule.prototype = {
	initialize: function(container) {
		
		$(container).prepend('<div class="img_container"></div>');
		this.image_container = $(".img_container", container);
		this.container = container;
		$("br", container).remove();  
		
		this.intervalMs = 5000;
		if($(container).attr("interval")) this.intervalMs = $(container).attr("interval");
		
		var ref_image_container = this.image_container;
		var ref = this;
		$("img",$(container)).each(function(){
			if(ref.num_images==0){
//				$clone = $(this).clone();
//				$clone.appendTo($(container));
//				$clone.css("visibility","hidden");
				$(this).appendTo($(ref_image_container));
			}else{
				$(this).appendTo($(ref_image_container));
			}
			ref.num_images ++;
		})
		$('<div class="SlideShowSpModuleBg"></div>').appendTo(container);
		this.init();
    },
	init: function() {
		$("img",$(this.image_container)).each(function(){
			$(this).css({"position":"absolute", "display":"none"});
			$(this).fadeTo(0,0);
		})
		if(this.num_images == 1){
			var newObj = $("img:first",$(this.image_container));
			newObj.fadeTo(1000,1);
			newObj.appendTo($(this.image_container))
		}else{
			this.start();
		}
    },
	start: function() {
		this.setNewImage(0);
		var ref = this;	
		setInterval(function(){ref.setNewImage(1000);}, this.intervalMs);
	},
	setNewImage: function(t) {
		var prevObj = $("img:last",$(this.image_container));
		var newObj = $("img:first",$(this.image_container));
		newObj.fadeTo(t,1, function(){prevObj.fadeOut(0);});
		newObj.appendTo($(this.image_container));
		$(".SlideShowSpModuleBg", this.container).height(newObj.height());
		
		var ref = this;
		var checkHeight = function(){
			if(newObj.height() == 0){
				setTimeout(function(){
						checkHeight();
					}, 30
				);
			}else{
				$(".SlideShowSpModuleBg", ref.container).height(newObj.height());
			}
		}
		checkHeight();
		
	}
};



var SlideShowLinkModule = function(){
	this.num_images = 0;
	this.image_container = null;
	this.initialize.apply(this, arguments);
}
SlideShowLinkModule.prototype = {
	initialize: function(container) {
		$(container).prepend('<div class="img_container"></div>');
		this.image_container = $(".img_container", $(container));
		var ref_image_container = this.image_container;
		var ref = this;
		$(".a_img",$(container)).each(function(){
			$(this).appendTo($(ref_image_container));
			ref.num_images ++;
		})
		this.init();
    },
	init: function() {
		$(".a_img",$(this.image_container)).each(function(){
			$(this).css({"position":"absolute", "display":"none"});
			$(this).fadeTo(0,0);
		})
		if(this.num_images == 1){
			var newObj = $(".a_img:first",$(this.image_container));
			newObj.fadeTo(1000,1);
			newObj.appendTo($(this.image_container))
		}else{
			this.start();
		}
    },
	start: function() {
		this.setNewImage(0);
		var ref = this;	
		setInterval(function(){ref.setNewImage(1000);}, 5000);
	},
	setNewImage: function(t) {
		var prevObj = $(".a_img:last",$(this.image_container));
		var newObj = $(".a_img:first",$(this.image_container));
		newObj.fadeTo(t,1, function(){prevObj.fadeOut(0);});
		newObj.appendTo($(this.image_container))
	}
};
//<-from slideshow.js

//->from gallery_dot.js
$(function(){
	$(".photogallery_dot").each(function(){
		var photoGalleryModule_dot = new PhotoGalleryModule_Dot(this)
	})
})

var PhotoGalleryModule_Dot = function(){
	this._container = null;
	this.initialize.apply(this, arguments);
}

PhotoGalleryModule_Dot.prototype = {
	initialize: function(container) {
		this._container = container;
		this.selected_id = 0;
		this.init();
    },
	init: function() {
		var ref = this;
		var my_id = -1;
		$(".photogallery_navi ul li",$(this._container)).each(function(){
			$(this).attr("my_id",++my_id);
			$(this).css({
			});
			$(this).mouseover(function(){
				ref.selectOnCSS($(this));
			});
			$(this).mouseout(function(){
				ref.selectOffCSS($(this));
			});
			var ref_li = $(this);
			$("a", $(this)).click(function(){
				ref.changeSelected(ref_li.attr("my_id"));
				$(".photogallery_main_frame img:last",$(ref._container)).after("<img src='"+$(this).attr("href")+"' alt=''>");
				$(".photogallery_main_frame img:last",$(ref._container)).fadeTo(0,0);
				$(".photogallery_main_frame img:last",$(ref._container)).fadeTo(500,1, function(){
					$(".photogallery_main_frame img:first",$(ref._container)).fadeOut(0,function(){
			            $(this).remove()
			        });	
				});
				$("div.photogallery_description",$(ref._container)).html($(this).attr("description"));
		        return false;
			})
		})
		this.changeSelected(0);
		this.start();
    },
	changeSelected: function(id){
		var prevObj = $(".photogallery_navi ul li:eq("+this.selected_id+") a",$(this._container));
		this.selected_id = id;
		this.selectOffCSS(prevObj);
		this.selectOnCSS($(".photogallery_navi ul li:eq("+id+") a",$(this._container)));
	},
	selectOnCSS :function(obj){
		obj.attr('class', 'rollover'); 
	},
	selectOffCSS :function(obj){
		obj.attr('class', '');
	},
	start: function() {
		this.setNewImage();
		var ref = this;	
		//setInterval(function(){ref.setNewImage();}, 5000);
	},
	setNewImage: function() {
	}
};
//<-from gallery_dot.js

//->from gallery.js
$(function(){
	$(".photogallery").each(function(){
		var photoGalleryModule = new PhotoGalleryModule(this)
	});
	$(".photogallery_text").each(function(){
		var photoGalleryModule = new PhotoGalleryWithTextModule(this)
	});
})

var PhotoGalleryModule = function(){
	this.num_images = 0;
	this.interval_timer;
	this.interval_ar = [];
	this._container = null;
	this.initialize.apply(this, arguments);
}

PhotoGalleryModule.prototype = {
	initialize: function(container) {
		this._container = container;
		this.selected_id = 0;
		this.autoPlay = true;
		this.init();
    },
	init: function() {
		var ref = this;
		var my_id = -1;
		
		$(".photogallery_navi ul li",$(this._container)).each(function(){
			$(this).attr("my_id",++my_id);
			$(this).mouseover(function(){
				ref.selectOnCSS($(this));
			});
			$(this).mouseout(function(){
				ref.selectOffCSS($(this));
			});
			var ref_li = $(this);
			if($(this).attr("interval")){
				if($(this).attr("interval") < 0){
					ref.interval_ar[ref.num_images] = 5000;
					ref.autoPlay = false;
				}else{
					ref.interval_ar[ref.num_images] = $(this).attr("interval");
				}
			}else{
				ref.interval_ar[ref.num_images] = 5000;
			}
			$("a", $(this)).click(function(){
				ref.setNewImage(ref_li.attr("my_id"));
				clearInterval(ref.intervalTimer);
				return false;
			});
			ref.num_images++;
		})
		this.changeSelected(0);
		this.start();
    },
	setNewImage: function(id){
		var ref = this;
		var ref_li_a = $(".photogallery_navi ul li:eq("+id+")>a",$(this._container));
		$(".photogallery_main img:last",$(this._container)).after("<img src='"+ref_li_a.attr("href")+"' alt=''>");
		$(".photogallery_main img:last",$(this._container)).fadeTo(0,0);
		$(".photogallery_main img:last",$(this._container)).fadeTo(1000,1, function(){
			$(".photogallery_main img:first",$(ref._container)).fadeOut(0,function(){
	            $(this).remove();
	        });	
		});
		this.changeSelected(id);
	},
	changeSelected: function(id){
		var prevObj = $(".photogallery_navi ul li:eq("+this.selected_id+")",$(this._container));
		this.selected_id = id;
		this.selectOffCSS(prevObj);
		this.selectOnCSS($(".photogallery_navi ul li:eq("+id+")",$(this._container)));
	},
	selectOnCSS :function(obj){
		obj.css({
			border:"1px solid #cccccc"
		});
	},
	selectOffCSS :function(obj){
		if(obj.attr("my_id") != this.selected_id){
			obj.css({
				border:"1px solid #ffffff"
			});
		}
	},
	start: function() {
		if(this.autoPlay){
			var ref = this;	
			this.intervalTimer = setInterval(function(){ref.onTimer();}, this.interval_ar[0]);
		}
	},
	onTimer: function() {
		var ref = this;
	    var newId = this.selected_id + 1;
		if(newId > this.num_images-1){
			newId = 0;
		}
		clearInterval(this.intervalTimer);
		this.intervalTimer = setInterval(function(){ref.onTimer();}, this.interval_ar[newId]);
		this.setNewImage(newId);
		
	}
};

//////////////////////////////////////////////////////////////////////////////////////

var PhotoGalleryWithTextModule = function(){
	this.num_images = 0;
	this.interval_timer;
	this.interval_ar = [];
	this._container = null;
	this.initialize.apply(this, arguments);
}

PhotoGalleryWithTextModule.prototype = {
	initialize: function(container) {
		this._container = container;
		this.selected_id = 0;
		this.init();
    },
	init: function() {
		var ref = this;
		var my_id = -1;
		
		$(".photogallery_text_navi ul li",$(this._container)).each(function(){
			$(this).attr("my_id",++my_id);
			$(this).mouseover(function(){
				ref.selectOnCSS($(this));
			});
			$(this).mouseout(function(){
				ref.selectOffCSS($(this));
			});
			var ref_li = $(this);
			if($(this).attr("interval")){
				if($(this).attr("interval") < 0){
					ref.interval_ar[ref.num_images] = 5000;
					ref.autoPlay = false;
				}else{
					ref.interval_ar[ref.num_images] = $(this).attr("interval");
				} 
			}else{
				 ref.interval_ar[ref.num_images] = 5000;
			}
			$("a", $(this)).click(function(){
				ref.setNewImage(ref_li.attr("my_id"));
				clearInterval(ref.intervalTimer);
				return false;
			});
			ref.num_images++;
		})
		this.changeSelected(0);
		this.start();
    },
	setNewImage: function(id){
		var ref = this;
		var ref_li_a = $(".photogallery_text_navi ul li:eq("+id+")>a",$(this._container));
		var ref_li = $(".photogallery_text_navi ul li:eq("+id+")",$(this._container));
		$(".photogallery_main img:last",$(this._container)).after("<img src='"+ref_li_a.attr("href")+"' alt=''>");
		$(".photogallery_main img:last",$(this._container)).fadeTo(0,0);
		$(".photogallery_main img:last",$(this._container)).fadeTo(1000,1, function(){
			$(".photogallery_main img:first",$(ref._container)).fadeOut(0,function(){
	            $(this).remove();
	        });	
		});
		$(".photogallery_main_text>p",$(this._container)).html($("p:first", ref_li).html());
		this.changeSelected(id);
	},
	changeSelected: function(id){
		var prevObj = $(".photogallery_text_navi ul li:eq("+this.selected_id+")",$(this._container));
		this.selected_id = id;
		this.selectOffCSS(prevObj);
		this.selectOnCSS($(".photogallery_text_navi ul li:eq("+id+")",$(this._container)));
	},
	selectOnCSS :function(obj){
		obj.css({
			border:"1px solid #cccccc"
		});
	},
	selectOffCSS :function(obj){
		if(obj.attr("my_id") != this.selected_id){
			obj.css({
				border:"1px solid #ffffff"
			});
		}
	},
	start: function() {
		var ref = this;	
		this.intervalTimer = setInterval(function(){ref.onTimer();}, this.interval_ar[0]);
	},
	onTimer: function() {
		var ref = this;
	    var newId = this.selected_id + 1;
		if(newId > this.num_images-1){
			newId = 0;
		}
		clearInterval(this.intervalTimer);
		this.intervalTimer = setInterval(function(){ref.onTimer();}, this.interval_ar[newId]);
		this.setNewImage(newId);
		
	}
};
//<-from gallery.js

/* Photo Gallery */
function embedPhotoGallery(_id,_w,_h,_files,_intervals){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	var filesLen = files.length;
	var innerHTML = '<div class="photogallery">';
	innerHTML += '<div class="photogallery_main" style="height:'+_h+'px; margin-bottom: 28px;"><img src="'+ files[0] +'"/></div>'
	innerHTML += '<div class="photogallery_navi"><ul>'  
	
	for(var i=0; i < filesLen; i++){             
		if(_intervals && i <= _intervals.length-1 && _intervals[i]){
			intervalHtml = 'interval="' + _intervals[i] + '"';     
		}else{
			intervalHtml = '';
		}
		if(i%10==0){
			innerHTML += '<li style="width: 41px; height: 41px; margin-bottom: 11px;" '+intervalHtml+'><a href="'+files[i]+'"><img src="'+ files[i].split('/images/').join('/images/thumbnail/') +'" width="41" height="41"/></a></li>';
		}else{
			innerHTML += '<li style="margin-left: 12px; width: 41px; height: 41px; margin-bottom: 12px;" '+intervalHtml+'><a href="'+files[i]+'"><img src="'+ files[i].split('/images/').join('/images/thumbnail/') +'" width="41" height="41"/></a></li>';
		}
	}
	innerHTML += '</ul><div class="clear"></div></div></div>'
	//alert(innerHTML);
	document.getElementById(_id).innerHTML = innerHTML;
}
function embedPhotoGallery2(_id,_w,_h,_files,_intervals){ embedPhotoGallery(_id,_w,_h,_files,_intervals); }

/* Photo Slideshow */
function embedPhotoSlide(_id,_w,_h,_files,isFrame,interval){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	if(!interval) interval = 5000;
	var filesLen = files.length;
	if(isFrame){
		var innerHTML = '<div class="slideshow_frame" style="width:'+ _w +'px; height:'+ _h +'px;" interval="'+interval+'">';
	}else{
		var innerHTML = '<div class="slideshow" style="width:'+ _w +'px; height:'+ _h +'px;" interval="'+interval+'">';
	}
	for(var i=0; i < filesLen; i++){
		innerHTML += '<img src="'+ files[i] +'" width="'+ _w +'px" height="'+ _h +'px"/>';
	}
	innerHTML += '</div>'
	document.getElementById(_id).innerHTML = innerHTML;
}
function embedPhotoSlide_frame(_id,_w,_h,_files){ embedPhotoSlide(_id,_w,_h,_files,true); }


/* Photo Switcher */
function embedPhotoSlide_dot(_id,_w,_h,_files){
	embedPhotoSlide_dot_credit(_id,_w,_h,_files,[])
}

function embedPhotoSlide_dot_credit(_id,_w,_h,_files,_descriptions){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	var descriptions = _descriptions;
	var filesLen = files.length;
	var descriptionsLen = descriptions.length;
	var innerHTML = '<div class="photogallery_dot">';
	innerHTML += '<div class="photogallery_main_frame" style="width:'+_w+'px; height:'+_h+'px; margin-bottom: 4px;"><img src="'+ files[0] +'"/></div>'
	innerHTML += '<div class="photogallery_navi"><ul>'
	for(var i=0; i < filesLen; i++){
		innerHTML += '<li class=""><div class="css_button_dcg_navi dcg_navi_dot"><a href="'+files[i]+'" description="'+ descriptions[i] +'">o</a></div></li>';
	}
	innerHTML += '</ul><div class="clear"></div>';
	if(descriptionsLen == 0){
		var addStyle = ' display:none;';
	}
	innerHTML += '<div style="width:'+ _w*2 +'px;'+ addStyle +'" class="photogallery_description">'+descriptions[0]+'</div></div>'
	document.getElementById(_id).innerHTML = innerHTML;
}

/* Video Player */
function embedVideoSlide(_id,_w,_h,_files){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	var files2 = [];
	var filesLen = files.length;;
	for(var i=0; i<filesLen; i++){
		if(files[i].indexOf(".jpg")>-1 || files[i].indexOf(".png")>-1){
			files2.push(files[i])
		}
	}
	embedPhotoSlide(_id,_w,_h,files2.join(","),true);
	document.getElementById(_id).innerHTML = document.getElementById(_id).innerHTML + '<p style="margin-top:0.5em">ビデオを再生するには、Java Scriptを有効にし、<a href="http://www.adobe.com/go/getflashplayer_jp" target="_blank">最新のFlash Player</a>をインストールして下さい。</p>';
	if(navigator.userAgent.indexOf("Android")<0){
		swfobject.embedSWF('/iida/common/swf/VideoSlide3.swf', _id, _w+"px", _h+"px", '9.0.28', '', {img:_files}, {bgcolor: '#ffffff', menu: 'false', allowFullScreen: 'false'},{id:_id});
	}
}

function embedVideoSlide1(_id,_w,_h,_files){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	var files2 = [];
	var filesLen = files.length;
	for(var i=0; i<filesLen; i++){
		if(files[i].indexOf(".jpg")>-1 || files[i].indexOf(".png")>-1){
			files2.push(files[i])
		}
	}
	//embedPhotoSlide(_id,_w,_h,files2.join(","),true);
	document.getElementById(_id).innerHTML = document.getElementById(_id).innerHTML + '<p style="margin-top:0.5em">ビデオを再生するには、Java Scriptを有効にし、<a href="http://www.adobe.com/go/getflashplayer_jp" target="_blank">最新のFlash Player</a>をインストールして下さい。</p>';
	if(navigator.userAgent.indexOf("Android")<0){
		swfobject.embedSWF('/iida/common/swf/VideoSlide3.swf', _id, _w+"px", _h+"px", '9.0.28', '', {img:_files}, {bgcolor: '#ffffff', menu: 'false', allowFullScreen: 'false'},{id:_id});
	}
}

function embedVideoSlide4(_id,_w,_h,_files){
	if(typeof(_files)=='string'){
		var files = _files.split(" ").join("").split(",");
	}else{
		files = _files;
	}
	var files2 = [];
	var filesLen = files.length;
	for(var i=0; i<filesLen; i++){
		if(files[i].indexOf(".jpg")>-1 || files[i].indexOf(".png")>-1){
			files2.push(files[i])
		}
	}
	//embedPhotoSlide(_id,_w,_h,files2.join(","),true);
	document.getElementById(_id).innerHTML = document.getElementById(_id).innerHTML + '<!--p style="margin-top:0.5em">ビデオを再生するには、Java Scriptを有効にし、<a href="http://www.adobe.com/go/getflashplayer_jp" target="_blank">最新のFlash Player</a>をインストールして下さい。</p-->';
	if(navigator.userAgent.indexOf("Android")<0){
		swfobject.embedSWF('/iida/common/swf/VideoSlide4.swf', _id, _w+"px", _h+"px", '9.0.28', '', {img:_files}, {bgcolor: '#ffffff', menu: 'false', allowFullScreen: 'false'},{id:_id});
	}
}
