/* WCI Virtual Tour Experience-related javascript */



current_page = 1;
pages = [];
pageHeight = 320;	// scroll pixels
defaultPageHeight = 300;
travelDuration = 250;

$(document).ready(function() {
						   
	if (typeof(vtPageHeight) != 'undefined') {
		pageHeight = vtPageHeight;	
	}
	if (typeof(vtDefaultHeight) != 'undefined') {
		defaultPageHeight = vtDefaultHeight;
	}
	if (typeof(vtTravelDuration) != 'undefined') {
		travelDuration = vtTravelDuration;	
	}
			   
	if ($('#virtual-tours')) {

		// reset vt css to allow scrolling 
		$('#virtual-tours').css({
			height:defaultPageHeight + "px",
			overflow:"hidden"
		});	
		$('#virtual-tours-scroller').css({
			height:"2000px"								 
		});
		
		// count the virtual tours and create a page button for each group of 4
		var pagesNeeded = Math.ceil(($('#virtual-tours .virtual-tour').length / 4)) ;
		for (var i=1; i<=pagesNeeded; i++) {
				$('#virtual-tour-nav p').append(
					' <a class="tour-page-button" href="javascript:void(0);">' 
						+ i + '</a>'
				);
		}
		
		// setup page bahavior click event
		$('#virtual-tour-nav .tour-page-button:first').addClass('active-page');
		$('#virtual-tour-nav .tour-page-button').each( 
			function(i,e) {
				var pageId = "page-" + (i+1);
				pages[pageId] = i+1;
				$(e).attr("id", pageId).bind("click", 
					function() {
						return changeVTPage(this);			
					}
				);
			}
		);
		
		if (pagesNeeded > 1) {  // don't show the nav if we only have one page
			$('#virtual-tour-nav').css({
				display:"block"					   
			});
		}
		
	} // end virtual tour setup
	
	
});

// process page change selection
function changeVTPage(obj) {

	var pageKey = $(obj).attr("id");
	var page = pages[pageKey];
	
	if (typeof(page) != "undefined" && page != current_page) {
		
		var travel = (page == 1)?0:(pageHeight * (page-1))*-1;
		$("#page-" + current_page).removeClass("active-page");
		$("#page-" + page).addClass("active-page");

		current_page = page;
		
		// move the page scroller to the next position
		$('#virtual-tours-scroller').animate( 
			{ top: travel }, { queue:false, duration:travelDuration } 
		); 
	
	} 
	return false;
}


