//Slideshow and Tab engine
//Dennis Heckman
//10-27-2009
$(document).ready(function(){
//slideshow begin
	var slideNum = 0;
	var maxNum = $('#tabs li').length-1;
	var clickInterrupt = false;
	
	$('#slideshow div:eq('+slideNum+')').css({'z-index' : ''+(maxNum+1), 'display' : 'block'});
	setInterval(switchTest, 7000);
	$('#tabs li a').bind('click', clickHandler);
	
	function switchTest(){
		var currentSlide = $('#slideshow div:eq('+slideNum+')');
		var nextSlide = (slideNum == maxNum) ? $('#slideshow div:eq(0)') : currentSlide.next('div');

		if(!clickInterrupt){
			$('#slideshow div').css('z-index', '');
			imageSwitch(currentSlide, nextSlide);
		}
		classChange(slideNum);
	}
	
	function imageSwitch(current, next){
		if(clickInterrupt){
			$('#tabs li a').unbind('click', clickHandler);
		}
		next.css('z-index', ''+maxNum);
		current.css('z-index', ''+(maxNum+1)).fadeOut('slow',function(){
			$(this).css({'z-index' : '', 'opacity' : '1', 'display' : ''});
			if(clickInterrupt){
				$('#tabs li a').bind('click', clickHandler);
			}
		});
		if(slideNum == maxNum){
			slideNum = 0;
		}else{
			slideNum++;
		}
	}
	//change tab color
	function classChange(num){
		$('#tabs li a').removeClass('current');
		$('#tabs li a:eq('+num+')').addClass('current');
	}
	
	function clickHandler(e){
			e.preventDefault();
			clickInterrupt = true;
			var clickedIndex = $('#tabs li a').index(this);
			if(slideNum !== clickedIndex){
				var previousSlide = $('#slideshow div:eq('+slideNum+')');
				var clickedSlide = $('#slideshow div:eq('+clickedIndex+')');
				imageSwitch(previousSlide, clickedSlide);
				slideNum = clickedIndex;
				classChange(slideNum);
			}
	}
//slideshow end
//tabs begin
	$('#archive a').each(function(){
		if($(this).hasClass('current')){
			var startIndex = $('#archive a').index(this);
			$('#tabContent > div').css('display','none').eq(startIndex).css('display','block');
		}
	});
	$('#archive a').click(function(e){
		e.preventDefault();
		$('#archive a').each(function(){
		   $(this).removeClass('current');
		});
		$(this).addClass('current');
		var newIndex = $('#archive a').index(this);
		$('#tabContent > div').css('display','none').eq(newIndex).css('display','block');
	});
//tabs end



// carousel
var ci=0;
 var cj= Math.ceil($('#carousel_ul li').length/2);
 while(ci<cj){
  var cr = 
Math.floor(Math.random()* $('#carousel_ul li').length);
  var 
ct = $('#carousel_ul li').eq(cr).html();
  $('#carousel_ul li').eq(cr).html($('#carousel_ul li').eq(ci).html());
  $('#carousel_ul li').eq(ci).html(ct);
  ci++;
 }
		//move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
	$('#carousel_ul li:first').before($('#carousel_ul li:last'));
	//when user clicks the image for sliding right
	$('#right_scroll img').click(function(){
	//get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '
	var item_width = $('#carousel_ul li').outerWidth() + 10;
	//calculae the new left indent of the unordered list
	var left_indent = parseInt($('#carousel_ul').css('left')) - item_width;
	//make the sliding effect using jquery's anumate function '
	$('#carousel_ul:not(:animated)').animate({'left' : left_indent},500,null,function(){
	//get the first list item and put it after the last list item (that's how the infinite effects is made) '
	$('#carousel_ul li:last').after($('#carousel_ul li:first'));
	//and get the left indent to the default -210px
	$('#carousel_ul').css({'left' : '-322px'});
	});
	});
	//when user clicks the image for sliding left
	$('#left_scroll img').click(function(){
	var item_width = $('#carousel_ul li').outerWidth() + 10;
	/* same as for sliding right except that it's current left indent + the item width (for the sliding right it's - item_width) */
	var left_indent = parseInt($('#carousel_ul').css('left')) + item_width;
	$('#carousel_ul:not(:animated)').animate({'left' : left_indent},500,null,function(){
	/* when sliding to left we are moving the last item before the first list item */
	$('#carousel_ul li:first').before($('#carousel_ul li:last'));
	/* and again, when we make that change we are setting the left indent of our unordered list to the default -210px */
	$('#carousel_ul').css({'left' : '-322px'});
	});
	});

// carousel end

});


//Randomize Banner order script
function randBanner(){
	var randomnumber=Math.floor(Math.random()*4);
	
	if(randomnumber == 0){
		//do nothing (leave in order)
	}else if (randomnumber==1){
		// switch 1st and 2nd 
		var temp = document.getElementById("banner2").innerHTML;
		document.getElementById("banner2").innerHTML = document.getElementById("banner1").innerHTML;
		document.getElementById("banner1").innerHTML = temp;
	}else if (randomnumber==2){
		//switch 1st and 3rd
		var temp = document.getElementById("banner3").innerHTML;
		document.getElementById("banner3").innerHTML = document.getElementById("banner1").innerHTML;
		document.getElementById("banner1").innerHTML = temp;
	}else if (randomnumber==3){
		//switch 1st and 4th
		var temp = document.getElementById("banner4").innerHTML;
		document.getElementById("banner4").innerHTML = document.getElementById("banner1").innerHTML;
		document.getElementById("banner1").innerHTML = temp;
	}
}
//end Banner randmoize script

