var Banner = Class.create();

Banner.prototype = {
	initialize : function () {
	},

	// 사용 예 : new Banner().image('A01', 3, false, 142, 60, true);
	image : function (type, limit, isHorizontally, width, height, isRand, style) {
		var d = this.getList(type, isRand);

		try {
			document.write('<div class="banner" id="_b_ini_' + type + '_"></div>');

			var div = $('_b_ini_' + type + '_');
			var tpl = new Template('<div class="image" style="#{css}">#{img}</div>');
			var cnt = 0;

			for (var i = 0; i < d.length; i++) {
				if (limit > 0 && cnt >= limit) {
					break;
				}

				div.insert(tpl.evaluate(
					{
						css : (isHorizontally ? 'float:left;' : '') + (style ? style : ''),
						img : this.getBanner(d[i].seq, d[i].path, width, height, d[i].swf, d[i].url, d[i].target, d[i].title, d[i].map)
					}));

				cnt++;
			}

			if (cnt == 0) {
				div.remove();
			}
		} catch (e) {
		}
	},

	// 사용 예 : new Banner().text('A01', 3, 20, true);
	text : function (type, limit, strLen, isRand, style) {
		var d = this.getList(type, isRand);

		try {
			document.write('<div class="banner" id="_b_ini_' + type + '_"></div>');

			var div = $('_b_ini_' + type + '_');
			var tpl = new Template('<div class="text" style="#{css}"><a href="#{url}" target="#{tgt}">#{sbj}</a></div>');
			var cnt = 0;

			for (var i = 0; i < d.length; i++) {
				if (limit > 0 && cnt >= limit) {
					break;
				}

				div.insert(tpl.evaluate(
					{
						css : style ? style : '',
						url : d[i].url,
						tgt : d[i].target,
						sbj : strLen > 0 ? d[i].title.truncate(strLen, "...") : d[i].title
					}));

				cnt++;
			}
		} catch (e) {
		}
	},

	// 사용 예 (이미지) : new Banner().slide('A01', 'up/down/left/right', 142, 60, 0.002, 2, true, false, 0);
	// 사용 예 (텍스트) : new Banner().slide('A01', 'up/down/left/right', 200, 20, 0.002, 2, true, true, 20);
	slide : function (type, direction, width, height, speed, interval, isRand, isText, strLen, style) {
		var d = this.getList(type, isRand);

		try {
			var div = new TextSlider('_b_ini_' + type + '_');
			var tpl = new Template(isText ? '<div style="#{css}"><a href="#{url}" target="#{tgt}">#{sbj}</a></div>' : '<div style="#{css}">#{img}</div>');

			for (var i = 0; i < d.length; i++) {
				if (isText) {
					div.item[i] = tpl.evaluate(
						{
							css : style ? style : '',
							url : d[i].url,
							tgt : d[i].target,
							sbj : strLen > 0 ? d[i].title.truncate(strLen, "...") : d[i].title
						}).toString();
				} else {
					div.item[i] = tpl.evaluate(
						{
							css : style ? style : '',
							img : this.getBanner(d[i].seq, d[i].path, width, height, d[i].swf, d[i].url, d[i].target, d[i].title, d[i].map)
						}).toString();
				}
			}

			div.width = width;
			div.height = height;
			div.speed = speed;
			div.interval = interval;
			div.size = Common.inArray(direction, ['up', 'down']) ? height + 1 : width + 1;
			div.direction = direction;
			div.init();
		} catch (e) {
		}
	},

	getList : function (type, isRand) {
		var s = [];
		var n = 0;

		try {
			var d = G_BANNER_LIST;

			for (var i = 0; i < d.length; i++) {
				if (d[i].type != type) {
					continue;
				}

				s[n++] = d[i];
			}

			if (isRand) {
				s = Common.shuffle(s);
			}
		} catch (e) {
		}

		return s;
	},

	getBanner : function (seq, src, width, height, swf, url, target, title, map) {
		if (swf == "Y") {
			return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + location.protocol + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"' + (width ? ' width="' + width + '"' : '') + (height ? ' height="' + height + '"' : '') + '" id="_' + Math.floor(Math.random() * 1000000) + '_">'
			     + '<param name="movie" value="' + G_TOP_DIR + src + '" />'
			     + '<param name="allowScriptAccess" value="always" />'
			     + '<param name="allowFullScreen" value="false" />'
			     + '<param name="wmode" value="transparent" />'
			     + '<embed src="' + G_TOP_DIR + src + '"' + (width ? ' width="' + width + '"' : '') + (height ? ' height="' + height + '"' : '') + '" wmode = "transparent" type="application/x-shockwave-flash" pluginspage="' + location.protocol + '//www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" />'
			     + '</object>';
		} else {
			if (map) {
				return '<img src="' + G_TOP_DIR + src + '"' + (width ? ' width="' + width + '"' : '') + (height ? ' height="' + height + '"' : '') + '" border="0" alt="' + title + '" usemap="#_b_map_' + seq + '_" /><map name="_b_map_' + seq + '_" id="_b_map_' + seq + '_">' + map + '</map>';
			} else {
				return (url ? '<a href="' + url + '" target="' + target + '">' : '') + '<img src="' + G_TOP_DIR + src + '"' + (width ? ' width="' + width + '"' : '') + (height ? ' height="' + height + '"' : '') + '" border="0" alt="' + title + '" /></a>';
			}
		}
	}
}

if ($A(document.getElementsByTagName("script")).findAll(function (s) {
		var include = s.src.match(/Banner.js\?.*load=([A-Za-z0-9.\:\/\_\?\=\&]*)/);

		// 배너 데이터 파일
		if (include) {
			document.write('<' + 'script language="javascript" src="' + include[1] + '"><' + '/script>');
		}

		return (s.src && s.src.match(/TextSlider\.js/));
	}) == '') {
		// TextSlider.js
		document.write('<' + 'script language="javascript" src="' + G_TOP_DIR + '/common/js/TextSlider.js"><' + '/script>');
	}
