/*
 *	Class: phone.keypad
 */
phone.keypad = {};


/*
 *	Function: setup
 *	Initiate everything for call. Setup API, buttons, vars, etc.
 */
phone.keypad.setup = function() {
	phone.keypad.$container 			= $('#keypad').show();
	phone.keypad.$number 					= phone.keypad.$container.find('.result .number').hint();
	phone.keypad.$backspace				= phone.keypad.$container.find('.result .backspace').button(phone.keypad.backspace);
	phone.keypad.$create_contact	= phone.keypad.$container.find('.result .create_contact').button(function() { phone.contacts.createContact(phone.keypad.$number.val()) });

	phone.keypad.default_value 	= phone.keypad.$number.attr('title');
	phone.keypad.focus					= 'TARGET';
	
	phone.keypad.$container.find('.key').disableTextSelect().click(function() {
		phone.keypad.addValue($(this).find('h1').text());
	});
	
	phone.keypad.$number.keyup(phone.keypad.updateButtons);
	
	phone.keypad.updateButtons();
};



/*
 *	Function: addValue
 *	Add the given number. If owner-input is focussed it will set the owner instead of the target.
 *	
 *	Parameters:
 *		value - The selected number (string)
 *	
 *	See Also:
 *		<phone.call.setTarget>
 *		<phone.call.setOwner>
 */
phone.keypad.addValue = function(value) {
	if(phone.keypad.focus == 'TARGET') {
		//TARGET
		if(phone.keypad.$number.val() == phone.keypad.default_value) phone.keypad.$number.val('');
		if(phone.keypad.$number.val().length<10)	phone.keypad.$number.val(phone.keypad.$number.val() + value);	
		phone.call.setTarget(phone.keypad.$number.val());
		phone.keypad.$number.removeClass('blank').setCursorPosition(phone.keypad.$number.val().length);
	} else {
		//OWNER
		phone.top.$number_input.focus();
		if(phone.top.$number_input.val() == phone.top.$number_input.attr('title')) phone.top.$number_input.val('');
		var new_value = phone.top.$number_input.val() + value;
		if(phone.top.$number_input.val().length<10)	{
			phone.top.$number_input.val(new_value);
			phone.top.$number_input.removeClass('blank');
		}
		phone.call.setOwner(phone.top.$number_input.val());
	}
	phone.keypad.updateButtons();
}



/*
 *	Function: backspace
 *	Remove the last digit from the selected number
 */
phone.keypad.backspace = function() {

	if(phone.keypad.focus == 'TARGET') {
		//TARGET
		var value = phone.keypad.$number.val().substr(0, phone.keypad.$number.val().length-1);
		phone.keypad.$number.val(value);
		
		console.log(phone.keypad.$number.val());
		if(phone.keypad.$number.val() == '') {
			phone.keypad.$number.val(phone.keypad.default_value);
			if(phone.keypad.$container.find('.result .number:focus').length>0) phone.keypad.$number.setCursorPosition(0).addClass('blank');
		}
		phone.call.setTarget(value);
	} else {
		//OWNER
		phone.top.$number_input.focus();
		if(phone.top.$number_input.val() != phone.top.$number_input.attr('title')) {
			var value = phone.top.$number_input.val().substr(0, phone.top.$number_input.val().length-1);		
			phone.top.$number_input.val(value);
			if(phone.top.$number_input.val() == '') {
				phone.top.$number_input.val(phone.top.$number_input.attr('title'));
				phone.top.$number_input.addClass('blank');
			}
			phone.call.setOwner(phone.top.$number_input.val());		
		}
	}
	phone.keypad.updateButtons();
}



/*
 *	Function: updateButtons
 */
phone.keypad.updateButtons = function() {
	if(phone.keypad.$number.val() == phone.keypad.default_value) {
		console.log('disable buttons');
		phone.keypad.$create_contact.disable();		
		phone.keypad.$backspace.disable();		
	} else {
		phone.keypad.$create_contact.enable();
		phone.keypad.$backspace.enable();		
	}
}



/*
 *	Function: reset
 *	Bring the keypad back to its original state.
 */
phone.keypad.reset = function() {
	phone.keypad.$number.val('');
	phone.keypad.updateButtons();
};
