var orange = "#fbaa29";
var white = "#ffffff";
var gray = "#969696";
var currentThumb;
var currentPhoto



//::::::::
function initGallery()
{
	$('#gallery-thumbs a')
	.css('opacity', 0.5)
	.click(
	function(){
		$(this).blur();
		changeGalleryPhoto($(this) );
		return false;
	})
	.hover(
	function(){
		if( $(this).hasClass("selectedThumb") == false )
		{
			changeHilite($(this), gray, 1)
		}
	},
	function(){
		if( $(this).hasClass("selectedThumb") == false )
		{
			changeHilite($(this), white, 0.5)
		}
	})
	


//::::::::Center thumbnails
	thumbAreaWidth = $('#gallery-thumbs').width()
	thumbWidth = 106
	allThumbsWidth = thumbWidth * $('#gallery-thumbs').children().length
	extraSpace = thumbAreaWidth -allThumbsWidth
	$('#gallery-thumbs').css('margin-left', extraSpace/2)
}

function changeGalleryPhoto(link)
{
	if( $(link).hasClass("selectedThumb") == false )
	{
		$('#gallery-viewer').addClass('loading')
		prevPhoto = $('#gallery-viewer img')

		prevThumb = currentThumb;
		prevThumb.removeClass('selectedThumb')
		changeHilite(prevThumb, white,  0.5)
		
		currentThumb = link
		currentPhoto = link.attr('href');
		
		link.addClass('selectedThumb')
		changeHilite(link, orange,  1)	
		
		prevPhoto.stop().fadeOut(350, function()
				{
					prevPhoto.remove()
					loadPhoto();
				})
	}
}


//::::::::Load Photo
 // define varible here to fix multiple images with clicking multliple times.

function loadPhoto()
{
	var img = new Image();
	$(img).load(function ()
	{
		$(this).hide();
		$('#gallery-viewer').append(this);
		$('#gallery-viewer').stop().animate( { height: $(img).height() }, 500,'',function()
			{
				$(img).stop().fadeIn(350, function()
					{
					$('#gallery-viewer').removeClass('loading')
					} );
			})
		
	}).attr('src', currentPhoto)
}

	
//::::::::changeHilite thumbs

function changeHilite(link, color, opacityNum)
{
	$(link)
	.stop()
	.animate({borderBottomColor: color, borderTopColor: color, borderLeftColor: color, borderRightColor: color, opacity:opacityNum}, 350, '',
		function(){ // fix firefox border not changing bug
		$(link).css('borderColor', color);
		})
}


//::::::::Load default photo
$(document).ready(function()
	{
		//$('#gallery-viewer').addClass('loading')
		currentThumb = $('#gallery-thumbs').find('li:first-child ').find('a:first-child')
		currentPhoto=currentThumb.attr('href')
		currentThumb.addClass('selectedThumb')
		changeHilite(currentThumb, orange,  1)	
		//loadPhoto()
		//$('#gallery-viewer img').removeAttr('src') // remove src
		//$('#gallery-viewer').append('<img src="../images/blank.gif">')
		//changeGalleryPhoto(currentThumb)
	})

