var community_slideshow_iterator = 1;
var community_slideshow_count = 0;

$(document).ready(function() {

	// Show elements that are hidden when the visitor has Javascript disabled
	
	$('#community-photo-area').show();
	$('.arrow').css('visibility', 'visible');	
	
	// Assign javascript listeners to slideshow elements
	
	$('.slider-image').click(function(e) {
		$('.slider-image').animate({		    
		    top: '-=460'
		  }, 1000, "easeOutCirc", function() {
		    // Animation complete.
		});
	});

	$('#left-arrow').click(function(e) {
		e.preventDefault();
		show_previous_community();
	});

	$('#right-arrow').click(function(e) {
		e.preventDefault();
		show_next_community();
	});

	$("#search-dialog").dialog({
		autoOpen: false,
		/*
		height: 403,
		width: 658,
		*/
		
		
		height: 388,
		width: 648,		
		
		
		
		modal: true
	});	
	

	$("#specials-button").click(function(e) {
		e.preventDefault();
		$('#specials').show();
	});	

	$('area').click(function(e) { 
		e.preventDefault();
	    var url = $(this).attr('href'); 
	    var coords = $(this).attr('coords').split(','); 

	    if (coords == '534,1,611,37'){
	    	$('#specials').hide();
	    }

    	return false; 
	});	
	
	
	$('#area-map-tab').show();
	
	$('#area-map-tab').click(function(e){
		if (navigator.appVersion.indexOf("MSIE 7.") == -1)
		{
			e.preventDefault();
			$("#search-dialog").dialog('open');
		}
		else
		{
			window.location = "/finding-your-home";
		}
	});
	
	$('#close-button img').click(function(e){
		e.preventDefault();
		$("#search-dialog").dialog('close');
	});
	
	
	community_slideshow_size = $("#slideshow-vault > div").size();
	
	bind_slider();
	
	
	// Add map tooltips to both the small map and the area map

	$('#small-map img.map-marker').mapTips({
		tailTop:40,
		tailPosition:'left',
		zIndex:1005
	});

	/* 
	 * 
	 * This has been moved.
	 * 
	 * See /public/js/pages/area-map.js
	 * 
	 * 
	*/
	
	/*
	$('#area-map img#mountain-view').mapTips({
		tailTop:100,
		tailPosition:'left',
		zIndex:1005
	});
	
	$('#area-map img#saratoga').mapTips({
		tailTop:135,
		tailPosition:'right',
		zIndex:1005
	});		
	
	$('#area-map img#palo-alto').mapTips({
		tailTop:130,
		tailPosition:'left',
		zIndex:1005
	});	

	$('#area-map img#san-bruno').mapTips({
		tailTop:40,
		tailPosition:'left',
		zIndex:1005
	});		

	$('#area-map img#san-jose').mapTips({
		tailTop:110,
		tailPosition:'left',
		zIndex:1005
	});
	
	$('#area-map img#san-ramon').mapTips({
		tailTop:25,
		tailPosition:'left',
		zIndex:1005
	});

	$('#area-map img#santa-clara').mapTips({
		tailTop:110,
		tailPosition:'left',
		zIndex:1005
	});
	*/
});


function bind_slider() {
	
	$('#slider').cycle({
		// fx: 'scrollHorz',
		// speed: 1000,
		fx:     'fade',
		speed: 2000,
		pause: 1,
		timeout: 4000,
		delay: 0,
		pager: "#photo-pagination-buttons",
		pauseOnPagerHover: 1,
		nowrap: 1,
		end: function() {
			show_next_community();
		}
	});
}


function show_next_community() {
	
	community_slideshow_iterator ++;

	if (community_slideshow_iterator > community_slideshow_size) {
		community_slideshow_iterator = 1;
	}
	
	$('#slider').cycle('destroy');

	$('#featured-propery-canvas').fadeOut('fast', function() {

		// Swap in new community information and slideshow images
		$('#slider').html($('#slideshow-community-' + community_slideshow_iterator).html());
		$('#snapshot-placeholder').html($('#featured-snapshot-' + community_slideshow_iterator).html());
		
		$('#slider').cycle('destroy');
		
		// Re-bind the slider
		bind_slider();	

		// HACK: Remove the link to view community photos for Creekside (#4)
		if (community_slideshow_iterator == 4)
		{
			$('#community-photo-area').hide();
		}
		else
		{
			$('#community-photo-area').show();
		}
		
		// Fade in the display
		$('#featured-propery-canvas').fadeIn('fast');
		
	});
}

function show_previous_community() {
	
	community_slideshow_iterator --;

	if (community_slideshow_iterator <= 0) {
		community_slideshow_iterator = community_slideshow_size;
	}
	
	$('#slider').cycle('destroy');

	$('#featured-propery-canvas').fadeOut('fast', function() {
		
		// Swap in new community information and slideshow images
		$('#slider').html($('#slideshow-community-' + community_slideshow_iterator).html());
		$('#snapshot-placeholder').html($('#featured-snapshot-' + community_slideshow_iterator).html());
		
		bind_slider();

		$('#featured-propery-canvas').fadeIn('fast');
	});
}
