/* DIMENSIONS PLUGIN - USED TO BRING JQUERY 1.2 UP TO DATE FOR CSS FUNCTIONS - NOT NEEDED IN JQ 1.4.6 */

(function($){
	$.dimensions={version:'1.2'};$.each(['Height','Width'],function(i,name){$.fn['inner'+name]=function(){if(!this[0])return;var torl=name=='Height'?'Top':'Left',borr=name=='Height'?'Bottom':'Right';return this.is(':visible')?this[0]['client'+name]:num(this,name.toLowerCase())+num(this,'padding'+torl)+num(this,'padding'+borr);};$.fn['outer'+name]=function(options){if(!this[0])return;var torl=name=='Height'?'Top':'Left',borr=name=='Height'?'Bottom':'Right';options=$.extend({margin:false},options||{});var val=this.is(':visible')?this[0]['offset'+name]:num(this,name.toLowerCase())+num(this,'border'+torl+'Width')+num(this,'border'+borr+'Width')+num(this,'padding'+torl)+num(this,'padding'+borr);return val+(options.margin?(num(this,'margin'+torl)+num(this,'margin'+borr)):0);};});$.each(['Left','Top'],function(i,name){$.fn['scroll'+name]=function(val){if(!this[0])return;return val!=undefined?this.each(function(){this==window||this==document?window.scrollTo(name=='Left'?val:$(window)['scrollLeft'](),name=='Top'?val:$(window)['scrollTop']()):this['scroll'+name]=val;}):this[0]==window||this[0]==document?self[(name=='Left'?'pageXOffset':'pageYOffset')]||$.boxModel&&document.documentElement['scroll'+name]||document.body['scroll'+name]:this[0]['scroll'+name];};});$.fn.extend({position:function(){var left=0,top=0,elem=this[0],offset,parentOffset,offsetParent,results;if(elem){offsetParent=this.offsetParent();offset=this.offset();parentOffset=offsetParent.offset();offset.top-=num(elem,'marginTop');offset.left-=num(elem,'marginLeft');parentOffset.top+=num(offsetParent,'borderTopWidth');parentOffset.left+=num(offsetParent,'borderLeftWidth');results={top:offset.top-parentOffset.top,left:offset.left-parentOffset.left};}return results;},offsetParent:function(){var offsetParent=this[0].offsetParent;while(offsetParent&&(!/^body|html$/i.test(offsetParent.tagName)&&$.css(offsetParent,'position')=='static'))offsetParent=offsetParent.offsetParent;return $(offsetParent);}});function num(el,prop){return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;};
})(jQuery);

/* FEATURED SLIDESHOW */

(function() {

	$.fn.featuredSlideshow = function(options) {
		var opts = $.extend({}, $.fn.featuredSlideshow.defaults, options);
		return this.each(function() {
			var	$this = $(this),
					o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			if(o.autoAdvance) {
				var hoverInterval = autoAdvance($this, o);

				$this.hover(function() {
					clearInterval(hoverInterval);
				}, function() {
					hoverInterval = autoAdvance($this, o);
				});

			}

			$('div.pagination ul.slides li a', $this).click(function() {
				var targetID = $(this).attr('href');
				$.fn.featuredSlideshow.advance(targetID, $this, o);
				return false;
			});
			
			$('ul.slides li', $this).animate({
				opacity: 0.5
			}, 0);
			$('ul.slides li.active', $this).animate({
				opacity: 1
			}, 0);
			
		});
	};

	function autoAdvance($this, o) {
		return setInterval(function() {
			var next = $('div.viewpane ul li.active', $this).next().size() == 0 ? $('div.viewpane ul li:first', $this) : $('div.viewpane ul li.active', $this).next();
			$.fn.featuredSlideshow.advance(next, $this, o);
		}, o.autoAdvanceInterval);
	};

	$.fn.featuredSlideshow.advance = function(slide, scope, o) {
	  if(window.videoPlayer) {
	    window.videoPlayer.pauseVideo();
	  }
	  
		var	$scope = $(scope),
				$slide = $(slide, $scope),
				slideID = '#'+$slide.attr('id');

		$slide.stop();
		var page = $('div.pagination ul.slides li a[href='+slideID+']', $scope).parent();
		page.animate({
			opacity: 1
		}, o.transitionTime, function() {
			$(this).addClass('active');
		}).find('span.indicator').animate({
			opacity: 1
		}, o.transitionTime);
		page.siblings('.active').animate({
			opacity: 0.5
		}, o.transitionTime, function() {
			$(this).removeClass('active');
		}).find('span.indicator').animate({
			opacity: 0
		}, o.transitionTime);
		
		$slide.animate({ opacity: 0 }, 1, function() {
			$(this).addClass('on-deck');
		});
		$slide.animate({ opacity: 1}, o.transitionTime, function() {
			$(this).siblings('.active').removeClass('active');
			$(this).addClass('active').removeClass('on-deck');
		});
		
		
		// hardcoded - needs abstraction
		var pageLeft = ( page.offset() ) ? page.offset().left : 0;
		var paginateLeft = ( $('div.pagination').offset() ) ?  $('div.pagination').offset().left : 0;
		if(pageLeft - paginateLeft >= 545 + 21 || pageLeft < paginateLeft +21) {
			$('div.pagination a.next').trigger('click');
		}
	};

	$.fn.featuredSlideshow.defaults = {
		autoAdvance: true,
		autoAdvanceInterval: 4000,
		transitionTime: 450
	};

})(jQuery);

/* SLIDESHOW CAROUSEL */

(function() {

	$.fn.carousel = function(options) {
		var opts = $.extend({}, $.fn.carousel.defaults, options);
		return this.each(function() {
			
			
			var $this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			o.slideCount = $('ul.slides li', $this).size();
			$('ul.slides', $this).css('width', o.slideWidth * o.slideCount);
			
			$('a.next', $this).bind('click', function(e) {
			  e.preventDefault();
				$.fn.carousel.advance(1, $this, o);
			});
			$('a.prev', $this).bind('click', function(e) {
			  e.preventDefault();
				$.fn.carousel.advance(-1, $this, o);
			});
			
			$('<span class="indicator"></span>').appendTo($('ul.slides li', $this));
			
		});
	};

	$.fn.carousel.advance = function(dir, scope, o) {
		
		var	$slides = $('ul.slides', scope), 
				currentPosition = parseInt($slides.css('left'),10),
				distance = dir * o.pageWidth;
		
		distance = dir * o.pageWidth;
		
		if(distance >= $slides.outerWidth() + parseInt($slides.css('left'),10) - o.offset && dir > 0) {
			distance = o.offset;
		} else if(2*distance > $slides.outerWidth() + parseInt($slides.css('left'),10) - o.offset && dir > 0) {
			distance = $slides.outerWidth() + parseInt($slides.css('left'),10) - o.offset - o.pageWidth;
			distance = -1*distance;
		} else if(parseInt($slides.css('left'),10) > 0 && dir < 0) {
			distance = (o.slideCount - o.visible) * o.slideWidth;
			distance = -1*distance;
		} else if( Math.abs(distance) > Math.abs(parseInt($slides.css('left'),10)) + o.offset && dir < 0) {
			distance = o.offset;
		} else {
			distance = -1*distance;
		}
		
		$slides.animate({
			left: distance
		}, o.transitionTime, 'swing', function() {});
		
	};

	$.fn.carousel.defaults = {
		pageWidth: 545,
		offset: 21,
		slideWidth: 109,
		transitionTime: 350,
		visible: 5
	};
	
})(jQuery);

(function() {

  $.fn.floorPlanTabs = function(options) {
    var opts = $.extend({}, $.fn.floorPlanTabs.defaults, options);
    return this.each(function() {
      var $this = $(this),
        o = $.meta ? $.extend({}, opts, $this.data()) : opts,
        $filteredModules = $this.find(o.filteredModules),
        $navLinks = $this.find(o.navLinks);
      
      $navLinks.filter(function() {
        return $(this).attr("href") != o.allHref;
      }).bind('click', function() {
        $(this).addClass('active').parent().siblings().find('a.active').removeClass('active');
        var className = $(this).attr('href').replace("#","");
        $this.find('.'+className).show();
        $this.find(o.filteredModules).not('.'+className).hide();
        return false;
      });
      
      $navLinks.filter(function() {
        return $(this).attr("href") == o.allHref;
      }).bind('click', function() {
        $(this).addClass('active').parent().siblings().find('a.active').removeClass('active');
        $this.find(o.filteredModules).show();
        return false;
      });
      
    });
  };

  $.fn.floorPlanTabs.defaults = {
    allHref: "#All_Floor_Plans",
    navLinks: 'ul.nav li a',
    filteredModules: 'ul.results li ul li'
  };

})(jQuery);


/* DOCREADY */

$(document).ready(function() {
  
  window.videoPlayer = document.getElementById('movie');
	
	$('div.slideshow').featuredSlideshow();
	$('div.slideshow div.pagination').carousel();
	
	$('ul.features').listColumns({cols:2,colWidth:450,equalHeight: false, startN:1});
	
	$('div.floor_plans').floorPlanTabs();
	
	$('form[name=propertyResultsFilters]').each(function() {
	  var $this = $(this);
	  $this.find('input[type=submit]').css("display","none");
	  $this.find('input[type=checkbox]').bind('change', function() {
	    $this.trigger('submit');
	  });
	});
	
});


