var houses = 	[
					{ id: 'almonte', img: 'almonte_1.jpg', name: 'The Almonte', url: '/guildcrest-homes/the-almonte', caption: '<li>Affordable 3-bedroom starter home.</li><li>Large living room.</li><li>Main floor bathroom.</li><li>Large kitchen with dinette.</li>' },
					{ id: 'ashwood', img: 'ashwood_1.jpg', name: 'The Ashwood', url: '/guildcrest-homes/the-ashwood', caption: '<li>3-bedroom raised ranch.</li><li>Up to 1,181 sq. ft.</li><li>Well-planned main floor.</li><li>Future expansion possible on lower level.</li><li>Larger model features 4-piece ensuite off master bedroom.</li><li>Options for all models include single or double garage, decks, gas or wood-burning fireplace.</li>' },
					{ id: 'banff', img: 'banff_1.jpg', name: 'The Banff', url: '/guildcrest-homes/the-banff', caption: '<li>One-and-a-half storey home</li><li>One bedroom on the ground floor</li><li>Space for two bedrooms upstairs</li><li>Main floor laundry and a full four piece bathroom.</li><li>Lots of closet space</li><li>Optional windows and decks to the front elevation.</li>' },
					{ id: 'breakwater', img: 'breakwater_1.jpg', name: 'The Breakwater', url: '/guildcrest-homes/the-breakwater', caption: '<li>Features 1,110 square feet of fully finished space on main floor</li><li>Potential to add 780 square feet upstairs</li><li>Spacious master bedroom and living room</li><li>Open plan kitchen and dining room</li><li>Coat closets at both entrances</li><li>Ground floor laundry</li>' },
					{ id: 'capecod', img: 'capecod_1.jpg', name: 'The Cape Cod', url: '/guildcrest-homes/the-cape-cod', caption: '<li>Charming New England-style 3-bedroom 2,016 sq. ft. home.</li><li>Features twin dormers, a spacious Great Room and a large eat-in kitchen.</li><li>Master bedroom has walk-in closet and full ensuite.</li><li>Main floor laundry room.</li><li>Separate side entry.</li>' },
					{ id: 'dundas', img: 'dundas_1.jpg', name: 'The Dundas', url: '/guildcrest-homes/the-dundas', caption: '<li>Spacious three bedroom home featuring a private master suite.</li><li>Stunning open plan great room dining room combination.</li><li>Main floor laundry</li><li>Optional cathedral ceiling, window wall, and gas fireplace in the great room.</li>' },
					{ id: 'embree', img: 'embree_1.jpg', name: 'The Embree', url: '/guildcrest-homes/the-embree', caption: '<li>Country feel, with wrap-around front porch.</li><li>Spacious living room.</li><li>Main floor powder room.</li><li>Master bedroom with 4-piece ensuite.</li><li>Double garage.</li>' },
					{ id: 'essex', img: 'essex_1.jpg', name: 'The Essex', url: '/guildcrest-homes/the-essex', caption: '<li>1,333 sq ft</li><li>3 bedroom</li><li>Split level</li>' },
					{ id: 'frontenac', img: 'frontenac_1.jpg', name: 'The Frontenac', url: '/guildcrest-homes/the-frontenac', caption: '<li>Spacious living room and kitchen with high vaulted ceilings</li><li>Oversized windows affording unfettered view of the lake</li><li>Three bedrooms and two bathrooms are nicely separated from the main areas</li><li>Optional screened-in porch is the perfect addition for entertaining family and friends</li>' },
					{ id: 'heritage', img: 'heritage_1.jpg', name: 'The Heritage', url: '/guildcrest-homes/the-heritage-house', caption: '<li>A classic 4-bedroom and one of Guildcrest\'s most popular models.</li><li>2,256 sq. ft.</li><li>Kitchen with breakfast room.</li><li>Main floor laundry and 2 1/2 baths.</li><li>Beautiful wrap-around porch.</li><li>Detached double garage available</li>' },
					{ id: 'huntingdon', img: 'huntingdon_1.jpg', name: 'The Huntingdon', url: '/guildcrest-homes/the-huntingdon', caption: '<li>Two-bedroom home</li><li>Huge kitchen/dining room combination</li><li>Generous living room with its beautifully designed windows</li><li>Master bedroom has a walk-in closet and ensuite bathroom</li><li>Ground-floor laundry room</li>' },
					{ id: 'lakelouise', img: 'lakelouise_1.jpg', name: 'The Lake Louise', url: '/guildcrest-homes/the-lake-louise', caption: '<li>Up to 1,218 sq. ft. 3-bedroom cottage.</li><li>Front elevation features multiple windows for that perfect view.</li><li>Large, sunken living room with cathedral ceiling.</li>' },
					{ id: 'laurentian', img: 'laurentian_1.jpg', name: 'The Laurentian', url: '/guildcrest-homes/the-laurentian', caption: '<li>Spacious estate home features four bedrooms and three bathrooms behind an elegant exterior</li><li>Generous windows and a large front porch</li><li>Great Room and ground floor master bedroom with luxurious ensuite</li><li>Large fourth bedroom features a 3-piece bath and private sitting room</li><li>Large double garage, ground floor laundry/mud room</li>' },
					{ id: 'lyndhurst', img: 'lyndhurst_1.jpg', name: 'The Lyndhurst', url: '/guildcrest-homes/the-lyndhurst', caption: '<li>Fully featured, compact home</li><li>Open living space joins living and dining rooms with kitchen and optional family room</li><li>Three good-sized bedrooms</li><li>Large windows, standard patio door</li><li>Optional covered porch and garage</li>' },
					{ id: 'meadowgate', img: 'meadowgate_1.jpg', name: 'The Meadowgate', url: '/guildcrest-homes/the-meadowgate', caption: '<li>spacious 1, 2 or 3-bedroom designs</li><li>pleasantly separated living and sleeping areas</li><li>centrallylocated full bathroom and main floor laundry</li><li>covered porch entrance and large foyer</li><li>large master bedroom with ensuite and walk-in closet</li>' },
					{ id: 'muskoka', img: 'muskoka_1.jpg', name: 'The Muskoka', url: '/guildcrest-homes/the-muskoka', caption: '<li>huge windows and vaulted cathedral ceiling in Great Room</li><li>generous windows and interesting architecture all around mean there is really no rear elevation, making the house ideal for recreational properties</li><li>three bedrooms and two bathrooms are nicely separated from the main areas</li><li>optional fireplace and a screened-in porch or attached garage</li>' },
					{ id: 'novar', img: 'novar_1.jpg', name: 'The Novar', url: '/guildcrest-homes/the-novar', caption: '<li>great home for those starting out as well as empty-nesters</li><li>open-plan kitchen looks out onto large living room</li><li>mud room/laundry room combination</li><li>larger version features eat-in kitchen with island</li><li>perfect for those kitchen parties we all enjoy!</li>' },
					{ id: 'princeedward', img: 'princeedward_1.jpg', name: 'The Prince Edward', url: '/guildcrest-homes/the-prince-edward', caption: '<li>3 bedroom</li><li>Split level</li><li>1,353 and 1,463 sq ft</li>' },
					{ id: 'ridgestone', img: 'ridgestone_1.jpg', name: 'The Ridgestone', url: '/guildcrest-homes/the-ridgestone', caption: '<li>Elegant front elevation and loads of windows</li><li>Open-concept floor plan and a beautiful central kitchen which opens onto a large family room and separate dinette</li><li>Ensuite with separate tub and shower mud room with an entrance off the double garage and a convenient laundry area</li>' },
					{ id: 'rosedale', img: 'rosedale_1.jpg', name: 'The Rosedale', url: '/guildcrest-homes/the-rosedale', caption: '<li>1,500 square feet of well-appointed space</li><li>Large eat-in kitchen</li><li>Spacious four-piece ensuite bath</li><li>Huge living room</li><li>Wide range of available options</li>' },
					{ id: 'stlawrence', img: 'stlawrence_1.jpg', name: 'The St. Lawrence', url: '/guildcrest-homes/the-st-lawrence', caption: '<li>two or three bedroom home with flexible design</li><li>two bedroom version works well with screened-in-porch, car port or garage</li><li>spacious great room and kitchen with eat-in dining room</li>' },
					{ id: 'stormont', img: 'stormont_1.jpg', name: 'The Stormont', url: '/guildcrest-homes/the-stormont', caption: '<li>Deep wrap-around porch</li><li>Luxurious ensuite off master bedroom</li><li>Open concept living and dining room</li><li>Sunroom off well-appointed kitchen</li><li>Generous closets and main floor laundry room</li><li>Wide range of available options</li>' },
					{ id: 'tigerlily', img: 'tigerlily_1.jpg', name: 'The Tiger Lily', url: '/guildcrest-homes/the-tiger-lily', caption: '<li>999 square feet of fully finished space on the ground floor, potential to add another 623 square feet upstairs</li><li>Covered porch, bay window, steeply pitched roof</li><li>Compact design with open plan kitchen, eating area and family room</li>' },
					{ id: 'touchstone', img: 'touchstone_1.jpg', name: 'The Touchstone', url: '/guildcrest-homes/the-touchstone', caption: '<li>Available in three sizes: 1,328 sq. ft., 1,517 sq. ft. or 1,613 sq. ft.</li><li>Double front gables, overhang returns and covered veranda</li><li>Island closet in front hall</li><li>Bevelled/coffered ceiling in living room</li><li>Generous kitchen with eat-at island and mud room or laundry room off the garage</li><li>Each “quadrant” is fully interchangeable with those of other two plans making design easily modified</li>' },
					{ id: 'trailhead', img: 'trailhead_1.jpg', name: 'The Trailhead', url: '/guildcrest-homes/the-trailhead', caption: '<li>The perfect vacation home for the perfect view.</li><li>Features stunning Great Room with vaulted cathedral ceiling</li><li>Kitchen work island and double corner sink</li><li>Optional greenhouse and gas or wood-burning fireplace</li>' },
					{ id: 'trillium', img: 'trillium_1.jpg', name: 'The Trillium', url: '/guildcrest-homes/the-trillium', caption: '<li>Spacious recreational home</li><li>Large kitchen, featuring an eat-at island and double corner sink</li><li>Soaring cathedral ceiling in great room with arch-topped windows</li><li>Optional screened-in porch</li><li>Three bedrooms and two full bathrooms</li>' },
					{ id: 'victoria', img: 'victoria_1.jpg', name: 'The Victoria', url: '/guildcrest-homes/the-victoria', caption: '<li>Available in two sizes: 1,266 sq. ft. or 1,410 sq. ft.</li><li>3-bedrooms.</li><li>Main floor powder room.</li><li>3-piece ensuite off master bedroom.</li><li>Large wrap-around porch.</li><li>Smaller model features eat-in kitchen.</li><li>Larger model has a kitchen work island and breakfast room.</li><li>Options for all models include single or double garage, gas or wood-burning fireplace.</li>' },
					{ id: 'westport', img: 'westport_1.jpg', name: 'The Westport', url: '/guildcrest-homes/the-westport', caption: '<li>Ranch-style home, ideal for future development</li><li>Well-appointed kitchen</li><li>Private entrance from the master bedroom to the large, 4-piece bath</li><li>Large range of available options</li>' }
				];
var current_house_index = 0;
var house_gallery_mode = 'static'; // options are static = follow url on mouse click of arrows, or dynamic just shift houses
var starting_house_index = 0;
var house_gallery_image_path = '/house_gallery/images/';
var house_gallery_house_image_path = '../images/stories/guildcrest_homes/drawings/';

function notImplemented(){
	window.alert('Not Implemented');
}

function initNav()
{
	var data = 	[
					['#house_gallery_nav_left img', house_gallery_image_path+'nav_left_bg.jpg', house_gallery_image_path+'nav_left_bg_ov.jpg'],
					['#house_gallery_nav_right img', house_gallery_image_path+'nav_right_bg.jpg', house_gallery_image_path+'nav_right_bg_ov.jpg']
				];

	function getNavHandler(id, img)
	{
		return function(){
			jQuery(id).attr('src', img);
		};
	}

	function handlerClick(change)
	{
		return function(){ changeHouse(change) };
	}

	for(i = 0; i < data.length; i++)
	{
		jQuery(data[i][0]).mouseover( getNavHandler(data[i][0], data[i][2]) );
		jQuery(data[i][0]).mouseout( getNavHandler(data[i][0], data[i][1]) );
	}

	jQuery(data[0][0]).click( handlerClick(-1) );
	jQuery(data[1][0]).click( handlerClick(1) );

} // function initNav

function initReadMore()
{
	function func(url)
	{
		return function(){ window.location = url; };
	}
	var id = '#house_gallery .readmore';
	// houses and current_house_index are imported from global scope
	jQuery(id).hide();
	jQuery(id).click( func(houses[current_house_index]['url']) );
}

function initImages()
{
	var id = 'img.house';

	// Add all images to the DOM, Pre-loaded
	var image_tmp = jQuery('<img />');
	for(i = 0; i < houses.length; i++)
	{
		image_tmp = jQuery('<img />').attr('src', house_gallery_house_image_path+houses[i]['img']);
		jQuery(image_tmp).attr('class', 'house');
		image_tmp.appendTo('#house_gallery_canvas');
	}

	function handlerClick(houses)
	{
		return function(index, element){
				function gotoUrl(url){ return function(){ window.location = url;}; }
				jQuery(element).click( gotoUrl(houses[index]['url']) );
			};
	}

	function handlerOver(houses)
	{
		return function(index, element){
				jQuery(element).mouseover( function() {
					jQuery('#house_gallery .readmore').show();
				});
				jQuery(element).mouseout( function() {
					jQuery('#house_gallery .readmore').hide();
				});
			};
	}

	// houses and current_house_index are imported from global scope
	for(i = 0; i < (houses.length - 1); i ++)
	{
		jQuery(id).each( function(index, element){
				if( index == 0 )
					jQuery(element).show();
				else
					jQuery(element).hide();
			});
		jQuery(id).each( handlerClick(houses) );
		jQuery(id).each( handlerOver(houses) );
	}
}

function findHouse(house_id)
{
	for(i = 0; i < houses.length; i++)
	{
		if( houses[i]['id'] == house_id )
		{
			changeHouse(i);
			refreshDisplay();
			break;
		}
	}
}

function changeHouse(change)
{
	current_house_index += change;
	if(current_house_index >= (houses.length-1))
		current_house_index = (houses.length-1);
	if(current_house_index < 0)
		current_house_index = 0;
	if(house_gallery_mode == 'static')
		window.location = houses[current_house_index]['url'];
	else
		refreshDisplay();
}

function refreshDisplay()
{
	function func(houses, current_house_index){
		return function(index, element){
			if( index == current_house_index )
				jQuery(element).show();
			else
				jQuery(element).hide();
		};
	}

	function func2(id, heading, body)
	{
		return function() {
			jQuery(id).html(sprintf("<h5>%s</h5><div>%s</div>", heading, body));
		};
	}

	id = 'img.house';
	jQuery(id).each( func(houses, current_house_index) );

	id = 'div#house_gallery_count';
	jQuery(id).html( (current_house_index+1)+' / '+houses.length );

	id = '#house_index';
	jQuery(id).val(current_house_index);

	id = '#house_gallery_caption'
	jQuery(id).text('').animate( {width:'hide'}, 'fast', func2(id, houses[current_house_index]['name'], houses[current_house_index]['caption']) ).delay(300).animate({width:'show'}, 'fast');
}

function initCaption()
{
	id = '#house_gallery_caption';
	caption = jQuery('<div></div>').attr('id', 'house_gallery_caption');
	jQuery(caption).html('<h5>&nbsp;</h5><div>&nbsp;</div>');
	jQuery(caption).css('background', "url('"+house_gallery_image_path+"caption_bg.png')");
	jQuery(caption).appendTo('#house_gallery_canvas');
	jQuery(caption).hide();
}

function initSelector()
{
	id = '#house_index';
	jQuery(id).change( changeSelector );
	var option;
	for(i = 0; i < houses.length; i++)
	{
		option = jQuery('<option></option>').val(i).html(houses[i]['name']);
		option.appendTo(id);
	}
}

function changeSelector()
{
	id = '#house_index';
	current_house_index = parseInt(jQuery(id).val());
	if( house_gallery_mode == 'static' )
		window.location = houses[current_house_index]['url'];
	else
		refreshDisplay();
}

function debug_print(msg)
{
	jQuery('#house_gallery_debug').html(msg);
}

jQuery(document).ready( function(){
	initNav();
	initReadMore();
	initImages();
	initCaption();
	initSelector();
	refreshDisplay();
});

