function initFloorplanList()
{
		
	$('#bungalows, #two-storey, #attached').addClass('hidden')
		
	//flooplan expander
	$('#subnav > li > a').click(function(e){
			// hide visible group
			if(visibleList = $('.visible'))
			visibleList.slideUp('',function(){
				visibleList.addClass('hidden');
				visibleList.removeClass('visible');								   
				}) 

			
			sublist = $(this).parent().find('ul:first');
			if(sublist.hasClass('hidden') )
				{
				sublist.slideDown('', function(){
					sublist.removeClass('hidden')
					sublist.addClass('visible')				   
					 });
				}
			else 
				{
				sublist.slideUp('', function(){
					sublist.removeClass('visible')
					sublist.addClass('hidden');								 
					});
				}
			
			e.preventDefault();
			$(this).blur();
			})
}

var homeType

function syncFloorplanList() // sync sidebar nav based on filename
{
if(document.getElementById)
	{
	path = location.href
	dot = path.lastIndexOf('.')
	slash = path.lastIndexOf('/')
	filename = path.substr( (slash +1), (dot - slash -1) ) // start,length
	
	if(navItem = document.getElementById(filename))
		{
		navItem.className = 'active' //hilight active floorplan
		
		//traverse up until homeType <ul> is found
		homeType = navItem.parentNode
		showCurrentHomeType()
		}
	}
}

function showCurrentHomeType()
{
	if( (homeType.id =='bungalows') || (homeType.id =='two-storey') || (homeType.id =='attached' ) )
	{
		homeType.className = 'visible'
	}
	else
	{
		homeType = homeType.parentNode
		showCurrentHomeType()
	}
}

// Floorplan Viewer

var orange = "#fbaa29"
var white = "#ffffff"
var gray = "#969696"
var currentView
var currentElevation
var currentElevationThumb
var elevationCaption
var	prevElevation



function initFloorplans()
{

//set default height for extra elevations
	extraElevationView = $('.extra-elevation').parent()
	extraElevationView.css('height', extraElevationView.height() )

$('#floorplan-view > div').css('display','none') // hide all items
$('#elevations-view').show() // show elevation as default
changeColor( $('#elevations'), orange, orange );

currentView = $('#elevations') // set currentView to #elevations


//Viewser Links
$('#floorplan-nav a').click(function(e){
		$(this).blur();
		changeView( $(this) )
		e.preventDefault();
	})

//Elevation Nav Click
$('.elevation-navigation a')
	.click(
	function(){
		$(this).blur();
		changeElevation($(this) );
		return false;
	})
		.hover(
	function(){
		if( $(this).hasClass("selectedThumb") == false )
		{
			hiliteBorder($(this), gray) 
		}
	},
	function(){
		if( $(this).hasClass("selectedThumb") == false )
		{
			hiliteBorder($(this), white)
		}
	})

	//:::::::::load default image
	currentElevationThumb = $('#elevation-nav').find('li:first-child ').find('a:first-child')
	//$('.elevation-image:visible img').removeAttr('src') // remove image
	changeElevation(currentElevationThumb) 
	


}

function changeView(link)
{
	//change color of prev link
	prevLink = currentView
	changeColor(prevLink, gray, white)
	
	//change color of current link
	changeColor(link, orange, orange)
	
	//set current view
	currentView = $(link);
	
	//hide prev view
	prevView = $('#' + prevLink.attr('id') + '-view' );
	prevView.fadeOut(350, showNextPanel)
	
	// hilite default extra elevation image & Caption
	if( (link.attr('id').indexOf('elevations') > -1) )
		{
			extraView = $('#' + link.attr('id') + '-view')
			if(extraView.find('.selectedThumb').length == 0 )// if thumb not selected
			{
				firstItem = extraView.find('li:first-child ').find('a:first-child')
				hiliteBorder(firstItem, orange)
				firstItem.addClass('selectedThumb')
				extraView.find('.caption').html(firstItem.attr('title') )
			}
			currentElevationThumb = extraView.find('.selectedThumb')
		}
}



function showNextPanel()
{
panel = 	$('#' + currentView.attr('id') + '-view' );
//panel.hide();
$('#floorplan-view').stop().animate( {height: panel.height() }, 300,'',function()
	{
		panel.fadeIn(500)
	})

}


function changeColor(link, textColor, underlineColor)
{

link.animate({ color: textColor }, 500, function()
	{ //remove style to restore hover on links
		if(textColor == gray)
			{
				$(this).removeAttr('style')
			} 
	}); 
	
link.parent().animate({ borderBottomColor: underlineColor }, 500);
}




//Change Elevations
function changeElevation(link)
{
	if( $(link).hasClass("selectedThumb") == false )
	{ 
		currentElevation = link.attr('href')
		$('.elevation-area:visible').addClass('loading')
		$('.caption:visible').animate({color: 'white'}, 'fast')
		prevElevation = $('.elevation-image:visible img')

		prevElevationThumb = currentElevationThumb
		hiliteBorder(prevElevationThumb, white);
		prevElevationThumb.removeClass('selectedThumb')
		
		elevationCaption = link.attr('title')
		
		currentElevationThumb = link
		currentElevationThumb.addClass('selectedThumb')
		hiliteBorder(currentElevationThumb, orange);
		prevElevation.stop().fadeTo(350, 0, function() // add stop to prevent multiple images with clicking multliple times.
				{
					loadElevation()
				})

	}
}


function loadElevation()
{
	var img = new Image();
	if(currentElevation.indexOf('jpg') > -1 )
		{
			$(img).addClass('color-elevation')	
		}
	$(img).load(function ()
	{
		$(this).hide();
		prevElevation.remove();
		$('.elevation-image:visible').append(this);
		caption = $('.caption:visible')
		caption.html(elevationCaption)
		caption.animate({color: 'black'}, 'fast');
		$(this).stop().fadeIn(350, function(){
				$('.elevation-area:visible').removeClass('loading')
				//set height of floorplan-view so it animates properly
				refHeight = $(this).parent().parent() // elevation-area
				$('#floorplan-view').css('height', refHeight.height() );
				
			});

	}).attr('src', currentElevation)
	
}

function hiliteBorder(link, color)
{
	$(link).stop().animate({borderBottomColor: color, borderTopColor: color, borderLeftColor: color, borderRightColor: color}, 350, '',
		function(){ // fix firefox border not changing bug
		$(link).css('borderColor', color);
		})
}

//hide inital image
$('html').addClass('preload')



