(function($){
	$.fn.carouselize=function(options){
		var options=$.extend({
			_animate:true,
			duration:5000,
			_animating:false
		},options||{});
		
		return this.each(function(){
			var __parent=$(this);
			var __images=$("#CarouselImages img",__parent);
			var __descs=$("#CarouselDescription p",__parent);
			var __descHolder=$("#CarouselDescription",__parent);
			var __nextButton=$("#CarouselPrevNext a.next",__parent);
			var __prevButton=$("#CarouselPrevNext a.prev",__parent);
			
			var __max=0;
			var __current=0;
			__images.each(function(i){
				$(this).attr("rel",i);
				__max=i;
			});
			__descs.each(function(i){
				$(this).attr("rel",i);
			});
			
			__parent.bind("mouseenter",function(){
				options._animate=false;
			}).bind("mouseleave",function(){
				options._animate=true;
			});
			__nextButton.bind("click",function(){
				next();
				return false;
			})
			__prevButton.bind("click",function(){
				prev();
				return false;
			})
			
			setInterval(function(){
				if(options._animate){
					next();
				}
			},options.duration);
			
			function next(){
				animate("next");
			}
			
			function prev(){
				animate("prev");
			}
			
			function animate(type){
				if(options._animating==true){
					return false;
				}else{
					options._animating=true;
					var __animate=0;
					if(type=="next"){
						if(__current==__max){
							__animate=0;
						}else{
							__animate=__current+1;
						}
					}else if(type=="prev"){
						if(__current==0){
							__animate=__max;
						}else{
							__animate=__current-1;
						}
					}
					var __lastImage=__images.filter(":visible");
					var __lastdesc=__descs.filter(":visible");
					__descHolder.slideUp("slow",function(){
						$(__images[__animate]).fadeIn("slow",function(){
							__lastImage.fadeOut("slow");
							__lastdesc.hide();
							$(__descs[__animate]).show();
							__descHolder.slideDown("slow",function(){
								__current=__animate;
								options._animating=false;
							});
						});
					})						
				}
				
			}
			
		});
		
		
		
		
	}
})(jQuery)


$(document).ready(function() {
	$('#header ul li').ifixpng();
	$('.rightPaneHeader,.rightPaneBottom').ifixpng();
	$('#content .contact,#content .contact div,#CarouselPrevNext img').ifixpng();
	$('#CarouselDescription').css({opacity: '0.7'});
	
	$('#menuHolder ul li').hover(
	function(){
		$(this).addClass("hovering");
	},
	function(){
		$(this).removeClass("hovering");
	}
	);
	
	$('#homeCarousel').carouselize();
});

