/* HOMEVIEW */
/* Photo Browser/Viewer Functions
----------------------------------------------------------------------------- */


	var $j = jQuery.noConflict();

	$j(window).load(function() {


/* ADD CONTROLS
----------------------------------------------------------------------------- */


	$j('ul.properties > li').each(function() {

		var $listing = $j(this).children('div.property');
		var $frame = $j(this).children('div.property').children('div.photos');	
		var $browser = $j(this).children('div.property').children('div.photos').children('ul');	
		var $enlarged = $j('#enlarged').children('div.center').children('div.panel');	
		var photos = $browser.children('li').length;

		if ($frame.find('img').length > 1) {
		
			$j('<a class="action next" href="#">Next</a>').appendTo($frame).hide();
			$j('<a class="action back" href="#">Back</a>').appendTo($frame).hide();
			$j('<a class="action thumbnails" href="#" title="Switch to Thumbnail View">Switch to Thumbnail View</a>').appendTo($frame).hide();
			$j('<a class="action slideshow" href="#" title="Switch to Slideshow View">Switch to Slideshow View</a>').appendTo($frame).hide();
	
			var $next = $j(this).find('a.next');
			var $back = $j(this).find('a.back');
			var $thumbnails = $j(this).find('a.thumbnails');
			var $slideshow = $j(this).find('a.slideshow');

		}

		function hideViews() {

			$frame.children('a.next, a.back').click(function() {
			
				$frame.children('a.thumbnails, a.slideshow').fadeOut('fast');
				return false;
			
			});

		}


/* THUMBNAILS VIEW
----------------------------------------------------------------------------- */

	
	function loadThumbnails() {
		
		$browser.cycle('stop');
		$browser.children('li').show().removeAttr('style');
		$slideshow.show();
		$thumbnails.hide();
		$frame.addClass('thumbnails').removeClass('slideshow');

		$next.unbind().click(function() {
					
			$browser.children('li:visible').each(function(index) {
	
				$j(this).hide();
				if (index == 3) { return false;	}
			
			});

			if ($browser.children('li:visible').length == 0) {
				$browser.children('li').show();
			}

			return false;
		
		});

		$back.unbind().click(function() {
		
			if ($browser.children('li:visible').length == photos) {
				$browser.children('li').hide();
			}
			
			$browser.children('li:hidden').reverse().each(function(index) {
	
				$j(this).show();
				if (index == 3) { return false;	}
			
			});

			return false;
		
		});

		$frame.children('a.next, a.back').mouseout(function() {
		
			$frame.children('a.slideshow').fadeIn('fast');
		
		});
		
		hideViews();

	}


/* SLIDESHOW VIEW
----------------------------------------------------------------------------- */


	function loadSlideshow() {

		$browser.addClass('active').children('li').hide();
		$frame.removeClass('thumbnails').addClass('slideshow');
		$slideshow.hide();
		$thumbnails.show();

		$browser.cycle({

			prev: $back,
			next: $next,
			speed: 0,
			timeout: 5000

		});

		$frame.children('a.next, a.back').mouseout(function() {
		
			$frame.children('a.thumbnails').fadeIn('fast');
		
		});

		$browser.cycle('pause');							
		$listing.hoverIntent(function() {
			$browser.cycle('resume');
		}, function() {
			$browser.cycle('pause');
		});
		
		hideViews();

	}

	if ($frame.find('img').length > 1) {
	
		$browser.mouseover(function() {
		
			if (!$browser.hasClass('active')) {
			
				loadSlideshow();
	
			}
	
		});

	}


/* ENLARGED VIEW
----------------------------------------------------------------------------- */


	$browser.children('li').children('a.enlarge').click(function() {

		$enlarged.empty().append('<ul></ul>');
		
		var $insertion = $enlarged.children('ul');
		var selected = $j(this).attr('href');

		$browser.children('li').each(function() {
		
			var image = $j(this).children('a').attr('href');
			var caption = $j(this).children('img').attr('title');

			if (caption) {

				$j('<li><img src="' + image + '" alt="' + caption + '" /><h4>' + caption + '</h4></li>').appendTo($insertion);

			} else {

				$j('<li><img src="' + image + '" alt="Photo" /></li>').appendTo($insertion);
			
			}
			
			if ($j(this).hasClass('portrait')) {
				$insertion.children('li:last-child').addClass('portrait');
			}

			if (image == selected) {
				$insertion.children('li:last-child').prependTo($insertion);
			}

		});

		if ($frame.find('img').length > 1) {

			$j('<a class="action next" href="#">Next</a>').appendTo($enlarged);
			$j('<a class="action back" href="#">Back</a>').appendTo($enlarged);
	
			var $next = $j('#enlarged a.next');
			var $back = $j('#enlarged a.back');
	
			$insertion.cycle({
	
				fx: 'fade',
				next: $next,
				prev: $back,
				pause: true,
				speed: 500,
				timeout: 6000
	
			});

			$j('#enlarged').removeClass('single');

		} else {

			$j('#enlarged').addClass('single');
		
		}
	
		openOverlay('#enlarged');

		return false;
	
	});


/* TOGGLES
----------------------------------------------------------------------------- */


	if ($frame.find('img').length > 1) {

		$thumbnails.click(function() {
	
			loadThumbnails();
			return false;
	
		});
	
		$slideshow.click(function() {
		
			loadSlideshow();
			return false;
			
		});

		$frame.hoverIntent(function() {
	
			$next.fadeIn('fast');	
			$back.fadeIn('fast');	
	
			if ($frame.hasClass('thumbnails')) {
				$slideshow.fadeIn('fast');
			} else {
				$thumbnails.fadeIn('fast');
			}
	
		}, function() {
	
			$next.fadeOut('fast');	
			$back.fadeOut('fast');	
			$slideshow.fadeOut('fast');	
			$thumbnails.fadeOut('fast');	
	
		});

	}


/* END PROPERTY LOOP */

	});

/* END FILE */

	});