var interval = "";
function slideSwitch() {
    var $active = $('#slideshow img.active');
	
	//alert($("#slideshow img").length);
	
	
    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');
	
	

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			
			$("#slide-nav a").each(function(i){
				$(this).removeClass("on");
			})
			var imageNumber = $('#slideshow img.active').attr("id").split("-img-");
			//imageNumber[1] = parseInt(parseInt(imageNumber[1])+1);
			//alert(imageNumber[1]);
			$("#slide-link-"+imageNumber[1]).addClass("on");	
        });
		
	
	
}

function fadeCurrent(imageNumber)
{
	clearInterval(interval);
	 var $active = $('#slideshow img.active');
      if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');
    
	$active.addClass('last-active');
	
	$("#slide-nav a").each(function(i){
		$(this).removeClass("on");
	})
	
	$("#slide-link-"+imageNumber).addClass("on");
	
	$("#slide-img-"+imageNumber).css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {             
			$active.removeClass('active last-active');
        });	
	interval = setInterval( "slideSwitch()", 5000 );	

}

$(function() {
    interval = setInterval( "slideSwitch()", 5000 );
	setNavigation();
});

function setNavigation()
{
	
	var imageCount = $("#slideshow img").length;
	for(var i=1;i<=imageCount;i++){
		var applyClass='';
			if(i==1) {
				applyClass="on";	
			}else {
				applyClass='';
			}
   		//$('#'+d[i].id).css({left:((i-pos)*1000)});
   		$('#slide-nav').append('<a id="slide-link-'+i+'" class="'+ applyClass +'" href="#" onclick="fadeCurrent('+i+');return false;" onfocus="this.blur();">&nbsp;</a>');
  	}	
	
}

