var carousel = null;



carousel = {

    initial_index: 1,
    current_index: 0,
    number_of_items: 0,
    rotate_interval: 4000,
    prev_element_id: "prop-carousel-button-previous",
    next_element_id: "prop-carousel-button-next",

    on_rotate: function () { },
    on_rotate_complete: function () { },

    init: function () {
        this.rotate(); //initial rotate to start off the first slide.
        this.start();
    },

    start: function () {
        var _this = this; 	// in function setInterval, "this" refers to the window object
        timer = setInterval(function () { _this.rotate(); }, _this.rotate_interval);
    },

    stop: function () {
        if (typeof (timer) !== 'undefined') {
            clearTimeout(timer);
        }
    },

    rotate: function () {

        $(".intro-box").hide();
        $("#prop-carousel-list").children().fadeOut();
        $("#prop-carousel-list").children(".prop-carousel-current").removeClass("prop-carousel-current");


        if (this.current_index >= this.number_of_items) {
            this.current_index = 1;
        }
        else {
            this.current_index++;
        }

        $(".select").removeClass("select");
        $("#prop-carousel-list-item-" + this.current_index + " .pager").toggleClass("select");

        $("#prop-carousel-item-" + this.current_index).addClass("prop-carousel-current");


        var fn_fadeInintroBox = function () { $(".intro-box").fadeIn('slow'); };
        $("#prop-carousel-item-" + this.current_index).fadeIn('slow', fn_fadeInintroBox);


    },

    getCurrentItem: function () {
        return parseInt($(".prop-carousel-current").attr("id").match(/prop-carousel-item-[1-9]/) ? $(".prop-carousel-current").attr("id").match(/\d+/) : '1');
    },

    gotoNext: function () {
        this.stop();
        this.rotate();
        this.start();
    },

    gotoPrevious: function () {
        this.stop();

        if (this.getCurrentItem() == 1) {
            this.current_index = this.number_of_items - 1;
        }
        else {
            this.current_index = this.current_index - 2;
        }

        this.rotate();
        this.start();

    },
    gotoIndex: function (index) {
        this.stop();
        this.current_index = index - 1;
        this.rotate();
        this.start();
    }

};

$(document).ready(function () {

    if ($.trim(carousel.prev_element_id) != "") {
        $("#" + carousel.prev_element_id).click(function () {
            carousel.gotoPrevious();
        });
    }
    if ($.trim(carousel.next_element_id) != "") {
        $("#" + carousel.next_element_id).click(function () {
            carousel.gotoNext();
        });
    }

    $('#prop-carousel-links li .pager').click(function () {
        carousel.gotoIndex(parseInt($(this).parent().attr("id").match(/prop-carousel-list-item-[1-9]/) ? $(this).parent().attr("id").match(/\d+/) : '1'));
    });

    $(".carousel-item img").click(function () {
        window.location = $(this).parent().find(".intro-box p a").attr("href");
    });

});
