var statuses = { 1:'In Progress', 2:'Waiting', 3:'Done' };

var each = function(array, fn, scope) {
	var s = window;
	if (scope) {
		s = scope;
	}
	for (var i = 0; i < array.length; i++) {
		var result = fn.call(s, array[i]);
		if (result === false) {
			return;
		}
	}
};

var map = function(keys, values) {
	var obj = {};
	for (var i = 0; i < keys.length; i++) {
		obj[keys[i]] = values[i];
	}
	if (!obj.id || obj.id == -1) {
		obj.id = generateId();
	}
	return obj;
};

var showIssueListView = function(projectName) {
	couchStore.loadIssues(projectName);
};

var _generateIssueList = function(issues) {
	var html = '';
	var lastStatus = -1;
	each(issues, function(issue) {
		if (lastStatus != issue.status) {
			lastStatus = issue.status;
			html += '<div class="status-header">' + statuses[issue.status] + '</div>';
		}
		html += '<div class="issue status-' + issue.status + '" id="issue-' + issue.id + '" onClick="fnEdit(' + issue.id + ');">';
		html += '<div class="issue-id"><span class="hash">#</span>' + issue.id + '</div>';
		html += '<div class="summary">' + issue.summary + '</div>';
		if (issue.status == 1) {
			html += '<div class="bp">' + issue.businessPoints + '</div>';
		} else if (issue.status == 2) {
			html += '<div class="bp">' + issue.businessPoints + '</div>';
			html += '<div class="sp">' + issue.storyPoints + '</div>';
		} else if (issue.status == 3) {
			html += '<div class="sp">' + issue.storyPoints + '</div>';
		}
		html += '</div>';
	});
	return html;
};

var _showIssueListView  = function(store, ignoreShow) {
	var result = "<div class='header'>Issue List</div>";
	list = Ext.get('issueList');
	list.replaceWith({
		id: 'issueList',
		tag: 'div',
		cls: 'fluffContent',
		html: _generateIssueList(store.getIssues())
	});
	if (ignoreShow !== true) {
		showView('issueListView');
		list.setVisible(true, true);
	}
};

var currentViewInDisplay = null;
/**
 * Also shows the controls for the view.
 * @param {String} viewToShow
 */
var showView = function(viewToShow) {
	if (currentViewInDisplay != viewToShow && currentViewInDisplay) {
		debug('hiding ' + currentViewInDisplay);
		Ext.get(currentViewInDisplay).removeClass('fluff-show');

		var controls = currentViewInDisplay.substr(0, currentViewInDisplay.length - 4) + 'Controls'; 
		debug('hiding ' + controls);
		Ext.get(controls).removeClass('fluff-show');
	}
	currentViewInDisplay = viewToShow;
	debug('showing ' + viewToShow);
	Ext.get(currentViewInDisplay).addClass('fluff-show');
	Ext.get(currentViewInDisplay.substr(0, currentViewInDisplay.length - 4) + 'Controls').addClass('fluff-show');
};

var showDialog = function(dialogToShow) {
	debug('showing ' + dialogToShow);
	Ext.get(dialogToShow).addClass('fluff-show');
};
var hideDialog = function(dialogToHide) {
	debug('showing ' + dialogToHide);
	Ext.get(dialogToHide).removeClass('fluff-show');
}

var couchStore = new CouchStorage();
couchStore.on('load-project', _showIssueListView);
couchStore.on('load-projects', function() { Ext.get('projectName').replaceWith({tag:'span', id:'projectName', html: ''})});
couchStore.on('load-project', function() { Ext.get('projectName').replaceWith({tag:'span', id:'projectName', html: couchStore.getProjectName()}) });
couchStore.on('update', function(store) { _showIssueListView(store, true)});

Logger = {
	log: function(msg) {
		if (window.console && window.console.firebug) {
			console.log(msg);
		}
	},
	
	logEvent: function(event, obj) {
		Logger.log((this.id||this) + "." + event);
	},
	
	listenTo: function(object) {
		Ext.util.Observable.capture(object, this.logEvent, object);
	}
}
debug = function(msg) {
	Logger.log(msg);
}


