var VidBox = new Class({

    initialize: function() {

        var obj = this;

        // Select all videobox elements
        this.vids = $$('a.vidbox');

        // Create elements
        this.overlay = new Element('div', {
            'id': 'videoOverlay',
            'styles': {
                'opacity': 0.85,
                'background-color': '#000000',
                'position': 'absolute',
                'left': '0px',
                'top': '0px',
                'z-index': '10'
            }
        });
        this.vidbox = new Element('div', {
            'id': 'videoBox',
            'styles': {
                'position': 'absolute'
            }
        });
        this.vidcontainer = new Element('div', {
            'id': 'videoBoxContainer'
        });
        this.closebtn = new Element('div', {
            'id': 'videoClose'
        });
        
        // Add event listeners
        var i = 0;
        while (i <= (this.vids.length - 1)) {
            this.vids[i].store('url', this.vids[i].get('href'));
            this.vids[i].set('href', '#video');
            this.vids[i].addEvent('click', function(e){
                e = new Event(e);
                e.stop();
                obj.render_in(this);
            });
            i++;
        }
    },

    render_in: function (vid) {
        var obj = this;
        this.overlay.setStyles({
            'width': '100%',
            'height': $(document.body).getStyle('height')
        });
        
        if (vid.getProperty('rel')  == 'ytb') {
            this.vidbox.set('html', 
            '<object width="640" height="505">' +
            ' <param name="movie" value="'+vid.retrieve('url')+'&color1=0x3a3a3a&color2=0x999999"></param>' +
            ' <param name="allowFullScreen" value="true"></param>' +
            ' <param name="allowscriptaccess" value="always"></param>' +
            ' <embed src="'+vid.retrieve('url')+'&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed>' +
            '</object>'
            );
        } else {
           this.vidbox.set('html', 
                '<object width="640" height="360">' +
                ' <param name="allowfullscreen" value="true" />' +
                ' <param name="allowscriptaccess" value="always" />' +
                ' <param name="movie" value="'+vid.retrieve('url')+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" />' +
                ' <embed src="'+vid.retrieve('url')+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="360"></embed>' +
                '</object>'
           ); 
        }
        
        
        this.overlay.inject($(document.body));
        this.vidcontainer.inject($(document.body));
        this.vidbox.inject($('videoBoxContainer'));
        this.closebtn.setProperty('text', 'Lukk video');
        this.closebtn.inject($('videoBoxContainer'));
        $('videoBoxContainer').setPosition({
            position: {x: 'center', y: 'top'},
            offset: {x: 0, y: 40}
        });

        this.overlay.tween('opacity', [0,0.85]);
        this.overlay.addEvent('click', function() {
            obj.render_out();
        });

        $('videoClose').addEvent('click', function() {
            obj.render_out();
        });


    },

    render_out: function() {
        this.overlay.setStyle('opacity', [0.85,0]);
        this.overlay.destroy();
        this.vidcontainer.destroy();
        this.vidbox.destroy();
        //window.location.hash = '';
    }

});

var EnSlider = new Class({
    
    unit: null,
    current: null,
    total: null,
    //slideFx: new Fx.Tween($('entepriseContainer')),

    initialize: function(cfg) {
        var obj = this;
        this.cfg = cfg;
        
        this.unit = this.cfg.unit;
        if  ((this.cfg.el.length % this.cfg.page_size) != 0) {    // Backup:  '#enterpriseContainer li' % 16
            this.total = parseInt(this.cfg.el.length / this.cfg.page_size) + 1;
        } else {
            this.total = (this.cfg.el.length / this.cfg.page_size);
        }
        this.current = 0;         

        this.cfg.canvas.setStyles({
            height: this.cfg.unit + 'px',
            overflow: 'hidden'
        });
        this.cfg.container.setStyle('top', '0px');
        this.cfg.togglers.setStyle('visibility', 'visible');
        this.cfg.toggler_forward.addEvent('click', function() {
            obj.forward();
        });
        this.cfg.toggler_back.addEvent('click', function() {
            obj.back();
        });
        this.togglersActive();        

    },


    forward: function() {
        if (this.current > -(this.total - 1)) {
            this.cfg.container.tween('top', (this.current * this.unit) + 'px', (this.current * this.unit) - this.unit + 'px');
            this.current--;
        }
        this.togglersActive();
    },

    back: function() {
        if (this.current < 0) {
            this.cfg.container.tween('top', (this.current * this.unit) + 'px', ((this.current * this.unit) + this.unit) + 'px');
            this.current++;
        }
        this.togglersActive();
    },

    togglersActive: function() {

        if (this.current == 0 && !this.cfg.toggler_back.hasClass('disabled')) {
            this.cfg.toggler_back.addClass('disabled');
        } else if (this.current != 0 && this.cfg.toggler_back.hasClass('disabled')) {
            this.cfg.toggler_back.removeClass('disabled');
        }

        if (this.current == -(this.total - 1) && !this.cfg.toggler_forward.hasClass('disabled')) {
            this.cfg.toggler_forward.addClass('disabled');
        } else if (this.current != -(this.total - 1) && this.cfg.toggler_forward.hasClass('disabled')) {
            this.cfg.toggler_forward.removeClass('disabled');
        }
        
    }

});

window.addEvent('domready', function(){

    var vidvbox = new VidBox();
    
    if ($('enterpriseCanvas')) {
        var eslider = new EnSlider({
            unit: 288,
            page_size: 16,
            canvas: $('enterpriseCanvas'),
            container: $('enterpriseContainer'),
            el: $$('#enterpriseContainer li'),
            togglers: $('enterpriseTogglers'),
            toggler_back: $('enterpriseTogglerBack'),
            toggler_forward: $('enterpriseTogglerForward')
        });
    }

    if ($('subNavCanvas')) {
        var exslider = new EnSlider({
            unit: 540,
            page_size: 20,
            canvas: $('subNavCanvas'),
            container: $('subNavFailboat'),
            el: $$('#subNavFailboat li'),
            togglers: $('subNavTogglers'),
            toggler_back: $('subNavTogglerBack'),
            toggler_forward: $('subNavTogglerForward')
        });
    }

	$$('.fade').set('opacity', 0.85).addEvents({
		mouseenter: function(){
			this.morph({
				'opacity': 1
			});
		},
		mouseleave: function(){
			this.morph({
				'opacity': 0.85
			});
		}
	});

	$$('.fadeMore').set('opacity', 0.5).addEvents({
		mouseenter: function(){
			this.morph({
				'opacity': 1
			});
		},
		mouseleave: function(){
			this.morph({
				'opacity': 0.5
			});
		}
	});

	$$('.reason').addEvents({
		mouseenter: function(){
			this.addClass('active');
		},
		mouseleave: function(){
			this.removeClass('active');
		}
	});

    if ($('interviewByline')){
        var quoteHeight = $('interviewQuote').getSize().y;
        $('interviewByline').setStyle('margin-top', quoteHeight + 9);
    }

    if ($('interviewBackground')){
        var siteHeight = $('contentPage').getStyle('height');
        $('interviewBackground').setStyle('height', siteHeight);
    }
       
    if ($$('.overwievToggler')){
        var overviewAccordion = new Fx.Accordion($$('.overviewToggler'), $$('.overviewContent'), {
            duration: 250,
            alwaysHide: true,
            display: 0
        });
    }

    if ($$('.subNavToggler')){
        var subNavAccordion = new Fx.Accordion($$('.subNavToggler'), $$('.subNavContent'), {
            duration: 250,
            alwaysHide: true,
            onActive: function(toggler, element){
                toggler.addClass('active');
            },
            onBackground: function(toggler, element){
                toggler.removeClass('active');
            },
            display: -1 
        });
    }

    if ($$('.faqToggler')){
        var subNavAccordion = new Fx.Accordion($$('.faqToggler'), $$('.faqContent'), {
            duration: 250,
            alwaysHide: true,
            onActive: function(toggler, element){
                toggler.addClass('active');
            },
            onBackground: function(toggler, element){
                toggler.removeClass('active');
            },
            display: 0 
        });
    }
    
    if ($('interviewQuote')) {
        $('interviewQuote').addEvent('click', function(){
            var HTTPRequset = new Request.HTML({
                
                onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
                    $('interviewWrapper').innerHTML = responseHTML;
                    $('interviewWrapper').setStyle('display', 'block');
                    var siteHeight = $('contentPage').getStyle('height');
                    $('interviewBackground').setStyle('height', siteHeight);
                    $('interviewContainer').setPosition({
                        position: {x: 'center', y: 'top'},
                        offset: {x: 0, y: 20}
                    });
                    $('interviewClose').addEvent('click', function() {
                        $('interviewClose').removeEvent('click');
                        $('interviewWrapper').innerHTML = '';
                        $('interviewWrapper').setStyle('display', 'none');
                    });
                    $('interviewBackground').addEvent('click', function() {
                        $('interviewClose').removeEvent('click');
                        $('interviewWrapper').innerHTML = '';
                        $('interviewWrapper').setStyle('display', 'none');
                    });

                    new SimpleCarousel($('interviewCarousel'), $$('#interviewCarousel img.slide'), $$('#interviewCarousel li.button-slide'), {
                        slideInterval: 5000,
                        rotateAction: 'click',
                        transitionDuration: 1500
                    });
                }

            }).get('/intervju/' + $('interviewId').get('text') + '/');
        });

    }


    if ($('carousel')) {
        if ($$('#carousel img.img-slide').length > 1) {
            new SimpleCarousel($('carousel'), $$('#carousel img.img-slide'), $$('#carousel li.button-slide'), {
                slideInterval: 5000,
                rotateAction: 'click',
                transitionDuration: 1500
            });
        }
      
    }
    
    if ($('enterpriseSearch')) {
        
        //var searchTokens = new Array();
        var searchRequest = new Request.HTML({
            onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
                //alert(responseHTML);
                var searchTokens = responseHTML.split(';');
                new Autocompleter.Local('enterpriseSearchQ', searchTokens, {
                    minLength: 1,
                    overflow: true,
                    autoSubmit: true
                });

            }

        }).get('/bedriftsok/');

        new OverText($('enterpriseSearchQ'));
        $$('label.overTxtLabel').setStyles({
            top: '13px',
            left: '8px'
        });
    }


});
