/*!
 * Copyright 2010, Root-Sea
 * 
 */

////// クロスフェードのスライドショークラス //////

function RSCrossFade(id, w, h, interval, fade) {
	
	//現在のページ番号
	this.current = 1;
	
	//ID
	this.id = id;
	
	//全体(スライドショー、UI)を表示するdiv
	this.area = document.all && document.all(id) || document.getElementById && document.getElementById(id);
	if(this.area){
		with(this.area){
			style.width  = w + "px";
			style.height = h + "px";
			style.position = "relative";
		}
	}
	
	//スライドショーのラッパー
	this.wrapper = makeRectangle(this.id + "_wrapper", 0, 0, this.area.offsetWidth, this.area.offsetHeight, "#FFFFFF", 100);
	this.wrapper.style.position = "absolute";
	this.area.appendChild(this.wrapper);
	
	//setTimeout管理変数
	this.timerID;
	
	//ロードした画像の総数
	this.loaded = 0;
	
	//サムネールのサイズおよび余白
	this.th_w = 0;
	this.th_h = 0;
	
	//// HTMLからの入力必須項目 ////
	
	//表示時間とフェード時間
	this.objTime = {sleep:interval, fade:fade};
	
	//ファイル名、サムネール、リンク先、ターゲット
	this.aInfo = new Array();
	
	//// カスタム設定情報 ////
	
	//サムネール間のマージン
	this.margin = 4;
	
	//サムネール位置("left", "center", "right")
	this.align = "left";
	
	//サムネールの下地のカラー
	this.bgColor = "#FFFFFF";
	
	//サムネールの下地の不透明度(0～100)
	this.bgAlpha = 50;
	
	//// UI ////
	this.ui = undefined;
	
	//デコレーションなどの配列
	this.aExtra = new Array();
	
	//静止画ファイルおよびリンク先を追加する。
	this.addInfo = function(file, url, target) {
		var tgt;
		if (target != undefined) {
			tgt = target;
		} else {
			tgt = "_self";
		}
		this.aInfo.push({ src:file, href:url, target:tgt });
	}
	
	this.changeParameter = function(paramName, value) {
		this[paramName] = value;
	}
	
	//実行
	this.start = function() {
		$("#" + id + "_area").empty().css({
			overflow: 'hidden',
			padding: 0, 
			zIndex: 1
		});
		
		var i;
		for(i = 0; i < this.aInfo.length; i++) {
			var img = new Image();
			img.style.position = "relative";
			img.onload = loadedImage;
			img.src = this.aInfo[i]["src"];
			
			var img_div = makeRectangle(this.id + "_img" + (i + 1), 0, 0, this.area.offsetWidth, this.area.offsetHeight, this.bgColor, 100);
			img_div.style.position = "absolute";
			img_div.style.overflow = "hidden";
			img_div.style.visibility = "hidden";
			img_div.appendChild(img);
			img_div.onmouseover = handCursor;
			img_div.onmouseout  = normalCursor;
			img_div.onclick = jump;
			this.wrapper.appendChild(img_div);
		}
	}
	
	var _this = this;
	
	//画像のonLoadイベントハンドラ
	loadedImage = function() {
		_this.loadCheck();
	}
	
	//サムネイルのonLoadイベントハンドラ
	loadedThumbnail = function () {
		_this.th_w = this.width;
		_this.th_h = this.height;
		_this.loadCheck();
	}
	
	//ロード数のチェック
	this.loadCheck = function() {
		this.loaded++;
		
		if (this.loaded == this.aInfo.length) {// ユーザインタフェースの位置を決定する
			this.setExtra();
			//this.setUI();
			
			// スライドショーを開始する
			if (this.ui == undefined) {
				this.switchSlide(1);
			}
		}
	}
	
	//ユーザインターフェースを追加する
	this.addUI = function(ui) {
		this.ui = ui;
		this.aExtra.push(this.ui.area);
		ui.setParent(this);
	}
	
	//ユーザインタフェースの位置を決定する
	this.setUI = function() {
		if (this.ui != undefined) {
			var area = this.ui.area;
			this.area.appendChild(this.ui.area);
			//this.ui.area.style.zIndex = 1;
			this.ui.area.style.overflow = "hidden";
			//area.style.top  = this.ui.top + "px";
			//area.style.left = this.ui.left + "px";
			this.ui.start();
		}
	}
	
	//デコレーションなどのファイルを追加する
	this.addExtra = function(file, left, top) {
		
		var area = makeRectangle(this.id + "_extra" + this.aExtra.length, 0, 0, 0, 0, "#FFFFFF", 100);
		area.style.position = "absolute";
		this.area.appendChild(area);
		this.aExtra.push(area);
		
		var img = new Image();
		img.style.position = "absolute";
		img.style.left = left + "px";
		img.style.top  = top  + "px";
		img.src = file;
		area.appendChild(img);
	}
	
	//デコレーションを配置する
	this.setExtra = function(file, left, top) {
		for (var i = 0; i < this.aExtra.length; i++) {
			var obj = this.aExtra[i];
			this.area.appendChild(obj);
			obj.style.zIndex = 1;
		}
		
		if (this.ui != undefined) {
			this.ui.start();
		}
	}
	
	//スライドの切り替え
	this.switchSlide = function(n) {
		this.current = n;
		
		if (this.ui == undefined) {
			if (this.timerID != undefined) clearTimeout(this.timerID);
			this.timerID = setTimeout(this.timeoutHandler, this.objTime.sleep * 1000);
		}
		
		for(var i = 1; i <= this.aInfo.length; i++) {
			var img = $('#' + _this.id + '_img' + i);
			//var thumb = $('#' + _this.id + '_thumb' + i);
			if (i == n) {
				img.css('visibility', 'visible');
				img.css('z-index', 0);
				img.fadeIn(this.objTime.fade * 1000);
			} else {
				img.fadeOut(this.objTime.fade * 1000);
			}
		}
	}
	
	//スライド切り替え用のタイムアウトハンドラ
	this.timeoutHandler = function () {
		var n = _this.current % _this.aInfo.length + 1;
		_this.switchSlide(n);
	}
	
	//カーソルを指マークに
	function handCursor() { this.style.cursor = "pointer";}
	
	//カーソルを矢印マークに
	function normalCursor() { this.style.cursor = "normal";}
	
	//リンク
	function jump() {
		var info = _this.aInfo[this.id.substr(_this.id.length + 4) - 1];
		window.open(info.href, info.target);
	}
}



