//adjust popular photos widget to proper height based on photos inside of it
$(window).load(
  function() {
		initHeight();
});

function initHeight() {
	var largestHeight = 0;
	$(".photos-collection.current").children().children().each(function(i,el) {
		if(parseInt($(this).height()) > largestHeight) {
			largestHeight = parseInt($(this).height());
		}
	});
	$("#browsable").animate({'height': (largestHeight+20) + 'px'}, 300);
	if($.browser.msie && parseInt($.browser.version) < 9) {
		$("#browsable").parent().animate({'height': (largestHeight+50) + 'px'}, 300);
	}
}

//jquery goodness
$(document).ready(function() {
	// initialize scrollable together with the navigator plugin
	$("#browsable").scrollable().navigator();	

	$(".navi a").unbind(); 
	
	//resize pop photos on next
	$("a.next.browse").click(function() {
		var largestHeight = 0;
		$($(".photos-collection.current").removeClass("current").next()[0]).addClass("current").children().children().each(function(i,el) {
			if(parseInt($(this).height()) > largestHeight) {
				largestHeight = parseInt($(this).height());
			}
		});
		$("#browsable").animate({'height': (largestHeight+20) + 'px'}, 300);
		if($.browser.msie && parseInt($.browser.version) < 9) {
			$("#browsable").parent().animate({'height': (largestHeight+50) + 'px'}, 300);
		}
		

	});
	
	//resize pop photos on prev
	$("a.prev.browse").click(function() {
		var largestHeight = 0;
		$($(".photos-collection.current").removeClass("current").prev()[0]).addClass("current").children().children().each(function(i,el) {
			if(parseInt($(this).height()) > largestHeight) {
				largestHeight = parseInt($(this).height());
			}
		});
		$("#browsable").animate({'height': (largestHeight+20) + 'px'}, 300);
		if($.browser.msie && parseInt($.browser.version) < 9) {
			$("#browsable").parent().animate({'height': (largestHeight+50) + 'px'}, 300);
		}
	});
	
	//setTimeout("initHeight()", 10);
});


//homepage sideshow
function slideSwitch() {
    var $active = $('#slideshow .testimage.active');
 
    if ( $active.length == 0 ) $active = $('#slideshow .testimage:last');
 
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow .testimage:first');
 
    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
 
    $active.addClass('last-active');
 
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
 
//more homepage slideshow
$(function() {
    setInterval( "slideSwitch()", 5000 );
});
