var carouselizePage = {

	init:function(page){
		
		this.page = page;
		this.setupOuterCarousel();
		
	},
	
	setupOuterCarousel:function(){
		//
		var outer_carousel = $('div#outer-carousel');
		var outer_carousel_nav_id = [];
		var sub_nav = $('p#sub-nav').find('a');
		var selected = '';
		
		
		// set up the outer carousel navigation
		sub_nav.each(function(){
			var anchor = $(this);
		
			anchor.click(function(e){
				e.preventDefault();
				$('a.selected').removeClass();
				$(this).addClass('selected');
				selected = $(this).attr("id")
				
			});
			// store the id of each span for use by the carousel plugin
			outer_carousel_nav_id.push("#"+anchor.attr('id'));
		});// end each
		
		
	
	
		// add a carousel
		if(outer_carousel_nav_id.length > 1) {
			outer_carousel.jCarouselLite({
		        btnGo: outer_carousel_nav_id,
				visible: 1,
				afterEnd:function(a){
					if ($("div:first-child", a).hasClass("loading")){
						var my_url = "/" + carouselizePage.page + "/" + selected;
						
						$.get(my_url,
							function(data){
							a.html(data).hide().fadeIn('slow');
							// add scrolling functionality to the content
							$('#outer-carousel div.text').jScrollPane({showArrows:true});
							// now call a function to add the inner content.
							carouselizePage.setupInnerCarousel(a);	
							}
						)	
					} // end if
				} // end afterEnd
			}); // end carousel
		} else {
			$('div.carousel_nav').css({ visibility:'hidden' });
		}

		// load the first discipline on page load
		$('p#sub-nav a').eq(0).click();		
	},
	
	setupInnerCarousel:function(a){
		// create an array to store the id's of the inside carousel navigation
		var inner_carousel_nav_id=[];

		$('div.carousel_nav a', a).each(function(){
			var anchor = $(this)
			// attach an on click event to each item in the carousel navigation to toggle the selected classes
			anchor.click(function(e){
				e.preventDefault();
				$('a.picture-selected', a).removeClass();
				anchor.addClass('picture-selected');
				}); // end click
				
				// store the id of each span for use by the carousel plugin
				inner_carousel_nav_id.push("#"+anchor.attr('id'));
			}); // end each

			var myCarousel = $('div.carousel', a);
			
			// add a carousel
			if(inner_carousel_nav_id.length > 1) {
				myCarousel.jCarouselLite({
			        btnGo: inner_carousel_nav_id,
					visible: 1
				});
			} else {
				$('div.carousel_nav', a).css({ visibility:'hidden'});
			}// end if

			// setup the image for hovering to reveal details
		$('div.details').hide()

			if($("div.details").text().length > 10) {

			$('img.work').hoverIntent({
					sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
					interval: 100,   // number = milliseconds of polling interval
					over: function(){$("div.details").slideDown(750 )},  // function = onMouseOver callback (required)
					timeout: 500,   // number = milliseconds delay before onMouseOut function call
					out: function(){$("div.details").slideUp(750)}    // function = onMouseOut callback (required)
				 });
			 }// end if
	}		
}

