function bindDeleteHandlers(){
	$("a.delete-button").unbind("click");
	
	$("a.delete-button").click(function(event){
		$('#delete-confirm')
			.dialog( "option" , "href", $(event.currentTarget).attr("href"))
			.dialog('open');
		return false;
	})
}

function buildDeleteConfirmButtons(){
	$('#delete-confirm').dialog({
		resizable: false,
		height: 190,
		width: 300,
		modal: true,
		autoOpen: false,
		draggable: false,
		buttons: {
			Ok: function() {
				$(location).attr("href", $(this).dialog("option", "href"));
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
}

function redrawProfileAnswers(){
	$(".profile-answers-group").each(function(){
		$('.q-check-all', this).bind('click',
			{group: this},
			function(event){
				this.checked = true;
				$('.q-check', event.data.group)
					.each(function(){
						this.checked = false;
					});
			}
		);
		
		$('.q-check', this).bind('click',
			{group: this},
			function(event){
				var extistsNotCheckedItem = false;
				var setChecksFlag = function(){
					if(this.checked == false){
						extistsNotCheckedItem = true;
					}
				}
				$('.q-check', event.data.group)
					.each(setChecksFlag);
				
				if(extistsNotCheckedItem === false){
					$('.q-check', event.data.group)
						.each(function(){
							this.checked = false;
						});
					
					$('.q-check-all', event.data.group)
						.each(function(){
							this.checked = true;
						});
				}
				else {
					$('.q-check-all', event.data.group)
						.each(function(){
							this.checked = false;
						});
				}
			}
		);
	});
}

function msgSystem(params){

	$("<div/>").attr("id", "msg-info").hide().appendTo(document.body);
	$("<div/>").attr("id", "msg-error").hide().appendTo(document.body);
	
	var dialogButtons = {};
	dialogButtons[params.closeTitle] = function(){
		$(this).html("");
		$(this).dialog('close');
	}
	
	$('#msg-info,#msg-error').dialog({
		modal: false,
		autoOpen: false,
		resizable: false,
		draggable: true,
		buttons: dialogButtons
	});
	
	this.addError = function(value){
		$("#msg-error").append(
			$("<div></div>")
				.addClass("msg-item")
				.addClass("error")
				.append(value)
		);
	};
	
	this.addInfo = function(value){
		$("#msg-info").append(
			$("<div></div>")
				.addClass("msg-item")
				.addClass("info")
				.append(value)
		);
	};
	
	this.emptyErrors = function(){
		$("#msg-error").html("");
	};
	
	this.emptyInfo = function(){
		$("#msg-info").html("");
	};
	
	this.showErrors = function(){
		$("#msg-error").dialog('open');
	};
	
	this.showInfos = function(){
		$("#msg-info").dialog('open');
	};
}

function doFlash(userId){
	$.ajax({
		url: '/flash/action:doFlash/',
		dataType: 'json',
		data: {
			userId: userId,
			ajax: 1
		},
		type: 'POST',
		success: function(data){
			if(data.infos.length){
				$.each(data.infos, function(index, value) {
					wInfo.addInfo(value);
				});
				wInfo.showInfos();
			}
					
			if(data.errors.length){
				$.each(data.errors, function(index, value) {
					wInfo.addError(value);
				});
				wInfo.showErrors();
			}
		}
	});
}

$(document).ready(function(){
	
	$("input.ui-button, a.ui-button").button();
	
	buildDeleteConfirmButtons();
	bindDeleteHandlers();
	redrawProfileAnswers();
});