/*
 *1	window.me
 */
window.me.page = {
	/*
	 *2	param
	 */
	param : {
		
	}
	/*
	 *2	parts
	 */
	,
	parts : {}
	/*
	 *2	init
	 */
	,
	init : function(){
		
		me.page.param.username = aulta.string.replaceText(aulta.string.urldecode(me.page.param.username, 2), 1);
		me.page.param.trip = aulta.string.replaceText(aulta.string.urldecode(me.page.param.trip, 2), 1);
		
		if (me.page.param.status == 0){
			if (me.page.param.direct == 1){
				jQuery('.bbs_list').eq(1)
				.after(
					me.bbs.info.regist()
				);
			} else {
				jQuery('.bbs_list').eq(1)
				.before(
					me.bbs.info.regist()
				);
			}
		} else if (me.page.param.status == 2){
			var jq = 
				jQuery('<div>').addClass('bbs_list main_area')
				.append(
					jQuery('<input type="button" />').val('レスを書く')
					.click(function(){
						var threadNo = me.page.param.threadno
							, resNo = 0;
						me.bbs.res.click(
							this,
							jQuery(this).parent().find('div.res_edit'),
							threadNo,
							resNo,
							true
						);
					})
				)
				.append(
					jQuery('<div>').addClass('res').css('margin-top', '10px').hide()
				);
			if (me.page.param.direct == 1){
				jQuery('.bbs_list').eq(1).after(jq);
			} else {
				jQuery('.bbs_list').eq(1).before(jq);
			}
		}
		me.page.bbs.initResForm(jQuery('.bbs_list'));
		
		me.page.pager.addButton();
		
		me.bbs.init.run();
	}
	,
	/*
	 *3	resize
	 */
	resize : function(){
		var windowW = jQuery(window).aultaWidth()
			, windowH = jQuery(window).aultaHeight()
			;
	}
	,
	/*
	 *2 bbs
	 */
	bbs : {
		/*
		 *3 initResForm
		 */
		initResForm : function(jqObject){
			
			me.bbs.link.init(jqObject);
			
			if (me.page.param.status < 2) return;
			jqObject.find('.res')
			.append(jQuery('<div>').addClass('res_edit'))
			.hover(
				function(){
					jQuery(this).find('div.commands').css('visibility', 'visible');
					jQuery(this).css('background-color', '#F0F8FF');
				},
				function(){
					jQuery(this).find('div.commands').css('visibility', 'hidden');
					jQuery(this).css('background-color', 'transparent');
				}
			)
			;
			
			jqObject.find('.res .note')
			.after(jQuery('<div>').addClass('commands'));
			
			jqObject.find('.res div.commands')
			.append(jQuery('<input type="button" value="レスを書く" class="button_res" />'));
			
			jqObject.find('.res[deletable="yes"] div.commands')
			.append(jQuery('<input type="button" value="削除する" class="button_delete" />'));
			
			
			jqObject.find('input.button_res')
			.click(function(){
				var threadNo = me.page.param.threadno
					, resNo = jQuery(this).parent().parent().attr('rel')
					, jq = jQuery(this).parent().parent().find('div.res_edit');
				me.bbs.res.click(
					this,
					jq,
					threadNo,
					resNo,
					false
				);
			})
			;
			
			jqObject.find('input.button_delete')
			.click(function(){
				var no = jQuery(this).parent().parent().attr('rel');
				me.page.bbs.ownDelete(no);
			});
		}
		,
		/*
		 *3 ownDelete
		 */
		ownDelete : function(resNo){
			if ( ! confirm('No.' + resNo + ' レスを削除しますか？')) return;
			jQuery.ajax({
				type: 'POST',
				url: '/ajax/' + me.page.param.url.write + aulta.text.time(),
				data: 'ajax=ajax'
					+ '&command=delete_res'
					+ '&threadno=' + me.page.param.threadno
					+ '&resno=' + resNo
					+ '&pagemax=' + me.page.param.pagemax
					+ '&direct=' + me.page.param.direct,
				cache: false,
				success: function(html){
					var jq = jQuery(html);
					if (jq.attr('rel') == '1'){
						alert(jq.text());
					} else {
						jQuery('.bbs_list div.res[rel="' + jq.attr('resno') + '"] div.note')
						.html(jq.find('#note').html());
						jQuery('.bbs_list div.res[rel="' + jq.attr('resno') + '"] div.res_edit').remove();
						if (me.page.param.pagemax > jq.attr('lastno')){
							for(var i = jq.attr('lastno')-0 + 1, j = me.page.param.pagemax; i <= j; i++){
								jQuery('.bbs_list div.res[rel="' + i + '"]').remove();
							}
							me.page.param.pagemax = jq.attr('lastno')-0;
							jQuery('.bbs_list:empty').remove();
						}
					}
					
				},
				error : function(msg, status){
					var s = status + "\n\n" + '通信エラーが発生しました。';
					alert(s);
				}
			});
		}
		
	}
	,
	/*
	 *2 pager
	 */
	pager : {
		/*
		 *3 init
		 */
		addButton : function(){
			var jqButtons = jQuery('<div>').addClass('bbs_list main_area');
			if (
				(me.page.param.direct == 1 && me.page.param.pagetotal > me.page.param.pagemax) ||	//	昇順
				(me.page.param.direct == 0 && me.page.param.pagemin > 1)	//	降順
			){
				jqButtons
				.append(
					jQuery('<input type="button" value="続きを表示する(50件分)" />')
					.click(function(){me.page.pager.loadArea(this, 'page');})
				)
				.append(
					jQuery('<input type="button" value="続きをまとめて全部表示する" />').css('margin-left', '20px')
					.click(function(){me.page.pager.loadArea(this, 'all');})
				)
				;
			}
			jqButtons
			.append(
				jQuery('<ul>').addClass('topic_path').css('padding', '10px')
				.append(jQuery('#banner_header ul.topic_path').html())
			)
			.append(jQuery(aulta.text.loading).hide())
			;
			//var jq = (me.page.param.direct == 1 ? jQuery('.bbs_list:last').prev() : jQuery('.bbs_list:last'));
			//jq.after(jqButtons);
			jQuery('.bbs_list:last').after(jqButtons);
		}
		,
		/*
		 *3 loadNextArea
		 */
		loadArea : function(obj, get_type){
			var jqList = jQuery(obj).parent();
			if (get_type == 'all'){
				if (jQuery.browser.msie){
					if ( ! confirm(
						"インターネットエクスプローラで「全部表示」をするとフリーズする恐れがあります。\n\n"
						+ "中止する場合は「キャンセル」を押してください。"
					)){
						jqList.find('input').show();
						jqList.find('.loading').hide();
						return false;
					}
				}
			}
			jQuery('html,body').scrollTop(
				aulta.utility.bodyScrollTop() - 10
			);
			jqList.find('input').hide();
			jqList.find('.loading').show();
			jQuery.ajax({
				type: 'POST',
				url: '/ajax/' + me.page.param.url.read + aulta.text.time(),
				data: 'ajax=ajax'
					+ '&command=res_list'
					+ '&get_type=' + get_type
					+ '&threadno=' + me.page.param.threadno
					+ '&pagemax=' + me.page.param.pagemax
					+ '&pagemin=' + me.page.param.pagemin
					+ '&direct=' + me.page.param.direct,
				cache: false,
				success: function(html){
					var jq = jQuery(html);
					if (jq.attr('rel') == '1'){
						alert(jq.text());
						jqList.find('input').show();
						jqList.find('.loading').hide();
					} else {
						jqList.empty().append(jq.find('#page'));
						me.page.bbs.initResForm(jqList);
						jqList.show();
						me.page.param.pagemax = jq.attr('pagemax')-0;
						me.page.param.pagemin = jq.attr('pagemin')-0;
						me.page.pager.addButton();
					}
				},
				error : function(msg, status){
					var s = status + "\n\n" + '通信エラーが発生しました。';
					alert(s);
					jqList.find('input').show();
					jqList.find('.loading').hide();
				}
			});
		}
	}
};


