// JavaScript Document
jQuery(document).ready(function($) {
	if( $('#notfound').size() > 0) { $('#notfound').jqm(); $('#notfound').jqmShow(); }
	
	//$('#Schedule').hide();
	/*$("a#ShowSchedule").fancybox({
		'width'		      :	300,
		'height'		      :	650,
		'overlayOpacity'	 :	'0.4',
		'overlayColor'		 :	'#000',
		'hideOnContentClick' :   false,
		'autoScale'     	 :   false,
   		'transitionIn'		 :   'elastic',
		'transitionOut'	 :   'elastic',
		'type'			 :   'iframe'
	});*/
	//$('#ShowSchedule').click(function(){
	///	$('#Schedule').toggle();
	//	return false;
	//});
	//$('#Schedule').jqm({trigger: '#ShowSchedule'});
	//var SchedButton = $('#MoveSched').html();
	//$('#MoveSched').remove();
	//$('#ForSale').append(SchedButton);
	
	$('.home #InnerContainer li').css('cursor','pointer');
	$('.home #InnerContainer li strong').hide();
	$('.bigpic').hide();
	if( $('.full').size() > 0 ){
		$('#PhotoBox').css('background', 'none');
	}
	var subht = $('#SubMenu').height();
	var subcount = $('#SubMenu li').size();
	var conht = $('.post').height();
	
	$('img.thumbnail').hover(function() {
		var bigID = $(this).attr('id');
		$("."+bigID).show();
	},function() {
		var bigID = $(this).attr('id');
		$("."+bigID).hide();								  
 	});
	
	$('.home #InnerContainer li').hover(function() {
		$(this).find('span').hide();
		$(this).find('a').css('background-position','center top').css('z-index', '500');
		$(this).find('strong').show();
		
	},function() {
		$(this).find('span').show();
		$(this).find('a').css('background-position','center bottom').css('z-index', '1');
		$(this).find('strong').hide();
	});
	$('.home #InnerContainer li').click(function() {
		var goto = $(this).find('a').attr('href');
		window.location.href = goto;
	});
	$("#searchform .submit").click(function(){
			$("#searchform").submit();
	});
	if( $('#dialog').size() > 0 )
		$('#dialog').jqm();
	
/*SCROLLER #1 START*/
$('#scroll-controls').hide();	
if( subht > 428 ){
	$('#scroll-controls').show();
	function getTop() {
		// get the top of the content
		var top = $('#scroll-content').css('top');
		return trimPx(top);
	}
	
	function getHeight(id) {
		// get the height, including padding
		var height = $(id).height();
		var paddingTop = trimPx($(id).css("padding-top"));
		var paddingBottom = trimPx($(id).css("padding-bottom"));
	
		return height + paddingTop + paddingBottom;
	}
	
	function trimPx(value) {
		// remove "px" from values
		var pos = value.indexOf("px");
		if (pos != 0)
			return parseInt(value.substring(0, pos));
		else
			return 0;
	}
	
	var container;
	var content;
	var hidden;	// # of pixels hidden by the container
	
	function setScrollerDimensions() {
		container = getHeight("#scroll-container");
		content = getHeight("#scroll-content");
		hidden = content - container;
	}
	
	function resetScroller() {
		setScrollerDimensions();
		$('#scroll-content').css('top', 0);
	}


	setScrollerDimensions();

	$('#scroll-controls a.up-arrow').click(function() {
		return false;
	});

	$('#scroll-controls a.down-arrow').click(function() {
		return false;
	});

	$('#scroll-controls a.down-arrow').hover(
		function() {
			if (hidden > 0) {
				var current = getTop();
				$('#scroll-content').animate({ top: -hidden }, Math.abs(current - hidden) * 5);
			}
		},
		function() {
			$('#scroll-content').stop();
		}
	);

	$('#scroll-controls a.up-arrow').hover(
		function() {
			if (hidden > 0) {
				var current = getTop();
				$('#scroll-content').animate({ top: "0" }, Math.abs(current) * 5);
			}
		},
		function() {
			$('#scroll-content').stop();
		}
	);
	var AllItems = $('#SubMenu li').size();
	var CurItem = $('#SubMenu li.current_page_item').prevAll().size();
	if( CurItem > 4){
		var less = 0;
		var start = ((CurItem)*54);
		if (CurItem >= (AllItems-6)){
			var less = 6*54;
			var start = ((AllItems)*54)-less;
			//alert(CurItem);
		}
		var current = getTop();
		$('#scroll-content').css('top', current-start);/*animate({ top: -start}, Math.abs(current - start) * 5);*/	
	}
}
/*SCROLLER #1 END*/
//alert(conht);
/*SCROLLER #2 START*/
$('#scroll2-controls').hide();	
if( conht > 360 ){
	$('#scroll2-controls').show();
	function getTop2() {
		// get the top of the content
		var top = $('#scroll2-content').css('top');
		return trimPx2(top);
	}
	
	function getHeight2(id) {
		// get the height, including padding
		var height = $(id).height();
		var paddingTop = trimPx2($(id).css("padding-top"));
		var paddingBottom = trimPx2($(id).css("padding-bottom"));
	
		return height + paddingTop + paddingBottom;
	}
	
	function trimPx2(value) {
		// remove "px" from values
		var pos = value.indexOf("px");
		if (pos != 0)
			return parseInt(value.substring(0, pos));
		else
			return 0;
	}
	
	var container2;
	var content2;
	var hidden2;	// # of pixels hidden by the container
	
	function setScrollerDimensions2() {
		container2 = getHeight2("#scroll2-container");
		content2 = getHeight2("#scroll2-content");
		hidden2 = content2 - container2;
	}
	
	function resetScroller2() {
		setScrollerDimensions2();
		$('#scroll2-content').css('top', 0);
	}


	setScrollerDimensions2();

	$('#scroll2-controls a.up-arrow').click(function() {
		return false;
	});

	$('#scroll2-controls a.down-arrow').click(function() {
		return false;
	});

	$('#scroll2-controls a.down-arrow').hover(
		function() {
			if (hidden2 > 0) {
				var current2 = getTop2();
				$('#scroll2-content').animate({ top: -hidden2 }, Math.abs(current2 - hidden2) * 5);
			}
		},
		function() {
			$('#scroll2-content').stop();
		}
	);

	$('#scroll2-controls a.up-arrow').hover(
		function() {
			if (hidden2 > 0) {
				var current2 = getTop2();
				$('#scroll2-content').animate({ top: "0" }, Math.abs(current2) * 5);
			}
		},
		function() {
			$('#scroll2-content').stop();
		}
	);
}
/*SCROLLER #2 END*/

/* Portfolio Scroller START */
	var isWide = $('.wide').size();
	var isSkinny = $('.skinny').size();
	var Width = 441;
	var Show = 3;
	if( isWide == 1 ){ Width = 600; Show = 4; }
	if( isWide == 1 ){ Width = 300; Show = 2; }
	var PicTotal = $('#MorePhotos img').size();
	var DivWidth = Math.ceil(PicTotal/Show)*Width;
	var Max = DivWidth-Width;
	$x = 0;
	$('#MorePhotos div').css('width', DivWidth+'px');
	if( PicTotal <= Show ){
		$('.prevPics, .nextPics').hide();	
	}
	$('.nextPics').click( function(){
		$x = $x-Width;
		if( $x < -Max ){ $x = 0; }
		$('#MorePhotos div').animate({ marginLeft: $x+'px'}, 1500 );
		return false;
	});
	$('.prevPics').click( function(){
		$x = $x+Width;
		if( $x > 0 ){ $x = -Max; }
		$('#MorePhotos div').animate({ marginLeft: $x+'px'}, 1500 );
		return false;
	});
	
/* Portfolio Scroller END */
	
	var nokeyword = $('#notfound').size();
	if( nokeyword ){
		
	}
	
	$('a.homeforsale, a.nohomeforsale').click(function(){
		$('#Default, .floorplans').toggle();
		$('#ForSale').toggle();
		$(this).toggleClass('active');
		return false;
	});
	
	
	if( $('#ShowForSale').length > 0 ){
		$('#Default, .floorplans, #ForSale').toggle();
		$('a.homeforsale').addClass('active');
	}
	
	$('.bubble').attr( 'title', '');
	$(function () {
		  $('.bubble').each(function () {
			// options
			var distance = -10;
			var time = 250;
			var hideDelay = 0;
		
			var hideDelayTimer = null;
		
			// tracker
			var beingShown = false;
			var shown = false;
			
			var trigger = $('.trigger', this);
			var popup = $('.popup', this).css('opacity', 0);
		
			// set the mouseover and mouseout on both element
			$([trigger.get(0), popup.get(0)]).mouseover(function () {
			  // stops the hide event if we move from the trigger to the popup element
			  if (hideDelayTimer) clearTimeout(hideDelayTimer);
		
			  // don't trigger the animation again if we're being shown, or already visible
			  if (beingShown || shown) {
				return;
			  } else {
				beingShown = true;
		
				// reset position of popup box
				popup.css({
				  top: -100,
				  left: -66,
				  display: 'block' // brings the popup back in to view
				})
		
				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
				  top: '-=' + distance + 'px',
				  opacity: 1
				}, time, 'swing', function() {
				  // once the animation is complete, set the tracker variables
				  beingShown = false;
				  shown = true;
				});
			  }
			}).mouseout(function () {
			  // reset the timer if we get fired again - avoids double animations
			  if (hideDelayTimer) clearTimeout(hideDelayTimer);
			  
			  // store the timer so that it can be cleared in the mouseover if required
			  hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
				  top: '-=' + distance + 'px',
				  opacity: 0
				}, time, 'swing', function () {
				  // once the animate is complete, set the tracker variables
				  shown = false;
				  // hide the popup entirely after the effect (opacity alone doesn't do the job)
				  popup.css('display', 'none');
				});
			  }, hideDelay);
			});
		  });
		});

});
