function reNifty() {
	Nifty("#content h2")
	Nifty("*.rounded")
	Nifty("*.rounded_fixed", "fixed-height")
	Nifty("*.rounded_trans", "transparent")
}

$(document).ready( function() {
	if (curTab == 'webserver') {
		// validate form fields
		preValidate()

		//alert($('form').attr('action'))

		// add a hidden field to any form indicating the project
		$('form').prepend(
			"<input type='hidden' name='projectId' value='"+projectId+"'>"
		)

		//make the submit button ajaxy
		$('form').submit(function() { 
			options = {
				beforeSubmit: function() {
					var url2 = "#TB_inline"
					url2 += "?height=400&width=500&inlineId=loading&modal=true"
					$('#loadingLog').html("Loading...")
					$('#loadingLog').append("<img src='../shared/images/loading_circle.gif' />")
					$('#loadingButton').hide()
					tb_show("foo", url2)
				},
				success: function(response) {
					var expId = response
					//$('#loadingLog').html(response)
					checkProgress($('#loadingLog'), expId)
				}
			}
			$(this).ajaxSubmit(options)
			return false
		})
	}
	reNifty()
})

function menu(tab, parameterString) {
	parameterString = parameterString || ""
	url = 'index.spy?tab=' + tab + parameterString
	window.location = url
}

function checkProgress(el, expId) {
	var done = false
	$.ajax({
		url: '../shared/cgi-bin/getProgress.spy?expId='+expId,
		success: function(html) {
			if (html !== "\n") {
				el.html(html)
				var docEl = $('#TB_ajaxContent').get(0)
				docEl.scrollTop = docEl.scrollHeight
				var doneEl = el.find("#done")[0]
				if (doneEl) {
					done = true
					var button = $('#loadingButton')
					button.show()
					var code = doneEl.getAttribute('code')
					switch(code) {
					case 'success':
						button.val('Go to Results')
						button.click(function() {
							menu('results', '&expId=' + expId)
						})
						break
					case 'toolong':
						userId = doneEl.getAttribute('param')
						button.val('Go to My Results List')
						button.click(function() {
							menu('results', '&userId=' + userId)
						})
						break
					case 'error':
						button.click(function() {
							tb_remove()
						})
						break
					}
				}
			}
        },
		complete: function() {
			if (!done) {
				window.setTimeout(function() {
					checkProgress(el, expId, false)
				}, 3000)
			}
		}
	})
}

function getResults(expId) {
	tb_remove()
}
