/**
 * ¸ÞÀÎ ·Ñ¸µ
 */
function MainVisualView(show_idx, max_cnt) {
	for (var i=0; i<max_cnt ; i++) {
		var obj = $('event'+i);
		if (show_idx==i) obj.show();
		else obj.hide();
	}
}

function MainVisualViewRolling(max_cnt) {
	var rand_pos = Math.floor(max_cnt*Math.random());

	MainVisualView(rand_pos, max_cnt);

	new PeriodicalExecuter(function(pe) {
		if (++rand_pos >= max_cnt) {
			rand_pos = 0;
		}
		MainVisualView(rand_pos, max_cnt);
	}, 4);
}

/**
 * ÃßÃµ ¾ÆÀÌÅÛ ÅÇ
 */
var ZoneHotItemTab = {
	init: function(cate_id) {
		this._cache = {};
		this._saved = { 'cate_id': cate_id, 'layer': $('zone_hot_item') };

		// ÃÊ±â°ª Ä³½¬
		this._cache[cate_id] = this._saved.layer.innerHTML;
	},

	show: function(cate_id) {
		if (this._saved.cate_id==cate_id) return;

		this._request(cate_id);
	},

	_request: function(cate_id) {
		if (this._cache[cate_id]) {
			this._update(cate_id, this._cache[cate_id]);
			return;
		}

		var url = "/zone/ajax/hot_item";
		var param = "cate_id=" + cate_id;

		new Empas.Ajax.Request(url, {
			parameters: param,
			onComplete: function(transport) {
				var response = transport.responseText;
				if (response!="") {
					eval(response);
					this._draw(cate_id, ZONE_HOT_ITEM_JSON);
				}
			}.bind(this),
			onFailure: function(transport) {
//				window.alert(transport.responseText);
			}
		});
	},

	_draw: function(cate_id, ZONE_HOT_ITEM_JSON) {
		var template = new Template("<dl class='img_list #{first}'> \
					<dt><a title=\"#{org_title}\" href='/view/#{article_sn}'>#{title}</a></dt> \
					<dd class='thum'><span><a href='/view/#{article_sn}'><img src='"+ g_IMAGE_URL +"/img/thumb/#{article_sn}/article' border='0'/></a></span></dd> \
					<dd class='star'><div class='StarGrade_s'><span class='StarSmall innerStar#{rating_style}'></span></div><span class='starnum'>#{rating_point}</span></dd> \
				</dl>");

		var sOutput = "";
		ZONE_HOT_ITEM_JSON.each(function(item, idx) {
			item.first = idx==0 ? "first":"";
			sOutput += template.evaluate(item);
		});

		this._update(cate_id, sOutput);

		this._cache[cate_id] = sOutput;
	},

	_update: function(cate_id, html) {
		// ¸Þ´º ON/OFF º¯°æ
		$('zone_hot_item_menu_'+this._saved.cate_id).className = "";
		$('zone_hot_item_menu_'+cate_id).className = "link_on";

		this._saved.layer.update(html);
		this._saved.cate_id = cate_id;
	}
}

var ZoneHotItemTabInHome = {
        init: function(cate_link_key) {
                this._cache = {};
                this._saved = { 'cate_link_key': cate_link_key, 'layer': $('zone_hot_item') };
 
                // ÃÊ±â°ª Ä³½¬
                this._cache[cate_link_key] = this._saved.layer.innerHTML;
        },
 
        show: function(cate_link_key) {
                if (this._saved.cate_link_key==cate_link_key) return;
 
                this._request(cate_link_key);
        },
 
        _request: function(cate_link_key) {
                if (this._cache[cate_link_key]) {
                        this._update(cate_link_key, this._cache[cate_link_key]);
                        return;
                }
 
                var url = "/zone/ajax/hot_item";
                var param = "cate_link_key=" + cate_link_key;
 
                new Empas.Ajax.Request(url, {
                        parameters: param,
                        onComplete: function(transport) {
                                var response = transport.responseText;
                                if (response!="") {
                                        eval(response);
                                        this._draw(cate_link_key, ZONE_HOT_ITEM_JSON);
                                }
                        }.bind(this),
                        onFailure: function(transport) {
//                              window.alert(transport.responseText);
                        }
                });
        },
 
        _draw: function(cate_link_key, ZONE_HOT_ITEM_JSON) {
                var template = new Template("<dl class='img_list #{first}'> \
                                        <dt><a title=\"#{org_title}\" href='/view/#{article_sn}'>#{title}</a></dt> \
                                        <dd class='thum'><span><a href='/view/#{article_sn}'><img src='"+ g_IMAGE_URL +"/img/thumb/#{article_sn}/article' /></a></span></dd> \
                                        <dd class='star'><div class='StarGrade_s'><span class='StarSmall innerStar#{rating_style}'></span></div><span class='starnum'>#{rating_point}</span></dd> \
                                </dl>");
 
                var sOutput = "";
                ZONE_HOT_ITEM_JSON.each(function(item, idx) {
                        item.first = idx==0 ? "first":"";
                        sOutput += template.evaluate(item);
                });
 
                this._update(cate_link_key, sOutput);
 
                this._cache[cate_link_key] = sOutput;
        },
 
        _update: function(cate_link_key, html) {
                // ¸Þ´º ON/OFF º¯°æ
                $('zone_hot_item_menu_'+this._saved.cate_link_key).className = "";
                $('zone_hot_item_menu_'+cate_link_key).className = "link_on";
 
                this._saved.layer.update(html);
                this._saved.cate_link_key = cate_link_key;
        }
}
