$(function() {
	var hostKey = "classRoomHost";
	
	function commit(host) {			
		if (typeof ucbook === 'object'){
			ucbook.getProperty('loginQueryString', function(qs) {
				var url = 'http://' +host+ ':4088/quiz-receiver.html#' + qs;
				url = encodeURI(url);
				window.location = url;
			});
		} else {
			alert('error: ucbook not exits!');
			window.location = 'http://' +host+ ':4088/quiz-receiver.html';
		}
	}
	
	var url = $.url();
	var autoCommit = url.fparam('autoCommit');
	var host;
	if (localStorage && localStorage[hostKey]) 
		host = localStorage[hostKey];
	if (typeof ucbook === 'object') {
		var host2 = ucbook.getProperty('currentClassRoomHost', function(host2) {
			if (host2)
				host = host2;
			if (autoCommit === 'true' && host) {
				$('#host,#commit,#auto-config').each(function() {
					this.disabled = true;
				});
				commit(host);
			}
		});
	}
	
	$('#commit').click(function(){
		var host = $('#host')[0].value;
		if (localStorage){
			localStorage[hostKey] = host;
		}
		if (host)
			commit(host);
		else
			alert('必须输入主机名或IP。');
	});
	
	$('#auto-config').click(function(){
		if (typeof ucbook === 'object') {
			ucbook.getProperty('currentClassRoomHost', function(host) {
				if (host)
					commit(host);
				else
					alert('自动设置未成功，请稍后重试。');
			});
		}
	});
});