$(document).ready(function(){

    var voted = 0;
    starsrc = new Array();
    
    // hlasovani
    $("p.vote > img").each(function(i) { // vyberu divy resp p na ktery chci aplikovat onclick
    
	$(this).click(function() {
	
	    $("#gamevote").fadeIn("slow");
	    
	    if (voted == 1) 
	    {
		$("#votemsg").html("již jste hlasoval(a)");
		return false;
	    }
      
	    var vote;
	    vote = i + 1;
	    voted = 1;
	    
            $.get("/xml/vote.php", { gid: ""+$("input#gameID").val()+"", vote: vote }, function(xml) {
		$("#ratingnumber").html($("ratingnumber", xml).text());
		$("#ratingcounter").html($("ratingcounter", xml).text());
		$("#votemsg").html($("votemsg", xml).text());
		for (z = 0; z <= 4; z++)
		{
		    if (($("ratingcounter", xml).text()) >= (z+1)) starsrc[z] = "/img/star-y.png";
		    else if (($("ratingcounter", xml).text()) >= (z + 0.5)) starsrc[z] = "/img/star-yg.png";
		    else starsrc[z] = "/img/star-g.png";
		}
		
		$("p.vote > img").each(function(i) {
		    $(this).attr({ src: starsrc[i] });
    		});	
            });
	    
	    return false;
            });	    
    });
    
    
    $("p.vote > img").mouseover(function(){
    
	if (voted == 1) return false;
	
	$("p.vote > img").css({cursor: "pointer"});
	
	var itemIndex = $("p.vote > img").index(this);
	$("p.vote > img").each(function(i) { 
	    starsrc[i] = $(this).attr("src");
	    if (i <= itemIndex) $(this).attr({ src: "/img/star-green.png"});
        });	
    });
    
    $("p.vote > img").mouseout(function(){
    
	var itemIndex = $("p.vote > img").index(this);
	$("p.vote > img").each(function(i) {
	    if (i <= itemIndex) $(this).attr({ src: starsrc[i] });
        });	
    });
    
    
    $("#addtofavorites").click(function() 
    {
	if ($("#addtofavoriteshref").attr("href") == "/oblibene/") return true;
	
        $.get("/xml/add.php", { gid: ""+$("input#gameID").val()+"" }, function(xml) 
	{
	    $("#favoritescnt").html($("favoritescnt", xml).text());
			
			if ($("errcode", xml).text() == 0) 
	    {
		$("#favorite > p").fadeIn("slow");
		$("#addtofavorites").fadeIn("slow");
		$("#addok").css({ display: "block" });
		$("#plus").css({ display: "none" });
		$("#addtofavoriteshref").attr("href","/oblibene/");
		$("#addtofavoriteshref").html("Moje oblíbené ("+$("favoritescnt", xml).text()+")");
	    }
	    else if ($("errcode", xml).text() == 1)  // neni prihlasen
	    {
		// zobrazime mu logovaci formular
		if ($("#floatlogform").size() == 0)
		{
		    load_logform();
		}
		$("#floatlogform").fadeIn();
	    }
    
	});
	return false;
    });
    
    $("#submitcom").click( function() {
	var s = parseInt(($("#one").val())) * parseInt(($("#two").val()));
	$("#submitcom").after("<input type=\"hidden\" name=\"three\" value=\""+s+"\">");
    });
    

    
    
});

function load_logform()
{
			$.getIfModified("/xml/floatlogform.php", { fake: "" }, function(xml) 
			{
			    $("body").prepend($("logform", xml).text());
			    var y = $("#addtofavorites").offset({ scroll: false }).top + 20;
		    	    var x = $("#addtofavorites").offset({ scroll: false }).left - 10;
			    $("#floatlogform").css({ top: y+"px", left: x+"px", display: "block" });
			    $("#floatlogform").fadeIn();
			});
			waitBoxLoaded('logformclose');
}


// funkce na zjistovani jestli je horni ram uz nahrany
function waitBoxLoaded(idname)
{
//      kdyz je nahrany existuje tento element
if (document.getElementById(idname))
{

		if ($.cookie('username') != null) 
		{
		    $("#usernamefloat").val($.cookie('username'));
		    $("#pwdfloat")[0].focus();
		} else $("#usernamefloat")[0].focus();

//    usernamefloat
    if (t) clearTimeout(t);
    $("#logformclose").click(function() {
		$("#floatlogform").hide();
    });
    
    $("#sss").click(function() 
    {
	var data;
	var options = { url: '/xml/login.php', dataType: 'xml', success: function(responseXML) 
	{ 
	    data = $("login", responseXML).text(); 
	    try
	    {
		if (data != 1) {  $("#logerror").fadeIn(); $("#logerror").html("&nbsp;&nbsp;Jméno a heslo nesouhlasí<br />&nbsp;&nbsp;<a href=\"/zapomenuteheslo/\">zapomenuté heslo ?</a>"); var t = setTimeout("hide_box('logerror')", 0); return false; } 
		else { $("#logerror").html(""); document.location.reload(); return true; } 
	    } catch (e){ show_msg('Jméno a heslo nesouhlasí<br /><a href="/zapomenuteheslo/">zapomenuté heslo ?</a>', 'error');
	    }
	}};
	$('#floatform').ajaxSubmit(options);
	return false;
    });
    

}
else
{
//    alert('NENI tam'+idname);
    t = setTimeout("waitBoxLoaded('"+idname+"')", 100); // zase se to zkusi za 100 msec
}
}

function inssm(tag)
{
    document.getElementById("textcomment").focus(); 
    document.getElementById("textcomment").value = document.getElementById("textcomment").value + tag;
//    alert();
}

function show_msg(msg, typemsg)
{
    $("#msg").html(msg);
    var H = (screen.height - 200) / 2;
    var L = (screen.width - 200) / 2;
    $("#msg").css({ top: H, left: L });
    $("#msg").show();
    var t = setTimeout("hide_box()", 2000); // za 2s to zmizi
}

function hide_box(idname)
{
    if (t) clearTimeout(t);
    $("#"+idname).hide();
}