/* for the BUILDING page */


var FCESlideShow = {
    interval : 5,                                   // # seconds between slides
    btnStopBG : 'url(img/btn.stop_slideshow.gif)',  // property for background image when slideshow is in progress.

    // private:
    ss_timeout : null,
    ss_pos : 1,
    ss_count : 0,
    did_init : false,
    is_showing : true,
    startPosX : 0,
    slideWidth : 0,
    portWidth : 0,
    slideSpeed : 520,
    direction : 1, // 1 = forward, -1 = back

    init: function() {
        this.menuItems = $('#buildingPanel_amenities').children();
        this.ss_count = this.menuItems.length;

        this.menuItems.bind("click", function(e) { 
                FCESlideShow.stop();
                FCESlideShow.select(this);
                FCEVirtualTour.disable();
                $(this).children('a').blur();
                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();

        // 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 > li.sel');
            if (curr) {
                this.ss_pos = (curr.attr('id').split('_')[1] * 1);
            }
            this.advanceSlides();
            $('#btnViewSlideShowShadow').addClass('running');
            $('#btnViewSlideShow').text('Stop slideshow');
        }
    },

    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.direction = 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.direction = -1;

        this.select();
    },

    select: function(elem, suppress_fade) {
        var pos;

        if (elem) {
            pos = parseInt($(elem).attr('id').split('_')[1]);
            this.direction = (pos > this.ss_pos)? 1 : -1;
        }
        else {
            elem = $('#buildingSSMenuItem_'+this.ss_pos);
            pos = this.ss_pos;
        }

        /* 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');

        // 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.


        this.slideBeginning();

        // 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();

    },

    slideBeginning: function() {
        $('#btnSlideShowControls > div').removeClass('selected');
        $('#buildingSSContent_'+this.ss_pos).addClass('selected');

        if (this.direction > 0) {
            $('#btnSlideShowNext').addClass('selected');
        }
        else {
            $('#btnSlideShowPrev').addClass('selected');
        }
        /* for LC, we hide all captions and fade all slides to 40% before sliding */
        $('#ssCaptionsW').children().hide();
        $('#buildingSSContentView > div').fadeTo('fast', 0.45);
        $('#buildingSSContentView > div > p').hide();
    },

    slideCompleted: function(e) {
        var pos = FCESlideShow.ss_pos;
        $('#buildingSSContent_'+pos+' > p').show();
        $('#buildingSSContent_'+pos).fadeTo('slow', (jQuery.browser.safari && jQuery.browser.version < 500)? 0.999 : 1); // saf2 dont like 1
    },

    stop: function() {
        if (this.ss_timeout) {
            window.clearTimeout(this.ss_timeout);
            this.ss_timeout = null;
            $('#btnViewSlideShowShadow').removeClass('running');
            $('#btnViewSlideShow').text('Play Slideshow');
        }
    },

    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;
    },

    popOverlay : function(slide) {
        this.stop();
        var url = $(slide).css('backgroundImage').match(/url\(['"]?([^)"']+)['"]?\)/);
        url = url[1].replace(/\/\d\d\d\d/, '/0600');
        var caption = $(slide).find('p').text();
        var title = $('#buildingPanel_amenities > li.sel').text();
        tb_show(title, url, null, caption);
    }

};


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();

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

     /* trigger the overlay on click of the selected item in the carousel */
     $('#buildingSSContentView').children().bind('click', function(e){ 
         if (this.className.match(/\bselected\b/)) {
             FCESlideShow.popOverlay(this);
         }
     });

     // special for LC.
     $('#btnSlideShowControls > div').hover( function() { $(this).addClass('selected'); },
                                             function() { $(this).removeClass('selected'); });

});

