/*  Variables   */
var sectionName = 'main';
var browser = navigator.userAgent.toLowerCase();
var eLocation = window.location.toString();
var currentCollection, currentCollectionPos, newCollection, newCollectionPos, tempArray;
var Num;
var baseLocation = 'furniture_solutions';
var eBase = document.getElementsByTagName('BASE')[0];





/*  Constructors  */

function navItem (src, width, height, alt) {
	this.length = 5;
	this.src = src;
	this.width = width;
	this.height = height;
	this.alt = alt;
}


function collectionColor (color, uri, href, itemNumber) {
	this.length = 5;
	this.color = color;
	this.uri = uri;
	this.href = href;
	this.itemNumber = itemNumber;
}

function pageElement (id, array, isLinked) {
	this.length = 4;
	this.id = id;
	this.array = array;
	this.isLinked = isLinked;
}





/* Arrays  */

var sectionArray = new Array ('mission', 'classic', 'apothecary', 'loft', 'plan');
var collectionLinks = new Array;
var supportedPageElements = new Array;

eNavArray = new Array
eNavArray[0] = new navItem('nav_logo', 256, 83, 'Staples Furniture Solutions: works wherever you do');
eNavArray[1] = new navItem('nav_top_mission', 75, 78, 'Mission Collection');
eNavArray[2] = new navItem('nav_top_classic', 75, 78, 'Classic Collection');
eNavArray[3] = new navItem('nav_top_apothecary', 75, 78, 'Apothecary Collection');
eNavArray[4] = new navItem('nav_top_loft', 75, 78, 'Loft Collection');
eNavArray[5] = new navItem('nav_top_plan', 109, 72, 'Plan Your Room');


eSubNavArray = new Array
eSubNavArray[0] = new navItem('nav_page_vp', 132, 17, 'Room Views');
eSubNavArray[1] = new navItem('nav_page_vc', 132, 17, 'See the Collection');


eFooterNavArray = new Array
eFooterNavArray[0] = new navItem('footnav_back', 125, 20, 'Back to Staples.com');
eFooterNavArray[1] = new navItem('footnav_ship', 197, 20, 'Shipping Information');
eFooterNavArray[2] = new navItem('footnav_order', 197, 20, 'Order Now');

eBackgroundArray = new Array
eBackgroundArray[0] = new navItem('nav_bkg', 780, 103, '');
eBackgroundArray[1] = new navItem('footnav_bkg', 780, 20, '');

var supportedBrowsers = new Array ('msie', 'safari', 'firefox', 'opera');

var pageElementArray = new Array
pageElementArray[0] = new pageElement('collectionnav', eNavArray, 0)
pageElementArray[1] = new pageElement('pagenav', eSubNavArray, 0);
pageElementArray[2] = new pageElement('footnav', eFooterNavArray, 0);
pageElementArray[3] = new pageElement('headerBackground', eBackgroundArray, 1);
pageElementArray[4] = new pageElement('footerBackground', eBackgroundArray, 1);

var apothecaryColors = new Array
apothecaryColors[0] = ([new collectionColor('mahogany', 's0198251'), new collectionColor('black', 's0198250')]); 
apothecaryColors[1] = ([new collectionColor('mahogany', 's0198245'), new collectionColor('black', 's0198244')]); 
apothecaryColors[2] = ([new collectionColor('mahogany', 's0198247'), new collectionColor('black', 's0198246')]); 
apothecaryColors[3] = ([new collectionColor('mahogany', 's0197680'), new collectionColor('black', 's0198248')]); 
apothecaryColors[4] = ([new collectionColor('mahogany', 's0198243'), new collectionColor('black', 's0198242')]); 
apothecaryColors[5] = ([new collectionColor('mahogany', 's0198253'), new collectionColor('black', 's0198252')]); 
apothecaryColors[6] = ([new collectionColor('mahogany', 's0198255'), new collectionColor('black', 's0198254')]); 
apothecaryColors[7] = ([new collectionColor('mahogany', 's0198257'), new collectionColor('black', 's0198256')]); 
apothecaryColors[8] = ([new collectionColor('mahogany', 's0198261'), new collectionColor('black', 's0198260')]); 
apothecaryColors[9] = ([new collectionColor('mahogany', 's0198265'), new collectionColor('black', 's0198264')]); 
apothecaryColors[10] = ([new collectionColor('mahogany', 's0198266'), new collectionColor('black', 's0198267')]); 
apothecaryColors[11] = ([new collectionColor('mahogany', 's0197678'), new collectionColor('black', 's0198268')]); 
apothecaryColors[12] = ([new collectionColor('mahogany', 's0197681'), new collectionColor('black', 's0198270')]); 
apothecaryColors[13] = ([new collectionColor('mahogany', 's0197670'), new collectionColor('black', 's0197961')]); 
apothecaryColors[14] = ([new collectionColor('mahogany', 's0198259'), new collectionColor('black', 's0197957')]); 




/*  Runtime   */

for (var x = 0; x < supportedBrowsers.length; x++) {

	var platform = window.navigator.platform.toLowerCase();
	
	var eObj = new RegExp(supportedBrowsers[x], 'ig');
	var result = browser.search(eObj);
	
	if (result > -1) {
		
		browser = supportedBrowsers[x];
		
	} 
}


// following code sets the location of the base tag

var pattern = new RegExp(baseLocation, "ig");
var newSearch = eLocation.split(pattern);

trailingURL = newSearch[1];
trailingURL = trailingURL.split('/')[0];

newSearch = newSearch[0];

eBase.href = newSearch + baseLocation + trailingURL + '/';


// to hide the H1 and P tags in the flash Div
document.write('<link href="hiddenstyles.css" rel="stylesheet" media="all" />');





/*  Functions  */

function setSection () {

	for (var x = 0; x < sectionArray.length; x++) {
	
		pattern = new RegExp(sectionArray[x], "ig");
		result = pattern.exec(eLocation);
		
		if (result) {
		
			sectionName = sectionArray[x];
			break;
		
		}
	}
}



function initializeOptions() {
	setSection();
	setLinks();
	setImages();
	addFlashMovie();
	addProductLinks();
}


function setImages() {

	/* The x loop is used to set how many elements we want to loop through, as well as
		what elements and arrays the y loop variables should be set to
		
		*/
		
	for (var x = 0; x < pageElementArray.length; x++) {
	
		if(document.getElementById(pageElementArray[x].id)) {
	
			tempElement = document.getElementById(pageElementArray[x].id);
			tempArray = pageElementArray[x].array;
			
			if (pageElementArray[x].isLinked == 0) {
			
				// for all linkable images except for background
			
				eLinkArray = tempElement.getElementsByTagName('A');
				
			} else {
			
				eLinkArray = tempArray;
				
			}
			
			
			
			
			// The y loop writes the correct image and appends it to the right node
			
			for (var y = 0; y < eLinkArray.length; y++) {
					
				if (tempElement.id == 'footerBackground') {
				
					// if so, use the second line item in the Array
					y++;
				}
			
				eNewImg = document.createElement('IMG');
				eNewImg.width = tempArray[y].width;
				eNewImg.height = tempArray[y].height;
				eNewImg.alt = tempArray[y].alt	
				
				
				if (pageElementArray[x].id == 'collectionnav') {
				
					eNewImg.src = 'images/nav/' + tempArray[y].src + '.gif';
					
				} else  {
				
					eNewImg.src = 'images/nav/' + tempArray[y].src + '_' + sectionName + '.gif';
				}
				
	
				if (pageElementArray[x].isLinked == 0) {
					
					/* This enclosing if statement only applies only to images that have a link as a parent.
					
					The following if statement removes our #text nodes from the links and replaces them with images.
					
					*/
					
					if (eLinkArray[y].firstChild) {
					
						while (eLinkArray[y].firstChild) {
						
							eLinkArray[y].removeChild(eLinkArray[y].firstChild);
						
						}
					}
					
					if (pageElementArray[x].id == 'collectionnav') {
					
						if (sectionName != 'main') {
					
							// if the main navigation is being worked with, look to see if our sectionName matches up
								pattern = new RegExp(sectionName, 'ig');
								result = pattern.exec(eNewImg.src);
						}
					
					} else {
						
						result = null;
					}
				
				
					if (eLinkArray[y].href == eLocation || result != null) {
					
						oldSrc = eNewImg.src;
						newSrc = oldSrc.split('.gif');
						newSrc = newSrc[0] + '_selected.gif';
						
						eNewImg.src = newSrc;
					
					} else {
					
						eNewImg.addEventListener ? eNewImg.addEventListener('mouseover', changeImage, false) : eNewImg.attachEvent('onmouseover', changeImage);
						eNewImg.addEventListener ? eNewImg.addEventListener('mouseout', changeImage, false) : eNewImg.attachEvent('onmouseout', changeImage);
					
					}
					
					eLinkArray[y].appendChild(eNewImg);
					
				
				} else {
				
					tempElement.appendChild(eNewImg);
				
				}
				
				if (tempElement.id == 'headerBackground') {
				
					// if so, terminate the loop
				 
					break;
				}
			}
		}
	}
}




function setLinks() {

	eBaseHref = document.getElementsByTagName('BASE')[0].href;
	
	for (var x = 0; x < document.links.length; x++) {
		
		for (var y = 0; y < sectionArray.length; y++) {
		
			if (document.links[x].href == eBaseHref + sectionArray[y] + '.html') {
			
				
				if (sectionArray[y] == 'mission') {
					Num = 1
				} else if (sectionArray[y] == 'classic') {
					Num = 2
				} else if (sectionArray[y] == 'apothecary') {
					Num = 3
				} else if (sectionArray[y] == 'loft') {
					Num = 4
				}
			
				document.links[x].href +='?currentCollection=' + Num;
				break;
			
			}
		}
	}
}


function changeImage (event) {
	
	event.target ? eTarget = event.target : eTarget = event.srcElement;	
	
	if (event.type == 'mouseover' || event.type == 'onmouseover') {
	
		oldSrc = eTarget.src;
		newSrc = oldSrc.split('.gif');
		newSrc = newSrc[0] + '_over.gif';
		eTarget.src = newSrc;
	
	} else if (event.type == 'mouseout' || event.type == 'onmouseout') {
		
		oldSrc = eTarget.src;
		newSrc = oldSrc.split('_over');
		newSrc = newSrc[0] + '.gif';
		eTarget.src = newSrc;
	
	}
}


function addFlashMovie () {

	// This function only modifies the document if the browser has the flash plug-in installed (see the function detectFlash)

	if (document.getElementById('flashMovie')) {
	
		eFlashMovie = document.getElementById('flashMovie');
		
		while (eFlashMovie.lastChild) {
			eFlashMovie.removeChild(eFlashMovie.lastChild);
		}
			
		if (browser == 'msie') {
									
			eEmbed = document.createElement('EMBED');
			sectionName == 'main' ? eEmbed.setAttribute('src', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/solutions-home.swf') : eEmbed.setAttribute('src', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/root.swf?currentCollection=' + Num);
			eEmbed.setAttribute('quality', 'high');
			eEmbed.setAttribute('pluginspage', 'http://www.macromedia.com/go/getflashplayer');
			eEmbed.setAttribute('type','application/x-shockwave-flash');
			eEmbed.setAttribute('width', '780');
			eEmbed.setAttribute('height', '446');
			eEmbed.setAttribute('AllowScriptAccess', 'always');
			
			var eObject = eEmbed;
		
		} else {
		
			eObject = document.createElement('OBJECT');
			eObject.setAttribute('height', '446')
			eObject.setAttribute('width', '780');
			
			eObject.setAttribute('type', 'application/x-shockwave-flash');
			sectionName == 'main' ? eObject.setAttribute('data', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/solutions-home.swf') : eObject.setAttribute('data', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/root.swf?currentCollection=' + Num);
			
			eNewParam = document.createElement('PARAM');
			eNewParam.setAttribute('name', 'movie');
			sectionName == 'main' ? eNewParam.setAttribute('value', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/solutions-home.swf') : eNewParam.setAttribute('value', 'http://media.staples.com/microsites/furniture_solutions_media/images/flash/root.swf');
			eObject.appendChild(eNewParam);
			
			eNewParam = document.createElement('PARAM');
			eNewParam.setAttribute('name', 'AllowScriptAccess');
			eNewParam.setAttribute('value', 'always');
			eObject.appendChild(eNewParam);
			
		}
		
		eFlashMovie.appendChild(eObject);
	}
}


function addProductLinks () {

	if (document.getElementById('availableColors')) {
	
		tempArray = window[sectionName + 'Colors'];
		
		var eP = document.getElementById('availableColors');
		var ePImgList = eP.getElementsByTagName('IMG');
		
		for (var x = 0; x < ePImgList.length; x++) {
		
			//ePImgList[x].addEventListener ? ePImgList[x].addEventListener('mouseover', setColor, false) : ePImgList.attachEvent('onmouseover', setColor);
			ePImgList[x].addEventListener ? ePImgList[x].addEventListener('click', setImageAndLinks, false) : ePImgList[x].attachEvent('onclick', setImageAndLinks);
		
		}
		
		addToCollectionArray();
		setImageAndLinks();
		
	} else if (document.getElementsByTagName('TABLE').length > 0) {
	
		var eTdImg = document.getElementsByTagName('TABLE')[0].getElementsByTagName('IMG');
		
		for (var x = 0; x < eTdImg.length; x++) {
		
			var eTdParent = eTdImg[x].parentNode;
			var eTdSibling = eTdParent.nextSibling;
			
			while (eTdSibling.nodeName != 'TD') {
				eTdSibling = eTdSibling.nextSibling;
			}
			
			var eDocFrag = document.createDocumentFragment();
			var eNewA = document.createElement('A');
			eNewA.setAttribute('href', eTdSibling.getElementsByTagName('A')[0].href);
			
	
			eDocFrag.appendChild(eTdImg[x]);
			eNewA.appendChild(eDocFrag.firstChild);
			eTdParent.appendChild(eNewA);
		
		}
	}
}

function setImageAndLinks (event) {

	// This function is overbuilt for the current functionality. It anticipates unlimited colors for unlimited collections

	if (event) {

		event.target ? newCollection = event.target.alt.toLowerCase() : newCollection = event.srcElement.alt.toLowerCase();
		
		for (var x = 0; x < tempArray[0].length; x++) {
			if (tempArray[0][x].color == newCollection) {
				newCollectionPos = x;
				break;
			}
		}
	} else {
	
		currentCollection = tempArray[0][0].color;
		newCollection = currentCollection;
		currentCollectionPos = 0;

	}
	
		var eTdImg = document.getElementsByTagName('TABLE')[0].getElementsByTagName('IMG');


	
	for (var x = 0; x < eTdImg.length; x++) {
 
		for (var y = 0; y < tempArray.length; y++) {
	
			// first find what image we have
			
			
			var pattern = new RegExp(tempArray[y][currentCollectionPos].uri, 'i');
			var result = pattern.exec(eTdImg[x].src);
			
			// then change it
			
			if (result) {
			
			
			   // change the image
		   
			   if (newCollection != currentCollection) {
		   
					eTdImg[x].src = 'images/featureart/' + sectionName + '/' + tempArray[y][newCollectionPos].uri + '_std.jpg';
					
				}
			   
			   var eTdImgParent = eTdImg[x].parentNode;
				
				
				
			   // if running for the first time, add an A node and append the IMG to it

			   if (eTdImgParent.nodeName != 'A') {
			   
				   var eDocFrag = document.createDocumentFragment();
	   
				   var eNewA = document.createElement('A');
				   eNewA.setAttribute('href', tempArray[y][currentCollectionPos].href);
				   
				   eDocFrag.appendChild(eTdImg[x]);
				   eNewA.appendChild(eDocFrag.firstChild);
				   eTdImgParent.appendChild(eNewA);
			   
			   } else {
			   
				   // if running a second time change only the href attribute
			   
					eTdImgParent.href = tempArray[y][newCollectionPos].href;
			   }
			   
			   
			   
			   // now look at the associated links in the next TD
					
				if (eTdImgParent.nodeName != 'TD') {
				
				   // set the parent to the correct node if its not.
				
				   eTdImgParent = eTdImgParent.parentNode;
				}
				
				eTdSibling = eTdImgParent.nextSibling;
				
				while (eTdSibling.nodeName != 'TD') {
				
					eTdSibling = eTdSibling.nextSibling;
				}
				
				
				
				// examine the A tags in the sibling TD
				
				var eTargetP = eTdSibling.getElementsByTagName('A')[0].parentNode;
				 
				var eA = eTargetP.getElementsByTagName('A')[0];
				newCollection == currentCollection ? vNum = currentCollectionPos : vNum = newCollectionPos;
				
				eA.href = tempArray[y][vNum].href;
				eA.firstChild.nodeValue = tempArray[y][vNum].itemNumber;
				
				var eStrong = eTargetP.getElementsByTagName('STRONG')[0];
				var collectionString = newCollection.toString();
				var eStrongText = collectionString.substring(0,1).toUpperCase();
				collectionString = collectionString.substring(1, collectionString.length);
				eStrongText = eStrongText + collectionString + ':';
				
				eStrong.firstChild.nodeValue = eStrongText;
				
				
				while (eTdSibling.lastChild != eTargetP) {
				
					eTdSibling.removeChild(eTdSibling.lastChild);
				}
			}
		} 
	}

  
	if (newCollection != currentCollection) {
	
		currentCollection = newCollection;
		currentCollectionPos = newCollectionPos;
	}
}



function addToCollectionArray() {

	var eTdImg = document.getElementsByTagName('TABLE')[0].getElementsByTagName('IMG');


	// The first time the page loads, we add the existing links on the page to the right slot in the array. Then, we remove the redundant link.
	
	for (var x = 0; x < eTdImg.length; x++) {

		for (var y = 0; y < tempArray.length; y++) {
		
			for (var z = 0; z < tempArray[y].length; z++) {
	
				// first find what image we have
				
				var pattern = new RegExp(tempArray[y][z].uri, 'i');
				var result = pattern.exec(eTdImg[x].src);
				
				// then add the link to the array
				
				if (result) {
				
					var nextSib = eTdImg[x].parentNode.nextSibling;
					
					// incase the browser reads an empty text node as the next sibling
					
					while (nextSib.nodeName != 'TD') {
						nextSib = nextSib.nextSibling;
					}
					
					var eRelatedLinks = nextSib.getElementsByTagName('A');
					
					
					for (var a = 0; a < eRelatedLinks.length; a++) {
					
						var eLinkP = eRelatedLinks[a].parentNode;
						var eColName = eLinkP.firstChild.firstChild.nodeValue.toLowerCase().split(':')[0];
						
						for (var b = 0; b < tempArray[y].length; b++) {
						
							if (eColName == tempArray[y][b].color) {
								tempArray[y][b].href = eRelatedLinks[a].href;
								tempArray[y][b].itemNumber = eRelatedLinks[a].firstChild.nodeValue;
							}
						}
					}
				} 
			}
		}
	}
}
