﻿if (location.hostname.match(/sake-tottori\.or\.jp/)) {
Event.observe(window, 'load', function(){
Modalbox.setOptions({width: 600});
// Start
var TOPICTITLE = "鳥取蔵元だより";
var $tbody = document.getElementsByTagName("div");
$tbody = $A($tbody).detect(function(obj, index){
	return $(obj).hasClassName("topic") && ($(obj).innerHTML == TOPICTITLE);
});
$tbody = $A($tbody.nextSiblings()).detect(function(obj, index){
	return ($(obj).getElementsByTagName("tbody").length > 0)? true :false;
}).firstChild;
var $infoDiv = $(document.createElement("div"));
$infoDiv.setStyle({color:"red", margin:"10px"});

$infoDiv.innerHTML = "＊新着情報読み込み後に、新着順に並び替えます。";
var ul_sub_menu_list;
$A(document.getElementsByTagName('ul')).each(function(obj){
	if (Element.hasClassName(obj, 'sub_menu_list')){
		ul_sub_menu_list = $(obj);
	}
});
new Insertion.Before(ul_sub_menu_list, $infoDiv);
var whatsnewArr = new Array();
// 蔵元ページアドレスリスト取得
var $userList = ul_sub_menu_list.getElementsByTagName("a");
var index = 0;
Element.update($tbody, "<tr><td>各蔵元の新着情報を読み込み中…</td></tr>");
var loadInfo = document.createElement("td");
loadInfo.id = "loadInfo";
var cnt = $A($userList).length;
$(loadInfo).innerHTML = "残り" + cnt + "蔵";
$tbody.firstChild.appendChild(loadInfo);

$$('a[href^=#]:not([href=#])').each(function(element) {
    element.observe('click', function(event) {
      new Effect.ScrollTo(this.hash.substr(1));
      //Event.stop(event);
    }.bindAsEventListener(element))
  });
$A($userList).each(ajaxReq);
function ajaxReq(value,index){
	var obj = $(value.cloneNode(true));
	var msec = (new Date()).getTime();
	var attrHref;
	if(location.hostname == "www.sake-tottori.or.jp"){
		attrHref = obj.readAttribute("href").replace("sake-tottori", "www.sake-tottori");
	} else {
		attrHref = obj.readAttribute("href");
	}
	new Ajax.Request(attrHref, {
	method: "get",
	parameters: "cache="+msec,
	onSuccess:function(httpObj){
		var trElem = document.createElement("div");
		$(trElem).innerHTML = getTable(httpObj.responseText, obj);
		trElem = $A(trElem.getElementsByTagName('tr')).sortBy( 
				function( value, index ) { return Date.parse(value.getElementsByTagName('td')[0].innerHTML) * -1; }　
			)[0];
			
		var dateElem = trElem.getElementsByTagName('td')[0];
		var topicTitle = "【" + obj.innerHTML + "】" + trElem.getElementsByTagName('td')[1].firstChild.innerHTML;
		trElem.getElementsByTagName('td')[1].childNodes[1].id = 'topics_20_' + index;
		Element.remove($(trElem.getElementsByTagName('a')[0]));
		var aElem = document.createElement("a");
		aElem.href = 'javascript: void(0)\;';
		aElem.innerHTML = topicTitle;
		Event.observe(aElem, "click", ModalboxShow, false);
		function ModalboxShow(e){
			Modalbox.show(Event.element(e).nextSibling, {title: topicTitle});
		}
		var kuramotoLink = $(document.createElement("div"));
		Element.addClassName(kuramotoLink,"content_topics_link");
		kuramotoLink.innerHTML = "蔵元ページ：";
		new Insertion.Bottom(kuramotoLink, obj.cloneNode(true));
		new Insertion.Bottom($(trElem).select('.content_topics_container')[0], kuramotoLink);
		new Insertion.Top(trElem.getElementsByTagName('td')[1], aElem);
		if (topicTitle != "【" + obj.innerHTML + "】"){
			whatsnewArr[index] = {trElem: trElem, aElem: obj};
		} else {
			whatsnewArr[index] = {trElem: null, aElem: obj};
		}
		cnt -= 1;
		$(loadInfo).innerHTML = obj.innerHTML + "読み込み完了<br />残り" + cnt + "蔵";
		if (cnt == 0){ajaxComplete();}
	},
	onFailure:function(httpObj){
		//alert("エラーで読み込めませんでした");
		cnt -= 1
		$(loadInfo).innerHTML = cnt;
		if (cnt == 0){ajaxComplete();}
	}
	});
}
function ajaxComplete(){
	Element.remove($tbody.firstChild);
	Element.remove($infoDiv);
	whatsnewArr.sortBy( 
		function( value, index ) {
			if(value.trElem != null){
				return Date.parse(value.trElem.getElementsByTagName('td')[0].innerHTML) * -1;
			} else {
				return 0;
			}
		}　
	).each(function(obj, index){
		Element.remove(ul_sub_menu_list.firstChild);
		var liElem = $(document.createElement("li"));
		liElem.innerHTML = "&#9670;&nbsp;";
		new Insertion.Bottom(liElem, obj.aElem);
		new Insertion.Bottom(ul_sub_menu_list, liElem);
		if (obj.trElem != null){
			new Insertion.Bottom($tbody, obj.trElem);
		}
	});	
}
function getTable(str, obj){
	var htmlStr = escape(str);
	var keyword1 = escape(sinchakuHTML(obj));
	var position1 = htmlStr.indexOf(keyword1);
	if (position1 == -1){
		return '<table><tbody><tr class="content_topics_tr1"><td class="content_topics_td1_date">2010/03/25</td><td class="content_topics_td1_title"><a href="javascript: void(0);">新着情報を取得できません。</a><div style="display: none;" id="topics_4_0"><div class="content_topics_container"><div class="content_topics_date">2010/04/21</div><div class="content_topics_main"><div class="content_topics_text">新着情報は直接蔵元ページ等でご確認ください。</div><br style="clear: both;"></div></div></div></td></tr></tbody></table>>';
	}
	var keyword2 = escape('</table>');
	var position2 = htmlStr.indexOf(keyword2, position1);
	htmlStr = htmlStr.substr(position1, position2 - position1 + keyword2.length);
	keyword1 = escape('<table');
	position1 = htmlStr.indexOf(keyword1);
	position2 = htmlStr.indexOf(keyword2, position1);
	htmlStr = htmlStr.substr(position1, position2 - position1 + keyword2.length);
	return unescape(htmlStr);
}

function sinchakuHTML(obj){
	var kuramoto =obj.innerHTML;
	var title;
	switch(kuramoto) {
		case "稲田本店（稲田姫）":
			title = '新着情報は稲田本店のホームページに掲載しております';
			break;
		default :
			title = '新着情報';
	}
	return '<div class="topic">' + title + '</div>';
}
// End	
}, false);

}

