// this is the classmate list scroller. See also slideshow2.js

var scroller;
var scrollerwhich;

function ScrollIt()
{
	var e = document.getElementById( scrollerwhich );
	var top = parseInt( e.style.top );
	if ( top < -e.offsetHeight )
		top = e.offsetParent.offsetHeight - 12;
	e.style.top = (top - 1) + "px";
}

function ClassmateScrollerInit()
{
	var div = $(".classmatescrollerholder"),
		siteid = div.find( "input[name=showsite]" ).val(),
		all = (div.find( "input[name=all]" ).val() == "1"),
		callbackurl = div.find( "input[name=callbackurl]" ).val(),
		classmatelistdestination = $("#classmatelistdestination"),
		mainid = "classmatescroller" + (all ? "all" : "");
		
	div.append( "<div id=\"" + mainid + "\">" +
				"<div id=\"classmatescrollerinner\">" +
				"<div id=\"classmatescrollermover\">" +
				"</div></div></div>" );
				
	if ( $("#callbackurl").size() == 0 )
		$("body").append( "<input type=\"hidden\" id=\"callbackurl\" value=\"" + callbackurl + "\" />" );
		
	Ajax( "scroller", "list", { siteid: siteid, all: (all ? 1 : 0) },
		function( xml )
		{
			var node = $(xml).find( "reply > classmates" ),
				pid = node.attr( "pid" ),
				s = "";
				
			node.find( "c" ).each(
				function()
				{
					var t = $(this),
						uid = t.attr( "u" ),
						name = t.attr( "n" );
						
					s += "<a href=\"../main/default.aspx?siteid=" + siteid + "&details=" + uid + "&pageid=" + pid + "\">" + name + "</a>";
				}
			);
			
			$("#classmatescrollermover").append( s );
			
			if ( classmatelistdestination.size() > 0 )
				classmatelistdestination.append( $("#" + mainid) );
			
			setTimeout( function() { Scroller( "classmatescroller" ); }, 0 );
		}
	);
}

function Scroller( which )
{
	scrollerwhich = which + "mover";
	document.getElementById( scrollerwhich ).style.top = (document.getElementById( scrollerwhich ).offsetParent.offsetHeight - 12) + "px"
 	scroller = setInterval( ScrollIt, 80 );
 	
 	if ( GetElement( which + "all" ) != null )
 		which = which + "all";
	$( "#" + which ).mouseover( function() { clearInterval( scroller ); } );
	$( "#" + which ).mouseout( function() { scroller = setInterval( ScrollIt, 80 ); } );
}

var slideshowrotator,
	maxphotoheight;
function InitSlideShow()
{
	// add click method to thumbs
	$("#slideshowthumbs").children( "div" ).not(".controller").bind("click", 
		function()
		{ 
			clearInterval( slideshowrotator );
			slideshowrotator = 0;
			SlideShowShowSlide( $(this) ); 
			
			if ( $("#pause").css( "display" ) != "none" )
			{
				$("#pause").hide();
				$("#play").show();
			}
		});
	
	// add click method to controllers
	$("#slideshowthumbs").children( "#play" ).click( function() { SlideShowPlay(); } );
	$("#slideshowthumbs").children( "#pause" ).click( function() { SlideShowPause(); } );
	
	SlideShowPlay();
}

function SlideShowPause()
{
	clearInterval( slideshowrotator );
	slideshowrotator = 0;
	$("#pause").hide();
	$("#play").show();
	return false;
}

function SlideShowPlay()
{
	clearInterval( slideshowrotator );
	slideshowrotator = 0;
	slideshowrotator = setInterval( NextSlide, 4000 );
	NextSlide();
	$("#play").hide();
	$("#pause").show();
	return false;
}

function NextSlide()
{
	var current = $("#slideshowthumbs").find( "div.current" );
	
	if ( current.next().not( ".controller" ).length > 0 )
		 SlideShowShowSlide( current.next() );
	else
		 SlideShowShowSlide( $("#slideshowthumbs").find( "div" ).eq( 0 ) );
}

function SlideShowShowSlide( div )
{
	$("#slideshowthumbs").find( "div" ).removeClass( "current" );
	div.addClass( "current" );

	$("#slideshowphoto").empty();
	$("#slideshowphoto").append( div.find( "a" ).children().clone() );


	if ( !maxphotoheight || $("#slideshowphoto img").height() > maxphotoheight )
		maxphotoheight = $("#slideshowphoto img").height() + $("#slideshowphoto div.caption").height();

	$("#slideshowphoto").height( maxphotoheight + 10 );
	
	return false;
}