var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=400,height=300';
var PopupParms = 'resizable=1,scrollbars=1,height=636,width=600,left=50,screenx=50,top=0,screeny=0';

var galPicWidth = (document.all)?106:110;
var imageNames = "/images/cabin6-dining.jpg,/images/cabin6-sunset.jpg,/images/cabin1-porch_t.jpg,/images/cabin1-kitchen.jpg,/images/cabin2-front.jpg,/images/cabin2-living.jpg,/images/cabin2-kitchen.jpg,/images/cabin2-bedroom.jpg,/images/cabin3-kitchen.jpg,/images/cabin3-kitchen2.jpg,/images/cabin4.jpg,/images/cabin5.jpg,/images/cabin4-kitchen.jpg,/images/cabin5-kitchen.jpg";

function raw_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  }
  if (isUndefined(target)) {
    target = '_blank';
  }
  var theWindow =
    window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, features) {
  return raw_popup(src.getAttribute('src'),
    src.getAttribute('target') || '_blank',
    features);
}

function event_popup_features(features) {
  return function(e) { 
    link_popup(e.currentTarget, features); 
    e.preventDefault(); 
  }
}

function fetchImages() {
	var imageArray = imageNames.split(",");
	for (var i= 0; i<imageArray.length;i++) {
		eval("img"+i+" = new Image();");
		eval("img"+i+".src = '" + imageArray[i] + "';");
	}
}											
	
function genGallery(pics,galID) {
	var newGalleryWidth = pics.length * galPicWidth;
	// set new gallery width
	document.getElementById("gallery-"+galID).style.width = newGalleryWidth + "px";
}

function swapImg(e) {
	var thumbImg = e.currentTarget;
	var cabinimg = document.getElementById("cabin-img_" + thumbImg.parentNode.id.split("-")[1]);
	var dotSplitLength = thumbImg.src.split(".").length;
	var cabinimgname = (thumbImg.src.split(".").concat(0,1).join(".")).split("_")[0];
	var cabinimgtype = thumbImg.src.split(".")[dotSplitLength-1];
	cabinimg.src = cabinimgname+"."+cabinimgtype;
	cabinimg.alt = thumbImg.alt;
//	cabinimg.width = thisImg.width;
//	cabinimg.height = thisImg.height;
}
function pageLoad() {
	var swapimgs;
	// add swapImg to all thumbnails in each cabin, if any
	for (var i=1;i<6;i++) {
		swapimgs = getAll("img",document.getElementById("gallery-"+i));
		if (swapimgs.length>0) {
			addEvent(swapimgs,"mouseover",swapImg);
//			addEvent(swapimgs,"mouseout",resetImg(swapimgs[0]);
			genGallery(swapimgs,i);
		}
		else {
			document.getElementById("gallery-"+i).style.width = 0;
			document.getElementById("gallery-"+i).style.height = 0;
			document.getElementById("gallery-"+i).parentNode.style.width = 0;
			document.getElementById("gallery-"+i).parentNode.style.height = 0;
			if (!document.all) {document.getElementById("gallery-"+i).parentNode.style.display = "none";}
		}
	}
	if(getElementsByClassName("availPopupButton").length >0) {
			addEvent(getElementsByClassName("availPopupButton"),"click",event_popup_features(PopupParms));
	}
	fetchImages();
}
addLoadEvent(pageLoad);
	