
var clearScreen = false;
var inAnimation = false;
var cl = 0;

function switchImage(nextImage, animation, animTime, animEase) {
	var img = new Image();	
	img.src = nextImage;
	var t = setInterval(function() {
		if(img.complete) 
		{	
			$('#viewer').removeClass('loading').children().remove();			

			if(animation) {
				var canvas = document.createElement('canvas');			
				$(canvas).attr({width: img.width, height: img.height, src: nextImage});
				var context = canvas.getContext("2d");
				context.drawImage(img, 0, 0, img.width, img.height);
				
				$('#viewer').append(canvas);
				resizeImage(img);
				$('#viewer canvas').css({opacity: 0.0}).animate({opacity:1.0}, animTime, animEase);
			}
			else {				
				$('#viewer').append(img);
				resizeImage(img);
			}			
			
			if($($('#pages li a')[0]).attr('href') == nextImage && clearScreen) switchMenu('show');		
			else if ($('#navigation ul:first, #back a').is(':visible') && clearScreen) switchMenu('hide');
			inAnimation = false; 
			clearInterval(t);
		}
	},50);
}

function resizeImage(img) {
	var id = imageData(img);
	$('#viewer').children()
		.css({
			 width: id[0], 
			 height: id[1], 
			 top: id[3]/2, 
			 left: id[2]/2
		});
}

function imageData(img) {
	var imagewidth = img.width;
	var imageheight = img.height;
	var browserwidth = $(window).width();
	var browserheight = $(window).height() - 25;
	var imageratio = imageheight / imagewidth;
	var screenratio = browserheight / browserwidth;				
	if(screenratio >= imageratio) {
		ih = browserwidth * imageratio;
		iw = browserwidth;
	}
	else {
		ih = browserheight;
		iw = browserheight / imageratio;
	}	
	var Vdiff = $(window).height() - ih - 25;
	var Hdiff = $(window).width() - iw;	
	return Array(iw, ih, Hdiff, Vdiff);
}

function gallery(animation, animTime, animEase) {
	var images = Array();	
	var colors = Array();
	$('#pages li a').each( function(i,val) {
		images[i]= $(this).attr('href');
		colors[i]= $(this).attr('color');
	});	
	var currentImage = images[0];	

	$('#next').click( function(e) {
		e.preventDefault();
		if(inAnimation) return false;
		else inAnimation = true;
		var index = jQuery.inArray(currentImage, images);
		if(index > images.length - 2 ) index = -1;
		switchImage(images[index + 1], animation, animTime, animEase);
		currentImage = images[index + 1];
		preloadImage(images[index + 2]);
		
		if(index > images.length - 2 ) var increment = 1;	
		else var increment = index + 2;
		$('#next, #prev').children('span').text(increment + '/' + images.length)		
		$('#prev, #next').removeClass(colors[index]).addClass(colors[index+1]);		
	});
	
	$('#prev').click( function(e) {
		e.preventDefault();
		if(inAnimation) return false;
		else inAnimation = true;
		var index = jQuery.inArray(currentImage, images);
		if( index < 1 ) index = images.length;
		switchImage(images[index - 1], animation, animTime, animEase);
		currentImage = images[index - 1];
		preloadImage(images[index - 2]);
	
		var increment = index;
		if(index < 1 ) increment = images.length;
		$('#prev, #next').children('span').text(increment + '/' + images.length)
		$('#prev, #next').removeClass(colors[index]).addClass(colors[index-1]);		
	});
	
	switchImage(currentImage, animation, animTime, animEase);
	preloadImage(images[1]); 
	$(window).bind("resize", function(){ 
		var img = new Image();
		img.src = $('#viewer').children().attr('src');
		resizeImage(img); 
	});
}


function switchMenu(action) {
	if(action == 'hide') $('#navigation ul:first, #back, #info, #video').css({display:'none'});
	else setTimeout( function() { $('#navigation ul:first, #back, #info, #video').css({display:'block'}); },50);
}

function navigation() {

	$('#navigation li').removeClass('hover');	
	$('#navigation a').mouseenter( function() {
		$(this).stop().animate({opacity: 0.0},250);	
	});		
	$('#navigation a').mouseleave( function() {
		$(this).stop().animate({opacity: 1.0},250);
	});
	
	if( $('.subnav').parent().attr('class').indexOf('active') == -1 ) 
	{		
		$('.navProjects').mouseenter( function() {
			if($.browser.msie) $('.subnav li').css({opacity:1.0});								   
			$('.subnav').stop().css({display:'block', opacity:0.0}).animate({opacity: 1.0},650,'easeCustom');	
		});
		$('.navProjects').mouseleave( function() {
			$('.subnav').stop().animate({opacity: 0.0},650,'easeCustom', function() { $(this).css({display:'none'}); });
		});
	}
	
	if(clearScreen) {		
		$('#header').mouseenter( function() { 
			if($('#viewer canvas, #viewer img').attr('src') != $($('#pages li a')[0]).attr('href')) {	
				if($.browser.msie) 
				$('#navigation ul:first, #back').css({display:'block'});
				else
				$('#navigation ul:first, #back').stop().css({display:'block', opacity: 0.0}).animate({opacity: 1.0},500,'easeCustom'); 
			}
		});	
		$('#header').mouseleave( function() { 
			if($('#viewer canvas, #viewer img').attr('src') != $($('#pages li a')[0]).attr('href')) {
				if($.browser.msie) 
				$('#navigation ul:first, #back').css({display:'none'});
				else
				$('#navigation ul:first, #back').stop().animate({opacity: 0.0},750,'easeCustom', function() {$(this).css({opacity:'',display:'none'});}); 
			}
		});
	}
	
	$('#next, #prev, #back a').children('span').css({opacity:0.0});	
	$('#next, #prev, #back a').mouseover( function() {
		$(this).children('span').stop().animate({opacity: 1.0},600,'easeCustom');
	});	
	$('#next, #prev, #back a').mouseout( function() {
		$(this).children('span').stop().animate({opacity: 0.0},600,'easeCustom');
	});
}

function projectsHover() {
	
	$('.project').removeClass('hover');			
	$('.project').mouseenter( function() { 
		$(this).children('a').stop().css({opacity: 0.0, display:'block'}).animate({opacity: 0.65},1000,'easeCustom');
	});
	$('.project').mouseleave( function() { 
		$(this).children('a').stop().animate({opacity: 0.0},1950,'easeCustom',function() { $(this).css({opacity: '', display: 'none'}) });
	});
	
}

function projectsSize() {

	var numProjects = $('.project').length;
	var screenWidth = $(window).width();
	if(screenWidth < 1300)
	var minWidth = 140;
	else if(screenWidth > 1300 && screenWidth < 1600)
	var minWidth = 160;
	else 
	var minWidth = 180;
	var origNum = Math.floor(screenWidth / minWidth);
	var origNumWidth = origNum * minWidth;
	var distance = screenWidth - origNumWidth;
	var add = Math.floor(distance / origNum);
	var newWidth = minWidth + add - 2;

	$('.project').parent().css({"width": (newWidth + 2) * origNum});
	$('.project').css({"width": newWidth + "px", "height": newWidth + "px"});
	$('.project a').css({"width": newWidth - 20 + "px", "height": newWidth - 20 + "px"});
	
	var arrowHeight = 44;
	var posY = Math.floor((newWidth - arrowHeight) / 2);
	$('.project a').css({"backgroundPosition": "80% "+ posY +"px"});
	
}

$.fn.resizenow = function() {
	var startwidth = 1800;
	var startheight = 1200;
	return this.each(function() {		
		//Define image ratio
		var ratio = startheight / startwidth;		
		//Gather browser and current image size
		var imagewidth = $(this).width();
		var imageheight = $(this).height();
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio){
			$(this).height(browserheight);
			$(this).width(browserheight / ratio);
		} else {
			$(this).width(browserwidth);
			$(this).height(browserwidth * ratio);
		}
		$(this).css('left', (browserwidth - $(this).width())/2);
		$(this).css('top', (browserheight - $(this).height())/2);
	});
};

function toggleInfo() {
 	if(cl == 0) { 
		$('#info div').css({display: 'none'}); 
		$('#info #close').removeClass('open').addClass('close');
		cl = '1'; 
	}
	else { 
		$('#info div').css({display: 'block'}); 
		$('#info #close').removeClass('close').addClass('open');
		cl = 0;
	}
}

function preloadImage(path) {
	var	image = new Image();
	image.src = path;
}

function galleryFullscreen() {

	var images = Array();		
	$('#pages li a').each( function(i,val) {
		images[i]= this.href;
	});	
	var currentImage = images[0];
	
	setInterval(function() 
	{						 
		var index = jQuery.inArray(currentImage, images);
		if(index > images.length - 2 ) index = -1;
		var nextImage = images[index + 1];
		
		var img = new Image();	
		img.src = nextImage;
		var t = setInterval(function() {
			if(img.complete) {
				$('#viewer img').remove();
				$('#viewer').append(img);
				$('#viewer img').resizenow();
				clearInterval(t);
			}
		},50);

		var increment = index + 2;
		if(index > images.length - 2 ) increment = 1;							 
		currentImage = nextImage;
							 
	},5000);

}



