var popupGallery;
$(function() {
	if($("#photocontainer .propertyimage").length > 0) {
		//console.log("HAVE PHOTO: " + $("#photocontainer .propertyimage").length);
		$("#largephotolink img").unwrap();
		$("#largephotolink").remove();
		//$("#thumbphotocontainer div a img").each(function(index) {
		$("#photocontainer img").each(function(index) {
			var thumbSrc = $(this).attr("src");
			var mainSrc = $(this).attr("src");
			//thumbSrc = thumbSrc.replace('&Width=1000','&Width=100');
			//thumbSrc = thumbSrc.replace('&Height=756','&Height=75');
			//var mainHtml = '<li data-thumb="' + thumbSrc + '" data-src="' + mainSrc + '"><img src="' + mainSrc + '" alt=""/></li>';
            //mainSrc = mainSrc.replace("DisplayImage","DisplayImageResize");
            // remove everything after ?
            mainSrc = mainSrc.substring(0, mainSrc.indexOf('?')) + '?useLargePhoto=true&cropToFill=false&width=2400';
            var mainHtml = '<li data-thumb="' + thumbSrc + '" data-src="' + mainSrc + '" style="background-image:url(' + mainSrc + ')" alt=""/></li>';
			$('#lightSlider').append(mainHtml);

		});
		$("#photocontainer").hide();
		$("#lightSlider").lightSlider({
			gallery: false,
			item: 1,
			loop: true,
			slideMargin: 0,
			galleryMargin: 0,
			thumbMargin: 15,
			thumbItem: 7,
			adaptiveHeight:true,
			onSliderLoad: function(el) {
				el.lightGallery({
					selector: '#lightSlider .lslide',
					download: false,
					counter: false,
					loop: true
				});
			}
		});

		$("#slideshowOpener").prependTo(".lSSlideOuter");

		$("#slideshowOpener").click(function() {
			$("#lightSlider .active").trigger("click");
		});

	} else {
		$('#lightSlider').hide();
		if($(".propertyimagelist .nophoto").length < 1) {
			$(".propertyimagelist").append('<img src="images/propertyphotos/nophoto-bg.jpg" class="nophoto" alt="Photos Coming Soon"/>');
		}
		$("#photocontainer").show();
	}
});

function resizeOuterFrame() {

	var winHeight = window.parent.innerHeight - 20;
	var winWidth = window.parent.innerWidth - 20;

	// 1. iframe
	// set width to window width - 20
	// set height to window height - 20

	$(".vbox-open iframe",window.parent.document).height(winHeight + "px");
	$(".vbox-open iframe",window.parent.document).width(winWidth + "px");

	console.log("iframe: " + $(".vbox-open iframe",window.parent.document).width() + " x " + $(".vbox-open iframe",window.parent.document).height());

}

function resizeSlideshow(el) {

	$("#slideshow #lightSlider").removeClass("cS-hidden");
	var galleryWidth = $(".slideshowouter").width();
	var galleryHeight = $(".slideshowouter").height();
	var galleryWidth = 1200;
	var galleryHeight = 907;
	var winHeight = window.parent.innerHeight - 20;
	var winWidth = window.parent.innerWidth - 20;


	if(galleryHeight != null) {
		console.log("galleryHeight: " + galleryHeight);
		var galleryTop = (winHeight - galleryHeight);
		galleryTop = parseInt(galleryTop / 2);
		console.log("gallery before: " + galleryWidth + " x " + galleryHeight);
		if(galleryTop > 0) {
			// 2. vbox-content
			// set margin top to half of (galleryHeight - windowHeight)
			// clear magin bottom
			// set width to iframe width
			console.log("vbox-content: " + galleryTop);
			$(".vbox-content",window.parent.document).css("margin",galleryTop + "px auto 0 auto");
			$(".vbox-container",window.parent.document).css("max-width",galleryWidth + 30 + "px");
			//$(el).refresh();
			//$(".vbox-content").css("margin-top",galleryTop + "px");
			console.log("VBOX CSS: " + $(".vbox-content",window.parent.document).attr("style"));
		}
	} else {
		console.log("no gallery yet...");
	}

	//console.log("HAVE GALLERY?: " + $(".lSSlideOuter").length);

	// 3. gallery
	// set width to iframe width
	// set height to auto
	// 4. Resize iframe to fit gallery
	// set height to gallery height

}