$(function(){

	var currentPic = 0;
	var nextPic;
	var timeout = 1000;
	var timer;
	var playing = false;
	var menu_open = false;
	var new_height = 500;
	var new_width = 500;
	var new_dimension = 500;
	var navOpen = false;



	function getParameterByName(name) {
	  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	  var regexS = "[\\?&]" + name + "=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(window.location.href);
	  if(results == null) {
		return "";
	  } else {
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	  }
	}	   
	if ( getParameterByName('p').length > 0) {
		$('.slideshow-photo-item').first().hide();
		currentPic = parseInt(getParameterByName('p')) - 1;
		$('.slideshow-photo-item').eq(currentPic).show();
	}
	

	new_height = $('#container').height() - 105;
	new_width = $('#container').width() - 100;
	
	if ((new_width) > (new_height)) {
		new_dimension = new_width;
	} else {
		new_dimension = new_height;
	}
	
	
	
	if ($('html.canvas').length) {
	  var len = $('.slideshow-photo').length;
	  gcanvas = document.createElement("canvas");
	  gctx = gcanvas.getContext("2d");
	  var watermark = new Image();
	  watermark.src = "";
	  watermark.src = "http://www.doanegregory.com/img/watermark-doane.png?abc";
	  var img = new Image();
	  img.src = "";
	}
	
	  
			  
			  



	function resizePic() {

		if ( $('.slideshow-photo-container').length != 0) {

			$('.slideshow-photo-container').hide();
			//$('.slideshow-thumb-container').hide();
			$('.slideshow-prev-next').hide();
			
			if ($(window).width() < 1100) {
			  $('#container').css({'width': '96%'});
			  $('#header').css({'width': '96%'});
			} else {
			  $('#container').css({'width': '85%'}); 
			  $('#header').css({'width': '85%'});  
			}
			
	
			new_height = $('#container').height() - 105;
			new_width = $('#container').width() - 100;
			if ((new_width) > (new_height)) {
				new_dimension = new_width;
			} else {
				new_dimension = new_height; 
			}
			$.cookie('doane_dimension', new_dimension, { expires: 7, domain: 'www.doanegregory.com' });
			
			
			
			//$('.slideshow-photo-container canvas').css({'max-height': new_height});
			//$('.slideshow-photo-container canvas').css({'max-width': new_width});
			
			
			if ($('html.canvas').length) {
			  var imgMaxSize = ".slideshow-photo, canvas { max-width: " + new_width + "px; max-height: " + new_height + "px; }";
			  $("style").append(imgMaxSize);
			}
			
			$('.slideshow-photo').css({'max-width':new_width+'px', 'max-height':new_height+'px'});
			$('canvas').css({'max-width':new_width+'px', 'max-height':new_height+'px'});

			
			//$('.slideshow-thumb-container').css("left", ($('#container').width()/2) - ($('.slideshow-thumb-container').width()/2) ).show();
			
			$('.slideshow-prev-next').css('left',(($('body').width()/2) - (($('.slideshow-prev-next').outerWidth()/2)) )).show();
			
			$('#nav').css('left',(($('#header').width()/2) - (($('#nav').outerWidth()/2)) ));

			
			
			var currentSrc = $('.slideshow-photo').eq(currentPic).attr('src');
			var currentSrcSplit = currentSrc.split("=");
			if (currentSrcSplit.length > 1) {
				var currentNewSrc = currentSrcSplit[0] + "=s" + new_dimension;
				$('.slideshow-photo').eq(currentPic).attr("src",currentNewSrc).fadeIn();
			}
			
			
			
			
			$('.slideshow-photo').each(function() {
				var thisSrc = $(this).attr('src');
				  var thisSrcSplit = thisSrc.split("=");
				  if (thisSrcSplit.length > 1) {
					var thisNewSrc = thisSrcSplit[0] + "=s" + new_dimension;
					$(this).css({'max-width':new_width+'px', 'max-height':new_height+'px'});
					$(this).attr("src",thisNewSrc).fadeIn();
				  }
				  var half_new_width = new_width/2;
				  if (!$('html.canvas').length) {
					  //alert(picPos.left);
					  // overlay watermark over images in IE
					  $(this).parent().append('<div class="spaceball" style="left:50%;margin-left:-'+half_new_width+'px;height:'+new_height+'px;max-width:'+new_width+'px;max-height'+new_height+'px;"><img src="http://clients.adam8.com/www.doanegregory.com/img/spaceball.gif" /></div>');
				  }
				  
			});

			
			
			// try to avoid the initial image resize jumpiness


			$('.slideshow-photo-container').fadeIn('slow');
			
			$('.slideshow-thumb-container').css({'top': ($('#container').height() - 98)});
			
			$(".slideshow-prev-next").appendTo("body").show();
			
			
			
			// Add icons to dropdown menu
			$('a.nav-link-tier2').each(function(i) {
			  $(this).parent().append('<div class="icon-container"></div>');
			  $(this).parent().find('.icon-container').load( $(this).attr('href') + 'gallery-icons/?g=1', function() {
				  //; 														
				  })
			});
			
			
			

			
			if ($('html.canvas').length) {
			  var load = false;
			  window.onload = function(){
				if(!load){
				  wmark.init({
				  "opacity": 100, // default 50
				  "className": "slideshow-photo", // default "watermark"
				  "path": "/img/watermark-doane.png"
				  });
				  load = true;
				}
			  }
			} 
			
			
			
			
			
			 

		}
		
		
		


		
	}
	
	
	

		
	


	
	
	$(".slideshow-thumb-pic").eq(currentPic).addClass('thumb-selected');
	caption = $('.thumb-selected').attr("title");
	
	if (caption != "") {
	  $(".slideshow-photo-caption").show();
	} else {
	  $(".slideshow-photo-caption").hide();  
	}
	
	
	$(".thumb-next").click(function(e) {
		e.preventDefault();
		next();
	});
	
	$(".thumb-prev").click(function (e) {
		e.preventDefault();
		prev();
	});
	

	$('.slideshow-thumb-pic').click(function (e) {
	  e.preventDefault();
	  if ($('#nav').is(':visible')) {
		$('.slideshow-thumb-container:first').fadeTo(50,1);
		$('#container').fadeTo(150,1);
		$('#nav').hide();
		navOpen = false;
		$('#special').css('background-image','none');
	  } 
	  currentPic = $(".slideshow-thumb-pic").index(this);
	  $('.thumb-selected').removeClass('thumb-selected');
	  $(".slideshow-photo-item").hide();
	  $(".slideshow-thumb-pic:eq("+currentPic+")").addClass('thumb-selected');
	  $(".slideshow-photo-item").eq(currentPic).show();
	});
	
	
	$('.slideshow-thumb-pic').mouseover(function(e) {
	  e.preventDefault();
	  if ($('#nav').is(':visible')) {
		$('.slideshow-thumb-container:first').fadeTo(50,1);
		$('#container').fadeTo(150,1);
		$('#nav').hide();
		navOpen = false;
		$('#special').css('background-image','none');
	  }
	  currentPic = $(".slideshow-thumb-pic").index(this);
	  $('.thumb-selected').removeClass('thumb-selected');
	  $(".slideshow-photo-item").hide();
	  $(".slideshow-thumb-pic:eq("+currentPic+")").addClass('thumb-selected');
	  $(".slideshow-photo-item").eq(currentPic).fadeTo(150,1);
	});
	


	
	function next() {
	  if ($('#nav').is(':visible')) {
		$('.slideshow-thumb-container:first').fadeTo(50,1);
		$('#container').fadeTo(400,1);
		$('#nav').hide();
		navOpen = false;
		$('#special').css('background-image','none');
	  } else {
		$('.thumb-selected').removeClass('thumb-selected');
		currentPic++;                                                             
		if (currentPic == $(".slideshow-thumb-pic").length) {          
			currentPic = 0;
			$(".slideshow-thumb-pic").first().addClass('thumb-selected');
		}		
		$(".slideshow-photo-item").hide();
		$(".slideshow-thumb-pic:eq("+currentPic+")").addClass('thumb-selected');
		$(".slideshow-photo-item").eq(currentPic).fadeIn(150);
	  }
	}
	
		
	
	function prev() {
	  $('.thumb-selected').removeClass('thumb-selected');
	  currentPic--;                                                             
	  if (currentPic < 0) {          
		  currentPic = $(".slideshow-thumb-pic").length - 1;
		  $(".slideshow-thumb-pic").last().addClass('thumb-selected');
	  }		
	  $(".slideshow-photo-item").hide();
	  $(".slideshow-thumb-pic:eq("+currentPic+")").addClass('thumb-selected');
	  $(".slideshow-photo-item").eq(currentPic).fadeIn(150);
	  if ($('#nav').is(':visible')) {
		$('.slideshow-thumb-container:first').fadeTo(50,1);
		$('#container').fadeTo(150,1);
		$('#nav').hide();
		navOpen = false;
		$('#special').css('background-image','none');
	  }
	}
	
	
	$(document).keyup(function(e) {
	  if (e.keyCode == '39' || e.keyCode == '38' || e.keyCode == '37' || e.keyCode == '40' ) {
			$('#logo').removeClass('logo-over');
			$('#nav-menu').removeClass('nav-over');
			$('.slideshow-thumb-container').fadeTo(150,1);
			$('#container').fadeTo(150,1); 
			$('#special').css('background-image','none');
	  }
	  if (e.keyCode == '39') {
		 e.preventDefault();
		 next();
	   } else if (e.keyCode == '38' || e.keyCode == '40') {
		 e.preventDefault();
		 triggerMenuClick();
	   } else if (e.keyCode == '37') {
		 e.preventDefault();
		 prev();
	   } 
	});

	
	$('.nav-link-tier1').click(function(e) {
		e.preventDefault();
	});
	
	

	$('#header').mouseleave(function(e) {
	  navOpen = false;
	  hideNav();
	});



	
	$('#nav-menu').click(function(e) {
		 e.preventDefault();
		 triggerMenuClick();
	});
	

	
	function triggerMenuClick() {
	  $('#nav').css('left',( ($('#header').width() / 2 ) - ($('#nav').outerWidth()/2) ) ); 
		$('#nav').fadeToggle('fast', function() {
		  if ($('#nav').is(':visible')) {
			$('#logo').addClass('logo-over');
			$('#nav-menu').addClass('nav-over');
			$('#logo').fadeTo(500,1);
			navOpen = true;
			$('#container').fadeTo(350,0.25);
			$('#container').css("z-index",30);
			$('.slideshow-thumb-container').hide();
			$('#special').css('background-image','url(http://static.adam8.com/img/arrow-up.png)');
		  } else { 
			$('#logo').removeClass('logo-over');
			$('#nav-menu').removeClass('nav-over');
			navOpen = false;
			$('.slideshow-thumb-container').fadeTo(150,1);
			$('#container').fadeTo(150,1);
			$('#container').css("z-index",32);  
			$('#special').css('background-image','none');
		  }
	  });
	}
	
	
	
	$('.slideshow-photo-item').click(function(e) {
		 e.preventDefault();
		 $('#logo').removeClass('logo-over');
		 $('#nav-menu').removeClass('nav-over');
		 if ( $('.home').length == 0 ) {
			 next();
		 } else {
			 triggerMenuClick();
		 }
	});
	
	
	
	
	
	
	  $(".nav-tier2").mouseover(function(e) {
		 //var $thisLink = $(this);
		 e.preventDefault();
		 //$(".icon-container").hide();
		 $(".icon-container").hide();
		 jQuery(this).find(".icon-container").show()
		 navOpen = true;
		 /*
		  $('#galleryicons').remove();
		  $('#header').prepend('<div id="galleryicons"></div>');
		  $('#galleryicons').load( $(this).attr('href') + 'gallery-icons/?g=1', function() {
			if ($('#nav').is(':visible')) {
				var p = $("#nav");
				var position = p.position();														 
				$('#galleryicons').css('left', position.left + $('#nav').outerWidth() );
				var topPosition = $thisLink.position();
				$('#galleryicons').css('top', topPosition.top + 67 + "px");
				$('#galleryicons').fadeIn();
				$('#galleryicons').css("background-image","none");
				$('#galleryicons').css("background-color","#000");
			}
		  });
		  */  
       });
	  
	  $(".nav-tier2").mouseleave(function(e) {
			//jQuery(this).find(".icon-container").hide();		
			navOpen = false;
			//hideNav();
		});
	  
	  $(".icon-container").mouseover(function(e) {
				navOpen = true;
		});
	  $(".icon-container").mouseleave(function(e) {
				navOpen = false;
				hideNav();
		});
	
	
	
	function hideNav() {
		setTimeout( function() {
			if (navOpen == false) {	  
				if ($('#nav').is(':visible')) {
					navOpen = false;
					$(".icon-container").hide();
					$('.slideshow-thumb-container:first').fadeTo(150,1);
					$('#container').fadeTo(150,1);				
					
					$('#nav').fadeOut();
					//$('#galleryicons').remove();
					
					$('#container').css("z-index",32); 
					
					$('#logo').removeClass('logo-over');
					$('#nav-menu').removeClass('nav-over');
					$('#special').css('background-image','none');
				}		  
			}
		}, 1000 );
	}
	  
	
	
	
	

	

	
	if ($('body.portraits').length || $('body.unit-work').length) {
	  
	  if (!$.cookie('doane_cookie')) {
		$('#page-container').append('<div id="disclaimer">The following images appear courtesy of 20th Century Fox, Warner Brothers, Miramax,  Buena Vista Pictures,  Dreamworks SKG, Hallmark, New Line Cinema, Summit Entertainment and Doane Gregory Photography. <a href="#">ACCEPT<a></div>');
	  
	  $('#disclaimer a').click(function(e) {
		e.preventDefault();
		$('#disclaimer').fadeOut('fast');
		$.cookie('doane_cookie', 'yep', { expires: 14, domain: 'www.doanegregory.com' });
	  });
	  
	  }
	}
	
	
	
		
	
	
		
	$(window).resize(function() {
	  resizePic();
	});
	
	resizePic();
	
	
	
	
	if (!$.cookie('doane_dimension')) {
		resizePic();
	} else {
		new_dimension = $.cookie('doane_dimension');

		new_height = $('#container').height() - 100;
		new_width = $('#container').width() - 100;

		$('.slideshow-photo').css({'max-height': new_height});
		$('.slideshow-photo').css({'max-width': new_width});
			
	}
	
	
	$(this).bind("contextmenu", function(e) {
		e.preventDefault();
    });

	

})
