﻿/*
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();

    //curtains up, start the show!
    $('#propertypics-lightbox').show().fadeTo(300, 0.6, startLightBox2);

}

//set the current image to use
function setCurrentImage() {
    var curCaption = propertydetails_pics.pics[lightboxslideshow_currentId].caption;
    pss.preloadNextImage();

    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('');
    }

    $("#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" });
	    }
    });
}

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) {
        totalImages = propertydetails_pics.pics.length - 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();
}

var pss = {
    nextImgIndex: function() {
        var tmp = 0;

        try {
            tmp = parseInt(lightboxslideshow_currentId) + 1;

            if (tmp >= propertydetails_pics.pics.length)
                tmp = 0;
        }
        catch (e) { }

        return tmp;
    },

    preloadNextImage: function() {
        try {
            var nextIndex = pss.nextImgIndex();
            var nextImage = new Image();
            nextImage.src = propertydetails_pics.pics[nextIndex].imgLg;
        }
        catch (e) { alert(e); }
    }
}