(function(){
	var currentCommunity = $('body').attr('id');
	var communities = [
	  {
		  name: "Toscana",
		  address: "10945 North Town Center Blvd, Highland, UT 84003",
		  location: "Highland",
		  // latlng: new google.maps.LatLng(40.431053, -111.789223),
		  description: "Custom townhomes from the $160’s.  Located right next to the new “Commuter Lanes” that run from Highland to I-15.  Parade of Homes award-winner.",
		  dir: "right"
	  },{
		  name: "Rock Creek",
		  address: "3635 E. Rock Creek Rd. Eagle Mountain City UT, 84005",
		  location: "Eagle Mountain",
		  // latlng: new google.maps.LatLng(40.364216, -111.975510),
		  description: "Condos from the $110’s.	 Ideal location just minutes from the new Pioneer Crossing road and from downtown Saratoga Springs.",
		  dir: "left"
	  },{
		name: "Hillcrest",
		address: "77 West Crest Road, Saratoga Springs UT, 84005",
		location: "Saratoga Springs",
		// latlng: new google.maps.LatLng(40.368858,-111.985352),
		description: "Condos from the $120’s.  Located just off of Redwood Road in north Saratoga Springs.",
	},{
		  name: "Silverlake",
		  address: "4629 Evans Drive, Eagle Mountain, UT 84005",
		  location: "Eagle Mountain",
		  // latlng: new google.maps.LatLng(40.359753, -111.955444),
		  description: "Affordable single-family homes from the $160’s.	 Located just minutes from the new Pioneer Crossing road and from downtown Saratoga Springs.",
		  dir: "right"
	  }	,{
		name: "Pemberton Lanes",
		address: "",
		location: "American Fork",
		// latlng: new google.maps.LatLng(40.400856,-111.769684),
		description: "Custom single-family homes from the low $300’s.	 Prime location just a stone’s throw from the Mount Timpanogos LDS temple.",
		dir: "right"
	},{
		name: "Fairways at Temple View",
		address: "1346 N. 100 E. American Fork, UT 84003",
		location: "American Fork",
		// latlng: new google.maps.LatLng(40.402702,-111.792191),
		description: "Single-family homes from the low $200’s.  Nestled on a ridge overlooking Fox Hollow Golf Course and the Mount Timpanogos LDS temple.",
		dir: "left"
	},{
		name: "River Cove",
		address: "River Ridge Lane, Spanish Fork",
		// latlng: new google.maps.LatLng(40.096874,-111.667557),
		location: "Spanish Fork",
		description: "Single-family homes from the low $200’s.  Beautiful river-bottom community just minutes away from I-15.",
	},{
		name: "Pioneer's Landing", 
		  // latlng: new google.maps.LatLng(40.439484,-111.904053),
		  description: "value",
		  dir: "left",
		  status: 1, 
	  },{
		  name: "Gateway Village",
		  // latlng: new google.maps.LatLng(40.356880,-111.754654),
		  dir: "right",
		  status: 1, 
	  },{
		  name: "The Cedars",
		  // latlng: new google.maps.LatLng(40.416557,-111.752457),
		  dir: "right",
		  status: 1, 
	  }
  ];

var gallery = $('#community-gallery'),
	galleryHeight = '400px',
	galleryWidth = gallery.width(),
	galleryClass = gallery.attr('class'),
	viewer = $('.viewer'),
	galleryLoaded = null,
	viewerStatus = null,
	viewerBackground = null;
	currentImage = null,
	closeButton = $('<button class="close-button">Close</button>'),
	nextButton = $('<button class="next-button">Next</button>'),
	previousButton = $('<button class="previous-button">Previous</button>'),
	galleryButtons = [closeButton, nextButton, previousButton];

	
	$(document).ready(function() {

		loadPhotoGallery();	

		$('li.photo-gallery a').bind('click', function() {

			var button = $(this).parent('li'),
				buttonClass = button.attr('class');

		  button.addClass('loading'); 

		  switch (buttonClass) {
			case 'floor-plans':
				viewer.load('floor-plans.html .'+currentCommunity);
				gallery.prepend(closeButton);
				return false;
			break;
			case 'photo-gallery':
				var communityHeader = $('.community.header');
				communityHeader.animate({opacity: +(!communityHeader.css('opacity'))});
			break;
		  }
	  
		  if (galleryLoaded) gallery.animate({height:'625px', 'margin-top': '-185px'}, 300, function() {

			// window.scroll(0, (window.innerHeight - gallery.height()+20) / 2);				
				
			button.removeClass('loading').addClass('active'); 

			gallery.addClass(buttonClass);
		
			viewer.css('background-image','url()').css('background-image',false);
				
			if (!viewerStatus) {
				viewerStatus = true;
				gallery.prepend(closeButton, nextButton, previousButton);
				viewer.cycle({ 
					prev:	  '.previous-button', 
					next:	  '.next-button', 
					timeout: 0,
					sync: 0,
					fit: 1,
					// height: '625px',
					// after: onAfter, 
					before: resizeViewer, 
					speed:  250
				});
			}
			else {
				$(currentImage).show();
				closeButton.fadeIn();
				nextButton.fadeIn();
				previousButton.fadeIn();
			}
		

			$('.close-button').bind('click', function() {
				// console.log($(currentImage).attr('src').replace('..',''));
				communityHeader.animate({opacity: 1});
				closeButton.fadeOut();
				nextButton.fadeOut();
				previousButton.fadeOut();
				button.removeClass('loading active');
				viewer.children('img').css('display','none');
				console.log(viewerBackground);
				viewer.css({'background-image': 'url('+viewerBackground+')', 'opacity':1});
				gallery.animate({
					height: galleryHeight,
					width: galleryWidth, 
					'margin-top': 0,
					'margin-left': '-10px',
					'margin-right': 0
				}, function(){
				
				}).css('overflow', 'visible');
			});

		  }).css('overflow', 'visible');
		  return false;
		});
		
		$('.share button').bind('click', shareCommunity);
	});
	
	function loadPhotoGallery () {
		viewer.load('/communities/gallery.html .'+currentCommunity, function(data, status, obj) {
			if (status === 'success') {
				galleryLoaded = true;
				viewerBackground = viewer.find('.featured').attr('src');
				viewer.css({'background-image': 'url('+viewerBackground+')', 'opacity':1});
				gallery.animate({'height':galleryHeight});

				if ('home' === currentCommunity) {
					viewer.css('background-image','url()').css('background-image',false)
					.cycle({ 
						timeout: 4000,
						sync: 0,
						fit: 1,
						width: '100%',
						speed:  800
					});
				}
			};
		});
	}

	function resizeViewer () {
		console.log(arguments[2].currSlide);
		var nextImage = arguments[1],
			$nextImage = $(nextImage),
			orientation = (nextImage.width > nextImage.height) ? 'landscape' : 'portrait';

			viewerBackground = $nextImage.attr('src');

		if (orientation === 'landscape') {
			$nextImage.width(galleryWidth);
			gallery.animate({width:galleryWidth,height:$nextImage.height(), 'margin-left': '-10px', 'margin-right': 0}).css('overflow', 'visible');
		}
		else if(nextImage){
			$nextImage.height('100%');
			gallery.animate({width:$nextImage.width(), 'margin-left': ((galleryWidth-$nextImage.width())/2)-10, 'margin-right': (galleryWidth-$nextImage.width())/2}).css('overflow', 'visible');
		};

		currentImage = nextImage;

	}
	
	function shareCommunity () {
		var button = $(this),
			buttonClass = button.attr('class');
			
		switch(buttonClass){
		case 'email':
			var subject = 'Flagship Homes',
				messageBody = window.location;
			var mailto_link = 'mailto:ts@ohhush.net?subject='+subject+'&body='+messageBody;

			win = window.open(mailto_link,'emailWindow');
			if (win && win.open &&!win.closed) win.close();
		break;
		
		case 'print':
			window.print();
		break;
		
		default:
				
		}
	}
	
	// If this is IE, include respond.js for IE6-8 media query support.
	if ($.browser.msie){
		(function(d){
			var s= d.createElement('script');
			s.src = 'js/respond.min.js';
			document.body.appendChild(s);
		})(document);
	}

})();
