/*
 * base.js
 * k. walters
 * ken@lostghost.com
 */

/*
 * linkHandler
 * k. walters
 * ken@lostghost.com
 */	
var linkHandler = new function() {
	this.rel={};
	this.attach = function () {
	  if (!document.getElementsByTagName) return false;
	  var linkElems = document.getElementsByTagName("a");
		for (var i=0; i < linkElems.length; i++) {
			if (linkElems[i].rel&&this.rel[linkElems[i].rel]) linkElems[i].onclick=this.rel[linkElems[i].rel];
		}
  }
}

/* window.onload = init; -- lets use the cool prototype abstraction instead */
Event.observe(window, 'load', jirecoInit, false);

function jirecoInit() {
	linkHandler.attach();
	processPhotoThumbs();
}

linkHandler.rel.external=function(){window.open(this.href);return false;}
linkHandler.rel.pdf=function(){window.open(this.href);return false;}
linkHandler.rel.virtualtour=function(){window.open(this.href);return false;}
linkHandler.rel.photospopup=function(){window.open(this.href, "JohnsIslandRealEstatePhotos", "menubar=no,status=no,scrollbars=no,resizable=no,width=680,height=500");return false;}
linkHandler.rel.map=function(){window.open(this.href, "JohnsIslandRealEstateMap", "menubar=no,status=no,scrollbars=no,width=680,height=500");return false;}
linkHandler.rel.emailpopup=function(){window.open(this.href, "JohnsIslandRealEstateEmail", "menubar=no,status=no,resizable=no,width=425,height=400");return false;}
linkHandler.rel.back=function(){if(document.referrer.match("/listings")){history.back();return false;}else{return true}}

var current_imagelink = 0;
var imagelink_elems = new Array();

function processPhotoThumbs() {
	if (!document.getElementById || !document.getElementsByTagName) return false;
	if (!document.getElementById("thumbnails")) return false;
	var imagelink_nodelist = document.getElementById("thumbnails").getElementsByTagName("a");
	//convert nodelist to array
	for (i=0; i<imagelink_nodelist.length; i++) {
		imagelink_elems.push(imagelink_nodelist[i]);
	}
	attachImageLinkBehaviors();
	preloadImages();
}

function attachImageLinkBehaviors() {
	for (i=0; i<imagelink_elems.length; i++) {
		eval("imagelink_elems[i].onclick = function () { switchFeatureImage("+i+"); this.blur(); return false; }")
	}
}

function switchFeatureImage(elem_key) {
	document.getElementById("featuredphoto").src = imagelink_elems[elem_key].href;
	if (document.getElementById("featuredcaption")) {
		document.getElementById("featuredcaption").lastChild.nodeValue = imagelink_elems[elem_key].title+" ";
	}
	imagelink_elems[current_imagelink].className = "";
	imagelink_elems[elem_key].className = "active";
	current_imagelink = elem_key;
}

function preloadImages() {
	var imgs = new Array();
	for (i=0; i<imagelink_elems.length; i++) {
		imgs[i] = new Image();
		imgs[i].src = imagelink_elems[i].href;
	}
}

function switchNextFeatureImage() {
	var next_key = current_imagelink + 1;
	if (next_key >= imagelink_elems.length) {
		next_key = 0;
	}
	switchFeatureImage(next_key);
}

function switchPreviousFeatureImage() {
	var previous_key = current_imagelink - 1;
	if (previous_key < 0) {
		previous_key = imagelink_elems.length - 1;
	}
	switchFeatureImage(previous_key);
}

// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header

function printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}
