$.widget("ui.projectsearch", $.ui.dialog, {
	_dialogCreate: $.ui.dialog.prototype._create,
	_create: function() {
		var widget = this;
		widget._dialogCreate();
		var height = $(window).height();
		if (height > 690) {
			height = 690;
		}
		widget.option({
			title: 'Projektihaku',
			modal: true,
			width: 600,
			height: height,
			buttons: {
				'Peruuta' : function() {
					widget.close();
				}
			}
		});
		widget.element.load('projektit/haku',function() {
			$(this).find('input.button').removeClass('button').button();
			$(this).find('a.cancel').remove();
			$(this).find('form').addClass('projectSearchForm').submit(function() {
				widget.searchResults.html('&#160;').addClass('ajax-loading');
				$.post($(this).attr('action'), $(this).serialize(), function(data) {
					widget._handleSearchResult(data);
				}, 'json');
				return false;
			});
			widget.pagination = $('<p></p>').appendTo($(this));
			widget.searchResults = $('<div />').appendTo($(this));
		});

		widget.element.find('a.projectselect').live('click', function() {
			var tr = $(this).parent().parent();
			var projectid = tr.find('.projectid').text();
			var projectname = tr.find('.projectname').text();
			widget._trigger('projectSelected', null, {
					projectid: projectid,
					projectname: projectname
			});
			return false;
		});
		widget.element.find('a.subprojectlist').live('click', function() {
			var projectid = $(this).parent().parent().find('.projectid').text();
			widget.listSubProjects(projectid);
			return false;
		});
		widget.element.find('a.subprojectselect').live('click', function() {
			var tr = $(this).parent().parent();
			var subprojectid = tr.find('.subprojectid').text();
			var subprojectname = tr.find('.subprojectname').text();
			widget._trigger('projectSelected', null, {
				projectid: widget.projectid,
				projectname: widget.projectname,
				subprojectid: subprojectid,
				subprojectname: subprojectname
			});
			return false;
		});
	},
	_handleSearchResult: function(data) {
		var widget = this;
		if (data.errors) {
			widget.searchResults.removeClass('ajax-loading');
			$('<div />').validationerrordialog({
				validationErrors: data.errors
			});
		}
		else {
			var cacheId = data.cacheid;
			var currentPage = data.page;
			var totalPages = data.lastpage;
			var totalCount = data.count;
			var resultsperpage = data.resultsperpage;
			if (totalPages > 20) {
				totalPages = 20;
			}
			var start = (currentPage - 1) * resultsperpage + 1;
			var end = start + resultsperpage - 1;
			if (end > totalCount) {
				end = totalCount;
			}
			this.pagination.show().html('').append('Projektit ' + start + '-' + end + '/' + totalCount + ' Sivut: ');
			for (var i=1; i<=totalPages; i++) {
				var anchor = $('<a href="projektit/haku/' + cacheId + ';' + i + '"></a>');
				if (i == currentPage) {
					anchor.html('<strong>' + i + '</strong>');
				}
				else {
					anchor.text(i);
				}
				anchor.click(function() {
					$.getJSON($(this).attr('href'), function(data) {widget._handleSearchResult(data);});
					return false;
				});
				this.pagination.append(anchor).append(' ');
			}
			var searchResultTable = $('<table cellpadding="0" cellspacing="0"><tr><th>Projektinumero</th><th>Nimi</th><th>&#160;</th><th>&#160;</th></table>');
			widget.searchResults.removeClass('ajax-loading').html('')
				.append(searchResultTable);
			$.each(data.rows, function(key, project) {
				searchResultTable.append('<tr><td class="projectid">' + project.Id + '</td><td class="projectname">' + project.Name + '</td><td><a class="subprojectlist" href="#">Listaa alaprojektit</td><td><a href="#" class="projectselect">Valitse</a></td></tr>');
			});
		}
	},
	listSubProjects: function(projectid) {
		if (projectid) {
			var widget = this;
			var url = 'projektit/' + projectid + '/alaprojektit';
			widget.pagination.hide();
			widget.searchResults.html('&#160;').addClass('ajax-loading');
			$.getJSON(url, function(data) {
				if (data.errors) {
					widget.searchResults.html('').removeClass('ajax-loading');
				}
				else {
					widget.projectid = data.projectid;
					widget.projectname = data.projectname;
					var searchTable = $('<table cellpadding="0" cellspacing="0"><tr><th>Alaprojektinumero</th><th>Nimi</th><th>&#160;</th></table>');
					widget.searchResults.html('').removeClass('ajax-loading').append(searchTable);
					$.each(data.subprojects, function(key, subproject) {
						searchTable.append('<tr><td class="subprojectid">' + subproject.Id + '</td><td class="subprojectname">' + subproject.Name + '</td><td><a class="subprojectselect" href="#">Valitse</a></td></tr>');
					});
				}
			});
		}
	},
	performEmptySearch: function() {
		this.element.find('form.projectSearchForm').submit();
	}
});
