$(function() {
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div");
	
	
	
	// this runs the 5 second switch from the Why You Screen to the websites screeen
	setTimeout("goScreen()", 5000);	
	getTarget();
	
	// tells the slideshows to scroll
	var wb = $("#website-content .scrollable").scrollable({size: 1}).navigator({
	navi: 'ol.navi',
	activeClass: 'current'
	}).autoscroll({autoplay: false, interval: 5000, clickable: false, api: true});
	
	// tells the slideshows to scroll
	var br = $("#brand-content .scrollable").scrollable({size: 1}).navigator({
	navi: 'ol.navi',
	activeClass: 'current'
	}).autoscroll({autoplay: false, interval: 5000, clickable: false, api: true});
	
	// tells the slideshows to scroll
	var te = $("#testimonial-content .scrollable").scrollable({size: 1}).navigator({
	navi: 'ol.navi',
	activeClass: 'current'
	}).autoscroll({autoplay: false, interval: 5000, clickable: false, api: true});

});

function getTarget(){
	var api = $("ul.tabs").tabs(); 
	api.onClick(function(){
		var activeDiv = $('div.panes > div').filter(function() {
		   return $(this).css('display') == 'block';
		}).attr("id");
		
		var wb = $("#website-content div.scrollable").scrollable();
		var br = $("#brand-content div.scrollable").scrollable();
		var te = $("#testimonial-content div.scrollable").scrollable();
		
		if(activeDiv == 'website-content')
		{
			setTimeout(function(){wb.play()},5000);
			br.stop();
			te.stop();
			var c = wb.getSize();
			var last = c - 1;
			wb.onSeek(function(){
				var i = this.getIndex();
				if (i == last)
				{
					setTimeout("nextSection()", 5000);
				}
			});
		}		
		
		if(activeDiv == 'brand-content')
		{
			setTimeout(function(){br.play()},5000);
			wb.stop();
			te.stop();
			var c = br.getSize();
			var last = c - 1;
			br.onSeek(function(){
				var i = this.getIndex();
				if (i == last)
				{
					setTimeout("nextSection()", 5000);
				}
			});
		}	
		
		if(activeDiv == 'testimonial-content')
		{
			setTimeout(function(){te.play()},5000);
			wb.stop();
			br.stop();
			var c = te.getSize();
			var last = c - 1;
			te.onSeek(function(){
				var i = this.getIndex();
				if (i == last)
				{
					setTimeout("nextSection()", 5000);
				}
			});
		}	
		
	});
}
// calls the tabs api to move to the next tab (websites)
function goScreen(){
	var api = $("ul.tabs").tabs(); 
	var i = api.getIndex();
	if(i == 0)
	{
		api.click(1);
	}
}

// calls the scrollable api for stopping the scrollers
// var tab is an arbitrary number to delineate between which slideshow we want to stop
function stopSlideshow(tab)
{
	// we need this to get the different scrollers to stop
	if(tab == 0)
	{
		// get api
		var api = $("div.scrollable").scrollable();
	}
	
	if(tab == 1)
	{
		//get api
		var api = $("#brand-content div.scrollable").scrollable();
	}
	
	if(tab == 2)
	{
		// get api
		var api = $("#testimonial-content div.scrollable").scrollable();
	}
	//stop it
	api.stop();
	//reload it so that it will work for the next scroller
	api.reload();
}

// calls the scrollable api for starting the scrollers
// var tab is an arbitrary number to delineate between which slideshow we want to start
function startSlideshow(tab)
{
	// we need this to get the different scrollers to start
	if(tab == 0)
	{
		// get api
		var api = $("div.scrollable").scrollable();
	}
	
	if(tab == 1)
	{
		// get api
		var api = $("#brand-content div.scrollable").scrollable();
	}
	
	if(tab == 2)
	{
		// get api
		var api = $("#testimonial-content div.scrollable").scrollable();
	}
	//start it
	api.play();
	//reload it so that it will work for the next scroller
	api.reload();
}

function nextSection()
{
	var api = $("ul.tabs").tabs(); 
		api.next();
}
