/*
-----------------------------------------------
getahome.org
Script: listing_details.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: May 1 2007
----------------------------------------------- */

listingDetails = {
	photoContainer: null,
	photoUL: null,
	photoLIs: null,
	current:0,
	currentLink: null,
	separator: ' | ',
	label: 'Choose a photograph: ',
	init:function() {
		if (!document.getElementById) { return; }
		listingDetails.buildResultsBacklink();
		if (!document.getElementById('property_photographs')) { return; }
		listingDetails.photoContainer = document.getElementById('property_photographs');
		listingDetails.photoUL = listingDetails.photoContainer.getElementsByTagName('ul')[0];
		listingDetails.photoLIs = listingDetails.photoContainer.getElementsByTagName('li');
		listingDetails.prepareListingPhotos();
		listingDetails.buildNavigation();
	},
	prepareListingPhotos:function() {
		vdwDOM.addClass(listingDetails.photoUL,'hasJS');
		vdwDOM.addClass(listingDetails.photoLIs[listingDetails.current],'current');
	},
	changePhoto:function(e) {
		var target = vdwUtil.getTarget(e);
		vdwDOM.removeClass(listingDetails.photoLIs[listingDetails.current],'current');
		listingDetails.current = Number(target.firstChild.nodeValue) - 1;
		vdwDOM.addClass(listingDetails.photoLIs[listingDetails.current],'current');
		vdwDOM.removeClass(listingDetails.currentLink, 'current');
		listingDetails.currentLink = target;
		vdwDOM.addClass(target, 'current');
	},
	buildNavigation:function() {
		if (listingDetails.photoLIs.length <= 1) { return; }
		var navContainer = document.createElement('p');
		navContainer.appendChild(document.createTextNode(listingDetails.label));
		for (var i = 0; i < listingDetails.photoLIs.length; i++) {
			var temp = document.createElement('span');
			var tempText = document.createTextNode(i + 1);
			temp.appendChild(tempText);
			vdwUtil.addEvent(temp, 'click', listingDetails.changePhoto, false);
			navContainer.appendChild(temp);
			if (i + 1 != listingDetails.photoLIs.length) {
				navContainer.appendChild(document.createTextNode(listingDetails.separator));
			}
		}
//		listingDetails.photoContainer.appendChild(navContainer);
		var lastChild = listingDetails.photoContainer.lastChild;
		while (lastChild.nodeType != 1) {
			lastChild = lastChild.previousSibling;
		}
		if (lastChild.nodeName.toUpperCase() != 'P') {
			listingDetails.photoContainer.appendChild(navContainer);
		} else {
			listingDetails.photoContainer.insertBefore(navContainer, lastChild);
		}
		listingDetails.currentLink = navContainer.getElementsByTagName('span')[0];
		vdwDOM.addClass(listingDetails.currentLink, 'current');
	},
	buildResultsBacklink:function() {
		if (!document.getElementById('rental-details')) { return; }
		var rbP = document.createElement('p');
		rbP.className = 'return_link';
		var rbA = document.createElement('a');
		rbA.setAttribute('href','');
		rbA.onclick = function() {
			history.back();
			return false;
		}
		var rbText = document.createTextNode('Return to Search Results');
		rbA.appendChild(rbText);
		rbP.appendChild(rbA);
		var description = document.getElementById('description');
		description.appendChild(rbP);
	}
}

vdwUtil.addEvent(window, 'load', listingDetails.init, false);