this.imagePreview = function(){	
    xOffset = 220;
    yOffset = 50;

    $("img.preview").hover(function(e){
            this.t = this.title;
            //this.title = "";	
            var c = "";
            //var c = (this.t != "") ? "<br/>" + this.t : "";
            $("body").append("<p id='preview'><img src='"+ this.title +"' alt='Image preview' />"+ c +"</p>");								 
            $("#preview")
                    .css("top",(e.pageY - xOffset) + "px")
                    .css("left",(e.pageX + yOffset) + "px")
                    .css("z-Index","100000")
                    .fadeIn("slow");						
        },
         function(){
            this.title = this.t;	
            $("#preview").remove();
        });	
            $("img.preview").mousemove(function(e){
                $("#preview")
                .css("top",(e.pageY - xOffset) + "px")
                .css("left",(e.pageX + yOffset) + "px")
                .css("z-Index","10000");
            });			
    };

function validateForm()
{
    $("#formularz #email_error").html("");
    $("#formularz #name_error").html("");
    $("#formularz #thnx").hide();
    var nazwisko = $("#formularz #nazwisko").val();
    var email = $("#formularz #email").val();
    var error=false;
    if(nazwisko=='')
    {
        $("#formularz #name_error").html("Pole wymagane");
        error=true;
    }
    if(email=='')
    {
        $("#formularz #email_error").html("Pole wymagane");
        error=true;
    }
    else if(! /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email))
    {
        $("#formularz #email_error").html("Zły format adresu email");
        error=true;
    }

    if(error)
    {
        $('#formularz input[type=submit]', this).removeAttr("disabled");
        return false;
    }
    else
    {
        return true;
    }
}

$(function() {
        imagePreview();
	$("img[rel]").overlay({oneInstance: false,closeOnClick: false, mask: '#595959', effect: 'apple'});
        var root = $("#photo1").scrollable();
        $("#loading").ajaxStart(function(){ $(this).show(); });
        $("#loading").ajaxStop(function(){ $(this).hide(); });
        
$("#formularz").submit(function(){
        
        $('input[type=submit]', this).attr('disabled', 'disabled');
        var nazwisko = $("#nazwisko").val();
        var email = $("#email").val();
        
        if(!validateForm())
        {
            $('input[type=submit]', this).removeAttr("disabled");
            return false;
        }
        
        
        var responses = [];
        $(".zestaw").each(function(){
            var z = $(this).attr("name");
            var p = $(this).attr("title");
            var response = new Object();
            response.Question = $("#"+z+"_"+p).html();
            response.Answer = $(this).val();
            response.Set = z;
            response.Q = p;
            responses.push(response);
        });
       
        $.post("formularz.php", { 'responses':responses, 'nazwisko':nazwisko, 'email':email }, function(data){
            
            if(data[0]!="{")
            {
                alert("Wystąpił błąd podczas wysyłania odpowiedzi. Przepraszamy za problemy techniczne");
                $('input[type=submit]', this).removeAttr("disabled");
                return false;
            }
            else
            {
                var info = jQuery.parseJSON(data);
            }
            
            if(info.Code==0)
            {
                var api = $("#photo1").data("scrollable");
                api.seekTo(0);
                $(".zestaw").each(function(){
                    $(this).val("");
                });
                $("#nazwisko").val("");
                $("#email").val("");
                $("#thnx").show();
            }
            else
            {   
                alert(info.Message);
            }
            
            $("img[rel]").overlay().close();
            
            
           
            $('input[type=submit]', this).removeAttr("disabled");
        });
        
        
        return false;
    });
    
});
