// Callout Carousel Function
(function () {
	$.fn.infiniteCarousel = function () {
		function repeat(str, n) {
			return new Array( n + 1 ).join(str);
		}
		return this.each(function () {
			var $wrapper = $('> .wrapper', this).css('overflow', 'hidden'),
				$slider = $wrapper.find('> div').width(9999),
				$items = $slider.find('> .section'),
				$single = $items.filter(':first')
	
				singleWidth = 738,
				visible = Math.ceil($wrapper.innerWidth() / singleWidth),
				currentPage = 1,
				pages = Math.ceil($items.length / visible);
	
			/* TASKS */
	
			// 3. reset scroll
			$wrapper.scrollLeft(0);
	
			// 4. paging function
			
			var pageNav = '<ul class="carousel-nav"><li><a href="#" class="arrow back">Previous</a></li>';
			for(var i=1; i <= pages; i++) {
				if(i==1) {
					pageNav += '<li class="pagination"><a href="#' + i + '" class="active">Page ' + i + '</a></li>';
				} else {
					pageNav += '<li class="pagination"><a href="#' + i + '">Page ' + i + '</a></li>';
				}
			}
			pageNav += '<li><a href="#" class="arrow forward">Next</a></li></ul>';
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir/* * visible*/ * n;
				if (page > pages) {
					$wrapper.animate({
						scrollLeft : '-=' + (singleWidth * pages)
					}, 1000);
					page = 1;
					currentPage = page;
				} else if (page == 0) {
					page = pages;
					$wrapper.animate({
						scrollLeft : '+=' + (singleWidth * (pages-1))
					}, 1000);
					currentPage = page;
				} else {
					$wrapper.filter(':not(:animated)').animate({
						scrollLeft : '+=' + left
					}, 500);
					currentPage = page;
				}
				$(".pagination .active").removeClass("active");
				var allATags	= $(".pagination a"),
				    dotPageURL	= [];
				$.each(allATags,function(i,e) {
					var href = $(e).attr("href");
					if(href.split("#")[1] == page) {
						$(e).addClass("active");
					};
				});
			}
			// 5. insert the back and forward link
			$wrapper.after(pageNav);
	
			// 6. bind the back and forward links
			$('a.back', this).click(function () {
				gotoPage(currentPage - 1);
				return false;
			});
	
			$('a.forward', this).click(function () {
				gotoPage(currentPage + 1);
				return false;
			});
			$('.pagination a').click(function () {
				var dotPage = $(this).attr("href");
				dotPage = dotPage.substring(dotPage.indexOf('#'));
				dotPage = dotPage.substring(1);
				gotoPage(dotPage);
			});
	
			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
	
			// THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
			$(this).bind('next', function () {
				gotoPage(currentPage + 1);
			});
		});
	};
})(jQuery);

/* Use SWFObject to open the different video file sizes */
function openVideo(vidFile) {
	// For Testimonials...
	if ($("#videos").hasClass("testimonials")) {
		$("#videos").flash({
			swf: "FLVPlayer_Progressive.swf",
			width: 640,
			height: 360,
			wmode: "transparent",
			flashvars: {
				MM_ComponentVersion: 1,
				skinName: "Clear_Skin_3",
				streamName: "assets/flash/"+vidFile,
				autoPlay: true,
				autoRewind: false
			}
		});
	}
	// For In the Media...
	if ($("#videos").hasClass("media")) {
		$("#videos").flash({
			swf: "FLVPlayer_Progressive.swf",
			width: 328,
			height: 240,
			wmode: "transparent",
			flashvars: {
				MM_ComponentVersion: 1,
				skinName: "Clear_Skin_3",
				streamName: "assets/flash/"+vidFile,
				autoPlay: true,
				autoRewind: false
			}
		});
	}
	// For Office Tour...
	if ($("#office")) {
		$("#office").flash({
			swf: "FLVPlayer_Progressive.swf",
			width: 328,
			height: 240,
			wmode: "transparent",
			flashvars: {
				MM_ComponentVersion: 1,
				skinName: "Clear_Skin_3",
				streamName: "assets/flash/"+vidFile,
				autoPlay: true,
				autoRewind: false
			}
		});
	}
}
$(document).ready(function() {
	/* Create Flash Objects using jQuery SWFObject: */
	
	// If there's a video list...
	if ($("#videos").length && $(".video-links").length) {
		var firstVid = $(".video-links li:first-child").find("a").attr("href").substr(1);
		openVideo(firstVid);
		$(".video-links a").each(function() {
			$(this).mouseup(function() {
				var vidFile = $(this).attr("href").substr(1);
				openVideo(vidFile);
				return false;
			});
		});
	}
	// If the #office DIV exists...
	if ($("#office").length) {
		openVideo("office-tour");
	}
	// If the #gallery DIV exists...
	if ($("#gallery").length) {
		$("#gallery").flash({
			swf: "assets/flash/gallery.swf",
			width: 325,
			height: 450,
			wmode: "transparent"
		});
	}

	// If you click anywhere in the callout carousel, take them to the link
	$("#carousel .section:not(.doctor)").click(function(){
		window.location=$(this).find("p:not(.callout)").find("a").attr("href");
	});
});
/* jQuery Carousel Scrolling Div */
$(function() {
	var ele   = $('#scroll');
	var speed = 45, scroll = 5, scrolling;
	
	$('#scroll-up').mouseenter(function() {
		// Scroll the element up
		scrolling = window.setInterval(function() {
			ele.scrollTop( ele.scrollTop() - scroll );
		}, speed);
	});
	
	$('#scroll-down').mouseenter(function() {
		// Scroll the element down
		scrolling = window.setInterval(function() {
		ele.scrollTop( ele.scrollTop() + scroll );
		}, speed);
	});
	
	$('#scroll-up, #scroll-down').bind({
		click: function(e) {
			// Prevent the default click action
			e.preventDefault();
		},
		mouseleave: function() {
			if (scrolling) {
				window.clearInterval(scrolling);
				scrolling = false;
			}
		}
	});
});
/* AFTER the window has loaded, initialize the carousel */
$(window).load(function() {
	// Callout Carousel Initializaing
	var autoscrolling = true;
	$('#carousel').infiniteCarousel().mouseover(function () {
		autoscrolling = false;
	}).mouseout(function () {
		autoscrolling = true;
	});
	setInterval(function () {
		if (autoscrolling) {
			$('#carousel').trigger('next');
		}
	}, 10000);
});

