﻿/*
these scripts rely on
/scripts/PropertyDetail_json.js?pid=[id]
to be loaded
*/

//keep track of how many images there are
var totalImages = 0;
var lightboxslideshow_currentId = 0;

$(document).ready(function(){
		$("#slide-lg a").hover(function(){
			$("#slide-lg a span").fadeTo(800, 0.9);
				},function(){
			$("#slide-lg a span").fadeTo(800, 0.0);
		});
});

//start up the light box
function startLightBox(elemImg)
{
	//determine what is our starting attribute
	lightboxslideshow_currentId = $(elemImg).find("a").attr("rel");
	
	//set the image and caption
	setCurrentImage();

	
	//setup the next/previous buttons
	setupButtons();	

	fitInViewport();
	//curtains up, start the show!
	$('#propertypics-lightbox').show().fadeTo(300, 0.6, startLightBox2);
		  	
}

//make sure everything fits in the viewport
function fitInViewport(iw, ih) {
	var ww = $(window).width();
	var wh = $(window).height();
	var dh = (Math.floor(wh/100) * 100) - 50;
	var dw = (Math.floor(ww/100) * 100) - 200;

	if(iw > ww)
	{
		var r = dw / iw;
		var h = ih * r;
		var w = dw;
	}
	if(ih > wh && typeof(w) == 'undefined')
	{
		var r = dh / ih;
		var w = iw * r;
		var h = dh;
	}

	var hw = w/2;
	if(iw > ww || ih > wh)
	{
		$("#propertypic-info img").css({'width':w+'px', 'height':h+'px'});
		$(".propertypicwrapper").css({'width':w+'px', 'margin-left':'-'+hw+'px'});
		$("#propertypic-info span#propertypic-caption").css({'width':w+'px'});
	}
	else
	{
		$("#propertypic-info img").css({'width':'', 'height':''});
		$(".propertypicwrapper").css({'width':'', 'margin-left':''});
		$("#propertypic-info span#propertypic-caption").css({'width':''});
	}
}

//set the current image to use
function setCurrentImage()
{
    var curCaption = propertydetails_pics.pics[lightboxslideshow_currentId].caption;
    
    if (curCaption != '')
    {
        curCaption = '<em>' + curCaption + '</em>';
	    $('#propertypic-info span').animate({top: "-150px", opacity: "0.8"}, {duration: "900", complete: function() {$('#propertypic-info').find("#propertypic-caption").html(curCaption);}});
	}
	else
	{
	    $('#propertypic-info').find("#propertypic-caption").html('');
	}

	var v = new Image();
	$(v).load(function(){

		fitInViewport(
			$(this).attr('width'),
			$(this).attr('height'));

		$("#propertypic-info").find("img").animate({opacity: "0"}, {duration: 100, easing: "easeOutSine", complete: 
			function(){
				$("#propertypic-info").find("img").attr("src", propertydetails_pics.pics[lightboxslideshow_currentId].imgLg).animate({opacity: "1.0"}, {duration: 1000, easing: "easeInSine"});
				$("#propertypic-info").find('#propertypic-caption').animate({top: "0"}, {duration: "900"});

				}
		});

	})
	.attr('src', propertydetails_pics.pics[lightboxslideshow_currentId].imgLg);
}

function swapPropertyImage(elemImg, elemCaption, idstring)
{
    var newcaption = propertydetails_pics.pics[idstring].caption;
    if (newcaption != '')
    {
        newcaption = '<span>' + newcaption + '</span>';
    }
    
    $(elemImg).find("img").fadeTo(0, 1.0, 
	    function(){
		    $(elemImg).find("img").attr("src", propertydetails_pics.pics[idstring].imgMd).fadeTo(500, 1.0);
			$('#slide-caption span').animate({top: "0", opacity: "0.95"}, {duration: "900"});
		});
    $(elemImg).find("img").attr("alt", propertydetails_pics.pics[idstring].alt);
    $(elemCaption).html(newcaption);
	

    $(elemImg).find("a").attr("rel", idstring);
	
}

//need to set the buttons to do their thing
function setupButtons()
{
	if (lightboxslideshow_currentId > 0)
	{
		$('.propertypic-previous').css({display: "block"});
	}
	else $('.propertypic-previous').css({display: "none"});
	
	if (lightboxslideshow_currentId < getPicTotal())
	{
		$('.propertypic-next').css({display: "block"});
	}
	else $('.propertypic-next').css({display: "none"});
}

//return the total number of pictures
function getPicTotal()
{	
	if (totalImages <= 0)
	{
		var objCount=0;
		for(_obj in propertydetails_pics.pics) objCount++;
		//do -1 because we want to start at 0
		totalImages = objCount - 1
	}
	return totalImages;
}

//finish loading up the show and setup the close button
function startLightBox2() {
	$('.propertypicwrapper').show().fadeTo(300, 1.0);	
	
	$('#propertypic-info').show();		
	$('a.close').click(
	function() {
				$('.propertypicwrapper').fadeTo(300, 0.0, closeLightbox);
		  	}
		);
	
}
function closeLightbox() {
	$('#propertypics-lightbox').fadeTo(300, 0.0, hideLightbox);
}

function hideLightbox() {
	$('.propertypicwrapper').hide();
	$('#propertypics-lightbox').hide();
}

//the next or previous button was clicked ... update the image
function lightboxswap(direction)	
{
    lightboxslideshow_currentId = lightboxslideshow_currentId - 0;
    
	if (direction == 0) lightboxslideshow_currentId = lightboxslideshow_currentId - 1;
	if (direction == 1) lightboxslideshow_currentId = lightboxslideshow_currentId + 1;
	
	//setup the next/previous buttons
	setupButtons();

	//set the image and caption
	setCurrentImage();
}

