/* for the BUILDING page */


var FCESlideShow = {
    interval: 4,                                   // # seconds between slides

    // private:
    ss_timeout: null,
    ss_pos: 1,
    ss_count: 0,
    did_init: false,
    is_showing: true,
    startPosX: 0,
    slideWidth: 0,
    portWidth: 0,
    slideSpeed: 520,

    init: function() {
        this.ss_count = $('#buildingPanel_amenities').children().length;
        this.menuItems = $('#buildingPanel_amenities').children();

        this.menuItems.bind("click", function(e) {
            FCESlideShow.stop();
            FCESlideShow.select(this);
            FCEVirtualTour.disable();
            e.preventDefault();
            return true;
        });

        $('#btnSlideShowPrev').bind('click', function(e) { FCESlideShow.stop(); FCESlideShow.previous(); });
        $('#btnSlideShowNext').bind('click', function(e) { FCESlideShow.stop(); FCESlideShow.next(); });

        var slides = $('#buildingSSContentView').children();
        if (slides.length > 0) {
            // clone last slide and put at the beginning
            var clone0 = $(slides[slides.length - 1]).clone();
            clone0.attr('id', 'ss_content_0');

            // clone first slide and put at the end
            var cloneZ = $(slides[0]).clone();
            cloneZ.attr('id', 'ss_content_Z');

            /* clones should do nothing except cover up a blank spot */
            clone0.prependTo('#buildingSSContentView');
            cloneZ.appendTo('#buildingSSContentView');

            this.startPosX = parseInt($('#buildingSSContentView').css('left'));
            this.slideWidth = parseInt(clone0.width()) + parseInt(clone0.css('marginLeft')) + parseInt(clone0.css('marginRight'));
            this.portWidth = parseInt($('#buildingSSContentW').css('width'));

            this.select();
        }

        this.did_init = true;
    },

    begin: function() {
        if (!this.did_init) this.init();

        if (this.ss_timeout) {
            this.stop();
        }
        else {
            FCEVirtualTour.disable();

            var curr = $('#buildingPanel_amenities').children('.sel');
            if (curr) {
                this.ss_pos = (curr.attr('id').split('_')[1] * 1);
            }
            this.advanceSlides();
            $('#btnViewSlideShow').addClass('running');
            $('#btnViewSlideShow').text('Stop slide show');
        }
    },

    advanceSlides: function() {
        this.next();
        this.ss_timeout = window.setTimeout("FCESlideShow.advanceSlides()", this.interval * 1000);
    },

    next: function() {
        if (this.ss_pos >= this.ss_count) { // fake circularity by jumpintg to the beginning
            this.ss_pos = 0;
            $('#buildingSSContentView').css('left', '0px'); // jump
        }
        this.ss_pos += 1;

        this.select();
    },

    previous: function() {
        if (this.ss_pos == 1) { // fake circularity by jumping to the end
            this.ss_pos = this.ss_count + 1;
            var jumpTo = -1 * (this.ss_count + 2) * this.slideWidth + this.portWidth;
            $('#buildingSSContentView').css('left', jumpTo + 'px'); // jump
        }
        this.ss_pos -= 1;

        this.select();
    },

    select: function(elem, suppress_fade) {
        var pos;

        if (elem) {
            pos = parseInt($(elem).attr('id').split('_')[1]);
        }
        else {
            elem = $('#buildingSSMenuItem_' + this.ss_pos);
            pos = this.ss_pos;
        }

        /* hide the controls for OFT */
        $('#btnSlideShowControls').hide();

        /* here is where the sliding along the x-axis animation begins */
        // first we apply the 'selected' class to the new slide only.
        $('#buildingSSContentView').children().removeClass('selected');
        $('#buildingSSContent_' + pos).addClass('selected');

        // here is where we want to end up to center the slide within the port
        var newXTo = -1 * pos * this.slideWidth + (this.portWidth - this.slideWidth) / 2;

        // if we are 2 or more slides away, jump ahead to the adjacent slide to avoid a jerky effect
        if (Math.abs(pos - this.ss_pos) > 1) {
            var adder = (pos > this.ss_pos) ? this.portWidth : -1 * this.slideWidth;
            var newXFrom = -1 * pos * this.slideWidth + adder;
            $('#buildingSSContentView').css('left', newXFrom + 'px'); // jump
        }
        //console.log("slideW: %d, currPos: %d, pos: %d, adder: %d, from: %d, to: %d", this.slideWidth, this.ss_pos, pos, adder, newXFrom, newXTo);

        this.ss_pos = pos; // remember where we are, or will be.

        // and jquery should handle the animation thusly */
        $('#buildingSSContentView').animate({ left: newXTo + 'px' }, { duration: this.slideSpeed, complete: this.slideCompleted });

        this.clearMenu();
        $(elem).addClass('sel');
        $(elem).blur();

    },

    slideCompleted: function(e) {
        $(this).children().find('p').hide();
        $(this).children('.selected').find('p').fadeIn('slow');
        $('#btnSlideShowControls').show();
    },

    stop: function() {
        if (this.ss_timeout) {
            window.clearTimeout(this.ss_timeout);
            this.ss_timeout = null;
            $('#btnViewSlideShow').removeClass('running');
            $('#btnViewSlideShow').text('View slide show');
        }
    },

    clearMenu: function() {
        this.menuItems.removeClass('sel');
    },

    showContent: function() {
        if (this.is_showing) return;
        this.select(false, true);
        $('#buildingSSContentW').show();
        this.is_showing = true;
    },

    hideContent: function() {
        if (!this.is_showing) return;
        this.clearMenu();
        $('#buildingSSContentW').hide();
        this.is_showing = false;
    }

};


var FCEVirtualTour = {
    enabled : false,
    did_init : false,

    init : function() {
        this.btnDefaultBG = $('#buildingBtnVT1').css('background-image');
        this.btnEnabledBG = this.btnDefaultBG.replace(/\.(\w+["')]+?)$/, '.enabled.$1');

        $('#buildingBtnVT1').bind('click', function(e){
                                              FCEVirtualTour.enable(); 
                                              preventDefault();
                                              return true;
                                           });
        this.did_init = true;
    },

    enable : function() {
        if (!this.did_init) this.init();

        if (this.enabled) {
            this.disable();
        }
        else {
            $('#buildingBtnVT1').css('background-image', this.btnEnabledBG);
            $('#buildingVTContentW').show();
            FCESlideShow.stop();
            FCESlideShow.hideContent();
            this.enabled = true;
            $('#btnViewSlideShowShadow').blur();
        }

    },

    disable : function() {
        $('#buildingBtnVT1').css('background-image', this.btnDefaultBG);
        $('#buildingVTContentW').hide();
        FCESlideShow.showContent();
        this.enabled = false;
    }
};


$(document).ready(function(){
     FCESlideShow.init();
     //FCEVirtualTour.init();

     $('#btnViewSlideShow').bind('click', function(e){ 
                                                    FCESlideShow.begin(); 
                                                    e.preventDefault();
                                                    return true;
                                                } );

    $('#buildingSSContentW').append('<div id="ssInnerGlowL"></div><div id="ssInnerGlowR"></div>');

});

