// image rollover script
// build an array of images
var images = new Array(30);
images[0]  = "/images/buttons/home_off.jpg";
images[1]  = "/images/buttons/home_over.gif";
images[2]  = "/images/buttons/products_off.jpg";
images[3]  = "/images/buttons/products_over.gif";
images[4]  = "/images/buttons/about_us_off.jpg";
images[5]  = "/images/buttons/about_us_over.gif";
images[6]  = "/images/buttons/where_to_buy_off.jpg";
images[7]  = "/images/buttons/where_to_buy_over.gif";
images[8] = "/images/buttons/product_support_off.jpg";
images[9] = "/images/buttons/product_support_over.gif";
images[10]  = "/images/buttons/fan_museum_off.jpg";
images[11]  = "/images/buttons/fan_museum_over.gif";
images[12] = "/images/buttons/press_room_off.png";
images[13] = "/images/buttons/press_room_over.gif";

images[14] = "/images/buttons/previous_up.jpg";
images[15] = "/images/buttons/previous_over.jpg";
images[16] = "/images/buttons/next_up.jpg";
images[17] = "/images/buttons/next_over.jpg";

images[18] = "/images/icons/blades_off.gif";
images[19] = "/images/icons/blades_over.gif";
images[20] = "/images/icons/housings_off.gif";
images[21] = "/images/icons/housings_over.gif";
images[22] = "/images/icons/lighting_off.gif";
images[23] = "/images/icons/lighting_over.gif";
images[24] = "/images/icons/controls_off.gif";
images[25] = "/images/icons/controls_over.gif";
images[26] = "/images/icons/downrods_off.gif";
images[27] = "/images/icons/downrods_over.gif";
images[28] = "/images/icons/accessories_off.gif";
images[29] = "/images/icons/accessories_over.gif";

// preload the images
function preload() {
	var tmp = null;
	for(var j = 0; j < images.length; j++) {
		tmp = images[j];
		images[j] = new Image();
		images[j].src = tmp;
	}
}

void(preload());

// swap images
function imageSwap(image, swap) {
	if(images[swap]) {
		image.src = images[swap].src;
	}
}

function getObject(objectId) {

	// cross-browser function to get an object given the id;
	// prototype.js does this, but this is more lightweight
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	} else if(document.all && document.all(objectId)) {
		return document.all(objectId);
	} else { // we're not going to support other browsers
		return false;
	}
}

function switchPanel(tabToHighlightId, panelToShowId) {

	// store objects in local variables
	var tabToHighlight = getObject(tabToHighlightId);
	var panelToShow    = getObject(panelToShowId);

	var cT = getObject('cTab');
	var sT = getObject('sTab');
	var oT = getObject('oTab');

	var cP = getObject('cPanel');
	var sP = getObject('sPanel');
	var oP = getObject('oPanel');

	// deactivate tabs and hide all panels
	if(cT) cT.className = "";
	if(sT) sT.className = "";
	if(oT) oT.className = "";

	if(cP) cP.style.display = "none";
	if(sP) sP.style.display = "none";
	if(oP) oP.style.display = "none";

	// highlight active tab and show selected panel
	if(tabToHighlight)
		tabToHighlight.className = "active";
	if(panelToShow)
		panelToShow.style.display = "block";
}

function loadOptions(familyId, optionId) {
	switchPanel('oTab', 'oPanel');

	var partList   = getObject("part_list_iframe");
	var partDetail = getObject("part_detail_iframe");

	if(partList && partDetail) {
		partList.src = "/products/iframes/part_list.html?family_id=" + familyId + "&option_id=" + optionId;
		partDetail.src = "/products/iframes/part_detail.html?family_id=" + familyId + "&option_id=" + optionId;
	}
}

function newPopupWindow(url, w, h) {	
	popupWindow = window.open(url, "popup", "resizable=yes,toolbar=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,width=" + w + ",height=" + h);
	popupWindow.focus();
}

function swapOptionName(str) {
	var myTextNode = getObject('option_name');

	if(myTextNode) {
		if(str.length == 0)
			myTextNode.innerHTML = "options";
		else
			myTextNode.innerHTML = str;
	}
}

function getDocHeight(doc) {
	var docHt = 0, sh, oh;

	if(doc.height) docHt = doc.height;
	else if(doc.body) {
		if(doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
		if(doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
		if(sh && oh) docHt = Math.max(sh, oh);
	}

	return docHt;
}

function setIframeHeight(iframeName) {
	var iframeWin = window.frames[iframeName];
	var iframeEl = document.getElementById ? document.getElementById(iframeName) : document.all ? document.all[iframeName] : null;

	if(iframeEl && iframeWin) {
		var docHt = getDocHeight(iframeWin.document);

		iframeEl.style.height = "auto";
		iframeEl.style.height = (docHt > 244) ? docHt + "px" : "244px";
	}
}
