	
	
	
	// CND Image Slider
	//////////////////////////////////////////////////////////////////////////////////////
	
	$(document).ready(function() {
	
	
			
		var cndSlider = $("#fader .slides"), // DIV that contains slider Images
         cndText = $("#fader .testimonial .body"), // DIV that contains slider Images
         cndPanel = $("#fader .panel"), // DIV that contains slider Images
         prev = $(".prev"),
         next = $(".next"),
         delay = 10000, // Delay between automatic advancement
         cndCnt = 0, // Initialize Counter
         cndVisible = 0, // Default visible image (index)
         interval = null;
		  
		  
		function onChange() {

         var profileLink = $(".profileLink", cndPanel),
             roomsLink = $(".roomsLink", cndPanel),
             roomsNum = $(".rooms .roomsLink", cndPanel),
             roomsFull = $(".roomsFull", cndPanel);
             
         var pageType = $("#pageType").val();
             
         var h1 = $("h1", cndText),
             p = $("p", cndText);
             
         var img = $("img", cndSlider).eq(cndVisible),
             title = img.attr("data-h1"),
             testimonial = img.attr("data-testimonial"),
             bedrooms = img.attr("data-bedrooms"),
             url = img.attr("data-url");
         
         h1.html(title);
         p.html(testimonial);
         
         roomsNum.html(bedrooms);
         profileLink.attr("href", url);
         
         var roomsURL = "/on_campus/index.php?type=2&b=bedroom_" + bedrooms + "&bedrooms=" + bedrooms;
         
         roomsLink.attr("href", roomsURL);
         
         var bd = "ONE";
         if(bedrooms == "2") { bd = "TWO"; }
         if(bedrooms == "3") { bd = "THREE"; }
         if(bedrooms == "4") { bd = "FOUR"; }
         
         roomsFull.html(bd);
             
      
		}    

								  
			  
		function nextPic(d) {
			
         var orig = cndVisible;
    
         // If we've reached the end, wrap back to the beginning
         if(d == 'r') { 
         
            if((cndVisible - 1) < 0) { var feq = (cndCnt - 1); } else { var feq = (cndVisible - 1); }
            
            
         } else {  
         
            if((cndVisible + 1) == cndCnt) { var feq = 0; } else { var feq = (cndVisible + 1); }
         }
         
         cndVisible = feq; // Set index of new visible image
        
         // Bring in next image
         
         $("img", cndSlider).hide();
         
         $("img", cndSlider).eq(orig).css("z-index", "20").show();  
         $("img", cndSlider).eq(feq).css("z-index", "35").fadeIn();
                         
            

               onChange(); // Refresh information panel              
										   
		}    
			  
			  
		  
		// Count Slider Images
		$("img", cndSlider).each(function(i,v) {
			  
			cndCnt++;
			  
			$(this).css("z-index", 25 - i).show();
			  
		});
		  
		  

						  
		if(cndCnt > 1) {  // Only setup timer if there are multiple images loaded
			  
		  
			interval = setInterval(nextPic, delay); // Set timer
			onChange();  // Initialize information panel
			  
		} else {
         onChange();
      }
		
		
      
      next.click(function() {
      
         clearInterval(interval);
         nextPic('f');
         interval = setInterval(nextPic, delay); 
      
      });
      
      prev.click(function() {
      
         clearInterval(interval);
         nextPic('r');
         interval = setInterval(nextPic, delay); 
      
      });
		  
		 
		// This will stop the slideshow when the window/tab is not focused, 
		// and then it will start again when the window/tab is focused.
		  
		var blurred = false; 
			
		$(window).focus(function(){
		  
			if(blurred) {
				setTimeout(function() { nextPic(); }, delay);
			}
			  
		});
		  
		$(window).blur(function(){
		  
			 blurred = true;
			 clearInterval(interval);
			   
		});
			
			
	
	
	});
	
	
