//hoverIntent

(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

//innerfade
(function($) {

$.fn.innerfade = function(options) {

	this.each(function(){ 	
		
		var settings = {
			animationtype: 'fade',
			speed: 'normal',
			timeout: 2000,
			type: 'random',
			containerheight: 'auto',
			runningclass: 'innerfade'
		};
		
		if(options)
			$.extend(settings, options);
		
		var container = this;
		var slideCount = settings.slideContents.length;
		slideArray = new Array();
		for( var i = 0; i < settings.slideContents.length; i++ )
		{
			var tmpArray = new Array( settings.slideContents[i], 0 );
			slideArray.push( tmpArray );
		}
		
		//should contain only one element, the default which would show if no javascript was present...
		elements = $(this).children();
		
		if( slideArray.length > 1 )
		{
			$.innerfade.chainload( container, slideArray, 1 );
		}
		
	
		if( settings.slideContents.length > 1 )
		{
		
			$(container).css('position', 'relative');
	
			$(container).css('height', settings.containerheight);
			//$(container).addClass(settings.runningclass);
			
			for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				$(elements[i]).hide();
			};
		
			

				setTimeout(function(){
					$.innerfade.next( container, settings, 1, 0);
				}, settings.timeout);
				$(elements[0]).show();

		}
		
	});
};


$.innerfade = function() {}

$.innerfade.chainload = function( container, slideArray, current )
{
	if( current <= slideArray.length-1 )
	{
		$(container).append('<li class="slideImage"></li>');
		els = $(container).children();
		//append the latest slide
		$(els[els.length-1]).html( slideArray[current][0] );
		$(els[els.length-1]).hide();
		$(els[els.length-1]).append("<span></span>");
		$(els[els.length-1]).children("span").text( $(els[els.length-1]).children("img").attr("alt") );
		//if an image is found in the current slide, defer adding the next slide until this slide is loaded. Otherwise, load the next
		//so the first case will be the one fired for most slideshows.
		if( $(els[els.length-1]).find( "img" ) )
		{		
			
			$(els[els.length-1]).find( "img" ).load( function() {
				$(els[els.length-1]).show();
				$.innerfade.chainload( container, slideArray, current + 1)
				});
		}
		else
		{
			$.innerfade.chainload( container, slideArray, current + 1);
		}
	}
}
$.innerfade.next = function ( slideshowContainer, settings, current, last) {

	//get accurate list of container's children, based on # successfully loaded.
	var elements = $(slideshowContainer).children();
	for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				//$(elements[i]).hide();
				
			};

	if( elements.length > 1 )
	{
		
			
		$(elements[last]).fadeOut(settings.speed);

		$(elements[current]).fadeIn(settings.speed);
		
		/*$(elements[current]).bind("mouseenter", function(){
    	$(this).children("span").fadeIn();
    	
    }).bind("mouseleave", function(){
    	$(this).children("span").fadeOut();
    	
    });*/
		$(elements[current]).hoverIntent(
  	function(){
    	$(this).children("span").fadeIn();
    	
    },
    function(){
    	$(this).children("span").fadeOut();
    	
    }
    );
		/*$(elements[current]).hoverIntent(
			{    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 1500, // number = milliseconds for onMouseOver polling interval    
	     over: fadeCaptionIn, // function = onMouseOver callback (REQUIRED)    
	     timeout: 1500, // number = milliseconds delay before onMouseOut    
	     out: fadeCaptionOut // function = onMouseOut callback (REQUIRED)    
			}
		)*/
		function fadeCaptionIn(){  alert("here!");$(this).children("span").fadeIn();}
		function fadeCaptionOut(){ $(this).children("span").fadeOut();}



		if ( ( current + 1 ) < elements.length ) {
			current = current + 1;
			last = current - 1;
		} else {
			current = 0;
			last = elements.length - 1;
		};

		
		setTimeout((function(){$.innerfade.next(slideshowContainer, settings, current, last);}), settings.timeout);
		
	}
	else
		{
			
			setTimeout(function(){
					$.innerfade.next( slideshowContainer, settings, 1, 0);
				}, 1000);
		}
};
})(jQuery);

//the first element in this list needs to be included explicitly in the HTML, for more graceful degrading...
var contents = new Array(
'<img src="/image/front/1_davematthews.jpg" width="950" height="505" alt="Dave Matthews Band at the Bryce Jordan Center, 2005">',
'<img src="/image/front/1_veg_seller2.jpg" width="950" height="505" alt="Vegetable Seller, Campo dei Fiori, Roma, Winter 2006">',
'<img src="/image/front/2_fritz_and_sabri.jpg" width="950" height="505" alt="The Wedding of Fritz and Sabrina Westover">',
'<img src="/image/front/2_orvieto_path2.jpg" width="950" height="505" alt="Path, viewed from Orvieto, Winter 2006">',
'<img src="/image/front/3_dickey_betts.jpg" width="950" height="505" alt="Dickey Betts, State College 2006">',
'<img src="/image/front/3_bagnoreggio_cat2.jpg" width="950" height="505" alt="Cat, Bagnoreggio, Winter 2006">',
'<img src="/image/front/4_tarp2.jpg" width="950" height="634" alt="Tarp, Giardino di Boboli, Firenze, Winter 2006">',
'<img src="/image/front/5_view_from_ravello2.jpg" width="950" height="505" alt="View from Ravello, Winter 2006">',
'<img src="/image/front/6_cyclist.jpg" width="950" height="505" alt="Cyclist, Firenze, Winter 2006">'
);
var bio_contents = new Array(
'<img src="/image/bio/bio1.jpg" width="421" height="632" alt="African Immigrant Protester, Roma, Winter 2006">',
'<img src="/image/bio/bio2.jpg" width="421" height="632" alt="Blue Mask, Venezia, Carnevale, Winter 2006">',
'<img src="/image/bio/bio3.jpg" width="421" height="632" alt="Smoking Waiter, Verona, Winter 2006">',
'<img src="/image/bio/bio4.jpg" width="421" height="632" alt="View from Bagnoreggio, Winter 2006">',
'<img src="/image/bio/bio5.jpg" width="421" height="632" alt="Seen from the Spanish Steps, Roma, Winter 2006">'
);
var contact_contents = new Array(
'<img src="/image/contact/contact1.jpg" width="421" height="632" alt="Tunnel, Riomaggiore, Winter 2006">',
'<img src="/image/contact/contact2.jpg" width="421" height="632" alt="Smoker, Piazza Rotonda, Roma, Winter 2006">',
'<img src="/image/contact/contact3.jpg" width="421" height="632" alt="Conte Poleton, Carnevale, Verona, 2006">',
'<img src="/image/contact/contact4.jpg" width="421" height="632" alt="White Head, Carnevale, Venezia, 2006">',
'<img src="/image/contact/contact5.jpg" width="421" height="632" alt="Clockwatcher, Piazza San Pietro, Roma, Winter 2006">'
);
var news_contents = new Array(
'<img src="/image/news/news1.jpg" width="421" height="632" alt="San Giovanni in Laterano, Winter 2006">',
'<img src="/image/news/news2.jpg" width="421" height="632" alt="Ratto delle Sabine, Firenze, Winter 2006">',
'<img src="/image/news/news3.jpg" width="421" height="632" alt="Lamp, Corniglia, Cinque Terre, Winter 2006">',
'<img src="/image/news/news4.jpg" width="421" height="632" alt="Administrators, Basilica San Pietro, Roma, 2006">',
'<img src="/image/news/news5.jpg" width="421" height="632" alt="Pap&aacute; del Gnoco, Carnevale, Verona, Winter 2006">'
);
$(document).ready(
	function(){
		 $(document).pngFix(); 
  // Every six seconds execute the switchSlide() function
  $('ul#slideshow').innerfade(
  {
		speed: 2000,
		timeout: 8000,
		type: 'sequence',
		containerheight: '505px',
		slideContents: contents
	});
	
	$('ul#bioslideshow').innerfade(
	{
		speed: 2000,
		timeout: 8000,
		type: 'sequence',
		containerheight: '632px',
		slideContents: bio_contents
	});

	$('ul#contactslideshow').innerfade(
	{
		speed: 2000,
		timeout: 8000,
		type: 'sequence',
		containerheight: '632px',
		slideContents: contact_contents
	});
	$('ul#newsslideshow').innerfade(
	{
		speed: 2000,
		timeout: 8000,
		type: 'sequence',
		containerheight: '632px',
		slideContents: news_contents
	});
	//bind the first image, which we have placed directly into the page to degrade gracefully
	/*$('li.slideImage').bind("mouseenter", function(){
    	$(this).children("span").fadeIn();
    	
    }).bind("mouseleave", function(){
    	$(this).children("span").fadeOut();
    	
    });*/
  $('li.slideImage').hoverIntent(
  	function(){
    	$(this).children("span").fadeIn();
    	
    },
    function(){
    	$(this).children("span").fadeOut();
    	
    }
    );
    
	/*$('li.slideImage img').live("mouseover", function(  ){
    	$(this).siblings("span").fadeIn();
    	
    });
  $('li.slideImage img').live("mouseleave", function(  ){

	       $(this).siblings("span").fadeOut("slow");

    });*/
	
});
