function hovering(klasa) {
    var klasa_z_hoverem = (klasa + "-hover").substr(1,100);
	
    $(klasa).hover(function() {
        $(this).addClass(klasa_z_hoverem);
    }, function() {
        $(this).removeClass(klasa_z_hoverem);
    });
}


function dodajLink(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({
        my_speed: 600, //animation speed
        bg_alpha: 0.5, //background opacity
        radius: 4, //caption rounded corner
        scrollImage : true, // true == image follows the page, false == image remains in the same open position
        pirobox_next : "piro_next", // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
        pirobox_prev : "piro_prev",// 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 mapaGoogle() {
    var czystaprodukcja = new google.maps.LatLng(51.134927,16.987061);
    var center = new google.maps.LatLng(51.134927,16.987061);

    var myOptions = {
        zoom: 13,
        center: center,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("mapaGoogle"), myOptions);

    var pMarker = new google.maps.Marker({
        position: czystaprodukcja,
        map: map
    });

    var pInfo = new google.maps.InfoWindow({
        content: "<div style='font-size: 8pt'>Czysta Produkcja<br/>grobla Kozanowska<br/>53-633 Wrocław</div>"
    });


    google.maps.event.addListener(pMarker, 'click', function() {
        pInfo.open(map, pMarker);
    });

}



function topmenu() {
    $(".menu >li").mouseover(function() {
        if (! $(this).hasClass("open")) {
            $(this).addClass("open");
            $(this).find(".submenu").show("200");
            $(this).bind("mouseleave", function(){
                $(this).find(".submenu").hide("100", function() {
                    $(this).parents("li").removeClass("open");
                });
                $(this).unbind("mouseleave");
            });
        }
    });
}


function carousel() {
    $(".carousel_beforeafter").jCarouselLite({
        auto: true,
        timeout: 4000,
        speed: 1000,
        scroll: 1,
        easing: "easeOutQuad",
        circular: true,
        afterEnd: function() {
            $(".carousel_beforeafter .before").fadeIn();
            $(".carousel_beforeafter .after").fadeOut();

            var carousel_position = $(".carousel_beforeafter ul").css("left");
            carousel_position = carousel_position.substring(1, carousel_position.length-2);
            $(".carousel_beforeafter .active").removeClass("active");
            $(".carousel-item").each(function() {
                var pos = $(this).position();

                if ((pos.left) == carousel_position) {
                    $(this).addClass("active");
                }
            });

            $(".carousel_beforeafter .active").children(".before").delay(1000).fadeOut(1000);
            $(".carousel_beforeafter .active").children(".after").delay(1000).fadeIn(1000);
        }
    });
    

    var carousel_position = $(".carousel_beforeafter ul").css("left");

    carousel_position = carousel_position.substring(1, carousel_position.length-2);
    $(".carousel-item").each(function() {
        var pos = $(this).position();

        if ((pos.left) == carousel_position) {
            $(this).addClass("active");
        }
    });
    $(".carousel_beforeafter .active").children(".before").delay(1000).fadeOut(1000);
    $(".carousel_beforeafter .active").children(".after").delay(1000).fadeIn(1000);
    
    
    $(".carousel_top").jCarouselLite({
        auto: true,
        timeout: 3000,
        speed: 500,
        scroll: 1,
        easing: "easeOutBounce",
        circular: true
    });
    
}


$().ready(function(){
    $("#header-link").click(function(){
        window.location.href="/";
    });

    dodajLink(".addLink");
    external_links();
    antyspam();
    topmenu();
    carousel();
    pirobox();

    if ($("#mapaGoogle").length > 0) mapaGoogle();

});
