/**
 * Ä¿¹Â´ÏÆ¼ °øÅë
 * ¿ä±¸»çÇ×
 *   prototype.js  (tested 1.6)
 *   jsslib.js
 *   jsslib.css
 */

/**
 * Ä¿¹Â´ÏÆ¼ °øÅë °ü·Ã À¯Æ¿¸®Æ¼
 */
var Commons = {
	commentSubmit: function(f) {
		// check form is valid
		if (f.pid == null || f.pid.value == '') {
			alert('ÇÁ·Î±×·¥ ID°¡ ÁöÁ¤µÇÁö ¾Ê¾Ò½À´Ï´Ù.');
			return false;
		}
		if (f.cid == null || f.cid.value == '') {
			alert('ÄÁÅÙÆ® ID°¡ ÁöÁ¤µÇÁö ¾Ê¾Ò½À´Ï´Ù.');
			return false;
		}
		if (f['return'] == null || f['return'].value == '') {
			alert('º¹±Í ÆäÀÌÁö°¡ ÁöÁ¤µÇÁö ¾Ê¾Ò½À´Ï´Ù.');
			return false;
		}
		if (f.mode == null || f.mode.value == '') {
			alert('µ¿ÀÛ¸ðµå°¡ ÁöÁ¤µÇÁö ¾Ê¾Ò½À´Ï´Ù.');
			return false;
		}

		// check input is valid
		if (f.content == null || f.content.value == '') {
			alert('³»¿ëÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä.');
			f.content.focus();
			return false;
		}
		return true;
	}
};

/**
 * ÃßÃµÇÏ±â
 */
var CommonsRecommend = function(target, options) {
	if (options == null)
		options = {};
	this.pid = options.pid;
	this.cid = options.cid;
	this.target = target;
	this.target.recommend = this;
	Event.observe(this.target, 'click', this.handleClick.bind(this));
};
CommonsRecommend.autoInit = true;
AutoInitializer.add('CommonsRecommend', CommonsRecommend);
Object.extend(CommonsRecommend.prototype, {
	handleClick: function(event) {
		var parameters = {
						mode: 'RW',
						pid: this.pid,
						cid: this.cid
					};
		AjaxUtils.requestAjax('/common/commons_ajax.php', {
				method: 'post',
				parameters: parameters,
				onSuccess: this.onRecommendSuccess.bind(this)
			});
	},
	onRecommendSuccess: function(result) {
		alert('ÃßÃµµÇ¾ú½À´Ï´Ù.');
		var rcmdCount = result;
		var em = Element.down(this.target, 'em');
		if (em)
			em.innerHTML = NumberFormat.comma.format(rcmdCount);
	}
});

/**
 * ½Å°íÇÏ±â
 */
var CommonsIndict = function(target, options) {
	if (options == null)
		options = {};
	this.pid = options.pid;
	this.cid = options.cid;
	options.onchange = this.submitIndict;
	this.constructor.initInstance(this, target, options);
};
AutoInitializer.add('CommonsIndict', CommonsIndict);
Object.extend(CommonsIndict, ToolBinder);
Object.extend(CommonsIndict, {
	tool: new ToolLayer('CommonsIndict', ''),
	makeButton: false,
	autoInit: true
});
Object.extend(CommonsIndict.prototype, ToolBinderPrototype);
Object.extend(CommonsIndict.prototype, {
	submitIndict: function() {
		var value = this.getValue();
		var parameters = {
						mode: 'IW',
						indict_type: value.indictType,
						content: value.content,
						pid: this.pid,
						cid: this.cid
					};
		//alert(Object.toJSON(parameters));
		AjaxUtils.requestAjax('/common/commons_ajax.php', {
				method: 'post',
				parameters: parameters,
				onSuccess: this.onIndictSuccess.bind(this)
			});
	},
	onIndictSuccess: function(result) {
		alert('½Å°í°¡ Á¢¼öµÇ¾ú½À´Ï´Ù.');
	}
});
Object.extend(CommonsIndict.tool, {
	/**
	 * ·¹ÀÌ¾î°¡ Ç¥½ÃµÉ¶§ ÆûÀ» ÃÊ±âÈ­ÇÑ´Ù.
	 */
	setInitialValue: function(value) {
		FormUtils.clear(this.form.indict_type);
		this.form.content.value = this.form.content.defaultValue;
	},
	initTool: function() {
		var html = '<form id="CommonsIndictForm" method="get" action="javascript:void(0)" onsubmit="CommonsIndict.tool.handleSubmit();return false;">\n' +
			'<div class="pop_repert box1">\n' +
				'<div class="inner">\n' +
					'<div class="pop_title">½Å°íÇÏ±â</div>\n' +
					'<ul>\n' +
						'<li><input type="radio" name="indict_type" value="1" id="indict_type_1" class="in_ra" /><label for="indict_type_1">ºÎÀûÇÕÇÑ ³»¿ë</label></li>\n' +
						'<li><input type="radio" name="indict_type" value="2" id="indict_type_2" class="in_ra" /><label for="indict_type_2">ºñ°ø°³/»èÁ¦µÈ ±Û</label></li>\n' +
						'<li><input type="radio" name="indict_type" value="3" id="indict_type_3" class="in_ra" /><label for="indict_type_3">µµ¹è¼º ±Û</label></li>\n' +
						'<li><input type="radio" name="indict_type" value="4" id="indict_type_4" class="in_ra" /><label for="indict_type_4">ÀúÀÛ±ÇÄ§ÇØ ±Û</label></li>\n' +
						'<li><input type="radio" name="indict_type" value="5" id="indict_type_5" class="in_ra" /><label for="indict_type_5">±âÅ¸</label></li>\n' +
					'</ul>\n' +
					'<textarea name="content" rows="2" cols="35" onfocus="CommonsIndict.tool.handleFocus();">µ¡ºÙÀÏ ½Å°íÀÌÀ¯°¡ ÀÖ´Ù¸é Àû¾îÁÖ¼¼¿ä.</textarea>\n' +
					'<p>½Å°íµÈ °Ô½Ã¹°Àº °ü¸®ÀÚ°¡ È®ÀÎÈÄ »èÁ¦/¼öÁ¤/À¯Áö µË´Ï´Ù.<br />ÇãÀ§½Å°íÀÏ °æ¿ì È°µ¿¿¡ Á¦ÇÑÀ» ¹Þ°Ô µÉ ¼ö ÀÖ½À´Ï´Ù.</p>\n' +
					'<div class="button_c">\n' +
						'<input type="image" src="/images/common/btn1_report.gif" width="70" height="20" alt="½Å°íÇÏ±â" />\n' +
						'<a href="javascript:void(0)" onclick="CommonsIndict.tool.hide();"><img src="/images/common/btn1_cancel2.gif" width="70" height="20" alt="Ãë¼ÒÇÏ±â" /></a>\n' +
					'</div>\n' +
				'</div>\n' +
			'</div>\n' +
		'</form>';
		Element.update(this.layer, html);
		this.form = $('CommonsIndictForm');
	},
	/**
	 * DOM ÀÌº¥Æ® ÇÚµé·¯
	 */
	handleSubmit: function() {
		var indictType = FormUtils.get(this.form.indict_type);
		if (indictType == null) {
			alert('½Å°í ÀÌÀ¯¸¦ ¼±ÅÃÇØ ÁÖ½Ê½Ã¿À.');
			this.form.indict_type[0].focus();
			return;
		}
		var content = (this.form.content.value == this.form.content.defaultValue)? '': this.form.content.value;
		if (confirm('¹«½ÉÄÚ ´øÁø ½Å°í¿¡ »ç¶÷Àº »óÃ³¹Þ±âµµ ÇÕ´Ï´Ù.\n\n½Å°íÇÏ½Ã°Ú½À´Ï±î?')) {
			this.setReturn({
					indictType: indictType,
					content: content
				});
		}
	},
	handleFocus: function() {
		if (this.form.content.value == this.form.content.defaultValue)
			this.form.content.value = '';
	}
});

/**
 * º°Á¡ Ç¥½Ã¿ë
 */
var PointStar = {
	imageOn: '/images/common/point5_on.gif',
	imageOff: '/images/common/point5_off.gif',
	valueUnit: 2,
	values: {},
	render: function(id, readonly) {
		if (readonly == null)
			readonly = false;
		var container = $(id);
		if (container == null)
			return;
		var value = (this.values[id] == null)? 0: Number(this.values[id]);
		var html = '';
		for (var i = 0; i < 5; i++) {
			var src = (i + 1 <= value)? this.imageOn: this.imageOff;
			var margin = (i < 4)? 'style="margin-right:8px;"': '';
			if (readonly)
				html += '<img src="' + src + '" alt="' + (i + 1) + '" border="0" ' + margin + '/>';
			else
				html += '<a href="javascript:void(0)" onclick="PointStar.set(\'' + id + '\', ' + ((i + 1) * PointStar.valueUnit) + ');return false;" title="' + (i + 1) + '"><img src="' + src + '" alt="' + (i + 1) + '" border="0" ' + margin + '/></a>';
		}
		container.update(html);
	},
	renderCallback: function(id, callback, readonly) {
		if (readonly == null)
			readonly = false;
		var container = $(id);
		if (container == null)
			return;
		var value = (this.values[id] == null)? 0: Number(this.values[id]);
		var html = '';
		for (var i = 0; i < 5; i++) {
			var src = (i + 1 <= value)? this.imageOn: this.imageOff;
			if (readonly)
				html += '<img src="' + src + '" alt="' + (i + 1) + '" border="0">\n';
			else
				html += '<a href="javascript:void(0)" onclick="'+callback+'(\'' + id + '\', ' + value + ', ' + (i + 1) + ');return false;" title="' + (i + 1) + '"><img src="' + src + '" alt="' + (i + 1) + '" border="0"></a>\n';
		}
		container.update(html);
	},
	set: function(id, value) {
		ImageUtils.preload([PointStar.imageOn, PointStar.imageOff]);
		var container = $(id);
		if (container == null)
			return;
		this.values[id] = (value == null)? 0: Math.floor(value / PointStar.valueUnit);
		this.render(id);
	},
	get: function(id) {
		return (this.values[id] == null)? null: this.values[id] * PointStar.valueUnit;
	}
};

/**
 * ÄÞº¸¹Ú½º °ü·Ã
 */
var ComboBoxes = {
	map: {},
	add: function(combo) {
		this.map[combo.id] = combo;
	},
	select: function(id) {
		var combo = this.map[id];
		if (combo != null)
			combo.select();
	},
	toggle: function(id) {
		var combo = this.map[id];
		if (combo != null)
			combo.toggle();
	},
	close: function(id) {
		var combo = this.map[id];
		if (combo != null)
			combo.close();
	},
	open: function(id) {
		var combo = this.map[id];
		if (combo != null)
			combo.open();
	},
	writeComboBox: function(formId, inputName, options, styles, callbacks) {
		var id = formId + "." + inputName;
		var defaultOption = null;
		for (var i = 0; i < options.length; i++) {
			if (options[i] == null)
				continue;
			if (options[i].selected) {
				defaultOption = options[i];
				break;
			}
		}
		if (defaultOption == null && options.length > 0) {
			defaultOption = options[0];
			defaultOption.selected = true;
		}

		var width = (styles != null && styles.width != null)? styles.width: '100px';
		var height = (styles != null && styles.height != null)? styles.height: '400px';
		var boxWidth = (styles != null && styles.boxWidth != null)? styles.boxWidth: width;
		var boxHeight = ( options.length > 20 ) ? height: '';
		var overFlow = ( options.length > 20 ) ? 'scroll': 'visible';
		var className = (callbacks.className)? callbacks.className: '';
		if (className.indexOf('comboBox') < 0)
			className += ' comboBox';
		var html = ComboBoxes.templates.comboHead.evaluate({
			id: id,
			defaultText: (defaultOption)? defaultOption.text: '',
			width: width,
			overFlow: overFlow,
			boxWidth: boxWidth,
			boxHeight: boxHeight,
			className: className
		});
		for (var i = 0; i < options.length; i++) {
			if (options[i] == null)
				continue;
			var option = options[i];
			var values = {
				id: id, inputName: inputName, text: option.text, value: option.value,
				detail: ((option.detail != null)? '<div id="' + id + '.' + option.value +  '.detail"  style="display: none">' + option.detail + '</div>': "")
			};
			if (option.selected)
				html += ComboBoxes.templates.comboDefault.evaluate(values);
			else
				html += ComboBoxes.templates.comboRow.evaluate(values);
		}
		html += ComboBoxes.templates.comboFoot.template;

		document.write(html);
		var comboObj = new ComboBox(formId, inputName);
		if (callbacks && callbacks.onSelect)
			comboObj.onSelect = callbacks.onSelect;
		if (callbacks && callbacks.onClear)
			comboObj.onClear = callbacks.onClear;
		return comboObj;
	},
	templates: {
		comboHead: new Template('<div id="#{id}" class="#{className}" style="width:#{width};"><div class="comboBoxTxt">#{defaultText}</div>' +
					'<div class="comboBoxBtn"><a href="javascript:void(0);" onclick="ComboBoxes.toggle(\'#{id}\');return false;"><img src="/images/common/selectbox_arr.gif" alt="" /></a></div>' +
					'<div class="comboBoxContent" style="display:none;">' +
						'<div class="comboBoxOption" style="width:#{boxWidth};height:#{boxHeight};overflow-y:#{overFlow};"><ul id="#{id}.options">'),
		comboDefault: new Template('<li><input id="#{id}.#{value}" name="#{inputName}" type="radio" value="#{value}" checked="checked" class="radio" onclick="ComboBoxes.select(\'#{id}\')" />' +
						'<label for="#{id}.#{value}" id="#{id}.#{value}.label" style="font-weight:bold" onclick="$(this.htmlFor).click();">#{text}</label>#{detail}</li>'),
		comboRow: new Template('<li><input id="#{id}.#{value}" name="#{inputName}" type="radio" value="#{value}" class="radio" onclick="ComboBoxes.select(\'#{id}\')" />' +
					'<label for="#{id}.#{value}" id="#{id}.#{value}.label" onclick="$(this.htmlFor).click();">#{text}</label>#{detail}</li>'),
		comboFoot: new Template('</ul></div></div></div>')
	}
};
function ComboBox(formId, inputName) {
	this.id = formId + "." + inputName;
	this.container = $(this.id);
	this.form = $(formId);
	this.inputName = inputName;
	this.inputObj = this.form[inputName];
	this.txtLayer = (this.container.getElementsByClassName('comboBoxTxt'))[0];
	this.boxLayer = (this.container.getElementsByClassName('comboBoxContent'))[0];

	this.container.comboBox = this;
	if (this.inputObj)
		this.inputObj.comboBox = this;
	ComboBoxes.add(this);
}
ComboBox.prototype = {
	onClear: null,
	onSelect: null,
	select: function(ignoreOnSelect) {
		var close = true;
		for (var i = 0; i < this.inputObj.length; i++) {
			var radio = this.inputObj[i];
			var label = $(radio.id + ".label");
			var detail = $(radio.id + ".detail");
			if (radio.checked) {
				this.txtLayer.innerHTML = label.innerHTML;
				label.style.fontWeight = 'bold';
				if (detail != null) {
					detail.show();
					close = false;
				}
			}
			else {
				label.style.fontWeight = '';
				if (detail != null)
					detail.hide();
			}
		}

		if (!ignoreOnSelect && this.onSelect != null)
			this.onSelect();
		if (close)
			this.close();
	},
	setValue: function(value) {
		FormUtils.set(this.inputObj, value);
		this.select();
	},
	getValue: function() {
		return FormUtils.get(this.inputObj);
	},
	clear: function() {
		this.inputObj[0].checked = true;
		this.select();
		if (this.onClear != null)
			this.onClear();
	},
	toggle: function() {
		if (this.boxLayer.style.display == 'none')
			this.open();
		else
			this.close();
	},
	open: function() {
		this.boxLayer.style.display = 'block';
	},
	close: function() {
		this.boxLayer.style.display = 'none';
	}
};
