$(document).ready(function(){
	if($('body.home')[0]){
		var slides = [
			'<div class="slide" title="There’s always something going on Downtown, and you’re right in the middle of it.">',
				'<img src="'+root.template+'/slideshow/hue-nightlife.jpg"/>',
				'<div class="lens">&#160;</div>',
				'<h2>A Great Start <span>. . . for Raleigh’s nightlife.</span><a href="'+root.home+'/start/nightlife/">Learn more...</a></h2>',
			'</div>',
			//'<div class="slide" title="With residences starting from $141,000, you can own for the cost of renting.">',
			'<div class="slide" title="">',
				'<img src="'+root.template+'/slideshow/hue-homebuyer.jpg"/>',
				'<div class="lens">&#160;</div>',
				'<h2>A Great Start <span>. . . for first-time home buyers.</span><a href="'+root.home+'/start/first-time/">Learn more...</a></h2>',
			'</div>',
			'<div class="slide" title="Hue is located in Downtown Raleigh, a short hop from Raleigh and surrounding business centers.">',
				'<img src="'+root.template+'/slideshow/hue-career.jpg"/>',
				'<div class="lens">&#160;</div>',
				'<h2>A Great Start <span>. . . for your career.</span><a href="'+root.home+'/start/your-career/">Learn more...</a></h2>',
			'</div>'];
		$('table.structure tbody td.middle > *').remove();
		var slideshow = $('<div id="slideshow"/>').append(slides.join('')).appendTo('table.structure tbody td.middle');
		$('#slideshow div.slide').click(function(){
			var href = $('a', this).attr('href');
			document.location.href = href;
			return false;
		});
		$('table.structure tfoot td.middle p').css('color', 'white');
		$('table.structure tfoot td.middle').append('<p>&#160;</p>');
		$('#slideshow').cycle({
			timeout: 7000,
			before: function(){
				var elm = this;
				$('table.structure tfoot td.middle p').fadeOut('slow', function(){
					$('table.structure tfoot td.middle').html('<p>' + $(elm).attr('title') + '</p>').fadeIn('slow');
				});
			}
		});
	}

	if($('body#available')[0]){
		// handle clicks on each home
		$('table.units tr:not(.divider) td.unit a').click(function(){
			var $self = $(this);
			var ismodel = $self.parent().text().indexOf('model') > 0 ? true : false;
			var floorplanfunc = function(){
				var caption = $('#dlog-caption').html();
				$('#dlog-content').html('<p style="text-align: center;"><img src="' + $self.attr('href') + '"/></p>');
				$('#dlog-content img').draggable();
				// replace the caption only if there's already an anchor there
				if($('#dlog-caption a')[0]) $('#dlog-caption').html('Use your mouse to drag this floorplan around. You can also see the <a href="" class="show-slideshow">slideshow</a> of our models.');
				$('#dlog a.show-slideshow').click(slideshowfunc);
				return false;
			}
			var slideshowfunc = function(){
				var caption = $('#dlog-caption').html();
				$('#dlog-content').empty().append('<div id="dlog-stuff"></div>');
				modelshow.apply();
				$('#dlog-caption').html('Enjoy these photos of our homes. You can also see the <a href="" class="show-floorplan">floorplan</a> of this model. <span>Note: the home you purchase may differ from what is shown in these photos.</span>');
				$('#dlog a.show-floorplan').click(floorplanfunc);
				return false;
			}
			if(ismodel){
				var modelshow = function(){swfobject.embedSWF('http://www.flickr.com/apps/slideshow/show.swf?v=71649&offsite=true', 'dlog-stuff', '700', '465', '9.0.28', tsexpressinstall, { offsite: 'true', intl_lang: 'en-us', page_show_url: '/photos/hueraleigh/sets/72157623046862106/show/', page_show_back_url: '/photos/hueraleigh/sets/72157623046862106/', set_id: '72157623046862106', user_id: '45851273@N08', group_id: '' }, { wmode: 'opaque', bgcolor:'#000000', quality:'high', allowScriptAccess:'always', allowFullScreen:'true'}, {});};
				$.zoomer({
					content: modelshow,
					title: 'Photos related to ' + $self.text(),
					caption: 'Enjoy these photos of our homes. You can also see the <a href="" class="show-floorplan">floorplan</a> of this model. <span>Note: the home you purchase may differ from what is shown in these photos.</span>'
				}, function(){
					$('#dlog a.show-floorplan').click(floorplanfunc);
				});
			}else{
				var title = 'Floorplan for ' + $self.text();
				$.zoomer({
					content: null,
					title: title,
					caption: 'Drag the floorplan around to see the whole thing.'
				}, floorplanfunc);
			}
			if( window.pageTracker ){
				window.pageTracker._trackPageview( document.location.pathname + $self.attr('href').split('/').pop() );
			}
			return false;
		});
	}
	/*
	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=011e65d05758f5e283689535bcb58f9d&photoset_id=72157622922349391&user_id=45851273@N08&format=json&jsoncallback=?", function(data){
		var $parent = $('table.structure tbody td.middle');
		$.each(data.photoset.photo, function(i,photo){
			var thumb = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+'/'+photo['id']+'_'+photo['secret']+'_o.jpg';
			var $slide = $('<div class="slide"></div>').append('<img/>').attr('src', thumb).appendTo($parent);
			console.log($slide);
		});
	});
	// load the slides
	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=011e65d05758f5e283689535bcb58f9d&photoset_id=72157622922349391&format=json&jsoncallback=?", function(data){
		var $parent = $('table.structure tbody td.middle');
		$.each(data.items, function(i,item){
			var $slide = $('<div class="slide"/>');
			var $img = $('<img/>').attr('src', item.media.m);
			$slide.append($img);
			$slide.appendTo($parent);
		});
	});
	*/
});

/**
 * jQuery photo plugin
 *
 * Handles the behavior of each photo.
 */
(function($){
	$.zoomer = function(options, callback){

		// if we don't have an options object, create one
		options = options || {};
		// extend our default settings with options
		var o = $.extend({
			content: null,		// function to fun to create the content
			title: false,		// for the title bar
			caption: false		// for the caption at the bottom
		}, options);

		// pointer to the dialog div
		var $dlog = $('#dlog');
		if(!$dlog[0]){
			$dlog = $('<div id="dlog"><div id="dlog-content"><div id="dlog-stuff"></div></div><div id="dlog-caption"></div></div>').appendTo('body');
		}
		var $content = $('#dlog-content', $dlog);
		var $caption = $('#dlog-caption', $dlog);

		// if we have a function for content, run it
		if($.isFunction(o.content)){
			o.content.apply();
		}else{
			$content.html(o.content);
		}
		// handle the caption
		if(o.caption){
			$caption.html(o.caption);
			$caption.show();
		}

		// launch the dialog
		$dlog.dialog({
			modal: true,
			draggable: false,
			resizable: false,
			autoOpen: true,
			width: 730,
			minHeight: 450,
			title: o.title,
			open: function(event, ui){
				$('div.ui-widget-overlay').click(function(){
					$dlog.dialog('close');
				});
				if($.isFunction(callback)){
					callback.apply();
				}
			},
			close: function(event, ui){
				$dlog.dialog('destroy');
				$content.empty().append('<div id="dlog-stuff"></div>');
				$caption.empty();
			}
		});
	};
})(jQuery);
