function trace(s) {
  if (this.console && typeof console.log != "undefined")
    console.log(s);
}



function sendEmail()
{
protocol = 'mai' + 'lto' + ':'
address= 'ale' + 'x@creative'+'superhero' + '.ca';
location.href=protocol + address;
}

var viewportExpanded = false;
var viewportAnimating = false;

var fadeOpacity = 0.2
var fadeTime = 300

var navItemFaded = 0.3
var currentID;

$('html').addClass('preload')



function fadeInImages()
{
var allImagesFadeVisible = true;
fadingImages.each(function()
	{
	if($(this).get(0).complete == true)
		{
		$(this).fadeTo(700, 1)	
		}
	else 
		{
			allImagesFadeVisible = false
		}
	})
	if(allImagesFadeVisible == false)
	{
		setTimeout('fadeInImages()', 200)
	}

}



$(document).ready(function(){

$('#email').hover(function()
	{
		$(this).stop().fadeTo(300, 0.6)	
	},
	function(){
		$(this).stop().fadeTo(300, 1)	
	})


//fadeImages
fadingImages = $('#header img, #portfolio img')


setTimeout('fadeInImages()', 200)


introLinkColor = '#5d4c3d'


//Portfolio Link
$('#secret-id-link, #portfolio-link').unbind('mouseover').hover(function(){
		$(this).stop().animate({'color':'#9e8b7a'}, fadeTime)
	},
	function(){
		$(this).stop().animate({'color':introLinkColor}, fadeTime)
	})
	
	$('#portfolio-link')
	.click(function(e){
		e.preventDefault();
		$('body').scrollTo('#portfolio', 700, 'easeInOutQuad')		
	})


$('#portfolio').find('.thumb').hover(thumbOver,thumbOut)	
	//Get IDs
	.each(thumbEach)
	.click(thumbClick)

function thumbOver()
{
	if(!viewportExpanded)
	{
	//Fade out other thumbs
		$('#portfolio').find('.thumb').not(this).stop().fadeTo(fadeTime, fadeOpacity)
	}
	else if(viewportExpanded)
	{
	//Hilight single thumb in viewportExpanded mode
		$(this).stop().fadeTo(300, 1)

	}
}

function thumbOut()
{
	if(!viewportExpanded)
	{
	//Fade In all thumbs			
		$('#portfolio').find('.thumb').stop().fadeTo(fadeTime, 1)
	}
	else if(viewportExpanded)
	{            
	//Fadeout thumb
		if( $(this).hasClass('active') == false)
		{
			$(this).stop().fadeTo(fadeTime, fadeOpacity)
		}

	}
}

function thumbEach()
{
	if( $(this).attr('id').length < 1 )
	{
		$(this).attr('id', getIdName( $(this) ) )
	}
}

function thumbClick(e)
{
	e.preventDefault();
	$(this).blur()
	if( $(this).hasClass('active') == false)
		{
		prevThumb = $('#portfolio').find('.thumb.active')
		closeView(prevThumb)
		
		openView($(this))
		}
	else
		{
		//close view
		closeView( $(this) )

		}
}
	
//Fixed Watermark	
$(window).bind('scroll', function() 
	{
		var watermark =$('#vertical-watermark');
		var porfolioTop = $('#portfolio').offset()
		var top = porfolioTop.top + 0 //.watermark css top - .fixed css top
		if($(document).scrollTop() > top )
		{
			if(watermark.hasClass('fixed') == false)
			{
				var portfolio = $('#portfolio').offset()
				left = portfolio.left - 34;
				watermark.css('left', left)
				watermark.attr('class', 'fixed')
			}
		}
		if($(document).scrollTop() < top )
		{
			if(watermark.hasClass('fixed') == true)
			{
				watermark.attr('class', '')
				watermark.css('left', '')
			}
		}
	});
$(window).bind('resize', function()
	{
	var portfolio = $('#portfolio').offset();
	var watermark = $('#vertical-watermark');
	if(watermark.hasClass('fixed') == true)
	{
		watermark.css('left', (portfolio.left -34))
	}
})


/*Secret ID wrap*/

/*Open Secret ID*/
secretIdOpen = false

$('#secret-id-link').click(function(event)
	{
	if(secretIdOpen == false)
		{
		//$('#secret-id *').css('filter', 'inherit')
		$('#close-secret-id').css('opacity', 0.5)
		$('#secret-id').animate({'height':'330px', 'opacity': 1}, 400, function()
			{
				//$('#secret-id *').css('filter', 'none').css('opacity','none')
				$('#close-secret-id').css('opacity', 0.5)
			})
		event.preventDefault();
		secretIdOpen = true
		}
	else
		{
		event.preventDefault();
		closeSecretID()
		}
	})

/*Close Secret ID button*/

$('#close-secret-id').hover(
	function()
	{
		$(this).stop().fadeTo(fadeTime, 1)	
	},
	function()
	{
		$(this).stop().fadeTo(fadeTime, 0.5)
	})
	.click(closeSecretID)




})//end document ready

function closeSecretID()
{
	secretIdOpen = false;
	//$('#secret-id *').css('filter', 'inherit')
	$('#secret-id').animate({'height':'0px', 'opacity': 0}, 400)	
}

function openView(link)
{	
	id=link.attr('id')
	currentID = id
	idName=id + '-viewport';

	viewportExpanded = true;
	link.addClass('active')

	viewPort = $('<div />').addClass('viewport').attr('id', idName).hide().css('height', 0)
	link.after(viewPort)
	viewPort = $('#' + idName);
	viewPort.addClass('loading');
	
	//load document
	gallery=$('<div>').load(link.attr('href') + ' #gallery', loadDocument)
	viewPort.append(gallery)

	
//open viewport
	viewPort.addClass('active')
	viewportAnimating = true;
	viewPort.animate({'height':600}, 700, "easeInOutQuad", function()
		{
			viewportAnimating = false;
			$('body').scrollTo(link, 700)
		})
		
}

function loadDocument()
{
	buildGallery( $(this),id );
	startGallery()
}

	
		
function closeView(link) //link either this (thumb) or prevThumb
{
	idName=$(link).attr('id') + '-viewport';
	viewPort = $('#' + idName);

	viewportAnimating = true;
	if(window.slideshow)
		{
			clearTimeout(slideshow);
		}
	
	viewPort.stop().animate({'height':0}, 700, "easeInOutQuad", viewPortGalleryCloseComplete)
	if( viewPort.hasClass('custom') == false ) // fadeout gallery image
		{
			viewPort.find('.gallery-items img:visible').fadeOut(700)
		}
	
	//reset things
	//viewPort.removeClass('active')
	link.removeClass('active')
	link.stop().fadeTo(fadeTime, fadeOpacity)
	viewportExpanded = false;
}


function viewPortGalleryCloseComplete()
{
	viewportAnimating = false;
	$(this).remove()
}



/*:::::::::Build Gallery:::::::::*/
function buildGallery(gallery, id)
{
//change id name
gallery.find('#gallery').attr('id', id + '-gallery')

	
//nav items
navItemBox = $('<div class="nav-item-box" />')

//if custom gallery, import them
customView = gallery.find('.custom-view')
if (customView.length > 0)
	{
	customView.show().fadeTo(300,1)
	gallery.parent().addClass('custom')
	}

else//else build Normal Gallery
	{
	gallery.find('img').css('opacity', 0).hide()
	.each(function(index)
		{
			$(this).attr('id', id + index)
			.load(function(){
			//load function
			})
			navItem = $('<a>').addClass('nav-item').css('opacity', navItemFaded).attr('href', index).data('id', id)
			.click(
				function(e)
				{
				e.preventDefault();
				$(this).blur()
				changeImage($(this));
				//stop slideshow
				if(window.slideshow)
				{	
					clearTimeout(slideshow)
				}
			})
			.hover(
				function()
				{
					if($(this).hasClass('active') == false)
					{
						$(this).stop().fadeTo(300, 1 );
					}
			},
				function()
					{
					if($(this).hasClass('active') == false)
					{
						$(this).stop().fadeTo(300, navItemFaded );
					}
			})
			
			navItemBox.append(navItem)
		})
	}

gallery.find('.nav').prepend(navItemBox)

//close button
closeButton.data('target', $('#' + id)) // target = jquery thumbnail

	
gallery.find('.nav').append(closeButton)

gallery.find('.launch').hover(
	function() 
	{
		$(this).stop().animate({'color':'#fff'}, fadeTime)		
	},
	function()
	{
		$(this).stop().animate({'color':'#675647'}, fadeTime)
	})

} // end build gallery


//Close Button functions
closeButton = $('<a>').addClass('close-button')
$('.close-button').live('mouseover' , closeButtonOver).live('mouseout' , closeButtonOut).live('click' , closeButtonClick)

function closeButtonOver()
{
	$(this).stop().fadeTo(fadeTime, 1)
}

function closeButtonOut()
{
	$(this).stop().fadeTo(fadeTime, 0.5)
}

function closeButtonClick()
{
	closeView($(this).data('target')) // target = jquery thumbnail
		$(this).blur()
}




//::::::::Change Images:::::::::::
function changeImage(link) //link is $('a.nav-item')
{
	id=link.data('id')
	gallery = $('#' + id + '-gallery').find('.gallery-items')
	navBox = $('#' + id + '-gallery').find('.nav-item-box')
	
	//fadeout prev nav item
	navBox.find('.nav-item.active').removeClass('active').stop().fadeTo(fadeTime, navItemFaded)
	
	//hilite current item
	link.addClass('active')
	link.stop().fadeTo(700, 1)
	
	//fadeout prev image
	nextImage = $('#' + id + link.attr('href') )
	if( nextImage.get(0).complete == false )
		{
			viewPort.addClass('loading')	
		}
	gallery.find('img:visible').stop().fadeTo(700, 0, function()
		{
			$(this).hide()
			checkChangeImageIsLoaded()

		})
}

function checkChangeImageIsLoaded()
{
	if(nextImage.get(0).complete == true )
	{
		nextImage.show().fadeTo(700,1, function()
			{
				if(viewPort.hasClass('loading') )
					{
						viewPort.removeClass('loading')
					}
			})
	}
	else
	{
		setTimeout('checkChangeImageIsLoaded()', 200)
	}
}


//::::::::Start Gallery:::::::::::
function startGallery()
{
galleryIndex = 0;

firstImage = $('#' + currentID + '0')
if(firstImage.length > 0)
	{
	gallery = $('#' + currentID +'-gallery')
	galleryImages = gallery.find('.gallery-items img')
	navItems = gallery.find('.nav-item-box .nav-item')
	$(navItems[0]).addClass('active')
	$(navItems[0]).css('opacity', 1)
	setTimeout('showFirstImage()', 200)
	}
}

function showFirstImage()
{
if( firstImage.get(0).complete == true )
	{
	firstImage.show().fadeTo(700, 1, function()
		{
		viewPort.removeClass('loading')	
		})
	slideshow = setTimeout('getNextImage()', 3000)
	}
else
	{
		setTimeout('showFirstImage()', 200)
	}
}

function getNextImage()
{
	galleryIndex++	
	if(galleryIndex > galleryImages.length-1)
	{
		galleryIndex =0
	}
	nextNavItem = $(navItems[galleryIndex])
	nextImage = $('#' + currentID + galleryIndex)
	checkNextImageIsLoaded()
}

function checkNextImageIsLoaded()
{
	if(nextImage.get(0).complete == true )
	{
		
	changeImage( nextNavItem )
	if(galleryIndex > 0) //if gallery index hasn't looped around, continue
		{
			slideshow = setTimeout('getNextImage()', 3000)
		}
	}
	else
	{
		setTimeout('checkNextImageIsLoaded()', 200)
	}
}


//Get ID from Filename
function getIdName(link)
{
	url = link.attr('href')
	dot = url.lastIndexOf('.');
	slash = url.lastIndexOf('/');
	idName = url.substring(slash+1, dot)
	return idName
}


