// Require prototype.js
// Require function.js

var Banner = Class.create();

Banner.prototype = {

	initialize  : function() {
		this.dict = {};
    },

	setDict : function(value) {
		this.dict = value;
	},

	getDict : function() {
		return this.dict;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//作成日時
	
	getCreate_tstmp : function() {
		return this.dict['create_tstmp'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//ＩＤ(VARCHAR(255))

	setId : function(value) {
		this.dict['id'] = trim(value);
	},
	getId : function() {
		return this.dict['id'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//有効区分(INT)

	setEnable : function(value) {
		this.dict['enable'] = trim(value);
	},
	getEnable : function() {
		return this.dict['enable'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//区分(INT)

	setBanner_class : function(value) {
		this.dict['banner_class'] = trim(value);
	},
	getBanner_class : function() {
		return this.dict['banner_class'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//種別(INT)

	setType : function(value) {
		this.dict['type'] = trim(value);
	},
	getType : function() {
		return this.dict['type'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//名称(VARCHAR(255))

	setName : function(value) {
		this.dict['name'] = trim(value);
	},
	getName : function() {
		return this.dict['name'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//IDまたはURL(VARCHAR(512))

	setOwnerid : function(value) {
		this.dict['ownerid'] = trim(value);
	},
	getOwnerid : function() {
		return this.dict['ownerid'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//イメージファイル(VARCHAR(512))

	setImage : function(value) {
		this.dict['image'] = trim(value);
	},
	getImage : function() {
		return this.dict['image'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//表示頻度(INT)

	setViewrate : function(value) {
		this.dict['viewrate'] = trim(value);
	},
	getViewrate : function() {
		return this.dict['viewrate'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//表示回数(INT)

	setViewcount : function(value) {
		this.dict['viewcount'] = trim(value);
	},
	getViewcount : function() {
		return this.dict['viewcount'];
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//バリデーションチェック

	validate : function() {

		var errmsg = "";

		//種別(INT)
		if ( ! this.dict['type'] ) {
			errmsg += "・種別が設定されていません。\n";
		}

		//イメージファイル(VARCHAR(512))
/*
		if ( ! this.dict['image'] ) {
			errmsg += "・バナー画像が設定されていません。\n";
		}
*/

		return errmsg;
	},

	// - - - - - - - - - - - - - - - - - - - - -

	toJSON : function() {
		return hashToJSON(this.dict);
    }

};


var BannerList = Class.create();

BannerList.prototype = {

	initialize  : function() {
		this.array = [];
    },

	// - - - - - - - - - - - - - - - - - - - - -
	//バナー追加

	addBanner : function(banner) {
		this.array.push(banner);
	},

	merge : function(bannerlist) {
		var list = bannerlist.getBannerAll();
		if (list) {
			for(var idx = 0; idx < list.length ; idx++){
				this.addBanner( list[idx] );
			}
		}
	},


	// - - - - - - - - - - - - - - - - - - - - -
	//バナー数

	getNumOfTopic : function() {
		return this.array.length;
	},

	// - - - - - - - - - - - - - - - - - - - - -
	//バナー取得

	getBannerAll : function() {
		return this.array;
	},

	getBannerIndex : function(idx) {
		if (idx < this.array.length) {
			return this.array[idx];
		}
		return null;
	}

};

function readBanner(isHeadline, id, param, callback) {

	var asynchronous = (callback != null);
	var reqparam = "json";
	var url = "/banner/";

	if (isHeadline) {
		url += "headline/" + id;
	} else {
		url += "content/" + id;
	}

	var banner = null;

	if (param) {
		reqparam += "&" + param;
	}

	//alert(url);
	//alert(reqparam);

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": asynchronous,
			"requestHeaders": ['RequestPath', location.pathname],
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				var list = json.bannerResponse.bannerList.banner;
				if (! list ) {
					return;
				}

				banner = new Banner();
				banner.setDict( list[0] );

			},
			onComplete: function(request) {
				if (callback) {
					callback(banner);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return banner;
}

function readBannerList(isHeadline, idparam, type, num, callback) { 

	var url = "/banner/";

	if (isHeadline) {
		url += "headline/";
	} else {
		url += "content/";
	}

	return _readBannerList(url, idparam, type, num, callback);
}

function searchBannerList(idparam, type, num, callback) { 
	_readBannerList("/banner/search", idparam, type, num, callback);
}

function _readBannerList(url, idparam, type, num, callback) { 

	var bannerlist = null;
	var reqparam = "";
	var asynchronous = (callback != null);

	if (idparam) {
		reqparam += "&" + idparam;
	}

	if (type) {
		reqparam += "&type=" + type;
	}

	if (num) {
		reqparam += "&num=" + num;
	}

	//alert(url);
	//alert(reqparam);

	new Ajax.Request(
		url, 
		{
			"method": "get", 
			"parameters": reqparam + "&json&request_dummy=" + Math.random(), 
			"asynchronous": asynchronous,
			"requestHeaders": ['RequestPath', location.pathname],
			onSuccess: function(request) {
				//alert(request.responseText);
				var json; 
				eval("json="+replaceAll(request.responseText,"\n","\\n"));

				var list = json.bannerResponse.bannerList.banner;
				if ( list ) {
					bannerlist = new BannerList();
					for(var idx = 0; idx < list.length ; idx++) {
						var banner = new Banner();
						banner.setDict( list[idx] );
						bannerlist.addBanner( banner );
					}
				}

			},
			onComplete: function(request) {
				if ( callback ) {
					callback(bannerlist);
				}
			},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	);

	return bannerlist;
}




