﻿
// Author:  James Crain
// Date:  04/23/2011
// Purpose:  this script is meant to act as the code behind script for the Regular Pages (i.e. Buy and Rent pages, or
//           rental-page.php and buy-page.php).

jQuery(document).ready(function($) {

	var fhSlideShowTimer = 0;
	var fhNavClicked = 1;
	
	CycleNewsSlides();
	
	InitializeTheSlideShow();
	function InitializeTheSlideShow() {
	
		// JC_04/25/2011:  calculating the number of list elements (li), multiplying that
		//                 by their width (25px each) to get the total list (ul) width.
		//                 We need to do this so that it can stay centered.
		var noOfLi = $('div#page-news-nav ul li').length;
		if (noOfLi > 3){
			var theWidth = parseInt(noOfLi) * 25;
			$('div#page-news-nav ul').css('width', theWidth);
		}
	
		fhSlideShowTimer  = setInterval(function () {
			CycleNewsSlides();
		}, 5000);
		
	} //end function
	
	
	function StopSlideShow() {
		if (fhSlideShowTimer  > 0)
		{
			clearInterval(fhSlideShowTimer);
		}
	} //end function
	
	
	function CycleNewsSlides() {
	
		var theSlideNo = 0;
		var goToSlide = 0;
	
		if($('div.news-slide.active').length > 0) {
			var theCurSlide = $('div.news-slide.active').attr('id');
			var theSlides = theCurSlide.split("-");
			var theTotalNoSlides = $('div.news-slide').length;
			
			theSlideNo = parseInt(theSlides[1]);
			goToSlide = theSlideNo ;
			
			if ((theSlideNo + 1) <= theTotalNoSlides) {
			goToSlide = theSlideNo + 1;
			}
			else {
				goToSlide = 1;
			}
		}	
		else {
			goToSlide = 1;
		}
		
		
		if (goToSlide != theSlideNo) {
			
			SetActiveSlide(goToSlide);
		}
		
	} //end function
	
	
	// [TODO JC_04/23/2011]:  we could probably do a little bit more to clean this function up.  The logic in the 'else' statement
	//                        is actually being duplicated.
	function SetActiveSlide(slideNo) {
		var newSlide = "slide-" + slideNo;
			
		if($('div.news-slide.active').length > 0) {
			$('div#page-news-nav a.active').removeClass('active');
			$('div#page-news-nav a[id=' + newSlide + ']').addClass('active');
				
			$('div.news-slide.active').fadeOut('fast', function () {
				$(this).removeClass('active');
				$('div.news-slide[id=' + newSlide + ']').fadeIn('fast', function () {
					$(this).addClass('active');
					fhNavClicked = 0;
				});
			});
		}
		else {
			$('div#page-news-nav a[id=' + newSlide + ']').addClass('active');
			$('div.news-slide[id=' + newSlide + ']').fadeIn('fast', function () {
				$(this).addClass('active');
				fhNavClicked = 0;
			});
		}
		
	} //end function


	// JC_04/213/2011: remember that if I put any kind of an HREF tag on this Anchor, I need to return false here.
	$('div#page-news-nav a').live('click', function () {
		if (!$(this).hasClass('active')){
		
			if (fhNavClicked == 0){
			
				fhNavClicked = 1;
			
				StopSlideShow();
			
				var theSlides = $(this).attr('id').split("-");
				SetActiveSlide(parseInt(theSlides[1]));
			
			}
		}
	}); //end function
	
	
	// [TODO JC_04/23/2011]:  a lot of the beginning code in this function is duplicated in the CycleNewsSlides function up top.
	//                        I need to clean that up and put it in it's own function.  Perhaps even try to combine the two.
	$('div#page-news-inner-nav a').live('click', function () {
		var theCurSlide = $('div.news-slide.active').attr('id');
		var theSlides = theCurSlide.split("-");
		var theSlideNo = parseInt(theSlides[1]);
		var goToSlide = theSlideNo ;
		var theTotalNoSlides = $('div.news-slide').length;
		
		StopSlideShow();
		
		if ($(this).hasClass('slide-forward')) {
			if ((theSlideNo + 1) <= theTotalNoSlides) {
				goToSlide = theSlideNo + 1;
			}
			else {
				goToSlide = 1;
			}
		}
		else {
			if ((theSlideNo - 1) >= 1) {
				goToSlide = theSlideNo - 1;
			}
			else {
				goToSlide = theTotalNoSlides;
			}
		}
		
		if (goToSlide != theSlideNo) {
			
			SetActiveSlide(goToSlide);
		}
	}); //end function	
	
	
	// JC_04/23/2011:  remember that if that starts to act weird, we may need to wire it up to the 'live' event
	//                 handler first.
	$('div#page-news-cont').hover(
		function () {
			$('div#page-news-inner-nav').fadeIn('fast');
		}, 
		function () {
			$('div#page-news-inner-nav').fadeOut('fast');
		}
	); //end function	
	

}); //end document



