
function galleryImage() {
}


galleryImage.prototype.property = null;
galleryImage.prototype.imageUrl = "";
galleryImage.prototype.title = "";
galleryImage.prototype.description = "";
galleryImage.prototype.navigateUrl = "";
galleryImage.prototype.caption = "";
galleryImage.prototype.CssClass = "";

function galleryImage(property) {
    this.property = property;
}


function rsProperty() {
}

rsProperty.prototype.latitude = .0;
rsProperty.prototype.longitude = .0;
rsProperty.prototype.title = "";

function rsSettings() {
}

rsSettings.prototype.property = null;
rsSettings.prototype.items = new Array();
rsSettings.prototype.markers = new Array();
rsSettings.prototype.addImage = function(elm) {
    this.items.push(elm);
};



var _dbgal = {
    MODE_GALLERY: 0,
    MODE_MAP: 1,
    currentIndex: -1,
    g_map: null,
    settings: new rsSettings(),
    mode: 0,
    autotimerId: 0,
    autoplay: true,
    parentElm: null,
    imageWidth: 0,
    playing: false,

    onImageChanging: function(sender, ev) { },
    onImageChanged: function(sender, ev) { },

    onPreInitialize: function(sender, ev) { },
    onAfterInitialize: function(sender, ev) { },
    onPlay: function(sender, ev) { },
    onPause: function(sender, ev) { },
    onModeChanged: function(sender, ev) { },
    isMapInitialized: false,
    interval: 6000,
    slideTime: 1200,
    mapSlideTime: 1200,
    mapElement: null,
    imageEasing: "easeInOutQuint",
    mapEasing: "easeInOutQuint",

    setMode: function(mode) {
        if (this.mode != mode) {


            var elm = $(this.parentElm);

            if (mode == this.MODE_MAP) {

                this.pause();

                if (!this.isMapInitialized)
                    this.initializeMap();


                elm.find(".map").animate({ top: 0 }, this.mapSlideTime, this.mapEasing);
            }
            else {
                elm.find(".map").animate({ top: -430 }, this.mapSlideTime, this.mapEasing);

                this.play();
            }

            this.mode = mode;

            if (this.onModeChanged)
                this.onModeChanged(this, { newMode: mode });
        }
    },

    showMap: function() {
        setMode(this.MODE_MAP);
    },

    showGallery: function() {
        setMode(this.MODE_GALLERY);
    },

    addImage: function(elm) {
        this.settings.addImage(elm);
    },

    getImage: function(idx) {
        return this.settings.items[idx];
    },

    length: function() {
        return this.settings.items.length;
    },

    addImageRange: function(elements) {

        for (var i = 0; i < elements.length; i++) {
            addImage(elements[i]);
        }

    },

    currentImage: function() {
        return this.getImage(this.currentIndex);
    },

    initializeMap: function() {


        this.mapElement = document.getElementById("gmap");

        var _gm_opts = {
            zoom: 11,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControlOptions: { mapTypeIds: new Array() }
        };

        this.g_map = new google.maps.Map(this.mapElement, _gm_opts);

        if (this.settings.property) {
            this.settings.markers.push(new google.maps.Marker({
                clickable: false,
                draggable: false,
                flat: false,
                map: this.g_map,
                position: new google.maps.LatLng(this.settings.property.latitude, this.settings.property.longitude)
            }));

        }
        else {
            for (var i = 0; i < this.settings.items.length; i++) {
                var p = this.settings.items[i];

                if (p.property) {
                    this.settings.markers.push(new google.maps.Marker({
                        clickable: false,
                        draggable: false,
                        flat: false,
                        map: this.g_map,
                        position: new google.maps.LatLng(p.property.latitude, p.property.longitude)
                    }));
                }

            }
        }


        this.isMapInitialized = true;

        if (!this.centerMap(this.currentIndex))
            this.g_map.setCenter(new google.maps.LatLng(0, 0));

    },

    centerMap: function(idx) {
        if (this.g_map) {
            if (this.settings.property) {
                var p = this.settings.property;

                var center = new google.maps.LatLng(p.latitude, p.longitude);

                this.g_map.setCenter(center);

                return true;

            }
            else {
                var p = this.settings.items[idx];

                if (p) {
                    var center = new google.maps.LatLng(p.property.latitude, p.property.longitude);
                    this.g_map.setCenter(center);
                    return true;

                }


            }
        }

        return false;
    },

    initialize: function(parent) {

        this.parentElm = parent;

        var elm = $(parent);
        var container = elm.find(".slides");

        this.imageWidth = elm.width();

        if (this.onPreInitialize)
            this.onPreInitialize(this, {});

        // Create all slides
        for (var i = 0; i < this.length(); i++) {
            var img = this.getImage(i);

            var item = $("<div class=\"slide\" style=\"width: " + this.imageWidth + "px; left: " + parseInt(this.imageWidth * i) + "px; background-image: url('" + img.imageUrl + "');\"><div class=\"info\"><div class=\"title\">" + img.title + "</div><div class=\"description\">" + img.description.replace("$", "\n") + "</div></div></div>");




            if (img.caption && img.caption != "") {
                var nelm = $("<div class=\"caption\"></div>");

                if (img.navigateUrl) {
                    nelm.append($("<a href=\"" + img.navigateUrl + "\" class=\"label\">" + img.caption + "</a>"));
                }
                else {
                    nelm.append($("<span class=\"label\">" + img.caption + "</span>"));
                }

                item.append(nelm);
            }

            container.append(item);


        }

        //this.initializeMap();

        if (this.onAfterInitialize)
            this.onAfterInitialize(this, {});

        this.gotoImage(0);

        if (this.autoplay)
            this.play();
    },

    getSlideElement: function(index) {
        return $(this.parentElm).find(".slides").children(".slide")[index];
    },

    getCurrentSlideElement: function() {
        return this.getSlideElement(this.currentIndex);
    },

    gotoImage: function(index) {

        if (index == this.currentIndex)
            return;


        if (this.onImageChanging)
            this.onImageChanging(this, { index: index })

        var elm = $(this.parentElm);
        var c = elm.find(".slides");

        // Prevent unnecessary resource usage by not sliding when the map is shown
        if (this.mode == this.MODE_GALLERY)
            c.animate({ left: -(this.imageWidth * index) }, this.slideTime, this.imageEasing, function(ev) { _dbgal.onImageChanged(_dbgal, ev); });
        else {
            c.css("left", -(this.imageWidth * index));
            _dbgal.onImageChanged(_dbgal, {});
        }

        this.centerMap(index);


        this.currentIndex = index;


    },

    next: function(noreset) {
        this.gotoImage(this.currentIndex >= this.length() - 1 ? 0 : this.currentIndex + 1);

        if (noreset !== false) {
            clearInterval(this.autotimerId);
            this.autotimerId = setInterval(function() { _dbgal.next(); }, this.interval);
        }

    },

    previous: function(noreset) {
        this.gotoImage(this.currentIndex <= 0 ? this.length() - 1 : this.currentIndex - 1);

        if (noreset !== false) {
            clearInterval(this.autotimerId);
            this.autotimerId = setInterval(function() { _dbgal.next(); }, this.interval);
        }

    },

    play: function() {

        if (this.mode == this.MODE_GALLERY) {
            if (this.onPlay)
                this.onPlay(this, {});

            playing = true;

            clearInterval(this.autotimerId);
            this.autotimerId = setInterval(function() { _dbgal.next(true); }, this.interval);
        }
    },

    pause: function() {
        if (this.onPause)
            this.onPause(this, {});

        this.playing = false;

        clearInterval(this.autotimerId);
        this.autotimerId = 0;
    }

};

$(document).ready(function() {

    // Initialize gallery
    _dbgal.initialize($(".property-gallery")[0]);

});
