var propertyTour = new Class(
{
    initialize: function(slideshow, slider, video) {
        this.slideShow = slideshow;
        this.tabs = null;
        this.view = null;
        this.video = video;
        this.thumb = null;
        this.slider = $(slider);
        this.ctrl = new Hash({gallery: null,
                              tour: null,
                              plan: null});
        this.pstn  = {gallery: 0,
                      tour: 518,
                      plan: 1036};
    },
    
    attach: function(elements) {
        new Hash(elements).each(function(key, val) {
            el = $(val);
            el.hover = el.effect('background-color', {duration: 250});
            el.setStyle('cursor', 'pointer');
            el.flag = false;
            this.ctrl.set(key, el);
        }.bind(this));
        
        // Controls subnavigation tabs
        this.tabs = $ES('.tab');
        this.tabs.each(function(item) {
            if ($E('a', item)) {
                var link = $E('a', item);
                var panel = link.href.replace(/^.*\#/, '');
                if ($(panel)) {
                    link.addEvent('click', this.tab.pass([panel], this));
                }
            }}.bind(this));

        this.thumb = $ES('.thumbnail');
        
        var loop = 0;

        this.thumb.each(function(el) {
            el.setStyle('cursor', 'pointer');
            el.addEvent('click', function(lp) {
                this._select('gallery');
                this.slideShow.goTo(lp);
            }.pass([loop], this));
            loop++;
        }.bind(this));
        
        this.slideShow.max = loop - 1;
        
        this.ctrl.get('gallery').addEvent('click', this._select.pass(['gallery'], this))
                                .addEvent('mouseover', this._hover.pass(['gallery', true], this))
                                .addEvent('mouseout', this._hover.pass(['gallery', false], this))
                                .flag = true;
        this.ctrl.get('tour').addEvent('click', this._select.pass(['tour'], this))
                             .addEvent('mouseover', this._hover.pass(['tour', true], this))
                             .addEvent('mouseout', this._hover.pass(['tour', false], this));
        this.ctrl.get('plan').addEvent('click', this._select.pass(['plan'], this))
                             .addEvent('mouseover', this._hover.pass(['plan', true], this))
                             .addEvent('mouseout', this._hover.pass(['plan', false], this));
        
        this.slider.slide = this.slider.effect('left', {onStart: this._slideStart.bind(this),
                                                        onComplete: this._slideFinish.bind(this)});
        this.slider.slide.set('0px');
    },
    
    tab: function(panel) {
        this.tabs.each(function(tab) {
            link = $E('a', tab);
            if (link.href.replace(/^.*\#/, '') == panel) {
                tab.addClass('selected');
                link.addClass('selected');
            } else {
                tab.removeClass('selected');
                link.removeClass('selected');
            }
        });
        $ES('.panel').each(function(div) {
            div.addClass('inactive');
        });
        $(panel).removeClass('inactive');
        
        if (panel == 'panellocation') {
            $('tour').setStyle('display', 'none');
            this.slideShow.stop();
            townMap.show();
        } else if (panel == 'paneldescription') {
            $('tour').setStyle('display', 'block');
            townMap.hide();
            this._select('gallery');
        } else {
            $('tour').setStyle('display', 'block');
            townMap.hide();
            this.slideShow.start();
        }
        return false;
    },
    
    _hover: function(el, flag) {
        this.ctrl.get(el).hover.stop();
        this.ctrl.get(el).hover.start((flag) ? '#e5e5e5' : ((this.ctrl.get(el).flag) ? '#cbd7e6' : '#ffffff'));
    },
    
    _select: function(el) {
        this.ctrl.each(function(key, ctrl) {
            ctrl.hover.start((el == key) ? '#cbd7e6' : '#ffffff');
            ctrl.flag = (el == key);
        });

        this.slider.slide.stop();
        var x = this.pstn[el];
        if (x > 0) {
            x = -x;
        }
        this.slider.slide.start(x + 'px');
        this.view = el;
    },
    
    _slideStart: function() {
        if (this.view == 'tour') {
            $('video').setHTML('&nbsp;');
        }
    },

    _slideFinish: function() {
        if (this.view == 'tour') {
		    html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%">' +
                       '<param name="movie" value="/img/property/flvplayer.swf?' + this.video + '" />' +
                       '<param name="quality" value="high" />' +
                       '<param name="bgcolor" value="#000000" />' +
                       '<embed src="/img/property/flvplayer.swf?' + this.video + '" quality="high" bgcolor="#ffffff" ' +
                       'width="100%" height="100%" type="application/x-shockwave-flash" ' +
                       'pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
            $('video').setHTML(html);
        }
    }

});


        
