
//クラス定義
var cls_ameba_video = function(arg){
	this.className = "cls_ameba_video";
	this.page = 0;
	this.ameba_data = null;
	this.requestFlg = true;
	this.total = 0;
	this.get_count = 0;
	this.per_page = arg;
	this.ameba_pool_data = null;

}

//メソッド定義
cls_ameba_video.prototype = {

	//画像検索メソッド
	search : function(word, page) {
		if(!this.requestFlg){
			return true;
		}

		this.page += 1;

		$j.ajax({
			dataType: "jsonp",
			data: {
				"keyword": word,
				"offset": ((this.page-1)*this.per_page),
				"length":this.per_page,
				"format":"jsonp"
			},
			timeout: 3000,
			url: "http://vision.ameba.jp/api/get/search/keyword/rating.do",
			success: function(data){
				if(data && data['totalCount'] != 0){
					video_search.ameba_video.total = data['totalCount'];
//					if(!cls_ameba_video.ameba_data) cls_ameba_video.ameba_data = new Array;
//					cls_ameba_video.ameba_data = cls_ameba_video.ameba_data.concat(data['item']);

					//アメーバAPIが24件しかデータを返さないが、総件数は正しく返却するための対策
					if(data['totalCount'] > 24){
						video_search.ameba_video.total = 24;
					}

					cls_ameba_video.ameba_data = new Array;
					offset = (video_search.ameba_video.page -1 ) * video_search.ameba_video.per_page;
						tmp = new Array;
						for(i=offset; i< data['item'].length; i++){
						tmp = tmp.concat(data['item'][i]);
					}
					cls_ameba_video.ameba_data = cls_ameba_video.ameba_data.concat(tmp);
				}
			}
		});
		return false;
	},

	getImages : function() {

		image = new Array;

		if(cls_ameba_video.ameba_pool_data) {
			image = cls_ameba_video.ameba_pool_data;
			cls_ameba_video.ameba_pool_data = null;
		}
		var data = cls_ameba_video.ameba_data;

		if(data == null){
			this.requestFlg = false;
			return image;
		}

		for( var i=0; i<this.per_page; i++ ) {
			tmp = data.shift();
			if(!tmp){
				//取得できるデータが無い＝結果の末尾
				this.requestFlg = false;
				return image;
			}
			this.get_count++;
			info  = {};
			info['video'] = tmp['link'];
			info['thumb'] = tmp['imageUrlMedium'];
			info['name'] = tmp['title'];
			info['id'] = tmp['tagId'];
			image.push(info);
		}

		return image;
	},
	setImages : function(data) {
		cls_ameba_video.ameba_pool_data = data;
	}
};
