﻿/*********************************************
* KAI Touch Project!
* already Kaitouch_entry Content
* 
* extend JQuery
* @version  : 1.0.0
* @author   : Shunsuke.Hirota
*********************************************/
$.fn.KaitouchEntry = 
{
	path : '/jp/kaitouch/entry.php',	//PHPのパス
	previewPath : '/jp/kaitouch/prev_entry.php',	//プレビュー用パス
	maxLength : 5,		//表示最大ページ数（前後）
	_currentTitle : 0,	//現在表示しているエントリーID
	_currentPage : 1,	//現在表示中のページ
	_entryID : '',		//表示先のID
	_entryName : '',
	
	totalPage : 20,		//取得した総ページ数
	titleList : [],
	
	isPreview:0,
	linkList : [],
			
	/**
	* 初期化
	* ページ1で取得
	* @param __entryID -> 
	*/
	init : function(__entryID)
	{
		this.checkSearch();
		this._currentTitle = $('#title_id').text();
		this._entryID = $(__entryID);
		this._entryName = __entryID;
		this.getNewPage(1);
	},
	
	checkSearch : function()
	{
		var searchList = location.search.split('&');
		for(var i = 0; i < searchList.length; ++i)
		{
			if(String(searchList[i]).indexOf('preview=') != -1) 
				this.isPreview = String(searchList[i]).replace('preview=', '');
		}
	},
	
	/**
	* パジネート作成
	*/
	createPagenate : function()
	{
		if(this.totalPage == 0) return;
		
		var len = this.maxLength * 2 + 1;
		var result = '<div id="stripNav" class="stripNav"><div class="stripWrap">';//<ul>
		//矢印ついか
		result += this.createArrows();
		
		//最終ページへのリンク
		result += '&nbsp;' + this.getAnchor(this.totalPage) + '&nbsp;';
		
		//トータルページに及ばない
		if(this.totalPage > this._currentPage + this.maxLength + 1)
		{
			result += '&nbsp;...&nbsp;';
		}
		
		//各ページ分描画
		for(var i = len; i > 0; --i)
		{
			var num = Number(this._currentPage - (this.maxLength + 1) + i);
			if(num >= 2 && num < this.totalPage)
				result += '&nbsp;' + this.getAnchor(num) + '&nbsp;';
		}
		
		//現在のページが(1 + maxLength)より大きい
		if(this._currentPage - this.maxLength > 1)
		{
			result += '&nbsp;...&nbsp;';
		}
		
		//先頭ページへのリンク
		if(this.totalPage != 1) result += '&nbsp;' + this.getAnchor(1) + '&nbsp;';
		
		result += '</div></div>';//</ul>
		this._entryID.prepend(result);
	},
	
	/**
	* 左右矢印作成
	*/
	createArrows : function()
	{
		var size = this.totalPage;
		var result = '';
		//現在表示ページが1ではない -> 左矢印を作成
		if(this._currentPage != this.totalPage) result += this.createLeftArrow(1);
		else result += this.createLeftArrow(0);
		//現在表示ページがトータルページと等しくない -> 右矢印を作成
		if(this._currentPage != 1) result += this.createRightArrow(1);
		else result += this.createRightArrow(0);
		
		return result;
	},
	
	/**
	* 左矢印作成
	*/
	createLeftArrow : function($link)
	{
		var result = '<div id="stripNavL0" class="stripNavL">';
		if($link != 0) result += '<a class="" href="javascript:void(0);" onClick="$().KaitouchEntry.getNewPage(' + (this._currentPage + 1) + ')"><img src="../img/titles/arrow_left.gif"/></a>';
		else result += '&nbsp;';
		
		result += '</div>';
		return result;
	},
	
	/**
	* 右矢印作成
	*/
	createRightArrow : function($link)
	{
		var result = '<div id="stripNavR0" class="stripNavR">';
		
		if($link != 0) result += '<a class="" href="javascript:void(0);" onClick="$().KaitouchEntry.getNewPage(' + (this._currentPage - 1) + ')"><img src="../img/titles/arrow_right.gif"/></a>';
		else result += '&nbsp;';
		
		result += '</div>';
		return result;
	},
	
	/**
	* 取得したタイトル・リンクのリストから内容を作成して縦書き処理を実行
	*/
	createList : function()
	{
		if(this.totalPage == 0) return;
		var result = '<div id="titlesEntryContainer" class="clearfix csw"><div class="panelContainer"><div class="panel">';
		var len = this.titleList.length;
		
		for(var i = 0; i < len; ++i)
		{
			result += '<!-- ▼▼エントリー▼▼ -->';
			result += '<div class="titlesEntry">';
			result += '<p class="verticalWriting"><a href="' + this.linkList[i] + '" target="_blank">';
			result += this.titleList[i] + '</a></p>';
			result += '</div>';
			result += '<!-- △△エントリー△△ -->';
		}
		result += '</div></div></div>';
		
		this._entryID.append(result);
		startVertical("verticalWriting", 21, 12);
	},
	
	/**
	* データを取得して新しいページを作成する
	*/
	getNewPage : function(__page)
	{
		var _path = this.isPreview == 1 ? this.previewPath : this.path;
		var _params = 
		{
			act     : 'xmllist',
			page    : __page,
			title_id : this._currentTitle
		};
		this._currentPage = __page;
		
		var res = jQuery.post(_path, _params, function(event, status)
		{
			if(status == 'success')
			{
				$().KaitouchEntry.parseList(event);
				$().KaitouchEntry.clearEntry();
				$().KaitouchEntry.createPagenate();
				$().KaitouchEntry.createList();
			}
			else
			{
				alert('接続に失敗しました。');
			}
		});
		
	},
	
	/**
	* 取得したデータを解析する
	*/
	parseList : function(_xml)
	{
		var result = _xml.split('____');
		this.totalPage = result[0];
		this.titleList = result[1].split('|+|');
		this.linkList = result[2].split('|+|');
		this.titleList.pop();
		this.linkList.pop();
		
	},
	
	/**
	* エントリー要素を空にする
	*/
	clearEntry : function()
	{
		this._entryID.empty();
	},
	
	/**
	* anchorを取得
	*/
	getAnchor : function(num)
	{
		var result = '';
		if(this._currentPage != num)
		{
			result += '<a class="" href="javascript:void(0);" onClick="$().KaitouchEntry.getNewPage(' + num + ')">';
			result += num + '</a>';
		}
		else
		{
			result += '<span class="current">' + num + '</span>';
		}
		return result;
	}
}
$(function()
{
	$().KaitouchEntry.init('#titlesEntryContainerScroll');
});
