var path = location.href.substring(0,location.href.lastIndexOf("/")+1);
var imagePath = path + "images/buttons/";
//alert("starting, imagePath = " + imagePath);

function initButtons() {
	setCurrentPageButton();
}

function handleMouseOver(imageName,graphicName) {

	if (getCurrentPageName() == getMatchingPageName(imageName)) {
		return;
	}

	rolloverImage = imagePath + graphicName + '-02' + '.jpg';
	//alert("handleMouseOver, imageName = " + imageName + ", rolloverImage = " + rolloverImage);
	changeImageSrc(imageName,rolloverImage);
}

function handleMouseOut(imageName,graphicName) {
	outImage = imagePath + graphicName + getDefaultButtonPrefix(imageName) + '.jpg';
	//alert("outImage = " + outImage);
	changeImageSrc(imageName,outImage );
}


function changeImageSrc(imageName,newGraphic) {
	if (document.images) {
		document.images[imageName].src = newGraphic;
	}
}

function setCurrentPageButton() {
	//alert("setting current page button...");

	var homeImageSrc;

	docImages=document.getElementsByTagName("img"); 
	for(i=0; i-docImages.length; i++){
		currentImageName = docImages[i].name;
		currentImageSrc = docImages[i].src;
		if (currentImageName == 'home_btn') {
			homeImageSrc = currentImageSrc ;
		}
		if (currentImageName.indexOf('btn') >= 0 &&
			getCurrentPageName() == getMatchingPageName(currentImageName)) {
			//change to -03 from -01:
			newImageSrc = currentImageSrc .replace("-01", "-03");
			changeImageSrc(currentImageName,newImageSrc);
			return; // no need to continue
		}
	}
	// no match was found - assume this is the index page:
	newImageSrc = homeImageSrc .replace("-01", "-03");
	changeImageSrc("home_btn",newImageSrc);
}

function getDefaultButton(btnName) {
	alert("getDefaultButton");
	return imagePath + btnName + getDefaultButtonPrefix(btnName+ '_btn') + '.jpg'; 
}

function getDefaultButtonPrefix(imageName) {
	if (getCurrentPageName() == getMatchingPageName(imageName)) {
		return '-03';
	}
	else {
		return '-01';
	}
}

// get the button image name and retirn the html page it is associated with:
function getMatchingPageName(imageName) {
	
	//alert("getMatchingPageName, imageName = " + imageName);

	switch(imageName)
	{
	case 'home_btn':
		return 'index';
	case 'doors_btn':
		return 'doors';
	case 'windows_btn':
		return 'windows';
	case 'stairs_btn':
		return 'stairs';
	case 'hardware_btn':
		return 'hardware';
	case 'millwork_btn':
		return 'millwork';		
	case 'contact_btn':
		return 'contact';
	case 'testimonials_btn':
		return 'testimonials';
	case 'about_btn':
		return 'about';

	default:
		alert("wrong imageName: " + imageName);
		return 'index';
	}
}


// return name of current page (without the 'htm' or 'html' extension):
function getCurrentPageName() {
	currentPath = window.location.pathname;
	currentPage = currentPath .substring(currentPath.lastIndexOf('/') + 1,currentPath.indexOf('htm') - 1);
	return currentPage;
}
