function login_form() {
    var _this = this;
    this.form = $('#login form');

    this.hide = function() {
        $('#layer, #login').fadeOut(500);
    }
    this.show = function() {
        $('#layer, #login').fadeIn(500);
        $('#loginClose').click(function(e){
            _this.hide();
            $('#login form:eq(0)').submit(function() {
                _this.submit()
            });
            e.preventDefault();
        });
		
		$(function(){
            //if ($.browser.msie) {
                $('#login').delegate('input:text, input:password', 'keypress', function(e){
                    if(e.keyCode == 13) {
                        _this.submit();
                    }
                });
           // }
        });
    }

    this.submit = function() {
        $.post(
            _this.form.attr('action'),
            _this.form.serialize(),
            function(data){
                _this.form.html(data);
            }
        );
    }
}

function _send_form() {
    var _this = this;

    this.hide = function() {
        $('#layer, #sendPM').fadeOut(500);
    }
    this.show = function() {
        $('#layer, #sendPM').fadeIn(500);

    }

    this.submit = function(to) {
		var button = document.getElementById("replaceButtonSwf");
		var text = $('#sendPM textarea').val();
		if(!text) {
			alert('Please enter a message');
		}
		else {
			button.sendPM(to,text); 
			alert('Message sent');
			_this.hide();
		}
		
    }
}

function callme() {
    var _this = this;
	var _status = 'offline';
	
    this.form = $('#callme');

    this.hide = function() {
        $('#layer, #callme').fadeOut(500);
    }
	
    this.show = function(to) {
        if(_this.status() == 'online') {
			$('#layer, #callme').fadeIn(500);
			var button = document.getElementById("replaceButtonSwf");
			button.sendOutgoingCall(to);
		}
    }
	this.status = function(stat) {
		if(stat) {
			_this._status = stat;
			curuser.status = stat;
			$("#userstatus").attr('class', _this._status);
			$("#callMeBtn").removeClass('gbttn').removeClass('bttn-grey-small').removeClass('nopoint');
			if(_this._status == 'online')
				$("#callMeBtn").addClass('gbttn').addClass('nopoint');
			else
				$("#callMeBtn").addClass('bttn-grey-small');
		}
		else {
			return _this._status;
		}
	}
}

function incomingMessage() {
    var _this = this;
    this.form = $('#message');

    this.hide = function() {
        $('#layer, #message').fadeOut(500);
    }
	
    this.show = function() {
        $('#layer, #message').fadeIn(500);
    }
}

function incomingCall() {
    var _this = this;
    this.form = $('#yesno');
	this.sec = 0;
	this.min = 0;
	this.timer = undefined;
	this.button = document.getElementById("replaceButtonSwf");
	this.answerto = undefined;

    this.hide = function() {
        $('#layer, #yesno').fadeOut(500);
    }
	
    this.show = function(title, to) {
		var text = 'Answer to '+to+'?'
		this.answerto = to;
		$('#incomingTitle').html(title);
		$('#incomingText').html(text);
		$('#yesno a.bttn-green').show();
        $('#layer, #yesno').fadeIn(500);
    }
	
	this.accept = function() {
		/*_this.sec = 0;
		_this.min = 0;
		_this.timer = setInterval(function(){_this.__timedisplay();},1000);
		$('#yesno a.bttn-green').hide();*/
		
		this.hide();
		button = document.getElementById("replaceButtonSwf");
		button.sendCallAccepted(_this.answerto);
	    button.disconnectClient();
		
	    //swfCreate(_this.answerto);
		window.showFullViewCallAccepted(_this.answerto);
	}
	
	this.deny = function() {
		this.hide();
		clearInterval(_this.timer);
		button = document.getElementById("replaceButtonSwf");
		button.vphoneRequestDenied(_this.answerto);
		$('#calltimer').html('');
	}
/*	
	this.__timedisplay = function()
	{
		if (_this.sec>=59) {
			_this.sec=0;
			_this.min+=1;
		}
		else
			_this.sec+=1;
		_min = _this.min<10?'0'+_this.min:_this.min;
		_sec = _this.sec<10?'0'+_this.sec:_this.sec;
		$('#calltimer').html(_min+":"+_sec);
	}*/
}



var _forms = [];
$(document).ready(function(){
    _forms.login = new login_form();
    _forms.sendPM = new _send_form();
	_forms.callme = new callme();
	_forms.incomingMessage = new incomingMessage();
	_forms.incomingCall = new incomingCall();
});



