var LS = window.LS || {};

LS.PropertySlideShow = (function() {
	var $propertyScroller, $images, $propertyScrollNav, $propertyList, $properties, $arrows, $currentImage, $currentProperty, slideShowTimer, currentIndex;

	function changeProperty(e) {
		$currentImage.stop(true, true).animate({ opacity: 0.0 }, 600);
		$currentProperty.css({ display: 'none' });

		if (typeof e !== 'undefined') {
			e.preventDefault();
			
			var isNext = $(this).hasClass('next');

			if (isNext) {
				currentIndex = (currentIndex !== ($properties.length - 1)) ? ++currentIndex : 0;
			} else {
				currentIndex = (currentIndex !== 0) ? --currentIndex : ($properties.length - 1);
			}
		} else {
			currentIndex = (currentIndex !== ($properties.length - 1)) ? ++currentIndex : 0;
		}
		
		$currentImage = $images.eq(currentIndex);
		$currentProperty = $properties.eq(currentIndex);

		$currentImage.stop(true, true).animate({ opacity: 1.0 }, 600);
		$currentProperty.css({ display: 'block' });
	}
	
	return {
		init: function() {
			if ($('.propertyScroller').length > 0) {
				$propertyScroller = $('.propertyScroller');
				$images = $propertyScroller.find('img');
				$propertyScrollNav = $('.propertyScrollNav');
				$propertyList = $propertyScrollNav.find('.propertyList');
				$properties = $propertyList.find('li');
				$arrows = $propertyScrollNav.find('.arrows a');
				currentIndex = 0;

				$images.not(':eq(' + currentIndex + ')').css({ opacity: 0.0 });
				$properties.not(':eq(' + currentIndex + ')').css({ display: 'none' });
				
				$currentImage = $images.eq(currentIndex);
				$currentProperty = $properties.eq(currentIndex);

				$arrows.bind('click', changeProperty);

				slideShowTimer = setInterval(function() {
					changeProperty();
				}, 4000);
				
				$propertyScrollNav.hover(
					function() {
						clearInterval(slideShowTimer);
					},
					function() {
						slideShowTimer = setInterval(function() {
							changeProperty();
						}, 4000);
					}
				);
			}
		}
	}
})();

LS.PropertyRotator = (function() {
	var $propertyRotator, $images, $currentImage, rotatorTimer;

	function changeImage() {
		$currentImage.stop(true, true).animate({ opacity: 0.0 }, 600);
		$currentImage = ($currentImage.next().length) ? $currentImage.next() : $images.eq(0);
		$currentImage.stop(true, true).animate({ opacity: 1.0 }, 600);
	}
	
	return {
		init: function() {
			if ($('.propertyRotator').length > 0) {
				$propertyRotator = $('.propertyRotator');
				$images = $propertyRotator.find('img');
				
				$images.not(':first').css({ opacity: 0.0 });
				
				$currentImage = $images.eq(0);
	
				rotatorTimer = setInterval(function() {
					changeImage();
				}, 4000);
				
				$propertyRotator.hover(
					function() {
						clearInterval(rotatorTimer);
					},
					function() {
						rotatorTimer = setInterval(function() {
							changeImage();
						}, 4000);
					}
				);
			}
		}
	}
})();

$(function() {
	LS.PropertySlideShow.init();
	LS.PropertyRotator.init();

    $('.contactLink').bind('click', function(e) {
		e.preventDefault();

        $('#contact').lightbox_me({
            centered: true,
            onLoad: function(){
                $('#contact').find('input:first').focus();
            }
        });
    });
});


