/*
 *	top.js - utf-8
 *
 *	2009-12-10
 *	Copyright (c) HITP written by Hirotaka Yamashita.
 */

var AnimeElement = function (type, url, linkUrl) {
	this.type = type;
	if (this.type == 'img') {
		this.view = new Image();
		this.view.src = url;
	} else {
		this.view = url;
	}
	if (linkUrl == undefined) {
		this.view.link = null;
	} else {
		this.view.link = linkUrl;
	}
}

var timerID = setTimeout('', 1);
var timerID2 = setTimeout('', 1);
var index = 0;
var animeArray = new Array();
var flashFlag = true;

function loadFlash(url,width,height){
	html='';
	html+='<object type="application/x-shockwave-flash"';
	html+=' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	html+=' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"';
	html+=' width="'+width+'" height="'+height+'">';
	html+='<param name="movie" value="'+url+'">';
	html+='<param name="quality" value="high">';
	html+='<embed src="'+url+'" quality="high"';
	html+=' pluginspage="http://www.macromedia.com/jp/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"';
	html+=' type="application/x-shockwave-flash" width="'+width+'" height="'+height+'">';
	html+='</embed>';
	html+='</object>';

	return html;
}

function flashView(url) {
	var timer = 0;
	if (flashFlag) {
		var html = loadFlash(url,"420","250");
		$('div#indexMenuCenter').html(html);
		if (null == $('div#indexMenuCenter').html()) {
			timerID = setTimeout('anime()', 1000);
			return;
		}
		timer = 7000;		/*Flash time*/
	}
	timerID = setTimeout('anime()', timer);
}

function fadeOut() {
	clearTimeout(timerID2);
	$('img#imageView').fadeOut(2000);
}

function anime () {
	clearTimeout(timerID);
	if (animeArray[index].type == 'flash') {
		flashView(animeArray[index].view);
		index++;
		if (index >= animeArray.length) {
			index = 0;
		}
	} else {
		if (animeArray[index].view.link != null) {
			$('div#indexMenuCenter').html('<a href="' + animeArray[index].view.link + '"><img id="imageView" style="display:none" src="' + animeArray[index].view.src + '" /></a>');
		} else {
			$('div#indexMenuCenter').html('<img id="imageView" style="display:none" src="' + animeArray[index].view.src + '" />');
		}
		if (null == $('div#indexMenuCenter').html()) {
			timerID = setTimeout('anime()', 1000);
			return;
		}
		index++;
		if (index >= animeArray.length) {
			index = 0;
		}
		$('img#imageView').fadeIn(1500);
		timerID = setTimeout('anime()', 5000);	/*Image time*/
		timerID2 = setTimeout('fadeOut()', 3500);
	}
}

function init() {
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_opencampus.jpg", "./2010/opencampus/opencampus.html"));
//	animeArray.push(new AnimeElement('flash', "http://www.hitp.ac.jp/flash/fla_index.swf"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_kentiku_new.jpg", "./2010/subject/kentiku/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_senkouka_new.jpg", "./2010/subject/senkouka/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_machi_new.jpg", "./2010/subject/machi/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_denki_new.jpg", "./2010/subject/denki/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_kikai_new.jpg", "./2010/subject/kikai/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_it_new.jpg", "./2010/subject/it/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_info_new.jpg", "./2010/subject/jyouhou/index.html"));
//	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_media.jpg"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_onkyo_new.jpg", "./2010/subject/onkyou/index.html"));
	animeArray.push(new AnimeElement('img', "./2010/images/menu_title_eizo_new.jpg", "./2010/subject/eizou/index.html"));

	animeArray.push(new AnimeElement('flash', "http://www.hitp.ac.jp/flash/fla_index.swf"));

	for (i = 0; i < animeArray.length; i++) {
		if (animeArray[i].type == 'img' && animeArray[i].view.complete) {
			continue;
		}
	}

	if (swfobject.hasFlashPlayerVersion("9.0.18")) {
		flashFlag = true;
	} else {
		flashFlag = false;
	}
	anime();
}

function risk_startup () {
	var xotree = new XML.ObjTree();
	var objTree = xotree.parseHTTP("./2010/php/getrisk.php");

	if (objTree.risk) {
		var rank = objTree.risk.rank["-value"];
		if (rank == '0') {
			return;
		}
		var title1 = objTree.risk.title1;
		var title2 = objTree.risk.title2;
		var risk = "<p>";
		risk += "<strong>【" + title1 + "】</strong>";
		risk += title2 + ' <a href="./2010/other/risk.html">詳細情報はこちら</a></p>'

		$('div#information').html(risk);
		$('#information').css("display", "block");
	}
}

function dateStr2Time(str) {
	var dateArr = str.split('-');
	var dateObj = new Date(dateArr[0], dateArr[1], dateArr[2]);
	return dateObj.getTime();
}

function getlist () {
	var xotree = new XML.ObjTree();
	var objTree = xotree.parseHTTP("./2010/php/getlist.php");	//仮スクリプト

	var html = "";

	var nowDateObj = new Date();
	var nowDate = nowDateObj.getTime();
	if(objTree.news.nowdate) {
		nowDate = dateStr2Time(objTree.news.nowdate);
	}

	if(objTree.news.faculty) {
		if(!(objTree.news.faculty.item instanceof Array)) {
			html = makeSubjectNews(objTree.news.faculty.item, nowDate);
		} else {
			for(var i = 0; i < objTree.news.faculty.item.length; i++) {
				var newsItem = objTree.news.faculty.item[i];
				html += makeSubjectNews(newsItem, nowDate);
			}
		}
	}
	$('#newsdl').html(html);
	//
	html = "";
	if(objTree.news.article) {
		if(!(objTree.news.article.item instanceof Array)) {
			html = makeTopicsNews(objTree.news.article.item, nowDate);
		} else {
			for(var i = 0; i < objTree.news.article.item.length; i++) {
				var newsItem = objTree.news.article.item[i];
				html += makeTopicsNews(newsItem, nowDate);
			}
		}
	}
	$('#topicsdl').html(html);
}

function makeSubjectNews(item, nowDate)
{
	if(!item)
		return "";
	var topics  = "<dt>" + item.disp_date + "</dt>";
	topics += "<dd>";

	var dispDate = dateStr2Time(item.disp_date);
	var sabun = nowDate - dispDate;
	if (sabun <= 24 * 3600 * 1000 * 7) {
		topics += '<img class="newIcon" alt="[NEW]" src="./2010/images/newr.gif" height="16" width="42" />';
	} else if (sabun <= 24 * 3600 * 1000 * 14) {
		topics += '<img class="newIcon" alt="[NEW]" src="./2010/images/newg.gif" height="16" width="42" />';
	}

	topics += "<a href=\"./2010/php/news.php?faculty=" + item.faculty + "&id=" + item.id + "\">";
	topics += "<span class=\"small\">【" + item.faculty_name + "】</span>" + item.title + "</a>";
	topics += "</dd>";

	return topics;
}

function makeTopicsNews(item, nowDate)
{
	if(!item)
		return "";
	var topics  = "<dt>" + item.disp_date + "</dt>";

	topics += "<dd>";

	var dispDate = dateStr2Time(item.disp_date);
	var sabun = nowDate - dispDate;
	if (sabun <= 24 * 3600 * 1000 * 7) {
		topics += '<img class="newIcon" alt="[NEW]" src="./2010/images/newr.gif" height="16" width="42" />';
	} else if (sabun <= 24 * 3600 * 1000 * 14) {
		topics += '<img class="newIcon" alt="[NEW]" src="./2010/images/newg.gif" height="16" width="42" />';
	}

	topics += "<a href=\"./2010/news/topics.html?id=" + item.id + "\">";
	topics += item.title + "</a></dd>";

	return topics;
}

function getInfo () {
	var xotree = new XML.ObjTree();
	var objTree = xotree.parseHTTP("./mtblog/jimu/index.xml");

	var html = "";
	if(objTree.rss.channel) {
		if(!(objTree.rss.channel.item instanceof Array)) {
			html = makeInformation(objTree.rss.channel.item);
		} else {
			for(var i = 0; i < objTree.rss.channel.item.length; i++) {
				var infoItem = objTree.rss.channel.item[i];
				html += makeInformation(infoItem);
			}
		}
	}
	$('#informationdl').html(html);
}

function makeInformation(item)
{
	if(!item)
		return "";

	var info  = '<dt>' + getFormatDate(item.pubDate) + '</dt>';
	info += '<dd><a href="' + item.link + '">';
	info += item.title + '</a></dd>';

	return info;
}

function getFormatDate (dateString) {
	var dateObj = new Date(dateString);
	var year = dateObj.getYear() + 1900;
	var month = '0' + (dateObj.getMonth() + 1);
	var day = '0' + dateObj.getDate();

	var mlen = month.length;
	var dlen = day.length;
	month = month.substr(mlen - 2, 2);
	day = day.substr(dlen - 2, 2);

	return year + '-' + month + '-' + day;
}




//window.onload = init;
function onloadWait() {
	if(init) {
		init();
    return
  }
	setTimeout("onloadWait()", 200);
}

onloadWait();
risk_startup();
getlist();
getInfo();
