﻿Volvo.CWP.ApplicationLogin = function(id) {
    this.container = document.getElementById(id);
    this.init();
};

Volvo.CWP.ApplicationLogin.prototype.init = function() {    
    this.links = $(this.container).find('.appList li');
    this.links.click(Volvo.CWP.createDelegate(this, this.handleClick));
    this.items = $(this.container).find('.loginitem');
    this.items.hide();
};

Volvo.CWP.ApplicationLogin.prototype.handleClick = function(e) {
    var li = e.target;
    if (li.nodeName.toUpperCase() != 'LI') {
        li = $(li).parents('li').first().get(0);
    }
    var index = $(li).parents('span').first().find('li').index(li);
    this.showItem(index);
};

Volvo.CWP.ApplicationLogin.prototype.showItem = function(index) {
    $(this.container).find('div.appLoginBox').last().find('h3').first().hide();
    this.items.hide();
    $(this.items[index]).show();
    this.links.removeClass('active');
    $(this.links[index]).addClass('active');

    // set name properties of username and password boxes to user-supplied values
    var userCtrlId = $(this.items[index]).find('li.username-ctrl-id');
    var passCtrlId = $(this.items[index]).find('li.password-ctrl-id');
    var userBox = $(this.items[index]).find(':text').get(0);
    var passBox = $(this.items[index]).find(':password').get(0);
    var hiddenItems = $(this.items[index]).find('input[type=hidden]');
    hiddenItems.each(function(i, e) {
        e.name = $(e).attr('data');
    });
    userBox.name = userCtrlId.html();
    passBox.name = passCtrlId.html();
};