$(document).ready(function(){
	
	var width = 188;
	var height = 125;
	var widthto = 207;
	var heightto = 137;
	var opacityto = .8;
	
	var tns = $('.tn');
	var tn = tns.eq(0);
	var left = parseInt(tn.css('left'));
	var top = parseInt(tn.css('top'));
	var leftto = left - (widthto - width) / 2;
	var topto = top - (heightto - height) / 2;
	
	tns.css('opacity',opacityto);
	
	tns.mouseenter(function(){
		$(this).stop().animate({'height':heightto, 'width':widthto, 'left':leftto, 'top':topto, 'opacity':1}, 'fast');
	});
	
	tns.mouseleave(function(){
		$(this).stop().animate({'height':height, 'width':width, 'left':left, 'top':top, 'opacity':opacityto}, 'fast');
	});
	
	photoList = $('.imagelink');  //photoList is global

	photoList.click(function(){
		currentPhoto = photoList.index(this); //currentPhoto is global
		var img = $(this).attr('href');
		var title = $(this).attr('title');
		overlay('#imageholder');
	
		getImage(img, title);
		return false;
	}); 
	
	$(window).resize(function(){
		resizeImage('#image img');
		centerBlock('#imageholder');
	});
	
	$('#next').click(function(){
		nextImage();
		return false;
	});
	
	$('#prev').click(function(){
		prevImage();
		return false;
	});
	
	//-------------------------------------projects only

	// slide sidebar
	var workbar = $('#workleftbar');
	if(workbar.length) {
		var startingPos = 100;
		$(window).scroll(function() {
			windowpos = $(window).scrollTop();
			sidebarpos = workbar.offset().top;

			if(windowpos >= 85) {	
				workbar.css({'position':'fixed','top':'15px'});	
			} else {
				workbar.css({'position':'absolute','top':'100px'});		
			}
		});
	}
	
	
	var goldnomma = '<span class="awardgold">Gold</span> NOMMA Award';

	
	
	$('.trophy').hover(function(){					
		var bubble = $('#awardbubble');
		var trophy = $(this);
		var text = eval(trophy.attr('text'));
		
		bubble.children('p').html(text);
		centerObject('#awardpointer','#awardbubble', 3);
		
		var left = trophy.offset().left - (bubble.outerWidth() / 2 + 8);
		var top =  trophy.offset().top - (bubble.outerHeight() + 17);
		
		bubble.css({'display':'none','left':left,'top':top});
		if($.browser.msie) {
			var bubbleChildren = bubble.find('*')
			bubble.css('display','block');
			bubbleChildren.css('display','none');	
			bubble.fadeIn('fast', function(){
				this.style.removeAttribute('filter');						   
			});
			bubbleChildren.fadeIn('fast', function(){
				this.style.removeAttribute('filter');								 
			});
		} else {
			bubble.fadeIn('fast');
		}
		
	},function(){
		$('#awardbubble').fadeOut('fast', function(){
			$('#awardbubble').css({'left':-19999, 'display':'block'});									   
		});
		
	})

});

function centerObject(inner, outer, offset) {
	var largewidth = $(outer).outerWidth() / 2;
	var smallwidth = $(inner).outerWidth() / 2;
	var left = largewidth+smallwidth - offset;
	$(inner).css('left',left);
}
	
function nextImage() {
	currentPhoto+=1;
	currentPhoto = currentPhoto >= photoList.length ? currentPhoto = 0 : currentPhoto;
	var curLink = photoList.eq(currentPhoto);
	var img = curLink.attr('href');
	var title = curLink.attr('title');
	getImage(img, title);
	
}

function prevImage() {
	currentPhoto-=1;
	currentPhoto = currentPhoto < 0 ? currentPhoto =photoList.length - 1 : currentPhoto;
	var curLink = photoList.eq(currentPhoto);
	var img = curLink.attr('href');
	var title = curLink.attr('title');
	getImage(img, title);

}
function clearImage() {
	$('#image').html('');
	$('#image img').css({'height':'','width':''});
	$('#linkcontainer').css({'height':'','width':''});
	$('#preloaderholder').show();
	$('#picholder').css({'visibility':'hidden', 'display':'none'});
	$('#picholder h5').html('&nbsp;');
	centerBlock('#imageholder');	
}

var oWidth;
var oHeight;
function include(dialogue, callBackFunction) {
	$.get(dialogue, function(data){
		$('body').append(data);					 
	});
}
function getImage(img, title) {
		
	clearImage();
	
	var i = new Image();
	$(i).load(function () {// once the image has loaded, execute this code
			 
		$('#preloaderholder').hide();
		
		$('#image').html(this);
		
		oHeight = i.height;
		oWidth = i.width;
		
		$('#picholder').css('display','block');
		resizeImage(this);
		centerBlock('#imageholder');
		title = title == '' ? '&nbsp;' : title;	
		$('#picholder h5').html(title);
		$('#picholder').hide().css('visibility', 'visible').fadeIn();
	 
	})
	
	// if there was an error loading the image, react accordingly
	.error(function () {
	  // notify the user that the image could not be loaded
	})
	
	// set the src attribute of the new image to our image
	.attr('src', img);		 
}

function resizeImage(img) {
	 var percentOfWindow = .8;	 
	 var imgHeight = $('#imageholder').outerHeight();
	 var imgWidth = $('#imageholder').outerWidth();
	 var winHeight = $(window).height(); 
	 var winWidth = $(window).width();
	 
	 if(oHeight > winHeight * percentOfWindow || oWidth > winWidth * percentOfWindow) { //See if image needs to be resized
	 	 if(winHeight >= winWidth) { //use height
			var newWidth = winWidth * percentOfWindow;
			var newPercent = newWidth / oWidth;
			var newHeight = oHeight * newPercent;
		 } else if(winWidth > winHeight){
			var newHeight = winHeight * percentOfWindow; 
			var newPercent = newHeight / oHeight;
			var newWidth = oWidth * newPercent;
		 }
		 $(img).css({'height': newHeight, 'width':newWidth});
		 $('#linkcontainer').css({'height': newHeight, 'width':newWidth});
	 }
}
