//jquery.filmslide.js
//文字情報をオーバーレイできるスライドショー

/*
usage:

	<div id="slideshow" style="width:476px; height:268px; position:relative;">
		<div class="label-name-1"></div>
		<div class="label-name-2"></div>
	</div>

	var photolist = {
		"film-name-1" : {
			texts : {
				"label-name-1" : "..",
				"label-name-2" : "..",
				..
			},
			photos : [
				"photo-filename-1",
				"photo-filename-2",
				..
			]
		},
		..
	};

	$(document).ready(function(){
		$().filmslide({
			target : $("#slideshow"), 
			list   : photolist,   // photolist hash
			duration: 600,        // slide animate time.
			timeout : 5000,       // interval to slide next.
			prefix  : "/prefix/", // prefix and
			postfix : ".jpg"      // postfix to photos filename
		});
	});

	the photos
		/prefix/photo-filename-1.jpg, /prefix/photo-filename-2.jpg, .. 
	are shown in #slideshow box.

	and texts 
		label-name-1, label-name-2, ..
	are displayed in 
		<div class="label-name-1">, <div class="label-name-2">, ..
*/

/*
jquery.hoverhere.js:
  slideshow with texts overlay.
Copyright (C) 2010 Haruka Kataoka for drumsoft

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

contact: hrk8#drumsoft.com
*/


(function($){
	$.fn.filmslide = function(opt) {
		var defaults = {
			target   : null,
			list     : null,
			duration : 600,
			timeout  : 5000,
			prefix   : "",    	// prefix and postfix of filename
			postfix  : ".jpg"
		};
		opt = $.extend(defaults, opt);

		var target = opt.target;
		var photoarr = [], fields = {};
		var length, index, image;
		var preindex, preload;
		var width = target.width(), height = target.height();
		target.css({
			overflow: "hidden"
		});

		// flatten photolist (in initialize)
		function flatlist(){
			if (this) {
				var texts = this.texts;
				$.each(this.photos, function(i,e){
					if ( e ) {
						photoarr.push($.extend({file:opt.prefix + e + opt.postfix}, texts));
					}
				});
			}
		};
		// chache text fields jQuery objects (in initialize)
		function cachefields(target, texts) {
			for ( var i in texts ) {
				var field = $("." + i, target);
				if (field.length > 0) fields[i] = field;
			}
		}

		// event: slide next
		function next_slide() {
			for ( var i in fields ) {
				if ( fields[i].text() != photoarr[preindex][i] )
					fields[i].fadeOut(opt.duration);
			}
			image.animate({top:-height}, opt.duration, "swing");
			preload.animate({top:0}, opt.duration, "swing", after_slide);
		}
		// event: slide animation finished
		function after_slide() {
			index = preindex;
			preindex = (index+1) >= length ? 0 : index + 1;
			var temp = image;
			image = preload;
			preload = temp;
			preload
				.attr("src", photoarr[preindex].file)
				.animate({top:height},0);
			setTimeout(next_slide, opt.timeout);

			for ( var i in fields ) {
				fields[i]
					.text(photoarr[index][i])
					.fadeIn(opt.duration);
			}
		}

		// initialize
		if ( ! ("photos" in opt.list) ) {
			var i;
			for ( i in opt.list ) flatlist.apply(opt.list[i]);
			cachefields(target, opt.list[i].texts);
		}else{
			flatlist.apply(opt.list);
			cachefields(target, opt.list.texts);
		}
		length = photoarr.length;

		// make initial state
		index = Math.floor(Math.random() * length);
		preindex = (index+1) >= length ? 0 : index + 1;
		image = $("<img>")
			.attr("src", photoarr[index].file)
			.css({ position:"absolute", top:0, width:width, height:height })
			.prependTo(target);
		preload = $("<img>")
			.attr("src", photoarr[preindex].file)
			.css({ position:"absolute", top:height, width:width, height:height })
			.prependTo(target);

		for ( var i in fields ) {
			fields[i].text(photoarr[index][i]);
		}

		setTimeout(next_slide, opt.timeout);

		return this;
	};
})(jQuery);

