/* JavaScript for displaying pages

Version 2.0 - July 28, 2005 - added height element to "divHouseImage", cleaned up
================================================================================= */

/* --- Initialize variables --- */
var w = "";										// initial image number to load
var ii = ""; 									// initial image for right page
var arrQS = new Array();			// array that stores the query string values
/* these vars used to be more self-describing but to have it useable i must stick to generics... */
var str1 	= "";								// collection
var str2 	= "";								// house name
var str3 	= "";								// number that identifies the initial image to display	| width
var str4 	= "";								// folder name														| depth
var str5 	= "";								// title																	| total area
var str6	= "";								// main
var str7	= "";								// upper
/* --- spare vars for future use --- */
var str8	= "";								// spare
var str9	= "";								// spare
var str10	= "";								// spare


/* --- Main functions --- */
/* --- main function: loads the _floor plan_ page --- */
function loadPlans()
{
	getQS();												// retreive the query string
	loadColl( str1, 								// loads collection e.g. titles, links, images, house sizes
						str2, 
						str3, 
						str4, 
						str5, 
						str6, 
						str7 );		
	//loadMenu();											// loads the menu
}

/* main function: loads the _left frame_ of the _presentation_ page */
function loadLeft()
{	
	getQS();													// load collection link
	evaluateHN( str2,'left' );				// check if house name needs to be modified
	loadLinks( str1, str2, str4 );		// modify house name if need be
	//alert( "str1="+str1 +" "+ "str2="+str2 +" "+ "str4="+str4 )
	loadThumbs( str1, str4, str3 );		// load thumbnails
	loadMenu();
}

/* main function: loads the _right frame_ of the _presentation_ page */
function loadRight()
{	
	getQS();
	evaluateHN( str2, 'right' );
	loadTitle(str5);
	loadInitialImage( str1, str4, str3 );
	loadMenu();
}

/* --- end Main functions --- */

/* --- Helper functions --- */
/* helper function: retrieves the query string from the links in the _collection_ page */
function getQS()
{
	// todo: retrieve qs and save in the array
	var str = "";	

	if ( parent.location.search == "" )
	{
		str = location.search.substring( 1, location.search.length );	
	}
	else 
	{
		str = parent.location.search.substring( 1, parent.location.search.length ); 
	}
	var qryArr = str.split( '&' );
	for( var i = 0; i < qryArr.length; i++ )
		arrQS[i] = qryArr[i].split('=');
	// assign variables
	str1 	= arrQS[0][1];	// a
	str2 	= arrQS[1][1];	// b
	
	if (arrQS[2] != null)
	{
		str3 = arrQS[2][1]	// c
	}
	if (arrQS[3] != null)
	{
		str4 = arrQS[3][1]	// d
	}
	if (arrQS[4] != null)
	{
		str5 = arrQS[4][1]	// total area | e
	}
	if (arrQS[5] != null)
	{
		str6 = arrQS[5][1]	// main | f	
	}
	else
	{
		str6 = "000"
	}
	if (arrQS[6] != null)
	{
		str7 = arrQS[6][1]	// upper | g
	}	
	else
	{
		str7 = "000"
	}
			
	arrQS = "";						// clear the array
}

/* helper function: takes params from the _collection_ page links via query strings. */
// req: a=collection
// req: b=house name
// req: c=width
// req: d=depth
// req: e=total area
// req: f=main
// req: g=upper
function loadColl( a, b, c, d, e, f, g )
{
	// display links and labels on the page
	document.getElementById( 'lblColl' ).innerHTML = '<font face="Verdana" size="1" color="#FFFFFF"><a style="text-decoration: none" href="../maple%20bottom3.htm">&nbsp;&nbsp;Home Designs</a> - ' + a + ' Collection&nbsp;</font>';
	document.getElementById( 'lnkColl' ).innerHTML = '<span style="font-size: 10.0pt; font-family: Wingdings 3; color: #950E06">t</span><a style="text-decoration: none" href="../M_' + a.toLowerCase() + '.htm#' + b + '"><font size="1" color="#000000">&nbsp;' + a + ' Collection</font></a>';
	document.getElementById( 'lblWidth' ).innerHTML = c;
	document.getElementById( 'lblDepth' ).innerHTML = d;
	document.getElementById( 'lblTotalArea' ).innerHTML = e;	
	document.getElementById( 'lblMain' ).innerHTML = f;	
	document.getElementById( 'lblUpper' ).innerHTML = g;
	document.getElementById( 'spanHouseName' ).innerHTML = '<font size="2" color="#494733"><b>' + changeToTitle( b ) + '</b></font>';

	var n = "";
	if ( b.lastIndexOf( '2' ) != -1 || b.lastIndexOf( '1' ) != -1 || b.lastIndexOf( '3' ) != -1)
	{
		n = b.length - 1;
	}
	else
	{
		n = b.length
	}
	// display the house and plan images
	document.getElementById('divHouseImage').innerHTML = '<img border="1" src="' + b.substring( 0, n ).toLowerCase() + '/img' + b + '.jpg" width="576" height="384"><br /><br />';		
	document.getElementById('divPlanImage').innerHTML = '<a href="'+b.substring( 0, n ).toLowerCase()+'/' + b + '.pdf"><img border="1" src="' + b.substring( 0, n ).toLowerCase() + '/fp' + b + '.jpg"></a><br /><br />';		
	
	// todo:
	// if there are more images, load them
	// maybe try a try..catch loop
}

/* helper function: loads the links on the _left frame_ */
// req: x=collection
// req: y=house name
// req: z=folder name
function loadLinks( x, y, z )
{
	window.parent.frames[ 'left' ].document.getElementById( 'lnkColl' ).innerHTML = '<span style="font-size: 10.0pt; font-family: Wingdings 3; color: #FF9900"><a style="text-decoration: none" target="MyFrameTwo" href="../M_'+ x.toLowerCase() +'.htm#' + y + '"><font color="#FF9900">t</font></a></span><span><a style="text-decoration: none" target="MyFrameTwo" href="../M_' + x.toLowerCase() + '.htm#' + y + '"><font face="Verdana" size="1" color="#FFFFFF">'+ ' ' + x + ' ' + 'Collection</font></a></span>';
	window.parent.frames[ 'left' ].document.getElementById( 'lnkPlan' ).innerHTML = '<font face="Verdana" size="1" color="#FFFFFF"><a style="text-decoration: none; color: #FFFFFF" target="MyFrameTwo" href="' + z + '/M_' + y + '_fp.htm">Floor Plans</a></font><a style="text-decoration: none; color: #FFFFFF" target="MyFrameTwo" href="' + z + '/M_' + y + '_fp.htm"><font size="2" color="#FFFFFF">&nbsp;</font><font face="Wingdings 3" color="#FF9900" size="2">u</font></a>';
}

/* helper function: loads the thumbnail pics on the _left frame_ */
// req: x=collection
// req: y=foldername
function loadThumbs( x, y )
{
	for( var i = 0; i < 8; i++ )		// loop through 9 times	
	{
		window.parent.frames[ 'left' ].document.getElementById( 'th' + i ).innerHTML = '<input type="image" onclick="changeImage(\'' + x + '\',\'' + y + '\',\'' + i + '\')" src="../' + x + '/' + y + '/img/th' + str2 + '-0' + i + '00.jpg"  style="border: 1px solid #FFFFFF" width="125" height="94" />';
		//alert(window.parent.frames[ 'left' ].document.getElementById( 'th' + i ).innerHTML)
	}
}

/* helper function: evaluates the house name to check if it has a numeral in the name */
// req: y=house name
// req: n=left or right
function evaluateHN(y,n)
{
	// does house name have a number?
	if ( (y.lastIndexOf('2')) != -1 || (y.lastIndexOf('1')) != -1 || y.lastIndexOf('3') != -1 )
	{
		if ( n == 'right' )
		{ changeToTitle( y ); }
		changeToFolderName( y );
	}
	else
	{	
		if (n == 'right')
		{ str5 = y; }
		str4 = y.toLowerCase();
	}
}

/* helper function: loads the title */
// req: ttl=title
function loadTitle( x )
{
	document.getElementById( 'divTitle' ).innerHTML = '<p align="center"><b><font face="Verdana" size="2" color="#FFFFFF">' + x + '</font></b>';
}

/* helper function: loadds the initial image in the _right frame_ */
// req: x=collection  
// req: y=foldername
// req: z=number [for initial image]
function loadInitialImage( x, y, z )
{
	document.getElementById( 'divImage' ).innerHTML = '<img src="../' + x + '/' + y + '/img/img' + str2 + '-0' + z + '00.jpg"  style="border: 2px solid #FFFFFF" width="448" height="336" />';
}

function changeImage( x, y, z )			// params come from input links from the left page
{
	parent.frames[ 'right' ].document.getElementById( 'divImage' ).innerHTML = '<img src="../' + x + '/' + str4 + '/img/img' + str2 + '-0' + z + '00.jpg"  style="border: 2px solid #FFFFFF" width="448" height="336" />';
}

/* helper function: converts a house name to the folder name */
function changeToFolderName( y )	// if the name has a number e.g. "Willow2" it will be split to "willow_2"
{
	var str = y;
	var index = "";
	var txt = "";
	
	index = str.lastIndexOf( "2" );	// this is the index
	txt = str.substr( 0, index );		// return string less the last number
	str4 = txt.toLowerCase() + '_' + str.substr( index,(index-1) );
	return str4;
}

/* helper function: converts a house name to a title */
function changeToTitle(y)				// if the name has a number e.g. "Willow2" it will be split to "Willow 2"
{
	var str = y;
	var index = "";
	var txt = "";

	if (str.lastIndexOf("1") != -1)
	{
		index = str.lastIndexOf("1")
	}
	else if (str.lastIndexOf("3") != -1)
	{
		index = str.lastIndexOf("3")
	}
	else if (str.lastIndexOf("2") != -1)
	{
		index = str.lastIndexOf("2")
	}
	else
	{
		index = str
		return index;
	}
	
	txt = str.substr(0,index)
	str5 = txt + ' ' + str.substr(index,(index-1));
	return str5;
}

function showArrayValues2()	
{
	if (arrQS == "")
	{
		//alert("array is empty")
	}
	else
	{
	//alert(arrQS[0][1]);
	//alert(arrQS[1][1]);
	//alert(arrQS[2][1]);	
	}
}

function loadMenu()
{
	if ( parent.frames[0] && parent.frames['MyFrameOne'].Go )//PROBLEM IS HERE
		parent.frames['MyFrameOne'].Go()								//PROBLEM IS HERE
	else
		alert("Please click on the Back button of your browser.")	//not working message
}
/* --- end Helper functions --- */