// クラスの宣言
var ProductControl = Class.create();

// クラスの定義
ProductControl.prototype = {

	// 初期化処理
	initialize: function(scrollarea, scrollbar) {
	
		this.productList = scrollarea;

		// スクロールバー描画処理
		this.scrollbar = new FLScroll(scrollbar, this.productList);

	},

	// 商品情報の取得
	getProduct: function(shopno, brandno, brandname) {

		// ブランド名の編集
		this.setBrandName(brandname);

		while ( true ) {

			// 読み込み不能時
			if ( !shopno || !brandno ) {
				$(this.productList).innerHTML = '';
				break;
			}

			// 商品情報を読む
			var ajax = new Ajax.Updater(
				this.productList,
				'../shop/index.php',
				{
					asynchronous:true,
					method:'get',
					parameters:'act=ajax_product&sn=' + shopno + '&bn=' + brandno,
					onFailure: function() {
						$(this.productList).innerHTML = '';
					}
				}
			);

			// 全商品に対するイベントの追加
			var node = $(this.productList).getElementsByTagName('img');
			var products = $A(node);
			products.each(
				function(product) {
					this.addEvent(product);
				}.bindAsEventListener(this)
			);

			break;
		}
	
		// スクロールバーの再描画
		this.scrollbar.redraw();

	},

	// 先頭ブランドの商品情報取得	
	getTopProduct: function(brandlist) {

		if ( $(brandlist) == null ) {
			return;
		}

		// 先頭ブランドの要素を取得
		var node = $(brandlist).getElementsByTagName('a');
		var brands = $A(node);
		if ( brands.length <= 0 ) {
			return;
		}

		// onclickイベントを呼び出す
		brands[0].onclick();
		
	},
	
	setBrandName: function(brandname) {
		$('editablebrandname').innerHTML = $(brandname).innerHTML;
	},

	// イベント追加処理
	addEvent: function(product) {
		Event.observe(
			product,
			'click',
			function () {
				this.showProductWindow(product.getAttribute('rel'));
			}.bindAsEventListener(this),
			false
		);
	},

	// 商品画面を開く
	showProductWindow: function(pn) {
	
		if ( !pn ) {
			return;
		}

		new LITBox(
			'../shop/index.php?act=product&pn=' + pn,
			{
				width:590,
//				height:595,
				height:625,
				type:'window',
				overlay:true
			}
		);
	}

}
