function addLink(klasa) {
    $(klasa+" a").each(function() {
        $(this).parents(klasa).click(function() {
            window.location.href = $(this).find("a").attr("href");
        });
    });
}


function antyspam() {
    $(".antyspam").each(function() {
        var kod = "<a href=\"mailto:";
        var c = "";
        var l = "";
        var s = $(this).html();
        for (var i=0; i < s.length; i++) {
            l = s.charCodeAt(i)-2;
            c += String.fromCharCode(l);
        }
        kod += c;
        kod +=("\">");
        kod += c;
        kod +="</a>";
        $(this).html(kod);
        $(this).show();
    });
}



function external_links() {
    $("a").each(function() {
        if ( ($(this).attr("href")) && ($(this).attr("rel")=="external")) {
            $(this).attr("target","_blank");
        }
    });
}



function pirobox() {
    $().piroBox_ext({
        my_speed: 600, //animation speed
        bg_alpha: 0.5, //background opacity
        pirobox_next : "piro_next_out", // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
        pirobox_prev : "piro_prev_out",// Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
        close_all : ".piro_close, .piro_overlay",// add class .piro_overlay(with comma)if you want overlay click close piroBox
        slideShow : "", // just delete slideshow between "" if you don"t want it.
        slideSpeed : 4 //slideshow duration in seconds(3 to 6 Recommended)
    });
}


function slideshow() {
    $("#slideshow").nivoSlider({
        currentSlide: 0,
        currentImage: '',
        totalSlides: 0,
        animSpeed: 2000,
        pauseTime: 5000,
        randAnim: '',
        running: true,
        paused: false,
        stop:false,
        pauseOnHover: false,
        directionNav: false,
        controlNav: false
    });
}

function valid() {
    $("#form").validate({
        rules: {

            email: {
                email: true,
                required: true
            },
            name: {
                required: true
            },
            telephone: {
                required: true
            },
            message: {
                required: true,
                minlength: 30
            },
            captcha: {
                required: true,
                remote: {
                    url: "/ajax/checkCaptcha",
                    type: "post",
                    data: {
                        question: $("#captchav").val()
                    }
                }
            }
        },
        messages: {
            captcha: {
                remote: "Please enter correct sum."
            }
        },
        // the errorPlacement has to take the table layout into account
        errorElement: "div",

        errorPlacement: function(error, element) {
            error.insertAfter( element.parent() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function(form) {
            $.ajax({
                url: "/ajax/sendForm",
                type: "post",
                data: {
                    name: $("#name").val(),
                    email: $("#email").val(),
                    telephone: $("#telephone").val(),
                    message: $("#message").val()
                },
                success: function(data) {
                    $("#form").slideUp();
                    $("#thankyou").slideDown();
                    $("#name").val("");
                    $("#email").val("");
                    $("#telephone").val("");
                    $("#message").val("");
                    $("#captcha").val("");
                }
            }
            );
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set &nbsp; as text for IE
            label.html("&nbsp;").addClass("checked");
        }
    });
}


$().ready(function(){
    $("#header-link").click(function(){
        window.location.href="/";
    });

    addLink(".addLink");
    external_links();
    antyspam();
    pirobox();
    $("#portfolio a").tooltip({
        position: ["top", "center"],
        offset: [-100, 0],
        events: {
            def:     "mouseenter,mouseleave",
            input:   "focus,blur",
            widget:  "focus mouseenter,blur mouseleave",
            tooltip: ""
        },
        effect: "slide"
    });
});
