	
/***************************************** Global Variables */
	
	var homeFeatureIndex = 1;
	var homeFeatureLastIndex = 0;
	var homeFeaturePreviousIndex = 0;
	var homeFeatureInterval;
	
	
/***************************************** Document Ready */

	$(document).ready(function() {
		
		/* 
		*	Home Page Feature - Document Ready
		*/
			
			//We're on the home page and there are at least two items to scroll through
	   		if($('.home_feature_message').length > 1)
	   		{   	
	   			//Get the position of the last item so we no when to restart autoscroll	
	   			homeFeatureLastIndex = $('.hfm_dots:first > a:last').attr('pos');
	   			
	   			//After initial rotation, set interval for infinite loop
	   			homeFeatureInterval = setInterval("autoRotate()", 8000);
	   		}
	     
		/* 
		*	AT Comparison Tool  
		*/
		
			if ($('#why_at_comparison').length)
			{ 
				var currentIndex = 0;
				var currentSlide = $('div.why_at_ct_sections div.why_at_ct_section').eq(0);
				var topBullet = $('div.why_at_ct_heading ol li').length - 1;
				
				$('div.why_at_ct_heading ol li').click(function(){
					if ($(this).hasClass('current'))
					{
						return false;
					}
					else
					{
						$('div.next').hide();
						$('div.answers').hide();			
						currentIndex = topBullet - $(this).index();
						$(currentSlide).removeClass('current_section').hide();
						currentSlide = $('div.why_at_ct_sections div.why_at_ct_section').eq(currentIndex);
						$(currentSlide).show().children('p.q').removeClass('q_small').show();
						$(currentSlide).children('p.true,p.false').hide();
						$(currentSlide).addClass('current_section');
						$('div.answers').show();			
						$('div.why_at_ct_heading ol li.current').removeClass('current');
						$(this).addClass('current');
					};			
				});
				
				$('img.answer_true').click(function()
				{
					$(currentSlide).children('p.q').addClass('q_small');
					$(currentSlide).children('p.true').show();			
					$('div.answers').hide();			
					if((currentIndex+1) < $('div.why_at_ct_heading ol li').length) $('div.next').show();
				});	
				
				$('img.answer_false').click(function()
				{
					$(currentSlide).children('p.q').addClass('q_small');
					$(currentSlide).children('p.false').show();			
					$('div.answers').hide();			
					if((currentIndex+1) < $('div.why_at_ct_heading ol li').length) $('div.next').show();
				});	
				
				$('img.answer_next').click(function()
				{
					$('div.next').hide();
					currentIndex++;
					$(currentSlide).hide().removeClass('current_section');
					currentSlide = $('div.why_at_ct_sections div.why_at_ct_section').eq(currentIndex);
					$(currentSlide).show().children('p.q').removeClass('q_small').show();
					$(currentSlide).addClass('current_section');
					$(currentSlide).children('p.true,p.false').hide();
					$('div.answers').show();			
					$('div.why_at_ct_heading ol li.current').prev().addClass('current');
					$('div.why_at_ct_heading ol li.current').next().removeClass('current');
				});
			
			}
	
		/* 
		* 	Show / Hide Homeownership Steps 
		*/
		
			$('.show-hide').delegate('li .toggle', 'click', function(e) 
			{
				var $selectedStep = $(this)
					.parents('.show-hide li');
				
				if($(this).hasClass('toggle-hide')) 
				{
					// Change class on button that was clicked
					$(this)
						.removeClass('toggle-hide')
						.addClass('toggle-show');
					
					// Cancel any ongoing animation and fade out div that wraps content
					$selectedStep
						.children('.wrapper')
						.stop(true, true)
						.fadeOut (0)
					
					// Change class on list item
					$selectedStep 
						.removeClass('show-item')
						.addClass('hide-item'); 
				}
				
				else if($(this).hasClass('toggle-show')) 
				{
					// Change class on button that was clicked
					$(this)
						.removeClass('toggle-show')
						.addClass('toggle-hide');						
					
					// Fade-in content
					$selectedStep
						.children('.wrapper')
						.fadeIn (1000);
					
					// Change class on list item
					$selectedStep 
						.removeClass('hide-item')
						.addClass('show-item');
				}
			});
			
		/* 
		*	User Tracking
		*/

			// Cookie is set - if it has a value then we will replace form urls with consultant specific urls
			if(readCookie('consultant') > 1) 
			{	
				var ellieId = readCookie('consultant');
				var prequalifiedURL = '/tools/elliemae/' + ellieId + '/prequalify';
				var applyNowURL = '/tools/elliemae/' + ellieId + '/applynow';

				//Set urls to consultant specific values
				if( $('#prequalify').length > 0 )
				{
					$('#prequalify').attr('href', prequalifiedURL);
				}
				
				if( $('#apply').length > 0 )
				{
					$('#apply').attr('href', applyNowURL);
				}
			} 
			
			// No cookie set - if this is a consultant landing page AND a cookie has not already been set, set a cookie to store consultants form urls
			else if(parseInt(readCookie('consultant')) !== 1)
			{	
				// Store value of #nmls if id is present (indicates we are on a consultant page).
				var ellieId = $('#consultant-detail #nmls').length > 0
					? $('#consultant-detail #nmls').attr('rel')
					: 1;

				var d = new Date();
				
				// Set expires to 3 hours from current time
				expires = d.getTime() + (1000 * 60 * 60 * 3);
				
				// Set cookie with url 
				set_cookie('consultant', ellieId, d.getTime, '/');
				
				console.log('cookie is not set');
			}
			
			//set_cookie('consultant', '', -1, '/');
	});
	
	
/***************************************** Functions */

	/* 
	*	Home page functions 
	*/ 
	
		// Bind function to buttons
		$('.hfm_dots a').live('click', function(e) 
	        {        	
	        	// Select rel attribute value from current link
	        	var selectedRel = $(this).attr('rel');
	        	
	        	// Get current selection position
	        	var selectedPos = $(this).attr('pos');
	        	
	        	// Temporarily stop auto rotate, we will start again after the rotate function has run		
	    		window.clearInterval(homeFeatureInterval);
	        	
	        	// Run rotate function
	        	rotateHomeFeature(selectedRel, selectedPos);
	        	
	        	// Restart auto rotate
	        	homeFeatureInterval = setInterval("autoRotate()", 8000);
	        	
	        	// Prevent default anchor link function
	        	e.preventDefault();    	
			}
		);
		
		// Get data and run home feature function
		function autoRotate () 
		{
			// Select rel attribute value from current link
			var rel = $('.hfm_dots:first > a').eq(homeFeatureIndex).attr('rel');
			
			// Get current selection position
	      var selectedPos = $('.hfm_dots:first > a').eq(homeFeatureIndex).attr('pos');
	        
	      //Run rotate function
			rotateHomeFeature(rel, selectedPos);
		}
		
		// Execute fade in / fade out
		function rotateHomeFeature (selection, selectedPos)
		{   
			// Assign most recent index to previous index
			homeFeaturePreviousIndex = homeFeatureIndex;
			
			//Give previous image a z-index of 5 which is above other images and below current
	    	$('#home_feature > img[pos="' + homeFeaturePreviousIndex + '"]').css('z-index', 5);
	    	
	    	//Set all other images to z-index 0
	    	$('#home_feature > img').not($('#home_feature > img[pos="' + homeFeaturePreviousIndex + '"]')).fadeOut(250).css('z-index', 0);
	    	
	    	//Set current image to top of z-index stack and fade in
	    	$('#home_feature > img[rel="' + selection + '"]').fadeIn(750).css('z-index', 10);
	
			// Associate "current" class with appropriate title / button
	    	$('.home_feature_message').removeClass('current');
	    	$('#' + selection).addClass('current');
	    	
	    	// Remove "selected" class from all dots and add "selected" class to current dot
	    	$('.hfm_dots > a').removeClass('selected');
	    	$('.hfm_dots > a[rel="' + selection + '"]').addClass('selected');
			
			// If the current index is equal to the last index restart rotation by assigning "0" to homeFeatureIndex 
		   	homeFeatureIndex = selectedPos == homeFeatureLastIndex
	    		? 0
	    		: Number(selectedPos) + 1;
		}
	
	/* 
	*	Set / get cookies
	*/

		function set_cookie(name, value, expires, path)
		{
			var cookie_string = name + "=" + escape(value);
			var expires = new Date(expires);
			
			cookie_string += "; expires=" + expires.toGMTString();			
		    cookie_string += "; path=" + escape(path);
			
			document.cookie = cookie_string;
		}
		
		function readCookie(cookieName)
		{
			var theCookie=""+document.cookie;
			var ind=theCookie.indexOf(cookieName);
			
			if (ind==-1 || cookieName=="") {
				return "";
			}
			
			var ind1=theCookie.indexOf(';',ind);
			
			if (ind1==-1) {
				ind1=theCookie.length;
			} 
			
			return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
		}




















