var Header = {
	addFade : function(selector){
		$("<span class=\"fake-hover\"></span>").css("display", "none").prependTo($(selector)); 
		$(selector+" a").bind("mouseenter",function(){
			$(selector+" .fake-hover").fadeIn("slow");
		});
		$(selector+" a").bind("mouseleave",function(){
			$(selector+" .fake-hover").fadeOut("slow");
		});
	}
};

$(function(){
	Header.addFade("#logo");
});

// ui functions
$(document).ready(function() {
	$(".signup").toggle(
	    function() {
	        $("#content_sign").fadeIn();
			$("#content_body").toggle();
	    },
	    function() {
	        $("#content_sign").toggle();
			$("#content_body").fadeIn();
	    }
	);
	
	$(".forgot").click(function(){
	        $("#forgot_password").fadeIn(); 
			$("#login_box").toggle();
			return false;
	    }
	);
				
	$(".login").click(function(){
	        $("#login_box").fadeIn(); 
			$("#forgot_password").toggle();
			return false;
	    }
	);

});


// new exam functions
var total_questions = 0;
var total_alternatives = new Array();

function right_question(element, parent){
	alert('dever: atribuir ' + element.match(/[\d\.]+/g)[0] + ' no hidden de ' + parent);
	hidden = parent + 'h';
	hidden.value = element;
}

function add_question(){
	var id = null;
	var id_question = (total_questions + 1);
	var id = "p" + id_question;
	
	var label = document.createElement("label");
	label.innerHTML = "Pergunta " + id_question + ".";

	var input = document.createElement("input");
	input.type = "text";
	input.name = "exam[question_attributes][][question]";
	input.size = "46";
	input.id = id;
	
	var hidden = document.createElement("input");
	hidden.type = "hidden";
	hidden.name = "exam[question_attributes][][right]";
	hidden.id = id + 'h';
	hidden.value = "";
	var link = document.createElement("a");
	link.innerHTML = "Adicionar resposta";
	link.href = "javascript:add_alternative(" + id_question + ")";

	var ol = document.createElement("ol");
	ol.type = "a";
	ol.id = "ol" + id_question;
	
	var div = document.createElement("div");
	div.style.clear = "both";
	
	var li = document.createElement("li");
	li.id = "div_" + id;
	var delete_question = document.createElement("a");
	delete_question.href = "javascript:delete_question('div_" + id + "')";
	delete_question.innerHTML = "Remover";
	
	li.appendChild(div);
	
	alternatives = document.createElement("div");
	alternatives.style.clear = "both";
	alternatives.style.margin = "0 0 0 10px";
	
	//for (i = 0; i < 5; i++){
	//	temp = i + 1;
	//	var label_alternatives = document.createElement("label");
	//	label_alternatives.innerHTML = "Resposta " + temp + ".";
    //
	//	var radio_alternatives = document.createElement("input");
	//	radio_alternatives.type = "radio";
	//	radio_alternatives.size = "46";
	//	radio_alternatives.id = 'alternative_' + this.i;
	//	radio_alternatives.onchange = function() { right_question(this.id,input.id) }
    //
	//	var input_alternatives = document.createElement("input");
	//	input_alternatives.type = "text";
	//	input_alternatives.name = "exam[alternative_attributes][][alternative]";
	//	input_alternatives.size = "46";
	//	
	//	alternatives.appendChild(label_alternatives);
	//	alternatives.appendChild(radio_alternatives);
	//	alternatives.appendChild(input_alternatives);
	//}
	
	var p_ol = document.getElementById("perguntas").appendChild(li);
	div.appendChild(label);
	div.appendChild(input);
	div.appendChild(hidden);
	//div.appendChild(select);
	div.appendChild(delete_question);
	div.appendChild(link);
	div.appendChild(ol);
	div.appendChild(alternatives);
	

	total_questions++;
	total_alternatives[id_question] = 0;
}

function add_alternative(question){
	total_alternatives[question] += 1;

	var id = "ol" + question; // seta o id da lista de acordo com a pergunta
	var ol = document.getElementById(id); // seleciona lista a ser usada
	var li = document.createElement("li"); // cria o item a ser adicionado na lista
	li.id = "li_p" + question + "r" + total_alternatives[question];
	li.className = "alternative";

	var radio = document.createElement("input");
	radio.id = "p" + question + "c" + total_alternatives[question];
	//radio.type = "radio";
	radio.setAttribute("type", "radio");
	//radio.setAttribute("onclick", right_question(radio.id));
	
	//var input = document.createElement("input");
	//input.id = "p" + question + "r" + total_alternatives[question];
	//input.size = "44";
	//input.type = "text";
	
	var input = document.createElement("input");
	input.type = "text";
	input.name = "exam[alternative_attributes][][alternative][" + question + "]";
	input.size = "46";

	var remover = document.createElement("a");
	remover.innerHTML = "Remover ";
	remover.href = "javascript:delete_alternative('" + question +"','" + total_alternatives[question] + "')";

	ol.appendChild(li).appendChild(radio);
	ol.appendChild(li).appendChild(input);
	li.appendChild(remover);
}

function delete_alternative(question, alternative){
	var p = document.getElementById("ol" + question); 
	var r = document.getElementById("li_p" + question + "r" + alternative); 
	p.removeChild(r);
}

function delete_question(question){
	if(confirm("Tem certeza que deseja remover esta pergunta?")){
		var f = document.getElementById("perguntas"); 
		var p = document.getElementById(question); 
		f.removeChild(p);
	}
}


jQuery.fn.equalCols = function(){
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		$(this).css({'height': maxHeight});
	});
};