/*
 *	Class: phone.history
 *	List of previous made calls. For now this is fake/hardcoded data.
 */
phone.history = {};

/*
 *	Function: setup
 *	Initiate everything for call-history. Setup vars and start populating.
 */
phone.history.setup = function() {
	phone.history.$container 			= $('#history').show();
	phone.history.$sample 				= $('.vars ul.sample').html();
	phone.history.$list		 				= phone.history.$container.find('.scroll ul').empty();
	phone.history.$no_results 		= phone.history.$list.find('.no_results').hide();
	phone.history.$search_contact = phone.history.$container.find('.search').keypress(function(){phone.history.search($(this).val())});
	phone.history.$search_reset		= phone.history.$container.find('.search_reset').button(phone.history.resetSearch).disable();

	var scroller 									= phone.history.$container.find('.scroll').jScrollPane(phone.scroller_options);
	phone.history.$scroller 			= scroller.data('jsp');

	phone.history.api = new API({resource:'users/'+user.current.id+'/calls'}); //same as phone.call.api
	phone.history.api.call({success:function(data) {
		phone.history.populate(data.calls);
	}});
};



/*
 *	Function: populate
 *	Adds contact(s) to history and setup events for it
 *	
 *	Parameters:
 *		data - Array of contact-objects, or a single contact-object
 *	
 *	See Also:
 *		<setupContact>
 *		<selectContact>
 */
phone.history.populate = function(data) {
	
	if($.isArray(data)) {
		//Multiple contacts
		var contacts = data;	
	} else {
		//Single contact (after create)
		var contacts = new Array(data);
	}

	$.each(contacts, function(index, contactData) {

		//Set data
		if($.isArray(data)) {
			var $contact = $(phone.history.$sample).appendTo(phone.history.$list);
		} else {
			var $contact = $(phone.history.$sample).prependTo(phone.history.$list);
		}
		
		$.data($contact, 'contactData', contactData);
			
		//Select contact
		$contact.find('.name p, .number p, .call').button(function() { phone.history.selectContact($contact) });
		
		//Save contact
		$contact.find('.create').button(function() {
			phone.contacts.createContact(contactData.target);
		});
							
		//Hide stuff
		$contact.find('input, .edit, .save').hide();
		
		//Name
		var name = phone.contacts.getNameByPhoneNumber(contactData.target);
		if(name) { $contact.find('.create').hide() } else {	name = I18n.misc.unknown }
		$contact.find('.name p').html(name);
		
		//PhoneNumber
		$contact.find('.number p').html($.format.phone(contactData.target, true));
		
		//Created at
		$contact.find('.date').html($.format.mysql_to_datetime(contactData.created_at));

		//Hint fields
		$contact.find('input').hint();
	});
	
	phone.history.$scroller.reinitialise();
};



/*
 *	Function: selectContact
 *	Select a contact in the list, used to setup a call
 *	
 *	Parameters:
 *		$contact - jQuery-object of the contact to select
 *	
 *	See Also:
 *		<phone.call.setTarget>
 */
phone.history.selectContact = function($contact) {

	if(phone.call.current) return false;

	if($contact.hasClass('selected')) {
		phone.history.reset();
	} else {
		phone.history.$list.find('li').removeClass('selected');
		$contact.addClass('selected').fadeTo(100,1);
		phone.history.$list.find('li').not('.no_results').not('.selected').fadeTo(200,.2);
	}
			
	var contactData = $.data($contact, 'contactData');
	phone.call.setTarget(contactData.target);
};



/* 
 *	Function: reset
 *	Bring the list back to its original state. Hide edit-forms, remove hovers, etc
 *	
 *	See Also:
 *		<phone.call.showEnd>
 */
phone.history.reset = function() {
	phone.history.$list.find('li').not('.no_results').removeClass('selected').animate({'opacity':1},100);
};


