// Redirect mobile viewers
/* 
 * Temporarily removed until client pays for mobile site
 * When they do pay for the mobile site, the commented code in the
 * MobileController will need to be restored.
 * 
if (screen.width <= 699) {
	document.location = "/mobile";
}
*/

// start sidebar index
var sidebar_index =  0;
// number of slide
var number_of_slide;
// slide image width
var slide_image_width = 940;
	
// fade sidebar images
function fadeInOneByOne(myStartIndex) {
	if (sidebar_index < number_of_slide) {
		$('.side-image').eq(sidebar_index).animate({
				'opacity': 0.3
			}, 150, function() {
				$(this).css('visibility', 'visible');
				sidebar_index++;
				var statement = 'fadeInOneByOne(' + sidebar_index + ')';
				setTimeout(statement, 150);
		});
	}
}

// timeout id
var timeout_id

// slideshow play automatically
function playAutomatically() {
	var current_slide = $('#slider-pagination #sliderNav a.current').parent('li').next().find('a');
	
	if (current_slide.length) {
		current_slide.trigger('click');
	} else {
		$('#slider-pagination #sliderNav a').eq(0).trigger('click');
	}
	//timeout_id = setTimeout("playAutomatically()", 6000);
}



/******************************************/
/***   HOMEPAGE TESTIMONIAL SLIDESHOW   ***/
/******************************************/

function testimonialSlideshow() {	
	$('#testimonial #testimonial-ph').animate({
		top: '-=110'
	}, function() {
		$('#testimonial #testimonial-ph .testimonial').first().clone().appendTo('#testimonial #testimonial-ph');
		$('#testimonial #testimonial-ph .testimonial').first().remove();
		$(this).css({'top': 0});
		
		setTimeout('testimonialSlideshow()', 15000);
	});
}



$(document).ready(function() {
	
	/******************************/
	/***   HOMEPAGE SLIDESHOW   ***/
	/******************************/
	
	// start slide index
	var current_index =  0;
	number_of_slide =  $('#slider-pagination #sliderNav a').length;
	
	// fade sidebar images
	fadeInOneByOne(sidebar_index);

	// images shift left
	function shiftLeft() {
		// clone element
		$('.background-panel .middle-left li:eq(0)').clone().appendTo('.background-panel .middle-right');
		$('.background-panel .middle li:eq(0)').clone().appendTo('.background-panel .middle-left');
		$('.background-panel .middle-right li:eq(0)').clone().appendTo('.background-panel .middle');
		
		// remove element
		$('.background-panel .middle-left li:eq(0)').remove();
		$('.background-panel .middle li:eq(0)').remove();
		$('.background-panel .middle-right li:eq(0)').remove();
		
		// fade side images
		$('.side-image').fadeTo(800, 0.3);
	}
	
	// images shift right
	function shiftRight() {
		// clone element
		$('.background-panel .middle-right li:last').clone().prependTo('.background-panel .middle-left');
		$('.background-panel .middle li:last').clone().prependTo('.background-panel .middle-right');
		$('.background-panel .middle-left li:last').clone().prependTo('.background-panel .middle');
			
		// remove element
		$('.background-panel .middle-right li:eq(3)').remove();
		$('.background-panel .middle li:eq(1)').remove();
		$('.background-panel .middle-left li:eq(3)').remove();
		
		// fade side images
		$('.side-image').fadeTo(800, 0.3);
	}

	if($('#sliderNav').is(':visible')) {
		
		$('#sliderNav').tabs('#slider .panes > .pane', {
			rotate: true
		}).slideshow({
			clickable : false,
			next : '.next',
			prev : '.prev'
		});
		
		// backward nav
		$('#slider .backward').click(function(e) {
			e.preventDefault();
			
			var _temp = $('#slider-pagination #sliderNav a.current').parent().prev().find('a');
			if (_temp.length) {
				_temp.trigger('click');
			} else {
				$('#slider-pagination #sliderNav a').eq(number_of_slide - 1).trigger('click');
			}
		})
		
		// forward nav
		$('#slider .forward').click(function(e) {
			e.preventDefault();
			
			var _temp = $('#slider-pagination #sliderNav a.current').parent().next().find('a');
			if (_temp.length) {
				_temp.trigger('click');
			} else {
				$('#slider-pagination #sliderNav a').eq(0).trigger('click');
			}
			
		})
			
		// hover tooltip on pagination number
		$('#sliderNav a').hover(function(){
			// hover
			var This = $(this);
			if(!This.hasClass('current')){
				This.children('.thumbnail').css({
					left: 0
				})
			}
		},
		function(){
			var This = $(this);
			if(!This.hasClass('current')){
				This.children('.thumbnail').css({
					left: -99999
				})
			}
		}).click(function(){
			$('#sliderNav .thumbnail').css({
				left: -99999
			})
		})
		
		// click on slider pagination
		$('#slider-pagination a').click(function() {
			clearTimeout(timeout_id);
			
			var clicked_index =  $('#slider-pagination #sliderNav a.current').parent().index();
			var number_of_shift = clicked_index - current_index;
			var pos;
			
			if (clicked_index > current_index) {
				// shift slide image
				pos = number_of_shift * slide_image_width;
				$('#slide-image-placeholder').animate({'left': '-=' + pos + 'px'}, 'slow');
				
				// shift sidebar image
				for (var i = 0; i < number_of_shift; i++) {
					shiftLeft();
				}
			} else if (clicked_index < current_index) {
				number_of_shift = -1 * number_of_shift;
				
				// shift slide image
				pos = number_of_shift * slide_image_width;
				$('#slide-image-placeholder').animate({'left': '+=' + pos + 'px'}, 'slow');
				
				// shift sidebar image
				for (var i = 0; i < number_of_shift; i++) {
					shiftRight();
				}
			}
			
			current_index = clicked_index;
			
			timeout_id = setTimeout("playAutomatically()", 13000);
		});
		
		// hover on side image
		$('.side-image').live('mouseover mouseout', function(e) {
			if (e.type == 'mouseover') {
		    	$(this).fadeTo(800, 1.0);
		  	} else {
		    	$(this).fadeTo(800, 0.3);
		  	}
		});
		
		// click on side image
		$('.background-panel li a').live('click', function(e) {
			e.preventDefault();
			
			var slide_index = $(this).attr('id').split('small-promo-');
			slide_index = parseInt(slide_index[1]);
			$('#slider-pagination li:eq(' + slide_index + ') a').trigger('click');
		});
		
		setTimeout("playAutomatically()", 13000);
		
	}
	
	
	
	/***************************************/
	/***   HOMEPAGE FIND YOUR NEW HOME   ***/
	/***************************************/
	
	// initialize dropdown
	
	$('#state').selectmenu({
		style:'dropdown', 
		width: '164px',
		menuWidth: '164px'
	});
	
				
	// When the state drop-down menu changes, refresh the regions selection menu
	$('#state').change(function(e) {
		e.preventDefault();
		
		var params = 'state_seo_name=' + $(this).val();

		$.getJSON('/application/get-regions?',
			params,
			function(regions) {			
				
				$.each(regions, function(index){
					$('#region').append('<option value="' + regions[index].seo_name + '">' +  regions[index].name + '</option>');					
				});
				
				$('#region-dropdown-list').hide();
				$('#region').hide().selectmenu({
					style:'dropdown', 
					width: '164px',
					menuWidth: '164px'
				});
			}
		);
	});
	
	// go now button
	$('#find-home-homepage-form').submit(function(e) {
		e.preventDefault();
		
		var state_seo_name = $('#state').val();
		var region_seo_name = $('#region').val();
		
		if (region_seo_name) {
			var url = '/find-new-home/' + state_seo_name + '/' + region_seo_name;
			location.href = url;
		} else {
			//$('.region-ddl-error').show();
		}
	});
	
	$('#list-view-selector').click(function(e){
		
		$('#list-view-selector').fadeOut('fast');
		
		$('#map-view').fadeOut('fast', function(){
			$('#list-view').fadeIn('fast');
			$('#map-view-selector').fadeIn('fast');
			$('#find-new-home-wrapper .find-home-form').removeClass('map-background');
		});
		
	});
	
	$('#map-view-selector').click(function(e){
		
		$('#map-view-selector').fadeOut('fast');
		
		$('#list-view').fadeOut('fast', function(){
			$('#map-view').fadeIn('fast');
			$('#list-view-selector').fadeIn('fast');
			$('#find-new-home-wrapper .find-home-form').addClass('map-background');
		});
		
	});

	setTimeout('testimonialSlideshow()', 15000);
	
});

