// JavaScript Document
$(document).ready(function() {
	/* Centering Function */
	$("#project_image a").each( function() {
		img_width = this.getElementsByTagName('img')[1].offsetWidth;
		img_height = this.getElementsByTagName('img')[1].offsetHeight;
		$(this).css('left', (585-img_width)/2);
		$(this).css('top', (390-img_height)/2);
	});
	/* Begin Gallery Functions */ 	
	var index = 0;
	$("#project_image a").fancybox({"overlayOpacity":.7, "zoomOpacity":true});
	$("#project_image .image_container").hide().slice(0, 1).show();
	$('#project_thumbnails img:nth-child(4n)').each(function() {
		$(this).addClass('end')	;
	});
	$('#project_thumbnails img').click(function() {
		this_index=$('#project_thumbnails img').index($(this));
		if(this_index == index) {
			return;
		}
		else {
			/*if (jQuery.browser.version=='8.0') {
				$('#project_image div.image_container').hide();
				$('#project_image div.image_container').slice(this_index,this_index+1).show();	
			}
			else {*/
			$('#project_image div.image_container').fadeOut();
			$('#project_image div.image_container').slice(this_index,this_index+1).fadeIn();
			index = this_index;
		}
	});
	
	/* Begin Project Navigation Functions */
	var project_number = $('#project_nav a').size();
	
	function getNavPosition() {
		var first_project = $('#scroll_inner a:first').attr('href');
		first_project = first_project.split('?project=')[1];
		return first_project;
	}
	
	function calcNavPosition(direction) {
		if(direction=='right') {
			nav_position = parseFloat(getNavPosition()) + 8;
			if (nav_position >= project_number) {
				nav_position -= project_number;
			}
		}
		else if(direction=='left') {
			nav_position = parseFloat(getNavPosition()) - 8;
			if (nav_position < 0) {
				nav_position += project_number;
			}
		}
		
		if(nav_position < 10) {
			nav_position="0" + nav_position;
		}
		
		return nav_position;
	}
	
	$('#scroll_inner').width((project_number*105)+10);
	
	$('.scroll_arrow').click(function() {
		if ($('#scroll_inner:animated').size()>0) {
			return;
		}
		current_pos = parseFloat($('#scroll_inner').css('left'));
		if($(this).hasClass('right')) {
			$('#scroll_inner a').attr('href',function(index){return $(this).attr('href').split('&pos')[0] + "&pos=" + calcNavPosition('right');});
			$('#scroll_inner').animate({'left':-840},1500,'easeInOutSine',function(){$('#project_nav a').slice(0,8).appendTo('#scroll_inner');$('#scroll_inner').css('left',0);});
		}
		else if($(this).hasClass('left')) {
			$('#scroll_inner a').attr('href',function(index){return $(this).attr('href').split('&pos')[0] + "&pos=" + calcNavPosition('left');});
			$('#project_nav a').slice(project_number-8,project_number).prependTo('#scroll_inner');
			$('#scroll_inner').css('left',-840);
			$('#scroll_inner').animate({'left':0},1500,'easeInOutSine');
			
		}
		
		
	});
	
		/* Begin Button Functions */ 
	$('#scroll_inner a').hover(function() {
		$(this).fadeTo(200, 1.0); 
	}, function() {
		$(this).fadeTo(200, .5);
	});

	
	/* Begin Info Resize Functions */
	if($('#project_thumbnails img').size()<=4) {
		$('#project_text').css('height', 363);
	}
	else if($('#project_thumbnails img').size()>8&&$('#project_thumbnails img').size()<=12) {
		$('#project_text').css('height', 309);
	}
	else if($('#project_thumbnails img').size()>12&&$('#project_thumbnails img').size()<=16) {
		$('#project_text').css('height', 282);
	}
	 
  });