// JavaScript Document
//slideshow

var SlideShow = function(loc,option){

	//写真リスト（XML）場所
	this.url = loc;
	
	this.myoption = {
	
	//コンテナのID名
	imageId : "crossfade-container",

	textId : "",

	//最後の写真表示後スライドショーを止める・止めない
	loop : true,
	
	//画像表示の時間(ms) 0の場合xmlの時間を参照
	Interval : 0,
	
	//フェードの時間(s)
	ftimes : 1
	
	}
	
	for (var key in option)
		this.myoption[key] = option[key];

	this.myTimer = 0;
	this.idx = 0;
	this.itemcount = 0;
	this.imageUrls = new Array();
	this.descriptionArray = new Array();
	
	this.startSlide();
}
			
SlideShow.prototype.loadXMLFile = function () {
	new Ajax.Request(this.url, {
		method: "get", 
		onComplete: function (httpObj) { this.displayData(httpObj); }.bindAsEventListener(this, false)
		}
	);
};

SlideShow.prototype.displayData = function (httpObj) {
	var XML = httpObj.responseXML;
	var moviedataTag = XML.getElementsByTagName("moviedata");
	var settingsTag = moviedataTag[0].getElementsByTagName("settings");
	var mindisplaymsTag = settingsTag[0].getElementsByTagName("mindisplayms")[0].firstChild.nodeValue;
	var entry_itemsTag = moviedataTag[0].getElementsByTagName("entry_items");
	var itemTags = entry_itemsTag[0].getElementsByTagName("item");
	this.itemcount = itemTags.length;
	if (this.myoption.Interval == 0) this.myoption.Interval = mindisplaymsTag;

	for (var i = 0; i < this.itemcount; i++) {
		this.imageUrls[i] = itemTags[i].getElementsByTagName("href")[0].firstChild.nodeValue;
		this.descriptionArray[i] = itemTags[i].getElementsByTagName("description")[0].firstChild.nodeValue;
	}
			
	this.addElements();
				
	Effect.Appear("slideitem-" + this.idx, { duration:this.myoption.ftimes, from:0.0, to:1.0 });
	if (this.myoption.textId != "") {
		Effect.Appear("slidetext-" + this.idx, { duration:this.myoption.ftimes, from:0.0, to:1.0 });
	}
				
	this.startInterval();	
				
};
	
SlideShow.prototype.startInterval = function () {
	if (this.myTimer == 0) 
		this.myTimer = setInterval(function(){this.effectSlide(1);}.bindAsEventListener(this, false), this.myoption.Interval);
};
	
SlideShow.prototype.stopSlide = function () {
	clearInterval(this.myTimer);
	this.myTimer = 0;
};
	
SlideShow.prototype.nextSlide = function () {
	this.stopSlide();
	this.effectSlide(1);
	this.startInterval();
};
	
SlideShow.prototype.nextSlideStop = function () {
	this.stopSlide();
	this.effectSlide(1);
};
	
SlideShow.prototype.backSlide = function () {
	this.stopSlide();
	this.effectSlide(-1);
	this.startInterval();
};
	
SlideShow.prototype.backSlideStop = function () {
	this.stopSlide();
	this.effectSlide(-1);
};

SlideShow.prototype.effectSlide = function (effecttype) {

	if (this.myoption.loop == false) {
		if (this.idx == this.itemcount - 1) {
			this.stopSlide();
			return;
		}
	}

	Effect.Fade("slideitem-" + this.idx, { duration:this.myoption.ftimes, from:1.0, to:0.0 });
	if (this.myoption.textId != "") {
		Effect.Fade("slidetext-" + this.idx, { duration:this.myoption.ftimes, from:1.0, to:0.0 });
	}
	if (effecttype == 1) {
		this.idx++;
		if (this.idx == this.itemcount) this.idx = 0;
	}
	if (effecttype == -1) {
		this.idx--;
		if (this.idx < 0) this.idx = this.itemcount - 1;
	}
	Effect.Appear("slideitem-" + this.idx, { duration:this.myoption.ftimes, from:0.0, to:1.0 });
	if (this.myoption.textId != "") {
		Effect.Appear("slidetext-" + this.idx, { duration:this.myoption.ftimes, from:0.0, to:1.0 });
	}
};

SlideShow.prototype.startSlide = function () {
	//alert(idx.responseText);
	this.loadXMLFile();
};
		
SlideShow.prototype.addElements = function () {
		for(var i=0; i < this.itemcount; i++){	
			var element_div = document.createElement('div');
			element_div.id = "slideitem-" + i;
			element_div.style["position"] = "absolute";
			element_div.style["top"] = "0";
			element_div.style["left"] = "0";
			element_div.style["display"] = "none";

			var element_img = document.createElement('img');
			//element_img.style["position"] = "absolute";
			element_img.src = this.imageUrls[i];

			var objBody = $(this.myoption.imageId);
			objBody.appendChild(element_div);
			element_div.appendChild(element_img);
		}
		
		for(var i=0; i < this.itemcount; i++){	
				if (this.myoption.textId != "") {
					var element_comment = document.createElement('div');
					element_comment.id = "slidetext-" + i;
					element_comment.style["position"] = "absolute";
					element_comment.style["top"] = "0";
					element_comment.style["left"] = "0";
					element_comment.style["display"] = "none";
				
					var comment_box = document.createElement('div');
					comment_box.innerHTML = this.descriptionArray[i];
				
					var objText = $(this.myoption.textId);
					objText.appendChild(element_comment);
					element_comment.appendChild(comment_box);
				}
		}
};
