﻿
/* 指定したURLからパラメータを取り出すます
---------------------------------------------------------------------------*/

var URLVars = Class.create();

URLVars.prototype = {

	// コンストラクタでは対象のURLを指定できます

	initialize: function(url) {
		this.url    = url ? url : "";
		this.params = "";
	},

	// 指定したkeyの値を返す、
	// 引数がない場合は全パラメータを返します

	get: function(key) {
		if (this.url == "") return;
		
		this.extract();
		
		if (!key) return this.params;
		
		var s = this.params.indexOf( key, 0 );
		var e = this.params.indexOf( "?", s ) != -1 ? this.params.indexOf( "?", s )
		                                            : this.params.indexOf( "&", s );
		
		if (s == -1) return;
		
		s = s + key.length + 1;
		e = e != -1 ? e : this.params.length;
		
		return this.params.substring( s, e );
	},
	
	// URLからパラメータを取り出す
	
	extract: function() {
		var s = this.url.indexOf("?" , 0) + 1;
		var e = this.url.length;
		
		this.params = this.url.substring(s, e);
		return this.params;
	}

}


/* XMLのロードとパース JKL(ObjTreeを使用)
---------------------------------------------------------------------------*/

var XMLParser = Class.create();

XMLParser.prototype = {

	// 初期化

	initialize: function() {
		this.url = "";
		this.xotree  = new XML.ObjTree();
		this.method  = "post";
		this.parameters = "";
	},

	// ロード開始

	load: function() {

		if (this.url == "") return;

		options = {
			method: this.method,
			parameters: this.parameters,
			onLoaded:   this.onLoaded,
			onLoading:  this.onLoading,
			onSuccess:  this.onSuccess,
			onFailure:  this.onFailure
		}
		this.xotree.parseHTTP( this.url, options, this.onComplete );
	},
	
	// 以下は使用可能なイベント
	
	onComplete: function() {
	},
	
	onSuccess:  function() {
	},
	
	onFailure:  function() {
	},
	
	onLoading:  function() {
	},
	
	onLoaded:   function() {
	}

}


/* フォトアルバム専用クラス
---------------------------------------------------------------------------*/

var PhotoList = Class.create();

PhotoList.prototype = {

	initialize: function(xmlobj) {

		this.list    = xmlobj.photos;

		// フォトアルバム内の全画像数

		this.total   = 0;

		// 読み込み済みのXMLのリスト数

		this.length  = 0;

		// 現在見ている画像のインデックス

		this.current = 0;

		// カウンタ

		this.count = 0;

		// 分解するお

		this.extract();
	},
	
	// XML分解中です
	
	extract: function() {
		this.total  = parseInt( this.list.total );
		this.length = this.list.photo.length;

		if (this.total == 1) {
			this.current = 1; return;
		}

		for ( var i = 0; i < this.length; i++ ) {
			if ( this.list.photo[i].selected == "true" ) {
				this.count   = i;
				this.current = parseInt( this.list.photo[i].index );
			}
			this.list.photo[i].comment = this.list.photo[i].comment == undefined ? " " : this.list.photo[i].comment;
			var c = this.list.photo[i].comment;

			//if (c.indexOf("&lt;br&gt;",  0) != -1 || c.indexOf("&lt;wbr&gt;", 0) != -1) {
				//this.list.photo[i].comment = c.split("&lt;br&gt;").join("<br>").split("&lt;wbr&gt;").join("<wbr>");
			//}
		}
	},

	// Photoオブジェクトを返す
	// 内容はXMLのままです
	
	getPhoto: function() {
		return this.total > 1 ? this.list.photo[this.count] : this.list.photo;
	},

	// 単純にカウントアップ

	next: function() {
		this.count += 1;
		this.refresh();
	},
	
	// 単純にカウントダウン
	
	prev: function() {
		this.count -= 1;
		this.refresh();
	},

	// 画像の変更時に動作
	
	refresh: function() {
		
		var o = new Object();
	
		//カウントがリスト数以上で、画像のトータルがリスト数より大きい
	
		if ( this.count >= this.length && this.length < this.total ) {
			o.limit = "max";
			o.error = "overflow";
			this.onError(o);
		}
			
		//カウントが0より小さく0のインデックスが1より大きい
			
		else if (this.count < 0 && parseInt( this.list.photo[0].index ) > 1) {
			o.limit = "min";
			o.error = "overflow";
			this.onError(o);
		}
			
		else {
			this.current = parseInt( this.list.photo[this.count].index );
			this.onChange();
		}
	},
	
	// 
	onError: function() {
	},

	// 
	onChange: function() {
	}

}



/* ライトボックスのモーダル状態を解除する
---------------------------------------------------------------------------*/

var KillModalLightbox = Class.create();

KillModalLightbox.prototype = {

	initialize: function() {
		this.escape_elements = arguments;
		
		var closeFlag1 = false;
		var closeFlag2 = false;
		
		for (var i = 0; i < this.escape_elements.length; i++) {
			this.escape_elements[i].onmousedown = function() {
				closeFlag1 = true;
			}
		}
		
		$("lightbox").onmousedown = function() {
			closeFlag2 = true;
		}
		
		$("lightbox").onclick = function() {
			if (!closeFlag1 && closeFlag2) valid.deactivate();
			closeFlag1 = closeFlag2 = false;
		}
	}

}


/* 指定したtypeのすべてのエレメントを選択状態にする(仮
---------------------------------------------------------------------------*/

var FormCtrl = Class.create();

FormCtrl.prototype = {
	initialize: function(formName) {
		this.form = document[formName];
	},
	checkAll: function(elementType) {
		this.check(true, elementType);
	},
	uncheckAll: function(elementType) {
		this.check(false, elementType);
	},
	check: function(ischeck, elementType) {
		for (var i = 0; i < this.form.length; i++) {
			var element = this.form[i];
			switch (elementType) {
				case "checkbox":
					if (element.type == "checkbox")
						element.checked = ischeck;
					break;
			}
		}
	}
}

function initFormCtrl(formName) {
	Event.observe(window, 'load', function(event){ formControlObject = new FormCtrl(formName) }, false);
}

function checkAllCheckbox() {
	formControlObject.checkAll("checkbox");
}
function uncheckAllCheckbox() {
	formControlObject.uncheckAll("checkbox");
}



/* アンカーリンクへ飛ばす
---------------------------------------------------------------------------*/
Event.observe(window, "load",
	function() {
		u = location.href;
		s = u.lastIndexOf("#", u.length);
		if (s == -1) return;
		e = u.length;
		h = u.substring(s, e);
		location.href = h;
	}, false
);




