$(document).ready(function(){
	incAnketa();
});



function incAnketa() {
	var anketa_holder = $("#anketa");
	
	if (anketa_holder.html() == null)
		return false;
	
	var inputs_radio = $("li div.input input");

	$.each(inputs_radio, function(){
		var input = $(this);
		var input_holder = input.parent();
		
		input.css("visibility", "hidden");
		input_holder.append("<span class='new_input'></span>");

		var new_input = $(".new_input", input_holder);
		


		if (input.is(':checked') === true) {
			new_input.addClass("new_input_active");
		}
		
		
		
		new_input.click(function(){
			if (input.is(':checked') === false) {
				$("li div.input .new_input").removeClass("new_input_active");
				$("li div.input input").attr("checked", false);
				$(this).addClass("new_input_active");
				input.attr("checked", true);
				
				showTextarea( input.attr("value") );
			}
		});
		
		
		var question_holder = input_holder.next();
		var question = $("span.question", question_holder);

		question.click(function(){
			if (input.is(':checked') === false) {
				$("li div.input .new_input").removeClass("new_input_active");
				$("li div.input input").attr("checked", false);
				new_input.addClass("new_input_active");
				input.attr("checked", true);

				showTextarea( input.attr("value") );
			}
		});
		question.mouseover(function(){
			$(this).css("text-decoration", "underline");
		});
		question.mouseout(function(){
			$(this).css("text-decoration", "none");
		});
		
	});
	
	
	// nastavení textarey
	var textarea = $("#jine_textarea textarea", anketa_holder);
	textarea.click(function(){
		var value = $(this).attr("value");
		if (value == "napište vlastní odpověď") {
			$(this).attr("value", "");
			$(this).css("color", "#838383");
		}
	});
	textarea.blur(function(){
		var value = $(this).attr("value");
		if (jQuery.trim(value) == "") {
			$(this).attr("value", "napište vlastní odpověď");
			$(this).css("color", "#BCBCBC");
		}
	});


	
	// zobrazí box pro čtvrtou otázku
	var showTextarea = function(value) {
		var txt = $("#jine_textarea", anketa_holder);
		
		if (value == 4) {
			txt.show(200);
		} else {
			txt.hide(200);
		}
	}
	

	// nastavení inputů
	var input_code = $("input#input_code", anketa_holder);
	input_code.click(function(){
		var value = $(this).attr("value");
		if (value == "zde opište kód") {
			$(this).attr("value", "");
			$(this).css("color", "#838383");
		}
	});
	input_code.blur(function(){
		var value = $(this).attr("value");
		if (jQuery.trim(value) == "") {
			$(this).attr("value", "zde opište kód");
			$(this).css("color", "#BCBCBC");
		}
	});

	var input_email = $("input#input_email", anketa_holder);
	input_email.click(function(){
		var value = $(this).attr("value");
		if (value == "e-mail") {
			$(this).attr("value", "");
			$(this).css("color", "#838383");
		}
	});
	input_email.blur(function(){
		var value = $(this).attr("value");
		if (jQuery.trim(value) == "") {
			$(this).attr("value", "e-mail");
			$(this).css("color", "#BCBCBC");
		}
	});


	$("#input_submit").click(function(){
		if ($(this).hasClass("vote")) {
			$("li.verification", anketa_holder).show(500);
			$(this).removeClass("vote");
			
			return false;
		}
	});



	// archív
	var months_span = $("#anketa span.archive_month");
	
	months_span.click(function(){
		var parent_holder = $(this).parent(".archive_anketa_holder");
		var anketa_holder = $(".archive_anketa", parent_holder);
		
		if (anketa_holder.css("display") == "none") {
			anketa_holder.show();
		} else {
			anketa_holder.hide();
		}
		
	});
	
	
}




/**
 *  Zajisteni sledovani pozice mysi
 */
var mouseX = 0;
var mouseY = 0;

$().mousemove( function(e) {
	mouseX = e.pageX;
	mouseY = e.pageY;
});

var timer_bubble = "";
var timer_zpozdeni = 500;


function show_bubble(idb, resize){

  var bubble = document.getElementById('bubble'+idb);

  
  bubble.style.display="block";
  //alert(bubble.offsetHeight);  
  
  
  
  if (resize == true) {
  
  var leftMax =  document.body.offsetWidth - 450;
  var left = Math.min(leftMax, mouseX-23);  
  
  var top = Math.max(mouseY-bubble.offsetHeight+5, 20);

  //alert(left); 
  
  //bubble.style.top = (mouseY-bubble.offsetHeight+1)+"px";
  bubble.style.top = top+"px";
  //bubble.style.left = (mouseX-23)+"px";
  bubble.style.left = left+"px";  
  }  
  //alert(bubble.offsetHeight);

  if (timer_bubble){  
    clearTimeout(timer_bubble);  
  }

}

function hide_bubble(idb){

timer_bubble = setTimeout("timer_hide_bubble("+idb+")", timer_zpozdeni ); // načasuje akci 

//alert("jsem tady");

}

function timer_hide_bubble(idb){

  //alert ("jsem tu, " + idb_bubble );
 
  var bubble = document.getElementById('bubble'+idb); 
  
  
  bubble.style.display="none";

}