//var flames = new Array(); 
var bobj;
var m = 0;

var exposureFlame = {
	divName: null,
	expType: null,
	recordCount: [],
	curFlame: [],
	retrieveData: false,
	showData: false,
	flames: [],
	divCount: 0,
	currentCount: 0,
	imageUrl: null,
	baseBlogUrl: null,
	
	init: function() {
		this.divCount = this.divName.length;
		for (var i=0; i<this.divCount; i++) {
			if (!this.retrieveData) {
				// call get banner list
				this.currentCount = i;
				this.getBannerList();
			}
		}
		if (this.showData)
			this.showBanner();
		this.loadingTick();
	},
	getBannerList: function() {
		if (this.expType[this.currentCount] == null) {
			this.expType[this.currentCount] = "";
		}
		
		var url = this.baseBlogUrl + "bookpinion/getBookpinionExposureList.rdo";
		var params = { exposure_type:this.expType[this.currentCount] };
		
		$.ajax({type: "GET", url: url, async: false, dataType: "json", data: params, success: function(data) {
			exposureFlame.assignData(data);
		}});
	},
	assignData: function(data) {
		this.recordCount[this.currentCount] = data.length;
		this.curFlame[this.currentCount] = 0;
		
		for (var i=0; i<this.recordCount[this.currentCount]; i++) {
			var c = data[i];
			bobj = new Object();
			bobj.imgpath = c.imgpath;
			bobj.link_url = c.link_url;
			bobj.height = c.height;
			bobj.width = c.width;
			this.flames[m] = bobj;
			m++;
		}
		
		if (this.currentCount == (this.divCount - 1)) {
			this.retrieveData = true;
			this.showData = true;
			//this.showBanner();
		}
	},
	showBanner: function() {
		for (var i=0; i<this.divName.length; i++) {
			if ((this.divName[i] != "")) {
				var curPosition = this.getCurrentPos(i);
					
				if ((this.flames.length > 0) && (this.curFlame[i] < this.recordCount[i]) && (curPosition < this.flames.length)) {
					var t = this.flames[curPosition];
					
					// do not create if same img url with previous one.
					if ( $("#"+this.divName[i]).find("img").get(0)!=null && $("#"+this.divName[i]).find("img").get(0).src == this.imageUrl + t.imgpath ) continue;

					var imageHTML = "<img src=\""+ this.imageUrl + t.imgpath +"\" width=\""+t.width+"\" height=\""+t.height+"\" border=\"0\">";
					var anchorHTML = "";
				 //&bookblockname=bookpinion_wel&booklinkname=rigit_banner
					if ( t.link_url != "" ) anchorHTML = "<a href=\"" + t.link_url + "\" target=\"_blank\" onFocus=\"this.blur()\">" + imageHTML + "</a>";
					else anchorHTML = imageHTML;
					
					var inHtml = "<table width=\"100%\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"margin_bott_5\"><tr><td align=\"center\">";
					inHtml += anchorHTML;
					inHtml += "</td></tr></table>";
					
					$("#"+this.divName[i]).show();
					$("#"+this.divName[i]).html(inHtml);
				} else if (this.flames.length == 0) {
					$("#"+this.divName[i]).hide();
				}
			}
		}
	},

	//ÆäÀÌÁö ·ÎµùµÉ¶§ ¼Óµµ
	loadingTick: function() {
		this.nextBannerFlame();
		setTimeout("exposureFlame.tick();", 2000);
	},
	//·ÎµùµÈÈÄ¿¡ ¼Óµµ
	tick: function() {
		this.nextBannerFlame();
		setTimeout("exposureFlame.tick();", 5000);
	},
	nextBannerFlame: function() {
		for (var i=0; i<this.divName.length; i++) {
			this.curFlame[i]++;
			if (this.curFlame[i] >= this.recordCount[i]) {
				this.curFlame[i] = 0;
			}
		}
		this.showBanner();
	},
	getCurrentPos: function(currDivPost) {
		var r = 0;
		
		for (var j=0; j<parseInt(currDivPost); j++) {
			r = r + this.recordCount[j];
		}
		
		r = r + this.curFlame[currDivPost];
		
		return r;
	}
};