var Phones = {};
var Addresses = {};

function Contact(type, id){
	this.id = id + type;
	this.hidden = 0;
}
Contact.prototype = {
	id:null,
	container:null,
	data:null,
	visible: null,
	
	init: function(data) {
		var container = $('#' + this.id + ' div.tags');
		var self = this;

		var count = 0;
		var oneRowType = '';
		this.data = {};
		for (var rowType in data) {
			count++;
			oneRowType = rowType;
			this.data[rowType] = new Tag('', rowType, data[rowType]);
			this.data[rowType].disableClose();
			this.data[rowType].disableSelect();
			this.data[rowType].onclick(function(rowType){
				self.show(rowType);
			});
			container.append(this.data[rowType].getContainer());
			if ($('#' + this.id + ' div.' + rowType + '.row:hidden').length > 0)
				this.hidden++;
			else $(this.data[rowType].getContainer()).hide();
		}
		var tag = new Tag('', '', 'Done');
			tag.disableClose();
			tag.disableSelect();
			tag.onclick(function(){
				self.hideAddOptions();
				self.showAddLink();
			});
		container.append(tag.getContainer());
	},
	
	show: function(rowType) {
		var input = $('#' + this.id + ' div.' + rowType + '.row input');
			input.removeAttr('disabled');
		var container = $('#' + this.id + ' div.' + rowType + '.row');
			container.show();
			
		$(this.data[rowType].getContainer()).hide();
		this.hidden--;
		if (this.hidden == 0) {
			this.hideAddOptions();
			this.hideAddLink();
			this.hideDescription();
		}
	},
	
	hide: function(rowType, data) {
		try {
			if (!this.data) 
				this.init(data);
			$('#' + this.id + ' div.' + rowType + '.row input').attr('disabled', true);
			$('#' + this.id + ' div.' + rowType + '.row').hide();
			
			$(this.data[rowType].getContainer()).show();
			this.hidden++;
			if (this.hidden > 0 && $('#' + this.id + ' div.tags:hidden').length > 0)
				this.showAddLink();
		} 
		catch (e) {
			alert(e.message);
		}
	},
	
	showAddOptions: function(data){
		if (!this.data)
			this.init(data);
		var container = $('#' + this.id + ' div.tags').show();
		this.hideAddLink();
		
		if (this.count(this.data) == 1)
			this.show(this.getFirstKey(this.data));
	},
	
	hideAddOptions: function(){
		$('#' + this.id + ' div.tags').hide();
	},
	
	hideAddLink: function() {
		var link = $('#' + this.id + ' a.addLink');
			link.hide();
		this.showDescription();
	},
	
	showAddLink: function(){
		var link = $('#' + this.id + ' a.addLink');
			link.show();
		this.hideDescription();
	},
	
	hideDescription: function() {
		$('#' + this.id + ' span.description').hide();;
	},
	
	showDescription: function(){
		$('#' + this.id + ' span.description').show();
	},
	
	count: function(hash){
		var count = 0;
		for (var i in hash) 
			count++;
		return count;
	},
	getFirstKey: function(hash) {
		for (var i in hash)
			return i;
	}
};
