function showphoto(p, change)
{
    c = change;
    var parts = $("#foto").attr("src").split("/large/");
    $(".selected").removeClass("selected");
    $("#thumbimg" + p).addClass("selected");
    var path = parts[0] + "/large/" + p + ".jpg";
    $("#foto").animate(
    {
        opacity: 0
    }, 200, function ()
    {
        $("#fotoframe").html("<img src=\"" + path + "\" id=\"foto\" alt=\"\" />");
        setTimeout(function ()
        {
            $("#foto").animate(
            {
                opacity: 1
            }, 200)
        }, 300);
    });
}
var nc;
var c;
var cc;

function nextset()
{
    var current = $("#nextset").attr("name") * 1;
    var max = $("#prevset").attr("name") * 1;
    var next = current + 1;
    if (max >= next)
    {
        $("#thumbs" + current).hide("slide", {
            direction: "left"
        }, 300, function ()
        {
            $("#thumbs" + next).show("slide", {
                direction: "right"
            }, 300, function ()
            {
                /// $("#nextset").fadeIn(100);
                //  $("#prevset").fadeIn(100);
                next++;
                $("#prevset").animate(
                {
                    opacity: 1
                }, 200);
                if (max < next)
                {
                    $("#nextset").animate(
                    {
                        opacity: 0.2
                    }, 200);
                }
                else
                {
                    $("#nextset").animate(
                    {
                        opacity: 1
                    }, 200);
                }
            });
        });
        $("#nextset").attr("name", next);
    }
    return false;
}

function nextphoto()
{
    var parts = $("#foto").attr("src").split("/large/");
    cc = c;
    c++;
    if ($("#thumblink" + c).attr("name") != undefined)
    {
        var p = $("#thumblink" + c).attr("name");
        if ($("#thumblink" + c).parent().attr("id") != $("#thumblink" + cc).parent().attr("id"))
        {
            nextset();
        }
        var parts = $("#foto").attr("src").split("/large/");
        $(".selected").removeClass("selected");
        $("#thumbimg" + p).addClass("selected");
        var path = parts[0] + "/large/" + p + ".jpg";
        $("#foto").animate(
        {
            opacity: 0
        }, 200, function ()
        {
            $("#fotoframe").html("<img src=\"" + path + "\" id=\"foto\" alt=\"\" />");
            setTimeout(function ()
            {
                $("#foto").animate(
                {
                    opacity: 1
                }, 200)
            }, 300);
        });
    }
}
var subsHTML = "";
var unsubsHTML = "";

function subscribe()
{
    var str = $("#subsdata").serialize();
    $.post("/sections/subscribe.php", str, function (data, textStatus)
    {
        $("#subscribe").fadeOut("fast", function ()
        {
            subsHTML = $("#subscribe").html();
            $("#subscribe").html("<p>" + data + "</p>");
            $("#subscribe").fadeIn();
            setTimeout(function ()
            {
                $("#subscribe").fadeOut("fast", function ()
                {
                    $("#subscribe").html(subsHTML);
                    $("#subscribe").fadeIn();
                });
            }, 3000);
        });
    });
}

function unsubscribe()
{
    var str = $("#unsubsdata").serialize();
    $.post("/sections/unsubscribe.php", str, function (data, textStatus)
    {
        $("#unsubscribe").fadeOut("fast", function ()
        {
            unsubsHTML = $("#unsubscribe").html();
            $("#unsubscribe").html("<p>" + data + "</p>");
            $("#unsubscribe").fadeIn();
            setTimeout(function ()
            {
                $("#unsubscribe").fadeOut("fast", function ()
                {
                    $("#unsubscribe").html(unsubsHTML);
                    $("#unsubscribe").fadeIn();
                });
            }, 3000);
        });
    });
}

function hideNews() {
    if($(".news-item").length){
        var news = $(".news-item");
        var gaps = $(".linegapshort");
        var l = news.length;
        for(n = 4; n<l;n++){
        $(news[n]).hide();
        $(gaps[n]).hide();
        }
    }
}
var sT;
function onScroll() {
    if($(".news-item").length){
        var news = $(".news-item");
        var gaps = $(".linegapshort");
        var scrollPos = $(window).scrollTop();
        var l = news.length;
        var s = 0;
       
        if (scrollPos >= ($(document).height() - $(window).height() - $(window).height() * 0.4)) {
            
        clearTimeout(sT);
        sT = setTimeout(function(){
            for (var n = 4; n < l; n++) {
                if($(news[n]).is(":hidden")) {
                    s = n+1;
                    break;
                }
            }
        
            for (var nn = 0; nn < s+2; nn++) {
                    $(news[nn]).stop(true,true).fadeIn(1000);
                    $(gaps[nn]).stop(true,true).fadeIn(1000);
            
            }
        },300);
    }
}
}

function showsending()
{
    $("#infotekst").html("Trwa wysyłanie pliku...");
}
$(document).ready(function ()
{
    $(window).scroll(onScroll);
    hideNews();
    if ($("#foto").attr("name") != undefined)
    {
        c = $("#foto").attr("name");
    }
    $("#fotoframe").css("cursor", "pointer");
    $("#fotoframe").live('click', function (event)
    {
        nextphoto();
    });
    $("h1 a").css("position", "relative").css("left", 0).css("top", 0);
    $("#thumbsblock img").css("position", "relative").css("left", 0).css("top", 0);
    $(".celllast img, .cell img").css("position", "relative").css("left", 0).css("top", 0);
    $(".bigcol img, .smallcol img").css("position", "relative").css("left", 0).css("top", 0);
    $("h1 a").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $("#thumbsblock img").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $(".cell img, .celllast img").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $(".smallcol img, .bigcol img").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $(".email").val("e-mail");
    $(".email").focus(function ()
    {
        var savedval = $(this).val();
        if ($(this).val() == "e-mail")
        {
            $(this).val("");
        }
    });
    $("input").blur(function ()
    {
        if ($(this).val().length > 0)
        {
            $(this).val(savedval);
        }
        else
        {
            $(this).val("e-mail");
        }
    });
    var max = $("#prevset").attr("name") * 1;
    var current = $("#nextset").attr("name") * 1;
    var next = current + 1;
    var prev = current - 1;
    if (max < next)
    {
        $("#nextset").animate(
        {
            opacity: 0.2
        }, 200);
    }
    if (prev == 0)
    {
        $("#prevset").animate(
        {
            opacity: 0.2
        }, 200);
    }
    if (max == 1)
    {
        $("#nextset").hide();
        $("#prevset").hide();
    }
    $("#nextset").live('click', function (event)
    {
        var current = $(this).attr("name") * 1;
        var max = $("#prevset").attr("name") * 1;
        var next = current + 1;
        if (!($(".thumbsfirst, .thumbsnext").is(':animated')))
        {
            if (max >= next)
            {
                $("#thumbs" + current).hide("slide", {
                    direction: "left"
                }, 300, function ()
                {
                    $("#thumbs" + next).show("slide", {
                        direction: "right"
                    }, 300, function ()
                    {
                        /// $("#nextset").fadeIn(100);
                        //  $("#prevset").fadeIn(100);
                        next++;
                        $("#prevset").animate(
                        {
                            opacity: 1
                        }, 200);
                        if (max < next)
                        {
                            $("#nextset").animate(
                            {
                                opacity: 0.2
                            }, 200);
                        }
                        else
                        {
                            $("#nextset").animate(
                            {
                                opacity: 1
                            }, 200);
                        }
                    });
                });
                $(this).attr("name", next);
            }
        }
        return false;
    });
    $("#prevset").live('click', function (event)
    {
        var current = $("#nextset").attr("name") * 1;
        var max = $("#prevset").attr("name") * 1;
        var prev = current - 1;
        if (!($(".thumbsfirst, .thumbsnext").is(':animated')))
        {
            if (prev >= 1)
            {
                $("#thumbs" + current).hide("slide", {
                    direction: "right"
                }, 300, function ()
                {
                    $("#thumbs" + prev).show("slide", {
                        direction: "left"
                    }, 300, function ()
                    {
                        if (prev <= 1)
                        {
                            $("#prevset").animate(
                            {
                                opacity: 0.2
                            }, 200);
                        }
                        else
                        {
                            $("#prevset").animate(
                            {
                                opacity: 1
                            }, 200);
                        }
                        $("#nextset").animate(
                        {
                            opacity: 1
                        }, 200);
                        //    $("#nextset").fadeIn(100);
                        //     $("#prevset").fadeIn(100);
                    });
                });
                $("#nextset").attr("name", prev);
            }
        }
        return false;
    });
    if($(".current").length){
        var currentTab = $(".current").attr("id").replace("showTab", "");
    }
    $(".showTab").live('click', function (event)
    {
        if (!($(".innertab").is(':animated')))
        {
            var show = $(this).attr("id").replace("showTab", "");
            if (show > currentTab)
            {
                $("#tab" + currentTab).hide("slide", {
                    direction: "left"
                }, 400, function ()
                {
                    $("#showTab" + currentTab).removeClass("open");
                    $("#tab" + show).show("slide", {
                        direction: "right"
                    }, 400, function ()
                    {
                        $("#showTab" + show).addClass("open");
                        currentTab = show;
                    });
                });
            }
            else
            {
                $("#tab" + currentTab).hide("slide", {
                    direction: "right"
                }, 400, function ()
                {
                    $("#showTab" + currentTab).removeClass("open");
                    $("#tab" + show).show("slide", {
                        direction: "left"
                    }, 400, function ()
                    {
                        $("#showTab" + show).addClass("open");
                        currentTab = show;
                    });
                });
            }
        }
        return false;
    });
    $("#showNextTab").live('click', function (event)
    {
        if (!($(".innertab").is(':animated')))
        {
            var next = currentTab * 1 + 1;
            if ($("#showTab" + next).html() == undefined)
            {
                next = 1;
            }
            $("#showTab" + currentTab).removeClass("open");
            $("#tab" + currentTab).hide("slide", {
                direction: "left"
            }, 400, function ()
            {
                $("#tab" + next).show("slide", {
                    direction: "right"
                }, 400, function ()
                {
                    $("#showTab" + next).addClass("open");
                });
            });
            currentTab = next;
        }
        return false;
    });
    $("#showPrevTab").live('click', function (event)
    {
        if (!($(".innertab").is(':animated')))
        {
            var prev = currentTab * 1 - 1;
            if (prev <= 0)
            {
                prev = 1;
                while ($("#showTab" + prev).html() != undefined)
                {
                    prev++;
                }
                prev--;
            }
            $("#showTab" + currentTab).removeClass("open");
            $("#tab" + currentTab).hide("slide", {
                direction: "right"
            }, 400, function ()
            {
                $("#tab" + prev).show("slide", {
                    direction: "left"
                }, 400, function ()
                {
                    $("#showTab" + prev).addClass("open");
                });
            });
        }
        currentTab = prev;
        return false;
    });
    
        
    
});
