/* for the BUILDING page */


var FCESlideShow = {
    interval : 5,                                   // # seconds between slides
    slideSpeed : 520,                               // speed of sliding animation, in ms

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

    init: function() {
        this.ss_count = $('#buildingPanel_amenities > div').children().length;
        this.menuItems = $('#buildingPanel_amenities > div').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();

        //Jay: There is a freaky problem in IE7 that
        //occurs if slides.length = 0 with this code.
        //on buildings.asp.
        //basically javascript gets re-included...
        //and then... the javascript files are cached 
        //as if they contain nothing... its the weirdest thing.          //This causes other related pages not to function
        //(this shouldn't have been written with the assumption
        //that there always would be children anyway.
        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 = clone0.totalWidth();
            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').find('div.sel');
            if (curr) {
                this.ss_pos = (curr.attr('id').split('_')[1] * 1);
            }
            this.advanceSlides();
            $('#btnViewSlideShow').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', 0); // 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;
            //console.log("jump to prev: " + jumpTo);
            $('#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();
        if (!this.detectedMacFF()) {
            $('#buildingSSContentView > div').filter(':not(.selected)').css('opacity', 0.4);
        }
    },

    slideCompleted: function(e) {

        /* for LC, the captions are separate in the DOM (for logistical/design
         * reasons, they couldn't be in <p> siblings of the actual slides as usual) */
        var pos = FCESlideShow.ss_pos;
        $('#buildingSSContent_'+pos).fadeTo('slow', (jQuery.browser.safari && jQuery.browser.version < 500)? 0.999 : 1); // saf2 dont like 1
        var captions = $('#ssCaptionsW').children();
        $(captions[pos-1]).fadeIn('slow');
    },

    stop: function() {
        if (this.ss_timeout) {
            window.clearTimeout(this.ss_timeout);
            this.ss_timeout = null;
            $('#btnViewSlideShow').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(/\_[^\.]+/, '_wmax601_hmax601'); 
        var caption = $('#buildingSSMenuItem_'+this.ss_pos).text();
        var caption2 = $($('#ssCaptionsW').children()[this.ss_pos-1]).text();
        tb_show(caption, url, null, caption2);
    },

    detectedMacFF : function () { 
        return (jQuery.browser.mozilla && navigator.userAgent.toLowerCase().indexOf('macintosh') != -1); 
    }

};


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

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

     if (FCESlideShow.detectedMacFF()) {
         $('#buildingSSContentW').append('<div class="macFFhackL"></div><div class="macFFhackR"></div>');
     }

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

});


