﻿Volvo.CWP.QuickAccess = function(id) {
    this.container = document.getElementById(id);
    this.init();
};

Volvo.CWP.QuickAccess.scrollState = { stopped: -1, up: 0, down: 1 };

Volvo.CWP.QuickAccess.prototype.init = function() {
    this.scrollcontainer = $(this.container).find('.scroll-container')[0];
    this.scrollcontainer.style.overflow = 'hidden';
    this.scrollcontainer.style.position = 'relative';
    this.ul = this.scrollcontainer.getElementsByTagName('ul')[0];
    this.ul.style.position = 'absolute';
    this.accordion = new Volvo.CWP.Accordion(this.ul, { speed: { expand: 0.3, collapse: 0} });
    this.accordion.onselecteditemchanging.subscribe(Volvo.CWP.createDelegate(this, this.handleItemChanging));
    this.accordion.onselecteditemchanged.subscribe(Volvo.CWP.createDelegate(this, this.updateDisplay));
    this.addScrollControls(this.container);
    this.state = Volvo.CWP.QuickAccess.scrollState.stopped;
    this.position = 0;
    this.updateDisplay();
};

Volvo.CWP.QuickAccess.prototype.addScrollControls = function() {
    var up = document.createElement('a');
    up.className = 'scroll-up';
    up.onmouseover = Volvo.CWP.createDelegate(this, this.handleScrollOver);
    up.onmouseout = Volvo.CWP.createDelegate(this, this.handleScrollOut);
    var h3 = this.container.getElementsByTagName('h3')[0];
    h3.insertBefore(up, h3.childNodes[0]);

    down = document.createElement('a');
    down.className = 'scroll-down';
    down.onmouseover = Volvo.CWP.createDelegate(this, this.handleScrollOver);
    down.onmouseout = Volvo.CWP.createDelegate(this, this.handleScrollOut);
    var more = $(this.container).find('.more')[0];
    more.appendChild(down);

    this.up = up;
    this.down = down;
};

Volvo.CWP.QuickAccess.prototype.handleItemChanging = function(type, args) {
    var position = 0;
    for (var i = 0; i < args[0].index; i++) {
        position -= $(this.accordion.items[i]).find('.head-item')[0].offsetHeight;
    }
    this.scrollTo(position);
};

Volvo.CWP.QuickAccess.prototype.scrollBy = function(amount) {
    this.scrollTo(this.position + amount);
};

Volvo.CWP.QuickAccess.prototype.scrollTo = function(position) {
    this.position = position;
    this.ul.style.top = position + 'px';
    this.updateDisplay();
};

Volvo.CWP.QuickAccess.prototype.scroll = function() {
    if (this.state == Volvo.CWP.QuickAccess.scrollState.up && !this.isAtTop()) {
        this.scrollBy(2);
        window.setTimeout(Volvo.CWP.createDelegate(this, this.scroll), 5);
    } else if (this.state == Volvo.CWP.QuickAccess.scrollState.down && !this.isAtBottom()) {
        this.scrollBy(-2);
        window.setTimeout(Volvo.CWP.createDelegate(this, this.scroll), 5);
    }
};

Volvo.CWP.QuickAccess.prototype.isAtTop = function() {
    return this.position >= 0;
};

Volvo.CWP.QuickAccess.prototype.isAtBottom = function() {
    return this.position + this.ul.offsetHeight < this.scrollcontainer.offsetHeight;
};

Volvo.CWP.QuickAccess.prototype.updateDisplay = function() {
    if (this.isAtTop()) {
        $(this.up).addClass('scroll-disabled');
    } else {
        $(this.up).removeClass('scroll-disabled');
    }
    if (this.isAtBottom()) {
        $(this.down).addClass('scroll-disabled');
    } else {
        $(this.down).removeClass('scroll-disabled');
    }
};

Volvo.CWP.QuickAccess.prototype.handleScrollOver = function(e) {
    var entered = e && e.target || window.event.srcElement;
    if (entered.className.indexOf('down') > -1) {
        this.state = Volvo.CWP.QuickAccess.scrollState.down;
        this.scroll();
    } else if (entered.className.indexOf('up') > -1) {
        this.state = Volvo.CWP.QuickAccess.scrollState.up;
        this.scroll();
    }
};

Volvo.CWP.QuickAccess.prototype.handleScrollOut = function(e) {
    this.state = Volvo.CWP.QuickAccess.scrollState.stopped;
};