( function( $ )
{




	$(document).ready( function ()  // note: this method should only be called after all the documnet is loaded, excluding images etc.
	{
		$(document).pngFix();  //png fix
		
		var curr_case = Math.floor(Math.random() *  casestudy_body.length);
		$('#casestudy').html( casestudy_img[curr_case]+casestudy_body[curr_case] );	
		
		//// PRELOAD  banners into hidden div
		var img_count = banner_img.length;
	
		for (i=0; i < img_count; i++){
			$("#preload").append(banner_img[i])
		}
	
										
		var banner_delay = 6500; //animation delay
		
		var banner_timer_int =''; //scope our timeout intervals
		var check_img_load = '';
		
		var target_div = "a"// this is set for the intial image only. After the load we will switch the target div to b.
		var prev_target_div = "b"// this is set for the intial image only. After the load we will switch the target div to b.
		var first_pass=0;
		
		var b_count=0; //start the counter
		var banner_length=banner_img.length; //number of banners in the object
		var dir='';
		var first_pass=true;
		
		var lock_navigation=false;
		
		

		//STEP 5
		function setTheTimer(){
		lock_navigation=false;
			banner_timer(); //reset the timer
			
		}
		
		//STEP 4//
		function fade_new_and_replace(){
			
			$("#banner_"+ target_div +"_holder").fadeIn('slow', function(){ //fade in target holder
																		// alert(target_div);
			//console.log('fade in done')
			//$("#banner_a_holder").append(banner_img[b_count]); //replace the content in target a with what is in target b
			$("#banner_"+ target_div +"_holder .shifter_link a").attr('href', banner_url[b_count]); //add the  link
			
			$("#banner_"+ target_div +"_holder .shifter_text tr td h1").html( banner_head[b_count] ); //load the heading
			$("#banner_"+ target_div +"_holder .shifter_text tr td div").html( banner_paragraph[b_count] ); //load the paragraph
			
			//$("#banner_a_holder").show() // show target a
			//$("#banner_b_holder img").remove(); //replace the content in target a with what is in target b
			setTheTimer()
		});



	
		}
		
		//STEP 3
		function if_loaded(){
		//console.log('fireing')
			if($("#banner_" + target_div +"_holder").width() > 690){ //once our image is loaded, the div will have a width of 700
			clearInterval(check_img_load);
			//$("#banner_"+ target_div +"_holder").hide(); //hide the target  div
				if(first_pass!=true){ // if this is not the first time through
					$("#banner_"+ prev_target_div +"_holder").fadeOut('slow', function(){ //fade out the target a div
					$("#banner_"+ prev_target_div +"_holder img").remove(); // remove target a's content
					fade_new_and_replace();
					})
				}else{ //this is the first pass. Just set the timer and set first pass to false
				setTheTimer();
				first_pass=false;
				}
				
			} //end if
		};
		
		// STEP 2
		function load_image(){
			
			
				
				lock_navigation=true;
				if(first_pass!=true){ // if this is not the first time through
					if(dir=="f"){
						(b_count < banner_length -1 ? b_count++ : b_count=0); 
					}else if(dir=="r"){
						(b_count == 0 ? b_count = banner_length -1 : b_count--); 
					}
					(target_div=="a" ? target_div="b"  : target_div="a" ); //switch the target div
					(prev_target_div=="a" ? prev_target_div="b"  : prev_target_div="a" ); //switch the previous target div
				}
				//console.log('slide number=' + b_count);
				//console.log('target div=' + target_div);
				//console.log('prev target div=' + prev_target_div);
				dir="f"
				clearInterval(banner_timer_int); //clear out our interval if this is not the first banner
				$("#banner_"+ target_div +"_holder").append(banner_img[b_count]); //load the image
				$("#banner_"+ target_div +"_holder .shifter_link a").attr('href', banner_url[b_count]); //load the image
				$("#banner_"+ target_div +"_holder .shifter_text tr td h1").html( banner_head[b_count] ); //load the heading
				$("#banner_"+ target_div +"_holder .shifter_text tr td div").html( banner_paragraph[b_count] ); //load the paragraph
				
				if(banner_length>1){
				check_img_load=setInterval(if_loaded, 500); //start checking to see when the image is loaded
		}
		}
		
		//STEP 1
		function banner_timer(){
			banner_timer_int=setInterval(load_image, banner_delay);
		}
		
		
		$("#right_arrow").click(function(ev){
			if(lock_navigation!=true){
					clearInterval(banner_timer_int);
					ev.preventDefault();
					dir="f";
					load_image();
			}
		})
		
		$("#left_arrow").click(function(ev){
			if(lock_navigation!=true){
				clearInterval(banner_timer_int);
				ev.preventDefault();
				dir="r";
				load_image();
			}
		})
		
		//BEGIN INITIAL LOAD
		load_image();
		
	/*
	END Homepage banner rotator
	*/	
											
	});

})( jQuery );
