$(document).ready(function(){
	$(function(){

		$("#experience-video-placeholder").click(function() {
			_loadVideo(_getVideoAtIndex(0)); return false;
		});

		$("#testimonials .testimonial-item").quickpaginate({perpage: 3, showcounter: false, pager : $("#testimonials-counter")});

		$("#property-photos li").quickpaginate({perpage: 6, showcounter: false, pager : $("#property-photos-pager")});
		$("#lifestyle-photos li").quickpaginate({perpage: 6, showcounter: false, pager : $("#lifestyle-photos-pager")});
		$("#videos li").quickpaginate({perpage: 6, showcounter: false, pager : $("#videos-pager")});
		$("#views-and-neighborhoods li").quickpaginate({perpage: 6, showcounter: false, pager : $("#views-and-neighborhoods-pager")});

		$("#upcoming-events-all .news-item").quickpaginate({perpage: 8, showcounter: false, pager : $("#events-pager")});

		// Wire up previous navigation and deal with its logic.
		$("#experience-nav-btnPrev").live("mousemove", function(e) {
			var offset = $("#experience-image").offset();

			// Using the offset of the image, figure out where the arrow is.
			var arrowTop = offset.top + (.5 * parseInt($("#experience-nav").css("height"))) - (.5 * 43);
			var arrowLeft = offset.left;
			var arrowBottom = arrowTop + 43;
			var arrowRight = arrowLeft + 28;

			// Check to see if the mouse is over the arrow.
			if ((e.pageX >= arrowLeft && e.pageX <= arrowRight) && (e.pageY >= arrowTop && e.pageY <= arrowBottom)) {
				$(this).css({'background' : 'url(../../images/prev_on.gif) left 50% no-repeat'});
			} else {
				$(this).css({'background' : 'url(../../images/prev_off.gif) left 50% no-repeat'});
			}
		}).live("mouseout", function(e) {
			$(this).css({'background' : ''});
		}).live("click", function(e) {
			e.preventDefault();

			_loadPreviousImage();
		});

		// Wire up next navigation and deal with its logic.
		$("#experience-nav-btnNext").live("mousemove", function(e) {
			var offset = $("#experience-image").offset();

			// Using the offset of the image, figure out where the arrow is.
			var arrowTop = offset.top + (.5 * parseInt($("#experience-nav").css("height"))) - (.5 * 43);
			var arrowLeft = offset.left + parseInt($("#experience-nav").css("width")) - 28;
			var arrowBottom = arrowTop + 43;
			var arrowRight = arrowLeft + 28;

			// Check to see if the mouse is over the arrow.
			if ((e.pageX >= arrowLeft && e.pageX <= arrowRight) && (e.pageY >= arrowTop && e.pageY <= arrowBottom)) {
				$(this).css({'background' : 'url(../../images/next_on.gif) right 50% no-repeat'});
			} else {
				$(this).css({'background' : 'url(../../images/next_off.gif) right 50% no-repeat'});
			}
		}).live("mouseout", function(e) {
			$(this).css({'background' : ''});
		}).live("click", function(e) {
			e.preventDefault();

			_loadNextImage();
		});

		$("#lifestyle-photos, #property-photos, #views-and-neighborhoods").find("li a").click(function(e) {
			e.preventDefault();

			var ul = $(this).parent().parent();

			_loadImage($(this), ul.attr("id"));
		});

		$("#videos li a").click(function(e) {
			e.preventDefault();

			_loadVideo($(this));

////			$("#flash_box").hide();
//			$("#experience-callout").empty();
////			$("#experience-callout").hide();
//
////			var numberOfVideos = _getVideoGalleryLinks().size();
////			var currentIndex = _getCurrentIndexByVideoLink($(this));
//
//			var lightbox = '<div id="experience-flash"></div>'
//		//		+ '<div style="" id="flash-nav">'
//		//		+ '<a href="#" id="flash-nav-btnPrev"></a>'
//		//		+ '<a href="#" id="flash-nav-btnNext"></a>'
//		//		+ '</div>'
////				+ '<div id="experience-flash-details">'
////				+ '<span id="experience-flash-details-caption">' + $(this).attr("title") + '</span>'
////				+ '<span id="experience-flash-details-currentNumber">Video ' + (++currentIndex) + ' of ' + numberOfVideos + '</span>'
////				+ '</div>'
//				;
//
////			$("#experience-callout").append(lightbox);
//
//			$("#experience-callout").html("<div id='experience-flash'></div>");
//
//			var flashvars = {};
//			flashvars.videoURL = $(this).find('img').attr('longdesc');
//			flashvars.videoImage = $(this).attr('href');
//
////			alert(flashvars.videoURL);
//
//			var params = {};
//			params.bgcolor = "ffffff";
//			params.base = "/flash/videoplayer/";
//			params.allowfullscreen = "true";
//
//			var attributes = {};
//			attributes.id = "flashcontent";
//
//			swfobject.embedSWF("/flash/videoplayer/VideoPlayer.swf", "experience-flash", "640", "374", "9.0.124", "expressInstall.swf", flashvars, params, attributes, function(e) {
//				alert(e.ref);
//				$("#experience-callout").show();
//				$("#experience-flash").show();
//			});
//
////			$("#experience-callout").show();
////			$("#flashcontent").show();
		});

//		// Wire up previous navigation and deal with its logic.
//		$("#flash-nav-btnPrev").live("mousemove", function(e) {
//			var offset = $("#flash-nav").offset();
//
//			// Using the offset of the image, figure out where the arrow is.
//			var arrowTop = offset.top + (.5 * 350) - (.5 * 43);
//			var arrowLeft = offset.left;
//			var arrowBottom = arrowTop + 43;
//			var arrowRight = arrowLeft + 28;
//
//			// Check to see if the mouse is over the arrow.
//			if ((e.pageX >= arrowLeft && e.pageX <= arrowRight) && (e.pageY >= arrowTop && e.pageY <= arrowBottom)) {
//				$(this).css({'background' : 'url(../../images/prev_on.gif) left 50% no-repeat'});
//			} else {
//				$(this).css({'background' : 'url(../../images/prev_off.gif) left 50% no-repeat'});
//			}
//		}).live("mouseout", function(e) {
//			$(this).css({'background' : ''});
//		}).live("click", function(e) {
//			e.preventDefault();
//
//			_loadPreviousVideo();
//		});
//
//		// Wire up next navigation and deal with its logic.
//		$("#flash-nav-btnNext").live("mousemove", function(e) {
//			var offset = $("#flash-nav").offset();
//
//			// Using the offset of the image, figure out where the arrow is.
//			var arrowTop = offset.top + (.5 * 350) - (.5 * 43);
//			var arrowLeft = offset.left + 640 - 28;
//			var arrowBottom = arrowTop + 43;
//			var arrowRight = arrowLeft + 28;
//
//			// Check to see if the mouse is over the arrow.
//			if ((e.pageX >= arrowLeft && e.pageX <= arrowRight) && (e.pageY >= arrowTop && e.pageY <= arrowBottom)) {
//				$(this).css({'background' : 'url(../../images/next_on.gif) right 50% no-repeat'});
//			} else {
//				$(this).css({'background' : 'url(../../images/next_off.gif) right 50% no-repeat'});
//			}
//		}).live("mouseout", function(e) {
//			$(this).css({'background' : ''});
//		}).live("click", function(e) {
//			e.preventDefault();
//
//			_loadNextVideo();
//		});

		$(".toggle").click(function() {
			$('#experience-callout').toggle();
			$('#upcoming-events').toggle();
			$('#testimonials').toggleClass('testimonials-position-fix');
			$('#upcoming-events-all').toggle();
		});
	});
});

function _loadVideo(videoLink) {
	$("#flash_box").hide();
	$("#experience-callout").empty();
	$("#experience-callout").hide();

//console.log(videoLink);
	var numberOfVideos = _getVideoGalleryLinks().size()
	var currentIndex = _getCurrentIndexByVideoLink(videoLink);
	
	var lightbox = '<div id="experience-flash"></div>'
//		+ '<div style="" id="flash-nav">'
//		+ '<a href="#" id="flash-nav-btnPrev"></a>'
//		+ '<a href="#" id="flash-nav-btnNext"></a>'
//		+ '</div>'
		+ '<div id="experience-flash-details">'
		+ '<span id="experience-flash-details-caption">' + videoLink.attr("title") + '</span>'
		+ '<span id="experience-flash-details-currentNumber">Video ' + (++currentIndex) + ' of ' + numberOfVideos + '</span>'
		+ '</div>'
		;

	$("#experience-callout").append(lightbox);
//	$("#experience-callout").html("<div id='experience-flash'></div>");

	var flashvars = {};
	flashvars.videoURL = videoLink.find('img').attr('longdesc');//"/flash/videoplayer/assets/videos/test.flv";
	flashvars.videoImage = videoLink.attr('href');//"/flash/videoplayer/assets/images/test.jpg";

	var params = {};
	params.bgcolor = "ffffff";
	params.base = "/flash/videoplayer/";
	params.allowfullscreen = "true";
	params.wmode = "transparent";

	var attributes = {};
	attributes.id = "flashcontent";
	
	swfobject.embedSWF("/flash/videoplayer/VideoPlayer.swf", "experience-flash", "640", "374", "9.0.124", "expressInstall.swf", flashvars, params, attributes);

//	_hideVideoArrows();

	$("#experience-callout").show();
}

function _getVideoGalleryLinks() {
	return $("#videos li a");
}

function _getVideoAtIndex(index) {
	var videoLinks = _getVideoGalleryLinks();

	if (index >= videoLinks.size() || index < 0) {
		return null;
	}

	return videoLinks.eq(index);
}

function _getCurrentVideoIndex() {
	var videoLinks = _getVideoGalleryLinks();

	// Get the flashvars from the video player.
	// Example:
	//		videoURL=/assets/12f90d77f94dcd8bdc0efb88236b5883.flv&videoImage=/assets/ed0e656afac6fa180d25b979c6ef6b69.jpg
	var flashvars = $("#flashcontent param[name='flashvars']").val();
	var videoImage = gup("videoImage", "?" + flashvars)
	var currentVideoLink = videoLinks.filter("a[href='" + videoImage + "']");
	var index = videoLinks.index(currentVideoLink);

	return index;
}

function _getCurrentIndexByVideoLink(videoLink) {
	var videoLinks = _getVideoGalleryLinks();
	var index = videoLinks.index(videoLink);
	return index;
}

function _loadPreviousVideo() {
	var index = _getCurrentVideoIndex();

	if (index == -1) {
		// Error
	} else if (index == 0) {
		// Error
	} else {
		// Load previous image.
		var previousVideo = _getVideoAtIndex(--index);
		_loadVideo(previousVideo);
	}
}

function _loadNextVideo() {
	var index = _getCurrentVideoIndex();

	if (index == -1) {
		// Error
	} else if (index == _getVideoGalleryLinks().size() - 1) {
		// Error
	} else {
		// Load previous image.
		var nextVideo = _getVideoAtIndex(++index);
		_loadVideo(nextVideo);
	}
}

function _hideVideoArrows() {
	var currentIndex = _getCurrentVideoIndex();
	var videoLinks = _getVideoGalleryLinks();
	var size = videoLinks.size();

	if (currentIndex == 0 && currentIndex == size - 1) {
		// Hide both.
		$("#flash-nav-btnPrev").hide();
		$("#flash-nav-btnNext").hide();
	} else if (currentIndex == 0) {
		// Take away the previous arrow and link.
		$("#flash-nav-btnPrev").hide();
		$("#flash-nav-btnNext").show();
	} else if (currentIndex == size - 1) {
		// Take away the next arrow and link.
		$("#flash-nav-btnPrev").show();
		$("#flash-nav-btnNext").hide();
	} else {
		// Show both.
		$("#flash-nav-btnPrev").show();
		$("#flash-nav-btnNext").show();
	}
}






function _getPhotoGalleryImageLinks() {
	// Find out what gallery we are in and get the photos from it.
	var photos = null;
	if ($("#experience-image").hasClass("lifestyle-photos")) {
		photos = $("#lifestyle-photos").find("li a");
	} else if ($("#experience-image").hasClass("property-photos")) {
		photos = $("#property-photos").find("li a");
	} else if ($("#experience-image").hasClass("views-and-neighborhoods")) {
		photos = $("#views-and-neighborhoods").find("li a");
	}

	return photos;
}

function _getPhotoGalleryImageLinksByGallery(galleryName) {
	var selector = "#" + galleryName;
	var photos = $(selector).find("li a");
	return photos;
}

function _getPhotoAtIndex(index) {
	var photos = _getPhotoGalleryImageLinks();

	if (index >= photos.size() || index < 0) {
		return null;
	}
	
	return photos.eq(index);
}

function _getCurrentIndex() {
	// Find out what gallery we are in and get the photos from it.
	var photos = _getPhotoGalleryImageLinks();

	// Get the url of the image that is loaded.
	var currentImageSrc = $("#experience-image").attr("src");
//	console.log(currentImageSrc);

	// Find where the currently loaded image is in the list.
	var currentImageA = photos.filter("a[href='" + currentImageSrc + "']");
//	console.log(currentImageA);

	var index = photos.index(currentImageA);
//	console.log(index);

	return index;
}

function _getCurrentIndexByImageLink(imageLink, galleryName) {
	var photos = _getPhotoGalleryImageLinksByGallery(galleryName);
	var currentImageA = photos.filter("a[href='" + imageLink.attr("href") + "']");
	var index = photos.index(currentImageA);
	return index;
}

function _loadPreviousImage() {
	var index = _getCurrentIndex();
	
	if (index == -1) {
		// Error
	} else if (index == 0) {
		// Error
	} else {
		// Load previous image.
		var previousImage = _getPhotoAtIndex(--index);
		_loadImage(previousImage, _getGalleryName());
	}
}

function _loadNextImage() {
	var index = _getCurrentIndex();

	if (index == -1) {
		// Error
	} else if (index == _getPhotoGalleryImageLinks().size() - 1) {
		// Error
	} else {
		// Load previous image.
		var nextImage = _getPhotoAtIndex(++index);
		_loadImage(nextImage, _getGalleryName());
	}
}

function _loadImage(imageLink, galleryName) {
	$("#flash_box").hide();
	$("#experience-callout").empty();
	$("#experience-callout").hide();

	var numberOfImages = _getPhotoGalleryImageLinksByGallery(galleryName).size()
	var currentIndex = _getCurrentIndexByImageLink(imageLink, galleryName);

	var lightbox = '<img id="experience-image" class="' + galleryName + '" />'
		+ '<div style="" id="experience-nav">'
		+ '<a href="#" id="experience-nav-btnPrev"></a>'
		+ '<a href="#" id="experience-nav-btnNext"></a>'
		+ '</div>'
		+ '<div id="experience-image-details">'
		+ '<span id="experience-image-details-caption">' + imageLink.attr("title") + '</span>'
		+ '<span id="experience-image-details-currentNumber">Image ' + (++currentIndex) + ' of ' + numberOfImages + '</span>'
		+ '</div>';

	$("#experience-callout").append(lightbox);

	var calloutImage = $("#experience-image");
	calloutImage.attr("src", imageLink.attr("href"));
	calloutImage.hide();

	_hideArrows();

	var image = new Image();
	image.src = calloutImage.attr("src");
	image.onload = function() {
		var width = image.width;
		var height = image.height;

		// Need the image to be 430px or less in height and 640px or less in width.
		var optimalRatio = 640 / 430;
		var currentImageRatio = width / height;

		if (currentImageRatio <= optimalRatio) {
			// Resize the height.
			$(calloutImage).jScale(
				{h : "430px"},
				function() {
					_finalizeImageLoad(calloutImage);
				}
			);
		} else if (currentImageRatio > optimalRatio) {
			// Resize the width.
			$(calloutImage).jScale(
				{w : "640px"},
				function() {
					_finalizeImageLoad(calloutImage);
				}
			);
		}
	};
}

function _finalizeImageLoad(calloutImage) {
	$("#experience-callout").show();
	calloutImage.show();

	var newImageWidth = $("#experience-image").width();
	var newImageHeight = $("#experience-image").height();

	$("#experience-nav").css("width", newImageWidth + "px");
	$("#experience-nav").css("height", newImageHeight + "px");

	$("#experience-image-details").css("top", (newImageHeight + 22) + "px");
	$("#experience-image-details").css("width", newImageWidth + "px");
}

function _getGalleryName() {
	if ($("#experience-image").hasClass("lifestyle-photos")) {
		return "lifestyle-photos";
	} else if ($("#experience-image").hasClass("property-photos")) {
		return "property-photos";
	} else if ($("#experience-image").hasClass("views-and-neighborhoods")) {
		return "views-and-neighborhoods";
	}

	return null;
}

function _hideArrows() {
	var currentIndex = _getCurrentIndex();
	var photoLinks = _getPhotoGalleryImageLinks();
	var size = photoLinks.size();

	if (currentIndex == 0 && currentIndex == size - 1) {
		// Hide both.
		$("#experience-nav-btnPrev").hide();
		$("#experience-nav-btnNext").hide();
	} else if (currentIndex == 0) {
		// Take away the previous arrow and link.
		$("#experience-nav-btnPrev").hide();
		$("#experience-nav-btnNext").show();
	} else if (currentIndex == size - 1) {
		// Take away the next arrow and link.
		$("#experience-nav-btnPrev").show();
		$("#experience-nav-btnNext").hide();
	} else {
		// Show both.
		$("#experience-nav-btnPrev").show();
		$("#experience-nav-btnNext").show();
	}
}










function gup(name, url) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(url);
  if( results == null )
    return "";
  else
    return results[1];
}
