$(document).ready(function() {
        //glosowanie:
        $("a.vote").click(function() {
                if (!$(this).hasClass('login')) {
                    var url = $(this).attr('href');
                    $(this).parent().text('Dziękujemy za oddanie głosu');
                    $.getJSON(url, function(data) {
                            $(".statystyki h4").children('span').text(data.rate);
                            $(".statystyki h4").children('small').text('('+data.count+' głosów)');
                        });
                }
                return false;
            });
        $("a.vote").hover(function() {
                var ktory = parseInt($(this).children('img').attr('alt'))
                var przesun = ktory*17-11;
                $(this).parent().siblings('span.jakaOcena').text(ktory);
                $("#dziub").animate({left: przesun+"px"});
            },function() {
                    $(this).parent().siblings('span.jakaOcena').text('');
            });
        $("#dziub").hover(function() {
                var left = parseInt($(this).css("left").replace("px" ,""));
                var ktory = Math.round((left+11)/17);
                    $(this).parent().siblings('span.jakaOcena').text(ktory);                
            });
        $("#dziub").click(function() {
                var glos = (parseInt($(this).css('left'))+12)/17;
                $(this).siblings('a').eq(glos-1).click();
            });
        //powiekszanie fotki:
        $(".duzaFota").click(function() {
                var src = $(this).attr("src").replace("medium.jpg", "big.jpg");
                $("#fociunia img").remove();
                $("<img></img>").load(function() {
                        var width = parseInt($(this).attr("width"));
                        var height = parseInt($(this).attr("height"));
                        $("#fociunia").append($(this)).css('marginBottom', '15px');
                        $("div.info").hide('fast', function() {
                        });
                        $("#fotkaDuza").children().show(500);
                    }).attr("src", src);

                return false;
            });
        $("#fociunia").click(function() {
                $(this).hide('fast', function () {
                    $("div.info").show('slow');
                });
		$("div.fota").css('display', 'table');
            });
        //odkrywamy glosowanie:
        $("span.ocena").show();
        $("a.modal").live("click", function() {
                var div = $("<div></div>").attr("title", $(this).attr("title")).load($(this).attr("href"), null, function() {
                        
                    });
                $(div).dialog({width:650, height:600, close :function(event,ui) {
                            $(this).remove();
                        }});
                return false;
            });
        $("input.brand").live ("change",function() {
                var zaznaczone = $("input.brand:checked").length;
                if (zaznaczone > 3) {
                    alert('Możesz wybrać tylko 3 marki dla stylu!');
                    $(this).attr("checked", false);
                }
                
                return false;
            });
        $("input.tag").live ("change",function() {
                var zaznaczone = $("input.tag:checked").length;
                if (zaznaczone > 3) {
                    alert('Możesz wybrać tylko 3 tagi dla stylu!');
                    $(this).attr("checked", false);
                }
                
                return false;
            });
        //edycja:
        $("a.editDescription").click(function() {
                var form = $(this).parent("div.opcje").parent("div").siblings("form.editDescriptionForm");
                //tworze form
                var textHolder = $(this).parent().siblings("p");
                if (form.length==0) {
                    var id = $(this).attr("id").replace("editDescription_", "");
                    var text = textHolder.text();
                    form = $("<form></form>").attr("method", "post").attr("action", "/styles/editDescription/"+id).addClass("editDescriptionForm");
                    $("<textarea></textarea>").attr("name", "description").text(text).appendTo(form).addClass("rogi");
                    var action = $('<div></div>').addClass('action').appendTo(form);
                    $('<input type="submit"/>').val("Zapisz").appendTo(action);
                    $('<input type="button"/>').val("Anuluj").addClass("cancelEditDescription").appendTo(action);
                textHolder.parent().after(form);
                }
                //pokazuje:
                form.hide();
                textHolder.parent().hide();
                form.show();
                return false;
            });
        $("input.cancelEditDescription").live("click", function() {
                $(this).parent().parent().hide().siblings("div").show();
                return false;
            });
	$("form.editDescriptionForm input[type=submit]").live("click", function() {
		var form = $(this).parents("form");
                var data = form.serialize();
		$.post(form.attr("action"), data, function(html) {
                        form.siblings("div").fadeOut(1000, function() {
                                $(form).siblings("div").children("p").html(html);
                                $(this).fadeIn(500);
                            });
                    });
		    form.hide().siblings("div").show();

		return false;
	    });
	/*        $("form.editDescriptionForm").live("submit", function() {
                var data = $(this).serialize();
		  var form = $(this);
		  $.post($(this).attr("action"), data, function(html) {
			  form.siblings("div").fadeOut(1000, function() {
				  $(form).siblings("div").children("p").html(html);
				  $(this).fadeIn(500);
			      });
		      });
		  form.hide().siblings("div").show();
		  return false;
		  });*/
    });

