// JavaScript Document
var requestObj;
var propertyDOM;
var activeThumbnail;
var isActiveX;

function initialize() {
	if(window.ActiveXObject) {
		requestObj = new ActiveXObject("Msxml2.XMLHTTP");
		if(requestObj != null)
			isActiveX = true;
	} else if(window.XMLHttpRequest) {
		requestObj = new XMLHttpRequest();
	} else {
		alert('このブラウザではギャラリーを閲覧できません');
	}
	var thumbnailImages = document.getElementsByName('thumbnail_image');
	for(i=0;i<thumbnailImages.length;i++) {
		thumbnailImages[i].onmouseover = function () {if(this != activeThumbnail) this.parentNode.style.backgroundImage =  'url(images/selimg_bkgrnd.gif)';}
		thumbnailImages[i].onmouseout  = function () {if(this != activeThumbnail) this.parentNode.style.backgroundImage =  'url(images/sumimg_bkgrnd.gif)';}
		thumbnailImages[i].onclick = function () {selectImage(this);var id = this.getAttribute('id');if(id == '') id = null;if(id != null) sendRequest('properties/' + id + '.xml');}
	}
}

function response() {
	if(requestObj.readyState == 4 && requestObj.status == 200) {
		propertyDOM = requestObj.responseXML;
		var title = propertyDOM.getElementsByTagName('title');
		var source = propertyDOM.getElementsByTagName('source');
		var publicDate = propertyDOM.getElementsByTagName('public-date');
		var links = propertyDOM.getElementsByTagName('picture-link');
		var linkText = propertyDOM.getElementsByTagName('link-text');
		var linkURL = propertyDOM.getElementsByTagName('link-url');
		var comment = propertyDOM.getElementsByTagName('comment');
		var propertyDiv = document.getElementById("property");
		var propertyHTML = '<p>';
		propertyHTML = propertyHTML + '<img src=\"images/caption_title.gif\" /><br />';
		if(title[0].firstChild != null)
			propertyHTML = propertyHTML + title[0].firstChild.nodeValue;
		propertyHTML = propertyHTML + '<br /><br />';
		propertyHTML = propertyHTML + '<img src=\"images/caption_source.gif\" /><br />';
		if(source[0].firstChild != null)
			propertyHTML = propertyHTML + source[0].firstChild.nodeValue;
		propertyHTML = propertyHTML + '<br /><br />';
		propertyHTML = propertyHTML + '<img src=\"images/caption_publicdate.gif\" /><br />';
		if(publicDate[0].firstChild != null)
			propertyHTML = propertyHTML + publicDate[0].firstChild.nodeValue;
		propertyHTML = propertyHTML + '<br /><br />';
		propertyHTML = propertyHTML + '<img src=\"images/caption_picture.gif\" /><br />';
		for(i=0;i<links.length;i++) {
			if(linkURL[i].firstChild != null && linkText[i].firstChild != null) {
				propertyHTML = propertyHTML + '<a href=\"' + linkURL[i].firstChild.nodeValue + '\" target=\"_blank\" />';
				propertyHTML = propertyHTML + '<img src=\"images/pic_icon.gif\" border=\"0\" class=\"pic_icon\" />';
				propertyHTML = propertyHTML + linkText[i].firstChild.nodeValue + '</a><br />';
			}
		}
		propertyHTML = propertyHTML + '<br /><img src=\"images/caption_comment.gif\" /><br />';
		if(comment[0].firstChild != null)
			propertyHTML = propertyHTML + comment[0].firstChild.nodeValue;
		propertyDiv.innerHTML = propertyHTML + '</p>';
	}
}

function selectImage(image) {
	if(activeThumbnail != undefined) 
		activeThumbnail.parentNode.style.backgroundImage = 'url(images/sumimg_bkgrnd.gif)';
	image.parentNode.style.backgroundImage = 'url(images/actimg_bkgrnd.gif)';
	activeThumbnail = image;
}

function sendRequest(url) {
	if(isActiveX)
		requestObj = new ActiveXObject("Msxml2.XMLHTTP");
	if(requestObj) {
		requestObj.onreadystatechange = response;
		requestObj.open("GET", encodeURI(url), true);
		requestObj.send(null);
	}
}

