/*
 GOBEHEMOTH, All rights reserverd.
 Developed by sedgar: sedgar@gobehemoth.com
*/

var Application = {};

function yshout_onReady(chatObj) {
    if (Application.gunControl) {
        var inputs = $(':text, textarea');
        inputs.focusin(function(){Application.gunControl.controller.enable(false)});
        inputs.focusout(function(){Application.gunControl.controller.enable(true)});
    } else {
        var loginFrm = $('#loginForm');
        if (loginFrm.length) {
            var btn = $('#ys-input-submit');
            if (btn.length) {
                btn.attr('disabled', 'disabled');
                $('#ys-input-message').attr('disabled', 'disabled');
                $('#ys-input-nickname').attr('disabled', 'disabled');
            }
        }
    }
}

var UserInfo = new Class({ 
	Implements: [Options], 
	
	options: {
		shotsPanels:null
    },

    initialize: function (options) {
        this.setOptions(options);
        this.setData(options);
    },
    
    initShots: function() {
        this.shots = 0;
        if (this.options.shotsPanels) {
            var panel = false;
            // if array, use first element
            if (this.options.shotsPanels.length) {
                panel = this.options.shotsPanels[0];
            } else {
                panel = this.options.shotsPanels
            }
            
            if (panel) {
                panel = $(panel);
                if (panel) {
                    this.shots = parseInt(panel.innerHTML);
                } else {
                	throw('Panel for shots is undefined.');
                }
            }
        }
    },

    setData: function(data) {
        this.shots = Object.intval(data.shots);
        this.bonusShots = Object.intval(data.bonusShots);
        this.userName = data.username;
        this.userId = data.id;
    },
    
    dataBind: function() {
        this.updateShotsPanels();
        //$(this.options.userNamePanelId).update(this.userName);
        $(this.options.userNamePanelId).text(this.userName);
    },

    isCurrentUser: function(userObj) {
        return userObj && (Object.intval(userObj.isCurrentUser) == 1 || userObj.id == this.userId);
    },

    getShotsCount: function() {
        return this.shots;
    },
    
    setShotsCount: function(count) {
        if (typeof(count) == 'string') {
            count = parseInt(count);
        }
        
        if (Object.isNumber(count) && count >= 0) {
            this.shots = count;
            this.updateShotsPanels();
        }
    },
    
    decrementShots: function() {
        if (this.shots > 0) {
            this.setShotsCount(this.shots - 1);
        }
        return this.shots;
    },
    
    updateShotsPanels: function() {
        //if (Object.isArray(this.options.shotsPanels)) {
        if (this.options.shotsPanels.length) {
            for(var i = 0; i < this.options.shotsPanels.length; i++) {
                var panel = $(this.options.shotsPanels[i]);
                if (panel.length) {
                    //panel.update(this.shots);
                    panel.text(this.shots);
                }
            }
        }
    }
});

$(document).bind('ready', function() {
    //Application.userInfo.initShots();

    // initially hide all containers for tab content
    Application.loginForm = new LoginForm({formId:'#loginForm', messageId: '#messagesPanel'});
    Application.signupForm = new SignupForm({formId:'#signupForm', messageId: '#signupMessagesPanel'});
    Application.forgotForm = new ForgotForm({formId:'#forgotForm', messageId: '#forgotMessagesPanel'});
    Application.changeForm = new WebUserForm({formId:'#changeForm', messageId: '#changeMessagesPanel'});

    Application.cstTimer = new CSTTimer();
    Application.cstTimer.start('#cstTimeLabel');

});

