var timeout;
var changeTimeout;
var rotateTimeout;
var defaultNav = '';
var defaultSubNav = '';
var DEBUG = true;
var count = 0;
var IMAGES = new Array(
	'img/rot_pic1.jpg',
	'img/rot_pic2.jpg',
	'img/rot_pic3.jpg',
	'img/rot_pic4.jpg',
	'img/rot_pic5.jpg',
	'img/rot_pic6.jpg'
);

/**
 * Creates a new image object in cache for the preloadImages() function below.
 */
function newImage (arg) {
	if (document.images) {
		var rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
	
/**
 * Swaps the image named in the first argument with the one in the second.
 */
function changeImages () {
	if (document.images && (preloadFlag == true)) {
		for (var i = 0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
		}
	}
}

/**
 * Swaps the image named in the first argument with the one in the second after a timeout.
 */
function delayChangeImages (name, file) {
	changeTimeout = setTimeout('changeImages("' + name + '", "' + file + '")', 3000);
}
	
/**
 * Preloads the horizontal navigation images onLoad of the page so that there is
 * no initial delay when the user rolls over them.
 */
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		var about_off = newImage('/img/main/about_off.gif');
		var about_on = newImage('/img/main/about_on.gif');
		var care_off = newImage('/img/main/care_off.gif');
		var care_on = newImage('/img/main/care_on.gif');
		var commercial_off = newImage('/img/main/commercial_off.gif');
		var commercial_on = newImage('/img/main/commercial_on.gif');
		var contact_off = newImage('/img/main/contact_off.gif');
		var contact_on = newImage('/img/main/contact_on.gif');
		var floorprod_off = newImage('/img/main/floorprod_off.gif');
		var floorprod_on = newImage('/img/main/floorprod_on.gif');
		var inter_off = newImage('/img/main/inter_off.gif');
		var inter_on = newImage('/img/main/inter_on.gif');
		var locate_off = newImage('/img/main/locate_off.gif');
		var locate_on = newImage('/img/main/locate_on.gif');
		var residential_off = newImage('/img/main/residential_off.gif');
		var residential_on = newImage('/img/main/residential_on.gif');
		
		var subnav_commercial_on = newImage('/img/sub/commercial_on.gif');
		var subnav_commercial_off = newImage('/img/sub/commercial_off.gif');
		var subnav_compare_on = newImage('/img/sub/compare_on.gif');
		var subnav_compare_off = newImage('/img/sub/compare_off.gif');
		var subnav_duraceramic_on = newImage('/img/sub/duraceramic_on.gif');
		var subnav_duraceramic_off = newImage('/img/sub/duraceramic_off.gif');
		var subnav_durastone_on = newImage('/img/sub/durastone_on.gif');
		var subnav_durastone_off = newImage('/img/sub/durastone_off.gif');
		var subnav_faq_on = newImage('/img/sub/faq_on.gif');
		var subnav_faq_off = newImage('/img/sub/faq_off.gif');
		var subnav_floorcare_on = newImage('/img/sub/floorcare_on.gif');
		var subnav_floorcare_off = newImage('/img/sub/floorcare_off.gif');
		var subnav_flooringfeat_on = newImage('/img/sub/flooringfeat_on.gif');
		var subnav_flooringfeat_off = newImage('/img/sub/flooringfeat_off.gif');
		var subnav_history_on = newImage('/img/sub/history_on.gif');
		var subnav_history_off = newImage('/img/sub/history_off.gif');
		var subnav_installation_on = newImage('/img/sub/installation_on.gif');
		var subnav_installation_off = newImage('/img/sub/installation_off.gif');
		var subnav_investor_on = newImage('/img/sub/investor_on.gif');
		var subnav_investor_off = newImage('/img/sub/investor_off.gif');
		var subnav_myswatchbook_on = newImage('/img/sub/myswatchbook_on.gif');
		var subnav_myswatchbook_off = newImage('/img/sub/myswatchbook_off.gif');
		var subnav_privacy_on = newImage('/img/sub/privacy_on.gif');
		var subnav_privacy_off = newImage('/img/sub/privacy_off.gif');
		var subnav_prod_recomm_on = newImage('/img/sub/prod_recomm_on.gif');
		var subnav_prod_recomm_off = newImage('/img/sub/prod_recomm_off.gif');
		var subnav_residential_on = newImage('/img/sub/residential_on.gif');
		var subnav_residential_off = newImage('/img/sub/residential_off.gif');
		var subnav_roomvision_on = newImage('/img/sub/roomvision_on.gif');
		var subnav_roomvision_off = newImage('/img/sub/roomvision_off.gif');
		var subnav_sheet_on = newImage('/img/sub/sheet_on.gif');
		var subnav_sheet_off = newImage('/img/sub/sheet_off.gif');
		var subnav_tile_on = newImage('/img/sub/tile_on.gif');
		var subnav_tile_off = newImage('/img/sub/tile_off.gif');
		var subnav_warranties_on = newImage('/img/sub/warranties_on.gif');
		var subnav_warranties_off = newImage('/img/sub/warranties_off.gif');
		var subnav_whatsnew_on = newImage('/img/sub/whatsnew_on.gif');
		var subnav_whatsnew_off = newImage('/img/sub/whatsnew_off.gif');
		
		preloadFlag = true;
	}
}

/**
 * Jumps to the selected item in the dropdown list.
 */
function jumpURL (dropdown) {
	location.href = dropdown.options[dropdown.selectedIndex].value;
}

/**
 * Jumps to the selected item in the dropdown list.
 */
function jumpURLpage (prefix, dropdown) {
	try {
		location.href = prefix + dropdown.options[dropdown.selectedIndex].value;
	} catch (e) {
		alert('Please select a content item.');
	}
}

/**
 * Prompts the user as to whether he really wants to close RoomVision.
 */
function confirmClose () {
	if (!confirm('Are you sure you want to close RoomVision?')) {
		return;
	}
	self.close();
}

/**
 * Open the printer page for the Swatches.
 */
function openSwatchbookPrint () {
	var openSwatchbookWin;
	if (!openSwatchbookWin || openSwatchbookWin.closed) {
		openSwatchbookWin = open(
			'/my-swatchbook-print.php',
			'sb',
			"width=500,height=400,resizable=no,toolbar=yes,status=no,menubar=yes,scrollbars=yes"
		);
		openSwatchbookWin.focus();
	} else if (openSwatchbookWin || !openSwatchbookWin.closed) {
		openSwatchbookWin.focus();
	}
}

/**
 * Show the word "save" with the given id.
 */
function show (id) {
	try {
		document.getElementById(id).style.color = '#366495';
	} catch (ex) {
	}
}

/**
 * Hide the word "save" with the given id.
 */
function hide (id) {
	try {
		document.getElementById(id).style.color = '#fff';
	} catch (ex) {
	}
}


/**
 * Show the given node.
 */
function showNavById (node, parentId, parentImg) {
	resetNavs();
	clearTimeout(timeout);
	clearTimeout(changeTimeout);
	try {
		document.getElementById(node).style.display = 'block';
		changeImages(parentId, parentImg);
	} catch (ex) {
		if (DEBUG) alert('Error in showNavById(): ' + ex);
	}
}

/**
 * Hide the given node.
 */
function hideNavById (node, immediate, parentId, parentImg) {
	if (!immediate) {
		timeout = setTimeout('hideNode("' + node + '", "' + parentId + '", "' + parentImg + '")', 3000);
	} else {
		hideNode(node);
	}
}

/**
 * Hide the given node and its parent.
 */
function hideNode (node, parentId, parentImg) {
	try {
		if (parentId != null && parentImg != null) {
			changeImages(parentId, parentImg);
		}
		document.getElementById(node).style.display = 'none';
		clearTimeout(timeout);
		clearTimeout(changeTimeout);
		resetNavs();
	} catch (ex) {
		if (DEBUG) alert('Error in hideNode(): ' + ex);
	}
	if (defaultSubNav != '' && defaultNav != '') {
		document.images[defaultNav].src = document.images[defaultNav].src.replace('off.gif', 'on.gif');
		document.getElementById(defaultSubNav).style.display = 'block';
	}
}

/**
 * Find all DIVs with an id attribute containing "subnav" and hide them.
 * Find all IMGs with a name attribute and whose src contains on and turn them off.
 */
function resetNavs () {
	for (var i = 0; i < document.getElementsByTagName('div').length; i++) {
		var divEl = document.getElementsByTagName('div').item(i);
		if (divEl.getAttribute('id') != null && divEl.getAttribute('id').search('subnav') != -1) {
			divEl.style.display = 'none';
		}
	}
	for (var i = 0; i < document.getElementsByTagName('img').length; i++) {
		var imgEl = document.getElementsByTagName('img').item(i);
		if (imgEl.getAttribute('name') != null && imgEl.getAttribute('name') != '' && imgEl.src.search('on.gif') != -1) {
			imgEl.src = imgEl.src.replace('on.gif', 'off.gif');
		}
	}
}

/** 
 * Rotating ad banner
 * Picks a single rotating image string from the available values.
 */
function getRandomImage () {
	return IMAGES[parseInt(returnRandom(IMAGES.length))];
}

/** 
 * Cycle through all the images on a 10 second delay.
 */
function rotateImages () {
	if (count < IMAGES.length) {
		document.getElementById('rotator').src = IMAGES[count];
	} else {
		count = 0;
		document.getElementById('rotator').src = IMAGES[0];
	}
	count++;
	rotateTimeout = setTimeout('rotateImages()', 10000);
}

/**
 * Returns a random number between 0 and i inclusive.
 */
function returnRandom (i) {
	return (Math.random() * i);
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
    document.cookie = name + "=";
    document.cookie = document.cookie + ((path) ? "; path=" + path : "");
    document.cookie = document.cookie + ((domain) ? "; domain=" + domain : "");
    document.cookie = document.cookie + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
function login_as_another() {
		//deleteCookie('congoluid','/','.cogoleum.com');
		document.nav3.action = "login_as_another.php";
		document.nav3.submit();
}
function popitup(url)
{
	newwindow=window.open(url,'name','height=300,width=450,toolbar=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}
