$(function(){
		   
	
	ropein("#polaroids", true); //bring in the polaroids
	dev_in(true);

	
	var defaultopenparams = { 'left':'110px', 'top':'50px', 'height':'500px', 'width':'520px' }

	var openparams = defaultopenparams;
	var	closeparams = { 'left':'700px', 'top':'100px', 'height':'0px', 'width':'0px' };

	
	$('.pol').click(function(){ flyout(this); });
	
	
	//handlers for polaroids
	$('.biography').click( function(){
									
										dev_in(false);

									
										scroller(true);
									
										ropein('#polaroids', false);
										subbg("graphics/bg_bio_dark.jpg");
										
										openparams = { 'left':'110px', 'top':'50px', 'height':'500px', 'width':'520px' }
										
										subanimate('#polaroids', true);
										$('#sub_content_title').load('bio.html #title');
										$('#sub_body_text').load('bio.html #content');
										$('#sub_body_text').css('top', 0); 

																			
									});
	
	$('.contact').click( function(){
								  
								  											dev_in(false);

										
										openparams = { 'left':'110px', 'top':'50px', 'height':'500px', 'width':'520px' }
		
										ropein('#polaroids', false);
										subbg("graphics/bg_primary_dark.jpg");
										subanimate(true);
										$('#sub_content_title').load('contact.html #title');
										$('#sub_body_text').load('contact.html #content');
										
										scroller(false);
										$('#sub_body_text').css('top', 0); 

																			
									});
	
	
	$('.photos').click( function(){
								 
								 		dev_in(false);

										
										openparams = { 'left':'10px', 'top':'0px', 'height':'600px', 'width':'700px' }
		
										ropein('#polaroids', false);
										subbg("graphics/bg_primary_dark.jpg");
										subanimate(true);
										$('#sub_content_title').load('photos.html #title');
										$('#sub_body_text').load('photos.html #content', function(){prepare_gallery();});
										
										scroller(false);
										$('#sub_body_text').css('top', 0); 

																			
									});
	
	$('.branded').click( function(){
								  
				
										dev_in(false);

										scroller(true);
									
										ropein('#polaroids', false);
										subbg("graphics/bg_primary_dark.jpg");
										
										openparams = { 'left':'110px', 'top':'50px', 'height':'500px', 'width':'520px' }
										
										subanimate(true);
										
										$('#sub_content_title').load('branded.html #title');
										
										
										$('#sub_body_text').css('top', 0); 
										
										$('#sub_body_text').load('branded.html #content');

																			
									});
	
	$('#development').click( function(){
										
										dev_in(false);

										openparams = { 'left':'110px', 'top':'50px', 'height':'500px', 'width':'520px' }
		
										ropein('#polaroids', false);
										ropein('#polaroids_development', true);

										subbg("graphics/bg_primary_dark.jpg");
										//subanimate(true);
										$('#sub_content_title').load('development.html #title');
										$('#sub_body_text').load('development.html #content');
										$('#back_secondary').animate({'opacity' : '1'}, 2000);

										
										scroller(false);
										$('#sub_body_text').css('top', 0); 

																			
									});
	
	
	$('.features').click( function(){
										dev_in(false);		
										ropein('#polaroids', false);
										ropein('#polaroids_features', true);
										
										subbg("graphics/bg_primary_dark.jpg");
										
										$('#back_secondary').animate({'opacity' : '1'}, 2000);


																			
									});
	
	$('.shorts').click( function(){
										dev_in(false);		
										ropein('#polaroids', false);
										ropein('#polaroids_shorts', true);
										
										subbg("graphics/bg_primary_dark.jpg");
										
										$('#back_secondary').animate( {'opacity' : '1'} , 2000);


																			
									});
	
	
	$('.link_to_home').click( function(){
									
									dev_in(true);
										ropein('#polaroids_development', false);

										ropein('#polaroids', true);
										ropein('#polaroids_shorts', false);
										ropein('#polaroids_features', false);
										
										subbg(false);
										subanimate(false);
										projectanimate(false);
										pressanimate(false);


										
										scroller(false);										
										$('#back_secondary').animate({'opacity' : '0'}, 200);
									
									});
	
	$('.project').click( function(){
									
									
										var projectname = $(this).attr('project');
									
										ropein('#polaroids', false);
										ropein('#polaroids_features', false);
										ropein('#polaroids_shorts', false);
										
										scroller(false);
										
										subbg("graphics/bg_primary_dark.jpg");
										
										openparams = { 'left':'20px', 'top':'50px', 'height':'500px', 'width':'680px' }
										projectanimate(true);
										
										$('#project_content_title').load( projectname + '.html #title');
										$('#project_body_text').load(projectname + '.html #content');
										$('#project_body_image').load(projectname + '.html #image');
										
										$('#trailer_link').load(projectname + '.html #trailer');

										
										$('#laurels_div').load(projectname + '.html #laurels');

																			
									});
	
	
	$('.press').click( function(){
									
										dev_in(false);
									
									
										ropein('#polaroids', false);
										ropein('#polaroids_features', false);
										ropein('#polaroids_shorts', false);
										
										scroller(false);
										
										subbg("graphics/bg_primary_dark.jpg");
										
										openparams = { 'left':'20px', 'top':'20px', 'height':'780px', 'width':'680px' }
										pressanimate(true);
										
										
										
										$('#press_content_body').load( 'press.html').css('opacity', 0);

										$('#back_secondary').animate({'opacity' : '1'}, 200);

																			
									});
	
	

	
	
	
	function flyout(polaroid){
		
		var leftof = new Array();
		var rightof = new Array();
		var allarray = $('#polaroids > .pol');
				
		for(var i = 0 ; i < allarray.length ; i++){
			
		
			var id = parseInt(allarray[i].id.substr(4, 2));

			var click_id = parseInt(polaroid.id.substr(4, 2));				
			
						
			if( click_id > id ){											
				leftof.push(allarray[i]);				
			}
			
			
			else if( click_id < id ){												
				rightof.push(allarray[i]);				
			}
			
			else{
													
			}
			
		}
		
		//alert("there are " + leftof.length + " pols to the left and " + rightof.length + " pols to the right.");
	}
	
	function dev_in(bool){
		
		if(bool){
			
		$('#development').animate({'opacity' : '1', 'left' : '30px'}, 2000 , 'easeOutElastic');
			
		}
		else{
		
		$('#development').animate({'opacity' : '0', 'left' : '-50'}, 200);
			
		}
		
	}
	
	function ropein(id, bool){
	//clears screen of polaroids if they are on the screen... otherwise nothing
		
		if(bool){
		
			$(id).animate({'top' : 150}, 1000);
			
		}
		else{
			
			$(id).animate({'top' : -300}, 1000);
			
		}
		
		
	}
	
	
	function subbg(path){
		
		if(path){
			
			
			$('#bg_sub > img').attr('src', path);
						
			$('#bg_sub').animate({'opacity': 1}, 2000);
					
			
		}
		else{//clear subbackground
		
			$('#bg_sub').animate({'opacity': 0}, 200);
			
		}
		
		
	}	
	
	
	function subanimate(doit){
		
		if(doit){
			$('#sub_content').animate({ 'width':'0' }, 1000).animate( openparams, 800 , function(){$('#sub_content_title').animate( {'opacity':1},1000 ); $('#sub_below').animate( {'opacity':1},500, function(){ $('#sub_body_text').animate({ 'opacity' : 1 }, 500); } );} );
		}
		else{
			
			$('#sub_body_text').animate({ 'opacity' : 0 }, 100);
			
			$('#sub_content_title').animate( {'opacity':0},200 );
			$('#sub_below').animate( {'opacity':0},200,
									
									
									function(){$('#sub_content').animate(closeparams, 300);}
									
									
									);

		}
		
	}
	
	function projectanimate(doit){
		
		if(doit){
			$('#project_content').animate({ 'width':'0' }, 1000).animate( openparams, 800 , function(){$('#project_content_title').animate( {'opacity':1},1000 ); $('#project_below').animate( {'opacity':0},500, function(){ $('#project_body_text').animate({ 'opacity' : 1 }, 500); } );} );
		}
		else{
			
			$('#laurels_div').html("");
			
			$('#project_body_text').animate({ 'opacity' : 0 }, 100);
			
			$('#project_content_title').animate( {'opacity':0},200 );
			$('#project_below').animate( {'opacity':0},200,
									
									
									function(){$('#project_content').animate(closeparams, 300);}
									
									
									);

		}
		
	}
	
	function pressanimate(doit){
		
		if(doit){
			$('#press_content').animate({ 'width':'0' }, 1000).animate( openparams, 800, function(){$('#press_content_body').animate({ 'opacity':'1' }, 1000);});
		}
		else{
			
			$('#press_content').animate(closeparams, 300);
			
			}
									
	}
	
	
$('#sub_content_body').mousewheel(function(event, delta) {
	
	var prevtop = $('#sub_body_text').position().top;
	
	var maxtop = $('#sub_content_body').height() - $('#sub_body_text').innerHeight();
	
	var newtop = prevtop + (4 * delta);
	
	if( newtop >= maxtop &&  newtop <= 0){
		
		$('#sub_body_text').css('top', newtop);
	
	}
	else{
		
		

	}
	
	
	var dragheight = $('#dragtrack').height() - $('#dragger').height();
	
	
	var toprange = $('#sub_content_body').height() - $('#sub_body_text').innerHeight();
	

	
	var vector = $('#sub_body_text').position().top / toprange;
	
	
	$('#dragger').css('top', vector * dragheight);

	
});

	
	
	$( "#dragger" ).draggable({ 
							  
							  containment: "parent",
							  drag: function() {
								  
								  					//	$('#dragger').css('height', ($('#sub_content_body').height() / $('#sub_body_text').innerHeight()) * $('#dragtrack').height() );

								  								  
								  					var dragheight = $('#dragtrack').height() - $('#dragger').height();
													
													vector = $(this).position().top / dragheight;
																										
													var toprange = $('#sub_content_body').height() - $('#sub_body_text').innerHeight();
													
													
													$('#sub_body_text').css('top', vector * toprange); 
												
											   },
							  
							  
							  });
	

	
	function scroller(bool){
	
	if(bool){
		
		$('#dragtrack').animate({'opacity' : '1'}, 50);
		
	}
	else{
		
		$('#dragtrack').animate({'opacity' : '0'}, 50);
	
	}
		
		
	}
	
	
	
	
	
	
	var oldsrc = [];
		
	$('.pol').mouseenter(function(){
												
						oldsrc[this] = $('img', this ).attr('src');
						
						var url = oldsrc[this];
						
						var ext = url.substr(-4) ;
						
						var addition = "_over" ;
						
						var newsrc = url.substr( 0, url.length - 4 ) + addition + ext ;
						
						 $('img', this ).attr('src', newsrc);
						
						});
						
	$('.pol').mouseleave(function(){
													
						var newsrc = oldsrc[this];
												
						$('img', this ).attr('src', newsrc);

							
							
						});
	
	
	
	
	
	
		   
});

function prepare_gallery(){


//gallery code by Karl Scholz (2010) karl@miartbox.com - use however you like but at your OWN RISK//

get_from_flickr();

function get_from_flickr(){
	
  var icounter = 0;
  var numimg = 0;
		   
  var apiKey = "cbb441fc7b6aa8cb64180c7af7ab0002";	
  //old json request 'http://api.flickr.com/services/rest/?method=flickr.photosets.getphotos&user_id=21156263@N00&photoset_id=72157625280466319&api_key=cbb441fc7b6aa8cb64180c7af7ab0002&format=json&extras=url_sq,url_m&jsoncallback=?'
  $.getJSON('http://api.flickr.com/services/rest/?method=flickr.photosets.getphotos&user_id=21156263@N00&photoset_id=72157625280466319&api_key=cbb441fc7b6aa8cb64180c7af7ab0002&format=json&extras=url_sq,url_m&jsoncallback=?',  function(data) {displayimg(data);});
	function displayimg(data){
		
	  numimg = data.photoset.photo.length;

	  $.each(data.photoset.photo, function(i, photo){ 
										   
					var maxh = 400;
						var picw = 0;
						var pich = 0;
						var picscale = 1;
					
						
						if(photo.height_m > maxh){
	
							picscale = maxh / photo.height_m;						
							
						}
						else{
						
							
						}
						
						picw = picscale * photo.width_m;
						pich = picscale * photo.height_m;
												
						var margin_l = (parseInt($("#gallery_frame").width()) - picw)/2;
						var margin_h = (parseInt($("#gallery_frame").height()) - pich)/2;
			
			
		
	    $('<img/>')
		.attr('class', "thumbimg")
		.attr('src', photo.url_sq)
		.appendTo("#images_gal")
		.click(function(){
						
			
												
						$("#gallery_frame").html($('<img/>').attr('class', "gallery_img").attr('src', photo.url_m).css('width', picw).css('height', pich).css('margin-left', margin_l).css('margin-top', margin_h));
						});
		
		$('#images_gal').width( $('#images_gal').width() + $('.thumbimg').outerWidth() + 4);
		
		if(icounter == 0){
		
						$("#gallery_frame").html($('<img/>').attr('class', "gallery_img").attr('src', photo.url_m).css('width', picw).css('height', pich).css('margin-left', margin_l).css('margin-top', margin_h));

		}

		
		
		
		icounter++;
		
		if(icounter == numimg){
		
			scroller_k();
			
		}
	  });
	}
} 

	
	
}


	
	
function scroller_k(){
	
	 var window_width;
	 var drag_track_width;
	 var content_width;
	 var content_max_left;
	 var drag_max_left;
	 var q =0;
	 var animatebool = false;
	 var animatesize = 1;
	 var mouseX = 0;
	 var mouseY = 0;
	 var previousX = 1;
	 var previousY = 1;
	var iwindow ="#gallery_thumbs_holder";
	var icontent ="#images_gal";
			

			
			window_width = $(iwindow).css('width').slice(0, -2);
			
			content_width = parseInt($(icontent).css('width').slice(0, -2));
							   
			//determine value (negative) of left property
			content_max_left = window_width - content_width;					
					
								
			$("#launch_button_left").mousedown(function(e) { 
					
												animatesize = 80;
												animatebool = true;
												animatebarpre();
													
											}).mouseup(function(){handlemouseout();});
											
			$("#launch_button_right").mousedown(function() { 
												
												animatesize = -80;
												animatebool = true;
												animatebarpre();
													
											}).mouseup(function(){ handlemouseout();});
			
			$( "#hdragger" ).draggable({ 
							  
							  containment: "parent",
							  drag: function() {
								  
								  					//	$('#dragger').css('height', ($('#sub_content_body').height() / $('#sub_body_text').innerHeight()) * $('#dragtrack').height() );

								  								  
								  					var dragwidth = $('#hdragtrack').width() - $('#hdragger').width();
													
													vector = $(this).position().left / dragwidth;
																										
													var toprange = $(iwindow).width() - $(icontent).innerWidth();
													
													
													$(icontent).css('left', vector * toprange); 
													
																									
											   },
							  
							  
							  });
			
			
			function animatebarpre(){


			// retrieve current 'left' value of the content that is being scrolled 
			var currentLeft = parseInt($(icontent).css('left').slice(0,-2));
		
				
			if( ((currentLeft+animatesize) >= content_max_left) && ((currentLeft + animatesize) <= 0 ) && animatebool){
			// if the content is not at the extremes then proceed with animation and call the function again.
					
					$(icontent).animate({ left: currentLeft + animatesize }, 700, 'linear', function(){ animatebarpre(); });
				
			}
			
			else{//on the extremes just for the content to be a maximum or minimum value
			
				if( (currentLeft+animatesize) <= content_max_left ){
			
					$(icontent).animate({ left:  content_max_left }, 700, 'linear');
			
				}
				
				if( (currentLeft+animatesize) > 0 ){
			
					$(icontent).animate({ left:  '0' }, 500, 'linear');
			
				}
			
			}
			
			
		}
		
		function handlemouseout(){
	animatebool = false;	
}

								   
								   
}

		
		
	
		


