/*
 *	Class: phone
 */
var phone = {};
		phone.scroller_options = {verticalDragMinHeight:10};
/*
 *	Function: setup
 *	Initiates all components and fixes some iPhone-specific styling
 *	
 *	See Also:
 *		<phone.top>
 *		<phone.middle>
 *		<phone.bottom>
 */
phone.setup = function() {
	console.log('phone.setup() user:');
	console.log(user.current);
	
	$('input[type=text]').hint();
	phone.$container = $('#phone');
	
	phone.top.setup();
	phone.middle.setup();
	phone.bottom.setup();
	phone.language.setup();
	
	if(user.current) {
		phone.keypad.setup();
		phone.call.setup();
		phone.contacts.setup();
		//Done by contacts.setup: phone.history.setup(); 
		phone.credit.setup();
		phone.showSection('keypad');
	} else {
		phone.login.setup();
		phone.register.setup();
		phone.showSection('login', 'contacts');
	}
			
	$('input').live('keydown', function(event) {
		if(event.keyCode == 9) {
			var $section = $(this).parents('section');

			var allowed_types = 'input[type=text], input[type=password], input[type=checkbox]';
			var current	= $section.find(allowed_types).index(this);
			var amount 	= $section.find(allowed_types).length;

			if(!event.shiftKey 	&& 	current == amount - 1) 	return false;
			if(event.shiftKey 	&& 	current == 0) 					return false;			
		}
	});
};


/*	
 *	Function showSection
 *	Slides content of the phone
 *	
 *	Parameters:
 *		section - Classname of the section to show (string)
 *	
 *	See Also:
 *	 <phone.keypad>
 *	 <phone.contacts>
 *	 <phone.history>
 *	 <phone.credit>
 */
phone.showSection = function(section, active_button) {
	console.log('phone.showSection()', section);
	
	//Reset lists
	if(phone.contacts.$list) 	phone.contacts.reset();
	if(phone.history.$list)		phone.history.reset();
	
	//animate active
	var target_button 	= (active_button) ? active_button : section;
	var $target_button 	= phone.top['$'+target_button];
	
	if(typeof $target_button != 'undefined') {
		var active_left 		= $target_button.css('left');
		if(active_left == 'auto' || active_left == '0px') {
			phone.top.$active.stop().animate({'opacity':0}, 200);	
		} else {
			phone.top.$active.stop().animate({'opacity':1, 'left':active_left},200);
		}
	}
	
	//animate sections
	var sections_left = 0 - parseInt(phone[section].$container.css('left')) - 2;
	phone.middle.$sections.stop().animate({'margin-left':sections_left},200);
	
	//Update class
	phone.middle.$sections.removeClass('active');
	phone.middle.$sections.filter('#'+section).addClass('active');
}
