/** scrollable_content **/
var intervalId = false;

function scrollableContent(arg1, arg2){
	this.divId = arg1;
	this.indexDivId = arg2;
	this.api;
	this.pIndex = 0;
	this.lastIndex = 0;
	this.newEntryIndex;
	this.isFinishedInit = false;
	this.prevPage = function(){
		try {
			this.pIndex = this.api.getPageIndex();
			var lastIndex = this.lastIndex;
			if(lastIndex == 0){
				lastIndex = 1;
			}
			if (this.pIndex == 0 ) {
				// endに移動する。
				this.api.end();
				this.newEntryIndex.innerHTML = "("+ lastIndex +"/"+ lastIndex +")";
				return;
			}
			this.api.prevPage();
			this.newEntryIndex.innerHTML = "("+ (this.pIndex++) +"/"+ lastIndex +")";
			return;
		}catch(e){
			alert(e);
		}
		return;
	};
	this.nextPage = function(){
		try {
			this.pIndex = this.api.getPageIndex();
			var lastIndex = this.lastIndex;
			if(lastIndex == 0){
				lastIndex = 1;
			}
			if (this.pIndex >= (this.lastIndex - 1) ) {
				// beginに移動する。
				this.api.begin();
				this.newEntryIndex.innerHTML = "(1/"+ lastIndex +")";
				return;
			}
			this.api.nextPage();
			this.pIndex += 2;
			this.newEntryIndex.innerHTML = "("+ this.pIndex +"/"+ lastIndex +")";
			return;
		}catch(e){
			alert(e);
		}
		return;
	};
	this.initScrollable = function(){
		try{
			if(!this.isFinishedInit && $("#" + this.divId + " ul li").length > 0){
				this.api = $("#" + this.divId).scrollable({
					size:6,//送り記事数
					clickable:false,//クリックしてスクロールする設定。デザインが変わるので基本はfalse
					horizontal:true,
					items:'ul.mainObjS',//読み込みitem先
					naviItem: 'li',//読み込みitem先の子要素
					speed: 800,
					api: true
				});
				this.lastIndex = parseInt(this.api.getSize()/this.api.getConf().size);
				this.pIndex = this.api.getPageIndex();
				if(this.pIndex < 1){
					this.pIndex = 1;
				}
				this.newEntryIndex = document.getElementById(this.indexDivId);
				var lastIndex = this.lastIndex;
				if(lastIndex == 0){
					lastIndex = 1;
				}
				this.newEntryIndex.innerHTML = "("+ this.pIndex +"/"+ lastIndex +")";
				this.isFinishedInit = true;
			}
		} catch(e){
			alert(e);
		}
	};
}


$("window").ready(
	function scorollable_portal() {
		for(var i = 0; i < scrollableContents.length; i++){
			scrollableContents[i].isFinishedInit = false;
		}

		intervalId = setInterval(
			function(){
				for(var i = 0; i < scrollableContents.length; i++){
					scrollableContents[i].initScrollable();
				}
				var flag = scrollableContents[0].isFinishedInit;
				for(var i = 0; i < scrollableContents.length; i++){
					flag = flag && scrollableContents[i].isFinishedInit;
				}
				if(flag){
					clearInterval(intervalId);
				}
			},
			100
		);
	}
);


