jQuery(function(){
	Shadowbox.init({
		skipSetup: true // skip the automatic setup
	});
	
	function showCategoryPhoto(strCat) {
		var strPhotoPath = PATH_PHOTOS + "index_" + strCat + ".jpg";

		$('#catImage').attr("src", strPhotoPath);
	}

	function trackPage(oE) {
		var aryHref = oE.content.split("/");
		var strFileNa = aryHref[aryHref.length - 1];
		var strFileBaseNa = strFileNa.split(".")[0];
		//alert(strFileBaseNa);

		if (typeof(CC_GA) != "undefined") {
			CC_GA.trackPage('/' + strFileBaseNa);
		} else {
			//alert(strFileBaseNa);
		}
	}
	
	/***** Photo Category events *****/
	$('#photoCategory a').hover(
		function() {
			var strCat = $(this).attr("data-cat");
			var strToImg = PATH_PHOTOS + "index_" + strCat + ".jpg";

			if ($("#catImage").attr("src") == strToImg) {
				//alert("noChange");
				return false;
			}

			showCategoryPhoto(strCat);
			
			// change the link href as well
			$("#catImage")
				.parent("a")
				.attr("href", $(this).attr("href"));
		},
		function() {
		}
	);
	
	// Shadowbox.setup($('#tblRecentPhotos a, #container_projects a'), options);
	if ($('#tblRecentPhotos').length) {
		// add events to the recent images
		$('#tblRecentPhotos img').hover(
			function() {$(this).addClass("imgOver");}
		,	function() {$(this).removeClass("imgOver");}
		);

		var options = {
		        handleOversize:     'resize',
		        displayNav:         true,
		        handleUnsupported:  'remove',
		        autoplayMovies:     false,
				gallery: 			'Recent Photos',
				title:				'',
				animSequence:'sync',
				onOpen: trackPage,
				onChange: trackPage
		    };

		Shadowbox.setup($('#tblRecentPhotos a'), options);
	}

	// setup shadowbox for thumbnails on photo album page
	if ($('#tblAlbumPhotos').length) {
		var options = {
		        handleOversize:     'resize',
		        displayNav:         true,
		        handleUnsupported:  'remove',
		        autoplayMovies:     false,
				gallery: 			'Album Photos',
//				title: 				'',
				animSequence:'sync',
				onOpen: trackPage,
				onChange: trackPage,
				slideshowDelay: 3
		    };

		Shadowbox.setup($('#tblAlbumPhotos a'), options);
	}

	
	$('ba.view_album').click(function(){
		Shadowbox.open(this);		
		return false;
	});
	
	function resizeCImg(){
		var intMaxImgW = $(window).width() * 0.9;
		var intDefW = 948;
		
		if (intMaxImgW < 948) {
			$("#catImage").css("width", intMaxImgW + "px");	
		}
	}	
	
	$(window).resize(resizeCImg);
	resizeCImg();

	

	/*
	$('#cPhotoAlbumYears .all_albums').click(function(){
		$.get('photography_viewalbums2.php',
			{yr: $(this).attr('yr')},
			function(data){
				$('#albums_container').html(data);
			}
		);
		
		return false;
	});
	*/
	
	// albumRefresh();

	/* Preloads some images */
	
	if ((typeof PHOTO_CATEGORIES) != 'undefined') {
		var aryPreloadImages = new Array();
		var strPhotoCategories = PHOTO_CATEGORIES;
		
		var aryPhotoCategories = strPhotoCategories.split(",");
		var intCatLen = aryPhotoCategories.length;
		for (var i = 0; i < intCatLen; i++) {
			aryPreloadImages[i] = new Image();
			aryPreloadImages[i].src = PATH_PHOTOS + "index_" + aryPhotoCategories[i] + ".jpg";
		}
	}
	
	// category slideshow
	var baseSlideShowSpeed = 2000;
	var slideShowSpeed = 1800;//baseSlideShowSpeed / 2;
	var slideShowTimer = setInterval(categorySlideshow, baseSlideShowSpeed);
	var slideShowPause = false;
	$("#catImage, #photoCategory > a").hover(
		function(){
			slideShowPause = true;
			clearInterval(slideShowTimer);
		},
		function() {
			// subsequent slides double the time
			slideShowPause = false;
			slideShowTimer = setInterval(categorySlideshow, baseSlideShowSpeed);
		}
	);
	
	function categorySlideshow() {
		//slideShowSpeed = 8000;
		$("#catImage").animate({opacity: 0.0}, slideShowSpeed, showNextSlide);
		//slideShowPause = true;
		clearInterval(slideShowTimer);
		
	}
	
	function showNextSlide() {
		var aryPhotoCategories = strPhotoCategories.split(",");
		var intCatLen = aryPhotoCategories.length;
		var curImgURL = $("#catImage").attr("src");
		// alert($("#catImage").attr("src"));
		var nextIndex;
		
		

		for (var i = 0; i < intCatLen; i++) {
			if (curImgURL.indexOf(aryPhotoCategories[i]) >= 0) {
				// alert("Current is" + aryPhotoCategories[i]);
				//$("#catImage").animate({opacity: 0.0}, 1000);
				nextIndex = (i < intCatLen - 1 ? (i+1) : 0);
				$("#catImage")
					.parent("a")
					.attr("href", $("#photoCategory > a").eq(nextIndex).attr("href"));
				$("#catImage").attr("src", PATH_PHOTOS + "index_" + aryPhotoCategories[nextIndex] + ".jpg");
				
				
				
				$("#catImage").animate({opacity: 1.0}, slideShowSpeed, function(){
					//slideShowTimer = setInterval(categorySlideshow, showNextSlide);
					if (!slideShowPause) {
						slideShowTimer = setInterval(categorySlideshow, baseSlideShowSpeed);
					}
					
				});
				
				//clearInterval(slideShowTimer);
				//next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
				
				
				break;
			}
		}

	}
	

});