
//クラス定義
var cls_google_image = function(arg){
	this.className = "cls_google_image";
	this.page = 0;
	this.requestFlg = true;
	this.total = 0;
	this.get_count = 0;
	this.per_page = arg;
	this.pool_data = new Array;
	this.is_error = 0;
	this.success = false;
}

//メソッド定義
cls_google_image.prototype = {

	//画像検索メソッド
	search : function() {

		if(!this.requestFlg){
			return true;
		}
		if(this.page >= 7){
			this.requestFlg = false;
		}

		this.success = false;

		flg_g = false;
		$j.ajax({
			dataType: "jsonp",
			data: {
				"lstkp":"0",
				"q":(typeof g_word == "undefined") ? word : g_word,
				"rsz":"large",
				"hl":"ja",
				"key":"ABQIAAAAYHr0vLR8dC5u6LUQZFojfxQHZ2Kprwbf3Jzj0VlnqUmao9jn7hRmOcP1huMDJhfdW1HxQu_PN_tKcA",
				"v":"1.0",
				"start":(this.page * 8),
				"safe":safe_search
			},
			url: "http://www.google.co.jp/uds/GimageSearch",
			timeout: 3000,
			success: function(data){
				image_search.google_image.page++;
				image_search.google_image.success = true;
				image_search.google_image.is_error = 0;
				data = data['responseData'];
				if(data && data['cursor'] && data['cursor']['estimatedResultCount'] != 0){
					image_search.google_image.total = data['cursor']['estimatedResultCount'] < 64 ? data['cursor']['estimatedResultCount'] : 64;
					if(!cls_google_image.google_data) cls_google_image.google_data = new Array;

					results = data['results'];
					//除外設定
					var iLimit = results.length;

						//指定件数に満たない場合以後リクエストは行わない
						if(iLimit < image_search.google_image.request_per_page){
							image_search.google_image.requestFlg = false;
						}

					for( var i=0; i<iLimit; i++ ) {
						tmp = results[i];
						info  = {};
						info['id'] = tmp['imageId'];
						info['img'] = tmp['unescapedUrl'];
						info['thumb'] = tmp['tbUrl'];
						info['name'] = tmp['contentNoFormatting'];
						if(chkExclude(info['img']) == false){
							image_search.google_image.pool_data.push(info);
						}
					}
				}
				flg_g = true;
				org_stop();
			},
			complete: function(){
				if(image_search.google_image.success == false){
					 if(image_search.google_image.is_error >= 2){
						image_search.google_image.requestFlg = false;
					}else{
						image_search.google_image.is_error++;
					}
				}
			}
		});
	},
	getImages : function(needCount) {

		image = new Array;

		var iLimit = this.pool_data.length;
		for( var i=0; i<iLimit; i++ ) {
			//規定数に達したら終了
			if(needCount <= i){
				break;
			}
			info = this.pool_data.shift();
			image.push(info);
			image_search.google_image.get_count ++;
		}

		return image;
	}
};
