var cd_slideshowTimer;

(function($) {
	$.fn.cd_slideshow = function(interval){
		interval = interval || 4000; // default 4s
	    return this.each(function(){
	    	var $this = $(this);
	    	var $items = $this.find('img');
	    	var speed = 800;
	    	
	    	// Don't create a slideshow if there is only one side
	    	if($items.length <= 1) return;
	    	
	    	// Create the .slideTo() function
	    	$this.bind('slideTo', function(event, index) {
	    		index = index || 0;
				if(index == $items.length) index = 0;
	    		
	    		if($items.eq(index).hasClass('current').length) {
	    			return false;
    			}
	    		$items.eq(index).addClass('next');

	    		if(!$items.filter(':animated').length) {
					$items.filter('.current').animate({opacity:0}, speed, function() {
		    			$items.attr('style', '');
		    			$(this).removeClass('current');
		    			$items.filter('.next').removeClass('next').addClass('current');
		    		});
	    		}
	    	}); //slideTo
	    	
	    	$this.bind('nextSlide', function() {
    			var $next = $items.filter('.current').next();
	    		if($next.length < 1) {
	    			$next = $items.first();
	    		}

	    		$this.trigger('slideTo', $next.index());
	    	}); //nextSlide
	    	
	    	$this.bind('resetTimer', function() {
	    		cd_slideshowTimer = setInterval(function() { $this.trigger('nextSlide'); }, interval);	
	    	}); //resetTimer
	    	
	    	//init
	    	$this.trigger('resetTimer');
	    });	
	};
	
	
    // Creating custom :external selector
    $.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/)
                && (obj.hostname != location.hostname);
    };
    $('a:external').attr('target', '_blank').addClass("external");

	/*
	$('.slideshow').cycle({
		fx: 'fade',
	});
	*/
	$('.slideshow').cd_slideshow();
	
})(jQuery);
