;/**
 * *****************************************************************************
 *
 * Ytong Bausatzhaus Main Stage
 *
 *
 * @package:    zo.stage
 * @requires:   jQuery 1.4.2
 * @version:    0.1
 * @copyright:  c 2011 zeros+ones, Agentur fuer neue Medien GmbH
 *
 * *****************************************************************************
 */
/* define namespace */
var zo; // declares a global symbol
if (!zo) zo = {};
    else if (typeof zo != 'object')
        throw new Error('zo exists, but is not an object!');
zo.stage = {
    stage: null
    , menu: null
    , overlay: null
    , overlayMenu: null
    , relRE: /\w*#(\w+)/
    , init: function() {
        this.stage = jQuery('#homeStage');
        this.menu = jQuery('#homeStageObjMenu');
        this.overlay = jQuery('#homeStageOverlay');
        this.overlayMenu = jQuery('#homeStageOverlayMenu');
        this.initStage();
        this.initMenu();
        this.initOverlayMenu();
    }
    , initStage: function() {
        this.stage.find('.homeStageObj:not(:first)').hide();
    }
    , initMenu: function() {
        var _this = this
            , $links = this.menu.find('a')
        ;
        if (!$links.filter('.active').length) {
            $links.eq(0).addClass('active');
        }
        $links.live('click', function(ev) {
            ev.preventDefault();
            var $this = jQuery(this);
            if (!$this.hasClass('active')) {
                $links.filter('.active').removeClass('active');
                $this.addClass('active');
                _this.toggleStage({ active:$this });
            }
        }).end().animate({ bottom:0 }, 1000);
    }
    , toggleStage: function(opts) {
        var _this = this
            , rel = opts.active.attr('href').match(this.relRE)[1]
        ;
        this.stage
            .find('.homeStageObj:visible').css({ 'z-index':1 })
            .end()
            .find('#' +rel).hide().css({ 'z-index':2 }).fadeIn('slow', function() {
                _this.stage.find('.homeStageObj:visible:not(#' +rel+ ')').hide();
            })
        ;
    }
    , initOverlayMenu: function() {
        var _this = this
            , $links = this.overlayMenu.find('a')
        ;
        $links.bind('click', function(ev) {
            ev.preventDefault();
            var $this = jQuery(this);
            if (!$this.hasClass('active')) {
                $links.filter('.active').removeClass('active');
                $this.addClass('active');
                _this.menu.animate({ bottom:-74 }, 400);
                _this.showOverlay({ active:$this });
            } else {
                $this.removeClass('active');
                _this.hideOverlay();
                _this.menu.animate({ bottom:0 }, 500);
            }
        });
    }
    , showOverlay: function(opts) {
        var rel = opts.active.attr('href').match(this.relRE)[1];
        jQuery('div.youtubeBox').hide();
        jQuery('div.youtubeLink').hide();
        this.overlay
            .find('.homeStageOverlayItem:visible').hide()
            .end()
            .find('#' +rel).show()
            .end()
            .show()
        ;
    }
    , hideOverlay: function() {
        jQuery('div.youtubeBox').show();
        jQuery('div.youtubeLink').show();
        this.overlay
            .find('.homeStageOverlayItem:visible').hide()
            .end()
            .hide()
        ;
    }
};
zo.stageScroller = {
    init: function() {
        var self = this;
        this.$wrapper = jQuery('<div id="homeStageObjWrapper"></div>');
        this.$scroller = jQuery('<div id="homeStageObjScroller"></div>');
        this.$list = jQuery('#homeStageObjMenu > ul:first');
        this.position = 0;
        setTimeout(function() {
            self.scrollerWidth = self.$list.find('li').width() * self.$list.find('li').length;
            self.buildScroller();
        }, 300);
        return this;
    },
    buildScroller: function() {
        this.$scroller.append(this.$list.clone())
                      .width(this.scrollerWidth);
        this.$wrapper.append(this.$scroller);
        this.$list.replaceWith(this.$wrapper);

        zo.stage.init();
        if (this.scrollerWidth > this.$wrapper.width()) {
            this.registerHooks();
        }
        return this;
    },
    registerHooks: function() {
        var self = this;
        this.$wrapper.bind('mousemove', function(event) {
            var outerToInnerRelation = (self.scrollerWidth - self.$wrapper.width()) / self.$wrapper.width(),
                currentMouseposition = event.clientX - self.$wrapper.offset()['left'],
                interpolatedPosition = currentMouseposition * outerToInnerRelation;
            if (interpolatedPosition <= (self.scrollerWidth - self.$wrapper.width())) {
                self.$scroller.css('left', -interpolatedPosition);
            }
        });
        return this;
    }
}
